Xthreads.h 12 KB

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