00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _THREAD_M_H
00027 #define _THREAD_M_H
00028
00029 #if defined(_LIBC)
00030
00031 #include <libc-lock.h>
00032
00033 #ifdef PTHREAD_MUTEX_INITIALIZER
00034
00035 typedef pthread_t thread_id;
00036
00037
00038 typedef pthread_mutex_t mutex_t;
00039
00040
00041 typedef pthread_key_t tsd_key_t;
00042
00043 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
00044
00045 static Void_t *malloc_key_data;
00046
00047 #define tsd_key_create(key, destr) \
00048 if (__pthread_key_create != NULL) { \
00049 __pthread_key_create(key, destr); \
00050 } else { *(key) = (tsd_key_t) 0; }
00051 #define tsd_setspecific(key, data) \
00052 if (__pthread_setspecific != NULL) { \
00053 __pthread_setspecific(key, data); \
00054 } else { malloc_key_data = (Void_t *) data; }
00055 #define tsd_getspecific(key, vptr) \
00056 (vptr = (__pthread_getspecific != NULL \
00057 ? __pthread_getspecific(key) : malloc_key_data))
00058
00059 #define mutex_init(m) \
00060 (__pthread_mutex_init != NULL ? __pthread_mutex_init (m, NULL) : 0)
00061 #define mutex_lock(m) \
00062 (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (m) : 0)
00063 #define mutex_trylock(m) \
00064 (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (m) : 0)
00065 #define mutex_unlock(m) \
00066 (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (m) : 0)
00067
00068 #elif defined(MUTEX_INITIALIZER)
00069
00070
00071
00072
00073 #undef mutex_t
00074 #define mutex_t struct mutex
00075
00076 #undef mutex_lock
00077 #define mutex_lock(m) (__mutex_lock(m), 0)
00078
00079 #undef mutex_unlock
00080 #define mutex_unlock(m) (__mutex_unlock(m), 0)
00081
00082 #define mutex_trylock(m) (!__mutex_trylock(m))
00083
00084 #include <hurd/threadvar.h>
00085
00086
00087 typedef int tsd_key_t;
00088
00089 static int tsd_keys_alloced = 0;
00090
00091 #define tsd_key_create(key, destr) \
00092 (assert (tsd_keys_alloced == 0), tsd_keys_alloced++)
00093 #define tsd_setspecific(key, data) \
00094 (*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC) = (unsigned long)(data))
00095 #define tsd_getspecific(key, vptr) \
00096 ((vptr) = (void *)*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC))
00097
00098
00099 #define __pthread_initialize ((void (*)(void))0)
00100
00101 #else
00102
00103 #define NO_THREADS
00104
00105 #endif
00106
00107 #elif defined(USE_PTHREADS)
00108
00109 #include <pthread.h>
00110
00111 typedef pthread_t thread_id;
00112
00113
00114 typedef pthread_mutex_t mutex_t;
00115
00116 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
00117 #define mutex_init(m) pthread_mutex_init(m, NULL)
00118 #define mutex_lock(m) pthread_mutex_lock(m)
00119 #define mutex_trylock(m) pthread_mutex_trylock(m)
00120 #define mutex_unlock(m) pthread_mutex_unlock(m)
00121
00122
00123 typedef pthread_key_t tsd_key_t;
00124
00125 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
00126 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
00127 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
00128
00129 #elif USE_THR
00130
00131 #include <thread.h>
00132
00133 typedef thread_t thread_id;
00134
00135 #define MUTEX_INITIALIZER { 0 }
00136 #define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
00137
00138
00139
00140
00141
00142 typedef void *tsd_key_t[256];
00143 #define tsd_key_create(key, destr) do { \
00144 int i; \
00145 for(i=0; i<256; i++) (*key)[i] = 0; \
00146 } while(0)
00147 #define tsd_setspecific(key, data) (key[(unsigned)thr_self() % 256] = (data))
00148 #define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
00149
00150 #elif USE_SPROC
00151
00152 #include <sys/wait.h>
00153 #include <sys/types.h>
00154 #include <sys/prctl.h>
00155 #include <abi_mutex.h>
00156
00157 typedef int thread_id;
00158
00159 typedef abilock_t mutex_t;
00160
00161 #define MUTEX_INITIALIZER { 0 }
00162 #define mutex_init(m) init_lock(m)
00163 #define mutex_lock(m) (spin_lock(m), 0)
00164 #define mutex_trylock(m) acquire_lock(m)
00165 #define mutex_unlock(m) release_lock(m)
00166
00167 typedef int tsd_key_t;
00168 int tsd_key_next;
00169 #define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
00170 #define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
00171 #define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
00172
00173
00174 #elif WIN32
00175
00176 #include <windows.h>
00177 #include "parallel.h"
00178
00179 typedef DWORD thread_id;
00180
00181 #ifdef WIN32_CRITICAL_SECTIONS
00182
00183 typedef CRITICAL_SECTION mutex_t;
00184
00185 _inline int EnterCS( LPCRITICAL_SECTION pcs )
00186 { EnterCriticalSection(pcs);
00187 return(0) ;
00188 }
00189 _inline int TryEnterCS( LPCRITICAL_SECTION pcs )
00190 { int val = TryEnterCriticalSection(pcs);
00191 return(!val) ;
00192 }
00193 _inline int LeaveCS( LPCRITICAL_SECTION pcs )
00194 { LeaveCriticalSection(pcs);
00195 return(0) ;
00196 }
00197
00198 #define MUTEX_INITIALIZER { 0 }
00199 #define mutex_init(m) InitializeCriticalSection(m)
00200 #define mutex_lock(m) EnterCS(m)
00201 #define mutex_trylock(m) TryEnterCS(m)
00202 #define mutex_unlock(m) LeaveCS(m)
00203
00204 #else
00205
00206 typedef splock_t mutex_t;
00207
00208 #define MUTEX_INITIALIZER { L_UNLOCKED }
00209 #define mutex_init(m) S_INIT_LOCK(m)
00210 #define mutex_lock(m) S_LOCK(m)
00211 #define mutex_trylock(m) S_TRY_LOCK(m)
00212 #define mutex_unlock(m) S_UNLOCK(m)
00213
00214 #endif
00215
00216 #define tsd_key_t __declspec(thread) Void_t *
00217
00218 #define tsd_key_create(key, destr) ( *key = NULL )
00219 #define tsd_setspecific(key, data) ( key = data )
00220 #define tsd_getspecific(key, vptr) ( vptr = key )
00221
00222 #else
00223
00224 #define NO_THREADS
00225
00226 #endif
00227
00228 #ifdef NO_THREADS
00229
00230 typedef int thread_id;
00231
00232 typedef int mutex_t;
00233
00234 #define MUTEX_INITIALIZER 0
00235 #define mutex_init(m) (*(m) = 0)
00236 #define mutex_lock(m) (0)
00237 #define mutex_trylock(m) (0)
00238 #define mutex_unlock(m) (0)
00239
00240 typedef void *tsd_key_t;
00241 #define tsd_key_create(key, destr) do {} while(0)
00242 #define tsd_setspecific(key, data) do {} while(0)
00243 #define tsd_getspecific(key, vptr) (vptr = NULL)
00244
00245 #endif
00246
00247 #endif