1 /* 2 * 3 Copyright 1993, 1998 The Open Group 4 5 Permission to use, copy, modify, distribute, and sell this software and its 6 documentation for any purpose is hereby granted without fee, provided that 7 the above copyright notice appear in all copies and that both that 8 copyright notice and this permission notice appear in supporting 9 documentation. 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 21 Except as contained in this notice, the name of The Open Group shall not be 22 used in advertising or otherwise to promote the sale, use or other dealings 23 in this Software without prior written authorization from The Open Group. 24 * * 25 */ 26 27 #ifndef _XTHREADS_H_ 28 # define _XTHREADS_H_ 29 30 /* Redefine these to XtMalloc/XtFree or whatever you want before including 31 * this header file. 32 */ 33 # ifndef xmalloc 34 # define xmalloc malloc 35 # endif 36 # ifndef xfree 37 # define xfree free 38 # endif 39 40 # ifdef CTHREADS 41 # include <cthreads.h> 42 typedef cthread_t xthread_t; 43 typedef struct condition xcondition_rec; 44 typedef struct mutex xmutex_rec; 45 # define xthread_init() cthread_init() 46 # define xthread_self cthread_self 47 # define xthread_fork(func,closure) cthread_fork(func,closure) 48 # define xthread_yield() cthread_yield() 49 # define xthread_exit(v) cthread_exit(v) 50 # define xthread_set_name(t,str) cthread_set_name(t,str) 51 # define xmutex_init(m) mutex_init(m) 52 # define xmutex_clear(m) mutex_clear(m) 53 # define xmutex_lock(m) mutex_lock(m) 54 # define xmutex_unlock(m) mutex_unlock(m) 55 # define xmutex_set_name(m,str) mutex_set_name(m,str) 56 # define xcondition_init(cv) condition_init(cv) 57 # define xcondition_clear(cv) condition_clear(cv) 58 # define xcondition_wait(cv,m) condition_wait(cv,m) 59 # define xcondition_signal(cv) condition_signal(cv) 60 # define xcondition_broadcast(cv) condition_broadcast(cv) 61 # define xcondition_set_name(cv,str) condition_set_name(cv,str) 62 # else /* !CTHREADS */ 63 # if defined(SVR4) 64 # include <thread.h> 65 # include <synch.h> 66 typedef thread_t xthread_t; 67 typedef thread_key_t xthread_key_t; 68 typedef cond_t xcondition_rec; 69 typedef mutex_t xmutex_rec; 70 # if defined(__UNIXWARE__) 71 extern xthread_t (*_x11_thr_self)(); 72 # define xthread_self (_x11_thr_self) 73 # else 74 # define xthread_self thr_self 75 # endif 76 # define xthread_fork(func,closure) thr_create(NULL,0,func,closure,THR_NEW_LWP|THR_DETACHED,NULL) 77 # define xthread_yield() thr_yield() 78 # define xthread_exit(v) thr_exit(v) 79 # define xthread_key_create(kp,d) thr_keycreate(kp,d) 80 # ifdef __sun 81 # define xthread_key_delete(k) 0 82 # else 83 # define xthread_key_delete(k) thr_keydelete(k) 84 # endif 85 # define xthread_set_specific(k,v) thr_setspecific(k,v) 86 # define xthread_get_specific(k,vp) thr_getspecific(k,vp) 87 # define xmutex_init(m) mutex_init(m,USYNC_THREAD,0) 88 # define xmutex_clear(m) mutex_destroy(m) 89 # define xmutex_lock(m) mutex_lock(m) 90 # define xmutex_unlock(m) mutex_unlock(m) 91 # define xcondition_init(cv) cond_init(cv,USYNC_THREAD,0) 92 # define xcondition_clear(cv) cond_destroy(cv) 93 # define xcondition_wait(cv,m) cond_wait(cv,m) 94 # define xcondition_signal(cv) cond_signal(cv) 95 # define xcondition_broadcast(cv) cond_broadcast(cv) 96 # else /* !SVR4 */ 97 # ifdef WIN32 98 # include <X11/Xwindows.h> 99 typedef DWORD xthread_t; 100 typedef DWORD xthread_key_t; 101 struct _xthread_waiter { 102 HANDLE sem; 103 struct _xthread_waiter *next; 104 }; 105 typedef struct { 106 CRITICAL_SECTION cs; 107 struct _xthread_waiter *waiters; 108 } xcondition_rec; 109 typedef CRITICAL_SECTION xmutex_rec; 110 # define xthread_init() _Xthread_init() 111 # define xthread_self GetCurrentThreadId 112 # define xthread_fork(func,closure) { \ 113 DWORD _tmptid; \ 114 CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, (LPVOID)closure, 0, \ 115 &_tmptid); \ 116 } 117 # define xthread_yield() Sleep(0) 118 # define xthread_exit(v) ExitThread((DWORD)(v)) 119 # define xthread_key_create(kp,d) *(kp) = TlsAlloc() 120 # define xthread_key_delete(k) TlsFree(k) 121 # define xthread_set_specific(k,v) TlsSetValue(k,v) 122 # define xthread_get_specific(k,vp) TlsGetValue(k) 123 # define xmutex_init(m) InitializeCriticalSection(m) 124 # define xmutex_clear(m) DeleteCriticalSection(m) 125 # define _XMUTEX_NESTS 126 # define xmutex_lock(m) EnterCriticalSection(m) 127 # define xmutex_unlock(m) LeaveCriticalSection(m) 128 # define xcondition_init(cv) { \ 129 InitializeCriticalSection(&(cv)->cs); \ 130 (cv)->waiters = NULL; \ 131 } 132 # define xcondition_clear(cv) DeleteCriticalSection(&(cv)->cs) 133 extern struct _xthread_waiter *_Xthread_waiter(); 134 # define xcondition_wait(cv,m) { \ 135 struct _xthread_waiter *_tmpthr = _Xthread_waiter(); \ 136 EnterCriticalSection(&(cv)->cs); \ 137 _tmpthr->next = (cv)->waiters; \ 138 (cv)->waiters = _tmpthr; \ 139 LeaveCriticalSection(&(cv)->cs); \ 140 LeaveCriticalSection(m); \ 141 WaitForSingleObject(_tmpthr->sem, INFINITE); \ 142 EnterCriticalSection(m); \ 143 } 144 # define xcondition_signal(cv) { \ 145 EnterCriticalSection(&(cv)->cs); \ 146 if ((cv)->waiters) { \ 147 ReleaseSemaphore((cv)->waiters->sem, 1, NULL); \ 148 (cv)->waiters = (cv)->waiters->next; \ 149 } \ 150 LeaveCriticalSection(&(cv)->cs); \ 151 } 152 # define xcondition_broadcast(cv) { \ 153 struct _xthread_waiter *_tmpthr; \ 154 EnterCriticalSection(&(cv)->cs); \ 155 for (_tmpthr = (cv)->waiters; _tmpthr; _tmpthr = _tmpthr->next) \ 156 ReleaseSemaphore(_tmpthr->sem, 1, NULL); \ 157 (cv)->waiters = NULL; \ 158 LeaveCriticalSection(&(cv)->cs); \ 159 } 160 # else /* !WIN32 */ 161 # ifdef USE_TIS_SUPPORT 162 /* 163 * TIS support is intended for thread safe libraries. 164 * This should not be used for general client programming. 165 */ 166 # include <tis.h> 167 typedef pthread_t xthread_t; 168 typedef pthread_key_t xthread_key_t; 169 typedef pthread_cond_t xcondition_rec; 170 typedef pthread_mutex_t xmutex_rec; 171 # define xthread_self tis_self 172 # define xthread_fork(func,closure) { pthread_t _tmpxthr; \ 173 pthread_create(&_tmpxthr,NULL,func,closure); } 174 # define xthread_yield() pthread_yield_np() 175 # define xthread_exit(v) pthread_exit(v) 176 # define xthread_key_create(kp,d) tis_key_create(kp,d) 177 # define xthread_key_delete(k) tis_key_delete(k) 178 # define xthread_set_specific(k,v) tis_setspecific(k,v) 179 # define xthread_get_specific(k,vp) *(vp) = tis_getspecific(k) 180 # define XMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 181 # define xmutex_init(m) tis_mutex_init(m) 182 # define xmutex_clear(m) tis_mutex_destroy(m) 183 # define xmutex_lock(m) tis_mutex_lock(m) 184 # define xmutex_unlock(m) tis_mutex_unlock(m) 185 # define xcondition_init(c) tis_cond_init(c) 186 # define xcondition_clear(c) tis_cond_destroy(c) 187 # define xcondition_wait(c,m) tis_cond_wait(c,m) 188 # define xcondition_signal(c) tis_cond_signal(c) 189 # define xcondition_broadcast(c) tis_cond_broadcast(c) 190 # else 191 # ifdef USE_NBSD_THREADLIB 192 /* 193 * NetBSD threadlib support is intended for thread safe libraries. 194 * This should not be used for general client programming. 195 */ 196 # include <threadlib.h> 197 typedef thr_t xthread_t; 198 typedef thread_key_t xthread_key_t; 199 typedef cond_t xcondition_rec; 200 typedef mutex_t xmutex_rec; 201 # define xthread_self thr_self 202 # define xthread_fork(func,closure) { thr_t _tmpxthr; \ 203 /* XXX Create it detached? --thorpej */ \ 204 thr_create(&_tmpxthr,NULL,func,closure); } 205 # define xthread_yield() thr_yield() 206 # define xthread_exit(v) thr_exit(v) 207 # define xthread_key_create(kp,d) thr_keycreate(kp,d) 208 # define xthread_key_delete(k) thr_keydelete(k) 209 # define xthread_set_specific(k,v) thr_setspecific(k,v) 210 # define xthread_get_specific(k,vp) *(vp) = thr_getspecific(k) 211 # define XMUTEX_INITIALIZER MUTEX_INITIALIZER 212 # define xmutex_init(m) mutex_init(m, 0) 213 # define xmutex_clear(m) mutex_destroy(m) 214 # define xmutex_lock(m) mutex_lock(m) 215 # define xmutex_unlock(m) mutex_unlock(m) 216 # define xcondition_init(c) cond_init(c, 0, 0) 217 # define xcondition_clear(c) cond_destroy(c) 218 # define xcondition_wait(c,m) cond_wait(c,m) 219 # define xcondition_signal(c) cond_signal(c) 220 # define xcondition_broadcast(c) cond_broadcast(c) 221 # else 222 # include <pthread.h> 223 typedef pthread_t xthread_t; 224 typedef pthread_key_t xthread_key_t; 225 typedef pthread_cond_t xcondition_rec; 226 typedef pthread_mutex_t xmutex_rec; 227 # define xthread_self pthread_self 228 # define xthread_yield() pthread_yield() 229 # define xthread_exit(v) pthread_exit(v) 230 # define xthread_set_specific(k,v) pthread_setspecific(k,v) 231 # define xmutex_clear(m) pthread_mutex_destroy(m) 232 # define xmutex_lock(m) pthread_mutex_lock(m) 233 # define xmutex_unlock(m) pthread_mutex_unlock(m) 234 # ifndef XPRE_STANDARD_API 235 # define xthread_key_create(kp,d) pthread_key_create(kp,d) 236 # define xthread_key_delete(k) pthread_key_delete(k) 237 # define xthread_get_specific(k,vp) *(vp) = pthread_getspecific(k) 238 # define xthread_fork(func,closure) { pthread_t _tmpxthr; \ 239 pthread_create(&_tmpxthr,NULL,func,closure); } 240 # define XMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 241 # define xmutex_init(m) pthread_mutex_init(m, NULL) 242 # define xcondition_init(c) pthread_cond_init(c, NULL) 243 # else /* XPRE_STANDARD_API */ 244 # define xthread_key_create(kp,d) pthread_keycreate(kp,d) 245 # define xthread_key_delete(k) 0 246 # define xthread_get_specific(k,vp) pthread_getspecific(k,vp) 247 # define xthread_fork(func,closure) { pthread_t _tmpxthr; \ 248 pthread_create(&_tmpxthr,pthread_attr_default,func,closure); } 249 # define xmutex_init(m) pthread_mutex_init(m, pthread_mutexattr_default) 250 # define xcondition_init(c) pthread_cond_init(c, pthread_condattr_default) 251 # endif /* XPRE_STANDARD_API */ 252 # define xcondition_clear(c) pthread_cond_destroy(c) 253 # define xcondition_wait(c,m) pthread_cond_wait(c,m) 254 # define xcondition_signal(c) pthread_cond_signal(c) 255 # define xcondition_broadcast(c) pthread_cond_broadcast(c) 256 # if defined(_DECTHREADS_) 257 static xthread_t _X_no_thread_id; 258 # define xthread_have_id(id) !pthread_equal(id, _X_no_thread_id) 259 # define xthread_clear_id(id) id = _X_no_thread_id 260 # define xthread_equal(id1,id2) pthread_equal(id1, id2) 261 # endif /* _DECTHREADS_ */ 262 # if defined(__linux__) 263 # define xthread_have_id(id) !pthread_equal(id, 0) 264 # define xthread_clear_id(id) id = 0 265 # define xthread_equal(id1,id2) pthread_equal(id1, id2) 266 # endif /* linux */ 267 # if defined(_CMA_VENDOR_) && defined(_CMA__IBM) && (_CMA_VENDOR_ == _CMA__IBM) 268 # ifdef DEBUG /* too much of a hack to enable normally */ 269 /* see also cma__obj_set_name() */ 270 # define xmutex_set_name(m,str) ((char**)(m)->field1)[5] = (str) 271 # define xcondition_set_name(cv,str) ((char**)(cv)->field1)[5] = (str) 272 # endif /* DEBUG */ 273 # endif /* _CMA_VENDOR_ == _CMA__IBM */ 274 # endif /* USE_NBSD_THREADLIB */ 275 # endif /* USE_TIS_SUPPORT */ 276 # endif /* WIN32 */ 277 # endif /* SVR4 */ 278 # endif /* CTHREADS */ 279 typedef xcondition_rec *xcondition_t; 280 typedef xmutex_rec *xmutex_t; 281 # ifndef xcondition_malloc 282 # define xcondition_malloc() (xcondition_t)xmalloc(sizeof(xcondition_rec)) 283 # endif 284 # ifndef xcondition_free 285 # define xcondition_free(c) xfree((char *)c) 286 # endif 287 # ifndef xmutex_malloc 288 # define xmutex_malloc() (xmutex_t)xmalloc(sizeof(xmutex_rec)) 289 # endif 290 # ifndef xmutex_free 291 # define xmutex_free(m) xfree((char *)m) 292 # endif 293 # ifndef xthread_have_id 294 # define xthread_have_id(id) id 295 # endif 296 # ifndef xthread_clear_id 297 # define xthread_clear_id(id) id = 0 298 # endif 299 # ifndef xthread_equal 300 # define xthread_equal(id1,id2) ((id1) == (id2)) 301 # endif 302 /* aids understood by some debuggers */ 303 # ifndef xthread_set_name 304 # define xthread_set_name(t,str) 305 # endif 306 # ifndef xmutex_set_name 307 # define xmutex_set_name(m,str) 308 # endif 309 # ifndef xcondition_set_name 310 # define xcondition_set_name(cv,str) 311 # endif 312 313 #endif /* _XTHREADS_H_ */ 314