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