Debian Codegraph @

src/core/util_thread.h

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // name: util_thread.h
28 // desc: thread utilities
29 //
30 // authors: Ge Wang ([email protected] | [email protected])
31 // based on STK's Thread
32 // Perry R. Cook + Gary Scavone
33 // date: autumn 2004
34 //-----------------------------------------------------------------------------
35 #ifndef __UTIL_THREAD_H__
36 #define __UTIL_THREAD_H__
37
38 #include "chuck_def.h"
39 #include <stdio.h>
40
41
42 // forward declaration to break circular dependencies
43 class FastCircularBuffer;
44 template<typename T> class CircularBuffer;
45
46
47 #if defined(__PLATFORM_APPLE__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) || defined(__PLATFORM_EMSCRIPTEN__)
48 // 1.5.5.8 (ben, ge) added emscripten above since it uses POSIX
49 #include <pthread.h>
50 #define THREAD_TYPE
51 typedef pthread_t THREAD_HANDLE;
52 typedef void * THREAD_RETURN;
53 typedef void * (*THREAD_FUNCTION)(void *);
54 typedef pthread_mutex_t MUTEX;
55 #define CHUCK_THREAD pthread_t
56 #elif defined(__PLATFORM_WINDOWS__)
57 #include <windows.h>
58 #include <process.h>
59 #define THREAD_TYPE __stdcall
60 typedef unsigned long THREAD_HANDLE;
61 typedef unsigned THREAD_RETURN;
62 typedef unsigned (__stdcall *THREAD_FUNCTION)(void *);
63 typedef CRITICAL_SECTION MUTEX;
64 #define CHUCK_THREAD HANDLE
65 #endif
66
67
68
69
70 //-----------------------------------------------------------------------------
71 // name: struct XThread
72 // desc: ...
73 //-----------------------------------------------------------------------------
74 struct XThread
75 {
76 public:
77 XThread();
78 ~XThread();
79
80 public:
81 // begin execution of the thread routine
82 // the thread routine can be passed an argument via ptr
83 bool start( THREAD_FUNCTION routine, void * ptr = NULL );
84
85 // wait the specified number of milliseconds for the thread to terminate
86 bool wait( long milliseconds = -1, bool cancel = true );
87
88 public:
89 // test for a thread cancellation request.
90 static void test();
91
92 // get thread ID from a handle
93 static t_CKUINT getID( THREAD_HANDLE t );
94
95 public:
96 // get internal thread reference
97 THREAD_HANDLE getThread() { return thread; }
98 // get thread ID
99 t_CKUINT getID() { return getID( thread ); }
100
101 // clear
102 void clear() { thread = 0; }
103
104 protected:
105 THREAD_HANDLE thread;
106 };
107
108
109
110
111 //-----------------------------------------------------------------------------
112 // name: struct XThreadUtil
113 // desc: general thread utility
114 //-----------------------------------------------------------------------------
115 struct XThreadUtil
116 {
117 public:
118 // set priority on thread
119 static t_CKBOOL set_priority( CHUCK_THREAD tid, t_CKINT priority );
120 // set priority on current thread
121 static t_CKBOOL set_priority( t_CKINT priority );
122 // get max allowable system priority
123 static t_CKINT get_max_priority();
124 // get min allowable system priority
125 static t_CKINT get_min_priority();
126
127 public:
128 // not sure what this exactly
129 static t_CKINT our_priority;
130 };
131
132
133
134
135 //-----------------------------------------------------------------------------
136 // name: struct XMutex
137 // desc: ...
138 //-----------------------------------------------------------------------------
139 struct XMutex
140 {
141 public:
142 XMutex();
143 ~XMutex();
144
145 public:
146 void acquire( );
147 void release(void);
148
149 protected:
150 MUTEX mutex;
151 };
152
153
154
155
156 //-----------------------------------------------------------------------------
157 // name: XWriteThread()
158 // desc: utility class for scheduling writes to be executed on a separate
159 // thread.
160 //-----------------------------------------------------------------------------
161 class XWriteThread
162 {
163 public:
164 // get the shared instance
165 static XWriteThread * shared();
166 // static buffer size
167 const static size_t PRODUCER_BUFFER_SIZE;
168
169 public:
170 // constructor
171 XWriteThread( size_t data_buffer_size = 4096, size_t msg_buffer_size = 32 );
172
173 public:
174 size_t fwrite( const void * ptr, size_t size, size_t nitems, FILE * stream );
175 int fseek( FILE * stream, long offset, int whence );
176 int fflush( FILE * stream );
177 int fclose( FILE * stream );
178
179 // DO NOT DELETE INSTANCES OF XWriteThread
180 // instead call shutdown and they will be cleaned up in the background
181 void shutdown();
182
183 private:
184 // DO NOT DELETE INSTANCES OF XWriteThread
185 // instead call shutdown and they will be cleaned up in the background
186 ~XWriteThread();
187
188 // flush
189 void flush_data_buffer();
190
191 // callback
192 #if defined(__PLATFORM_APPLE__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) || defined(__PLATFORM_EMSCRIPTEN__)
193 static void * write_cb( void * _thiss );
194 #elif defined(__PLATFORM_WINDOWS__)
195 static unsigned THREAD_TYPE write_cb( void * _thiss );
196 #endif
197
198 // shared instance
199 static XWriteThread * o_defaultWriteThread;
200
201 private:
202 t_CKBOOL m_thread_exit;
203 XThread m_thread;
204 FastCircularBuffer * m_data_buffer;
205 size_t m_bytes_in_buffer;
206 t_CKBYTE * m_thread_buffer;
207 FILE * m_stream;
208
209 // internal message format
210 struct Message
211 {
212 // operation type
213 enum
214 {
215 NONE,
216 WRITE,
217 SEEK,
218 FLUSH,
219 CLOSE,
220 SHUTDOWN
221 } operation;
222
223 // file pointer
224 FILE * file;
225
226 // size / offset
227 union
228 {
229 struct
230 {
231 size_t data_size;
232 } write;
233
234 struct
235 {
236 long offset;
237 int whence;
238 } seek;
239 };
240
241 // 1.4.1.0 (ge) added
242 Message() : file(NULL) { seek.offset = 0; operation = NONE; }
243 };
244
245 // circular buffer
246 CircularBuffer<Message> * m_msg_buffer;
247 };
248
249
250
251
252 #endif