File indexing completed on 2025-01-18 10:14:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #ifndef _XTHREADS_H_
0028 # define _XTHREADS_H_
0029
0030
0031
0032
0033 # ifndef xmalloc
0034 # define xmalloc malloc
0035 # endif
0036 # ifndef xfree
0037 # define xfree free
0038 # endif
0039
0040 # ifdef CTHREADS
0041 # include <cthreads.h>
0042 typedef cthread_t xthread_t;
0043 typedef struct condition xcondition_rec;
0044 typedef struct mutex xmutex_rec;
0045 # define xthread_init() cthread_init()
0046 # define xthread_self cthread_self
0047 # define xthread_fork(func,closure) cthread_fork(func,closure)
0048 # define xthread_yield() cthread_yield()
0049 # define xthread_exit(v) cthread_exit(v)
0050 # define xthread_set_name(t,str) cthread_set_name(t,str)
0051 # define xmutex_init(m) mutex_init(m)
0052 # define xmutex_clear(m) mutex_clear(m)
0053 # define xmutex_lock(m) mutex_lock(m)
0054 # define xmutex_unlock(m) mutex_unlock(m)
0055 # define xmutex_set_name(m,str) mutex_set_name(m,str)
0056 # define xcondition_init(cv) condition_init(cv)
0057 # define xcondition_clear(cv) condition_clear(cv)
0058 # define xcondition_wait(cv,m) condition_wait(cv,m)
0059 # define xcondition_signal(cv) condition_signal(cv)
0060 # define xcondition_broadcast(cv) condition_broadcast(cv)
0061 # define xcondition_set_name(cv,str) condition_set_name(cv,str)
0062 # else
0063 # if defined(SVR4)
0064 # include <thread.h>
0065 # include <synch.h>
0066 typedef thread_t xthread_t;
0067 typedef thread_key_t xthread_key_t;
0068 typedef cond_t xcondition_rec;
0069 typedef mutex_t xmutex_rec;
0070 # if defined(__UNIXWARE__)
0071 extern xthread_t (*_x11_thr_self)();
0072 # define xthread_self (_x11_thr_self)
0073 # else
0074 # define xthread_self thr_self
0075 # endif
0076 # define xthread_fork(func,closure) thr_create(NULL,0,func,closure,THR_NEW_LWP|THR_DETACHED,NULL)
0077 # define xthread_yield() thr_yield()
0078 # define xthread_exit(v) thr_exit(v)
0079 # define xthread_key_create(kp,d) thr_keycreate(kp,d)
0080 # ifdef __sun
0081 # define xthread_key_delete(k) 0
0082 # else
0083 # define xthread_key_delete(k) thr_keydelete(k)
0084 # endif
0085 # define xthread_set_specific(k,v) thr_setspecific(k,v)
0086 # define xthread_get_specific(k,vp) thr_getspecific(k,vp)
0087 # define xmutex_init(m) mutex_init(m,USYNC_THREAD,0)
0088 # define xmutex_clear(m) mutex_destroy(m)
0089 # define xmutex_lock(m) mutex_lock(m)
0090 # define xmutex_unlock(m) mutex_unlock(m)
0091 # define xcondition_init(cv) cond_init(cv,USYNC_THREAD,0)
0092 # define xcondition_clear(cv) cond_destroy(cv)
0093 # define xcondition_wait(cv,m) cond_wait(cv,m)
0094 # define xcondition_signal(cv) cond_signal(cv)
0095 # define xcondition_broadcast(cv) cond_broadcast(cv)
0096 # else
0097 # ifdef WIN32
0098 # include <X11/Xwindows.h>
0099 typedef DWORD xthread_t;
0100 typedef DWORD xthread_key_t;
0101 struct _xthread_waiter {
0102 HANDLE sem;
0103 struct _xthread_waiter *next;
0104 };
0105 typedef struct {
0106 CRITICAL_SECTION cs;
0107 struct _xthread_waiter *waiters;
0108 } xcondition_rec;
0109 typedef CRITICAL_SECTION xmutex_rec;
0110 extern void _Xthread_init(void);
0111 # define xthread_init() _Xthread_init()
0112 # define xthread_self GetCurrentThreadId
0113 # define xthread_fork(func,closure) { \
0114 DWORD _tmptid; \
0115 CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, (LPVOID)closure, 0, \
0116 &_tmptid); \
0117 }
0118 # define xthread_yield() Sleep(0)
0119 # define xthread_exit(v) ExitThread((DWORD)(v))
0120 # define xthread_key_create(kp,d) *(kp) = TlsAlloc()
0121 # define xthread_key_delete(k) TlsFree(k)
0122 # define xthread_set_specific(k,v) TlsSetValue(k,v)
0123 # define xthread_get_specific(k,vp) TlsGetValue(k)
0124 # define xmutex_init(m) InitializeCriticalSection(m)
0125 # define xmutex_clear(m) DeleteCriticalSection(m)
0126 # define _XMUTEX_NESTS
0127 # define xmutex_lock(m) EnterCriticalSection(m)
0128 # define xmutex_unlock(m) LeaveCriticalSection(m)
0129 # define xcondition_init(cv) { \
0130 InitializeCriticalSection(&(cv)->cs); \
0131 (cv)->waiters = NULL; \
0132 }
0133 # define xcondition_clear(cv) DeleteCriticalSection(&(cv)->cs)
0134 extern struct _xthread_waiter *_Xthread_waiter();
0135 # define xcondition_wait(cv,m) { \
0136 struct _xthread_waiter *_tmpthr = _Xthread_waiter(); \
0137 EnterCriticalSection(&(cv)->cs); \
0138 _tmpthr->next = (cv)->waiters; \
0139 (cv)->waiters = _tmpthr; \
0140 LeaveCriticalSection(&(cv)->cs); \
0141 LeaveCriticalSection(m); \
0142 WaitForSingleObject(_tmpthr->sem, INFINITE); \
0143 EnterCriticalSection(m); \
0144 }
0145 # define xcondition_signal(cv) { \
0146 EnterCriticalSection(&(cv)->cs); \
0147 if ((cv)->waiters) { \
0148 ReleaseSemaphore((cv)->waiters->sem, 1, NULL); \
0149 (cv)->waiters = (cv)->waiters->next; \
0150 } \
0151 LeaveCriticalSection(&(cv)->cs); \
0152 }
0153 # define xcondition_broadcast(cv) { \
0154 struct _xthread_waiter *_tmpthr; \
0155 EnterCriticalSection(&(cv)->cs); \
0156 for (_tmpthr = (cv)->waiters; _tmpthr; _tmpthr = _tmpthr->next) \
0157 ReleaseSemaphore(_tmpthr->sem, 1, NULL); \
0158 (cv)->waiters = NULL; \
0159 LeaveCriticalSection(&(cv)->cs); \
0160 }
0161 # else
0162 # ifdef USE_TIS_SUPPORT
0163
0164
0165
0166
0167 # include <tis.h>
0168 typedef pthread_t xthread_t;
0169 typedef pthread_key_t xthread_key_t;
0170 typedef pthread_cond_t xcondition_rec;
0171 typedef pthread_mutex_t xmutex_rec;
0172 # define xthread_self tis_self
0173 # define xthread_fork(func,closure) { pthread_t _tmpxthr; \
0174 pthread_create(&_tmpxthr,NULL,func,closure); }
0175 # define xthread_yield() pthread_yield_np()
0176 # define xthread_exit(v) pthread_exit(v)
0177 # define xthread_key_create(kp,d) tis_key_create(kp,d)
0178 # define xthread_key_delete(k) tis_key_delete(k)
0179 # define xthread_set_specific(k,v) tis_setspecific(k,v)
0180 # define xthread_get_specific(k,vp) *(vp) = tis_getspecific(k)
0181 # define XMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
0182 # define xmutex_init(m) tis_mutex_init(m)
0183 # define xmutex_clear(m) tis_mutex_destroy(m)
0184 # define xmutex_lock(m) tis_mutex_lock(m)
0185 # define xmutex_unlock(m) tis_mutex_unlock(m)
0186 # define xcondition_init(c) tis_cond_init(c)
0187 # define xcondition_clear(c) tis_cond_destroy(c)
0188 # define xcondition_wait(c,m) tis_cond_wait(c,m)
0189 # define xcondition_signal(c) tis_cond_signal(c)
0190 # define xcondition_broadcast(c) tis_cond_broadcast(c)
0191 # else
0192 # ifdef USE_NBSD_THREADLIB
0193
0194
0195
0196
0197 # include <threadlib.h>
0198 typedef thr_t xthread_t;
0199 typedef thread_key_t xthread_key_t;
0200 typedef cond_t xcondition_rec;
0201 typedef mutex_t xmutex_rec;
0202 # define xthread_self thr_self
0203 # define xthread_fork(func,closure) { thr_t _tmpxthr; \
0204 \
0205 thr_create(&_tmpxthr,NULL,func,closure); }
0206 # define xthread_yield() thr_yield()
0207 # define xthread_exit(v) thr_exit(v)
0208 # define xthread_key_create(kp,d) thr_keycreate(kp,d)
0209 # define xthread_key_delete(k) thr_keydelete(k)
0210 # define xthread_set_specific(k,v) thr_setspecific(k,v)
0211 # define xthread_get_specific(k,vp) *(vp) = thr_getspecific(k)
0212 # define XMUTEX_INITIALIZER MUTEX_INITIALIZER
0213 # define xmutex_init(m) mutex_init(m, 0)
0214 # define xmutex_clear(m) mutex_destroy(m)
0215 # define xmutex_lock(m) mutex_lock(m)
0216 # define xmutex_unlock(m) mutex_unlock(m)
0217 # define xcondition_init(c) cond_init(c, 0, 0)
0218 # define xcondition_clear(c) cond_destroy(c)
0219 # define xcondition_wait(c,m) cond_wait(c,m)
0220 # define xcondition_signal(c) cond_signal(c)
0221 # define xcondition_broadcast(c) cond_broadcast(c)
0222 # else
0223 # include <pthread.h>
0224 typedef pthread_t xthread_t;
0225 typedef pthread_key_t xthread_key_t;
0226 typedef pthread_cond_t xcondition_rec;
0227 typedef pthread_mutex_t xmutex_rec;
0228 # define xthread_self pthread_self
0229 # define xthread_yield() pthread_yield()
0230 # define xthread_exit(v) pthread_exit(v)
0231 # define xthread_set_specific(k,v) pthread_setspecific(k,v)
0232 # define xmutex_clear(m) pthread_mutex_destroy(m)
0233 # define xmutex_lock(m) pthread_mutex_lock(m)
0234 # define xmutex_unlock(m) pthread_mutex_unlock(m)
0235 # ifndef XPRE_STANDARD_API
0236 # define xthread_key_create(kp,d) pthread_key_create(kp,d)
0237 # define xthread_key_delete(k) pthread_key_delete(k)
0238 # define xthread_get_specific(k,vp) *(vp) = pthread_getspecific(k)
0239 # define xthread_fork(func,closure) { pthread_t _tmpxthr; \
0240 pthread_create(&_tmpxthr,NULL,func,closure); }
0241 # define XMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
0242 # define xmutex_init(m) pthread_mutex_init(m, NULL)
0243 # define xcondition_init(c) pthread_cond_init(c, NULL)
0244 # else
0245 # define xthread_key_create(kp,d) pthread_keycreate(kp,d)
0246 # define xthread_key_delete(k) 0
0247 # define xthread_get_specific(k,vp) pthread_getspecific(k,vp)
0248 # define xthread_fork(func,closure) { pthread_t _tmpxthr; \
0249 pthread_create(&_tmpxthr,pthread_attr_default,func,closure); }
0250 # define xmutex_init(m) pthread_mutex_init(m, pthread_mutexattr_default)
0251 # define xcondition_init(c) pthread_cond_init(c, pthread_condattr_default)
0252 # endif
0253 # define xcondition_clear(c) pthread_cond_destroy(c)
0254 # define xcondition_wait(c,m) pthread_cond_wait(c,m)
0255 # define xcondition_signal(c) pthread_cond_signal(c)
0256 # define xcondition_broadcast(c) pthread_cond_broadcast(c)
0257 # if defined(_DECTHREADS_)
0258 static xthread_t _X_no_thread_id;
0259 # define xthread_have_id(id) !pthread_equal(id, _X_no_thread_id)
0260 # define xthread_clear_id(id) id = _X_no_thread_id
0261 # define xthread_equal(id1,id2) pthread_equal(id1, id2)
0262 # endif
0263 # if defined(__linux__)
0264 # define xthread_have_id(id) !pthread_equal(id, 0)
0265 # define xthread_clear_id(id) id = 0
0266 # define xthread_equal(id1,id2) pthread_equal(id1, id2)
0267 # endif
0268 # if defined(_CMA_VENDOR_) && defined(_CMA__IBM) && (_CMA_VENDOR_ == _CMA__IBM)
0269 # ifdef DEBUG
0270
0271 # define xmutex_set_name(m,str) ((char**)(m)->field1)[5] = (str)
0272 # define xcondition_set_name(cv,str) ((char**)(cv)->field1)[5] = (str)
0273 # endif
0274 # endif
0275 # endif
0276 # endif
0277 # endif
0278 # endif
0279 # endif
0280 typedef xcondition_rec *xcondition_t;
0281 typedef xmutex_rec *xmutex_t;
0282 # ifndef xcondition_malloc
0283 # define xcondition_malloc() (xcondition_t)xmalloc(sizeof(xcondition_rec))
0284 # endif
0285 # ifndef xcondition_free
0286 # define xcondition_free(c) xfree((char *)c)
0287 # endif
0288 # ifndef xmutex_malloc
0289 # define xmutex_malloc() (xmutex_t)xmalloc(sizeof(xmutex_rec))
0290 # endif
0291 # ifndef xmutex_free
0292 # define xmutex_free(m) xfree((char *)m)
0293 # endif
0294 # ifndef xthread_have_id
0295 # define xthread_have_id(id) id
0296 # endif
0297 # ifndef xthread_clear_id
0298 # define xthread_clear_id(id) id = 0
0299 # endif
0300 # ifndef xthread_equal
0301 # define xthread_equal(id1,id2) ((id1) == (id2))
0302 # endif
0303
0304 # ifndef xthread_set_name
0305 # define xthread_set_name(t,str)
0306 # endif
0307 # ifndef xmutex_set_name
0308 # define xmutex_set_name(m,str)
0309 # endif
0310 # ifndef xcondition_set_name
0311 # define xcondition_set_name(cv,str)
0312 # endif
0313
0314 #endif