home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-bin / x11r6.1 / include / x11 / xthreads.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-17  |  9.8 KB  |  284 lines

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