aboutsummaryrefslogtreecommitdiff
path: root/src/threading.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/threading.h')
-rw-r--r--src/threading.h55
1 files changed, 39 insertions, 16 deletions
diff --git a/src/threading.h b/src/threading.h
index 4a83229..7387764 100644
--- a/src/threading.h
+++ b/src/threading.h
@@ -1,8 +1,8 @@
1/* 1/*
2* THREADING.H 2* THREADING.H
3*/ 3*/
4#ifndef THREADING_H 4#ifndef __threading_h__
5#define THREADING_H 5#define __threading_h__ 1
6 6
7/* Platform detection 7/* Platform detection
8*/ 8*/
@@ -45,11 +45,19 @@ typedef unsigned int uint_t;
45*/ 45*/
46enum e_status { PENDING, RUNNING, WAITING, DONE, ERROR_ST, CANCELLED }; 46enum e_status { PENDING, RUNNING, WAITING, DONE, ERROR_ST, CANCELLED };
47 47
48#define THREADAPI_WINDOWS 1
49#define THREADAPI_PTHREAD 2
50
51#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
52#define THREADAPI THREADAPI_WINDOWS
53#else // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
54#define THREADAPI THREADAPI_PTHREAD
55#endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
48 56
49/*---=== Locks & Signals ===--- 57/*---=== Locks & Signals ===---
50*/ 58*/
51 59
52#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) 60#if THREADAPI == THREADAPI_WINDOWS
53 #define WIN32_LEAN_AND_MEAN 61 #define WIN32_LEAN_AND_MEAN
54 // 'SignalObjectAndWait' needs this (targets Windows 2000 and above) 62 // 'SignalObjectAndWait' needs this (targets Windows 2000 and above)
55 #define _WIN32_WINNT 0x0400 63 #define _WIN32_WINNT 0x0400
@@ -68,12 +76,13 @@ enum e_status { PENDING, RUNNING, WAITING, DONE, ERROR_ST, CANCELLED };
68 void MUTEX_LOCK( MUTEX_T *ref ); 76 void MUTEX_LOCK( MUTEX_T *ref );
69 void MUTEX_UNLOCK( MUTEX_T *ref ); 77 void MUTEX_UNLOCK( MUTEX_T *ref );
70 78
71 typedef unsigned THREAD_RETURN_T; 79 typedef unsigned int THREAD_RETURN_T;
72 80
73 #define SIGNAL_T HANDLE 81 #define SIGNAL_T HANDLE
74 82
75 #define YIELD() Sleep(0) 83 #define YIELD() Sleep(0)
76#else 84 #define THREAD_CALLCONV __stdcall
85#else // THREADAPI == THREADAPI_PTHREAD
77 // PThread (Linux, OS X, ...) 86 // PThread (Linux, OS X, ...)
78 // 87 //
79 #include <pthread.h> 88 #include <pthread.h>
@@ -114,7 +123,8 @@ enum e_status { PENDING, RUNNING, WAITING, DONE, ERROR_ST, CANCELLED };
114 #else 123 #else
115 #define YIELD() pthread_yield() 124 #define YIELD() pthread_yield()
116 #endif 125 #endif
117#endif 126 #define THREAD_CALLCONV
127#endif //THREADAPI == THREADAPI_PTHREAD
118 128
119void SIGNAL_INIT( SIGNAL_T *ref ); 129void SIGNAL_INIT( SIGNAL_T *ref );
120void SIGNAL_FREE( SIGNAL_T *ref ); 130void SIGNAL_FREE( SIGNAL_T *ref );
@@ -136,9 +146,10 @@ bool_t SIGNAL_WAIT( SIGNAL_T *ref, MUTEX_T *mu, time_d timeout );
136/*---=== Threading ===--- 146/*---=== Threading ===---
137*/ 147*/
138 148
139#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) 149#if THREADAPI == THREADAPI_WINDOWS
140 150
141 typedef HANDLE THREAD_T; 151 typedef HANDLE THREAD_T;
152# define THREAD_ISNULL( _h) (_h == 0)
142 // 153 //
143 void THREAD_CREATE( THREAD_T *ref, 154 void THREAD_CREATE( THREAD_T *ref,
144 THREAD_RETURN_T (__stdcall *func)( void * ), 155 THREAD_RETURN_T (__stdcall *func)( void * ),
@@ -147,7 +158,7 @@ bool_t SIGNAL_WAIT( SIGNAL_T *ref, MUTEX_T *mu, time_d timeout );
147# define THREAD_PRIO_MIN (-3) 158# define THREAD_PRIO_MIN (-3)
148# define THREAD_PRIO_MAX (+3) 159# define THREAD_PRIO_MAX (+3)
149 160
150#else 161#else // THREADAPI == THREADAPI_PTHREAD
151 /* Platforms that have a timed 'pthread_join()' can get away with a simpler 162 /* Platforms that have a timed 'pthread_join()' can get away with a simpler
152 * implementation. Others will use a condition variable. 163 * implementation. Others will use a condition variable.
153 */ 164 */
@@ -161,6 +172,7 @@ bool_t SIGNAL_WAIT( SIGNAL_T *ref, MUTEX_T *mu, time_d timeout );
161# endif 172# endif
162 173
163 typedef pthread_t THREAD_T; 174 typedef pthread_t THREAD_T;
175# define THREAD_ISNULL( _h) 0 // pthread_t may be a structure: never 'null' by itself
164 176
165 void THREAD_CREATE( THREAD_T *ref, 177 void THREAD_CREATE( THREAD_T *ref,
166 THREAD_RETURN_T (*func)( void * ), 178 THREAD_RETURN_T (*func)( void * ),
@@ -178,19 +190,30 @@ bool_t SIGNAL_WAIT( SIGNAL_T *ref, MUTEX_T *mu, time_d timeout );
178# define THREAD_PRIO_MIN (-2) 190# define THREAD_PRIO_MIN (-2)
179# define THREAD_PRIO_MAX (+2) 191# define THREAD_PRIO_MAX (+2)
180# endif 192# endif
181#endif 193#endif // THREADAPI == THREADAPI_WINDOWS
182 194
183/* 195/*
184* Win32 and PTHREAD_TIMEDJOIN allow waiting for a thread with a timeout. 196* Win32 and PTHREAD_TIMEDJOIN allow waiting for a thread with a timeout.
185* Posix without PTHREAD_TIMEDJOIN needs to use a condition variable approach. 197* Posix without PTHREAD_TIMEDJOIN needs to use a condition variable approach.
186*/ 198*/
187#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) || (defined PTHREAD_TIMEDJOIN) 199#define THREADWAIT_TIMEOUT 1
188 bool_t THREAD_WAIT( THREAD_T *ref, double secs ); 200#define THREADWAIT_CONDVAR 2
189#else 201
190 bool_t THREAD_WAIT( THREAD_T *ref, SIGNAL_T *signal_ref, MUTEX_T *mu_ref, volatile enum e_status *st_ref, double secs ); 202#if THREADAPI == THREADAPI_WINDOWS || (defined PTHREAD_TIMEDJOIN)
191#endif 203#define THREADWAIT_METHOD THREADWAIT_TIMEOUT
204#else // THREADAPI == THREADAPI_WINDOWS || (defined PTHREAD_TIMEDJOIN)
205#define THREADWAIT_METHOD THREADWAIT_CONDVAR
206#endif // THREADAPI == THREADAPI_WINDOWS || (defined PTHREAD_TIMEDJOIN)
207
208
209#if THREADWAIT_METHOD == THREADWAIT_TIMEOUT
210bool_t THREAD_WAIT_IMPL( THREAD_T *ref, double secs);
211#define THREAD_WAIT( a, b, c, d, e) THREAD_WAIT_IMPL( a, b)
212#else // THREADWAIT_METHOD == THREADWAIT_CONDVAR
213bool_t THREAD_WAIT_IMPL( THREAD_T *ref, double secs, SIGNAL_T *signal_ref, MUTEX_T *mu_ref, volatile enum e_status *st_ref);
214#define THREAD_WAIT THREAD_WAIT_IMPL
215#endif // // THREADWAIT_METHOD == THREADWAIT_CONDVAR
192 216
193void THREAD_KILL( THREAD_T *ref ); 217void THREAD_KILL( THREAD_T *ref );
194 218
195#endif 219#endif // __threading_h__
196 // THREADING_H