summaryrefslogtreecommitdiff
path: root/Xthreads.h
blob: 2027127eb9347d12ba2a6bf4b08de5249eb18e60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 *
Copyright 1993, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
 * *
 */

#ifndef _XTHREADS_H_
# define _XTHREADS_H_

/* Redefine these to XtMalloc/XtFree or whatever you want before including
 * this header file.
 */
# ifndef xmalloc
#  define xmalloc malloc
# endif
# ifndef xfree
#  define xfree free
# endif

# ifdef CTHREADS
#  include <cthreads.h>
typedef cthread_t xthread_t;
typedef struct condition xcondition_rec;
typedef struct mutex xmutex_rec;
#  define xthread_init() cthread_init()
#  define xthread_self cthread_self
#  define xthread_fork(func,closure) cthread_fork(func,closure)
#  define xthread_yield() cthread_yield()
#  define xthread_exit(v) cthread_exit(v)
#  define xthread_set_name(t,str) cthread_set_name(t,str)
#  define xmutex_init(m) mutex_init(m)
#  define xmutex_clear(m) mutex_clear(m)
#  define xmutex_lock(m) mutex_lock(m)
#  define xmutex_unlock(m) mutex_unlock(m)
#  define xmutex_set_name(m,str) mutex_set_name(m,str)
#  define xcondition_init(cv) condition_init(cv)
#  define xcondition_clear(cv) condition_clear(cv)
#  define xcondition_wait(cv,m) condition_wait(cv,m)
#  define xcondition_signal(cv) condition_signal(cv)
#  define xcondition_broadcast(cv) condition_broadcast(cv)
#  define xcondition_set_name(cv,str) condition_set_name(cv,str)
# else /* !CTHREADS */
#  if defined(SVR4)
#   include <thread.h>
#   include <synch.h>
typedef thread_t xthread_t;
typedef thread_key_t xthread_key_t;
typedef cond_t xcondition_rec;
typedef mutex_t xmutex_rec;
#   if defined(__UNIXWARE__)
extern xthread_t (*_x11_thr_self)();
#    define xthread_self  (_x11_thr_self)
#   else
#    define xthread_self thr_self
#   endif
#   define xthread_fork(func,closure) thr_create(NULL,0,func,closure,THR_NEW_LWP|THR_DETACHED,NULL)
#   define xthread_yield() thr_yield()
#   define xthread_exit(v) thr_exit(v)
#   define xthread_key_create(kp,d) thr_keycreate(kp,d)
#   ifdef __sun
#    define xthread_key_delete(k) 0
#   else
#    define xthread_key_delete(k) thr_keydelete(k)
#   endif
#   define xthread_set_specific(k,v) thr_setspecific(k,v)
#   define xthread_get_specific(k,vp) thr_getspecific(k,vp)
#   define xmutex_init(m) mutex_init(m,USYNC_THREAD,0)
#   define xmutex_clear(m) mutex_destroy(m)
#   define xmutex_lock(m) mutex_lock(m)
#   define xmutex_unlock(m) mutex_unlock(m)
#   define xcondition_init(cv) cond_init(cv,USYNC_THREAD,0)
#   define xcondition_clear(cv) cond_destroy(cv)
#   define xcondition_wait(cv,m) cond_wait(cv,m)
#   define xcondition_signal(cv) cond_signal(cv)
#   define xcondition_broadcast(cv) cond_broadcast(cv)
#  else /* !SVR4 */
#   ifdef WIN32
#    include <X11/Xwindows.h>
typedef DWORD xthread_t;
typedef DWORD xthread_key_t;
struct _xthread_waiter {
    HANDLE sem;
    struct _xthread_waiter *next;
};
typedef struct {
    CRITICAL_SECTION cs;
    struct _xthread_waiter *waiters;
} xcondition_rec;
typedef CRITICAL_SECTION xmutex_rec;
extern void _Xthread_init(void);
#    define xthread_init() _Xthread_init()
#    define xthread_self GetCurrentThreadId
#    define xthread_fork(func,closure) { \
    DWORD _tmptid; \
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, (LPVOID)closure, 0, \
		 &_tmptid); \
}
#    define xthread_yield() Sleep(0)
#    define xthread_exit(v) ExitThread((DWORD)(v))
#    define xthread_key_create(kp,d) *(kp) = TlsAlloc()
#    define xthread_key_delete(k) TlsFree(k)
#    define xthread_set_specific(k,v) TlsSetValue(k,v)
#    define xthread_get_specific(k,vp) TlsGetValue(k)
#    define xmutex_init(m) InitializeCriticalSection(m)
#    define xmutex_clear(m) DeleteCriticalSection(m)
#    define _XMUTEX_NESTS
#    define xmutex_lock(m) EnterCriticalSection(m)
#    define xmutex_unlock(m) LeaveCriticalSection(m)
#    define xcondition_init(cv) { \
    InitializeCriticalSection(&(cv)->cs); \
    (cv)->waiters = NULL; \
}
#    define xcondition_clear(cv) DeleteCriticalSection(&(cv)->cs)
extern struct _xthread_waiter *_Xthread_waiter();
#    define xcondition_wait(cv,m) { \
    struct _xthread_waiter *_tmpthr = _Xthread_waiter(); \
    EnterCriticalSection(&(cv)->cs); \
    _tmpthr->next = (cv)->waiters; \
    (cv)->waiters = _tmpthr; \
    LeaveCriticalSection(&(cv)->cs); \
    LeaveCriticalSection(m); \
    WaitForSingleObject(_tmpthr->sem, INFINITE); \
    EnterCriticalSection(m); \
}
#    define xcondition_signal(cv) { \
    EnterCriticalSection(&(cv)->cs); \
    if ((cv)->waiters) { \
        ReleaseSemaphore((cv)->waiters->sem, 1, NULL); \
	(cv)->waiters = (cv)->waiters->next; \
    } \
    LeaveCriticalSection(&(cv)->cs); \
}
#    define xcondition_broadcast(cv) { \
    struct _xthread_waiter *_tmpthr; \
    EnterCriticalSection(&(cv)->cs); \
    for (_tmpthr = (cv)->waiters; _tmpthr; _tmpthr = _tmpthr->next) \
	ReleaseSemaphore(_tmpthr->sem, 1, NULL); \
    (cv)->waiters = NULL; \
    LeaveCriticalSection(&(cv)->cs); \
}
#   else /* !WIN32 */
#    ifdef USE_TIS_SUPPORT
/*
 * TIS support is intended for thread safe libraries.
 * This should not be used for general client programming.
 */
#     include <tis.h>
typedef pthread_t xthread_t;
typedef pthread_key_t xthread_key_t;
typedef pthread_cond_t xcondition_rec;
typedef pthread_mutex_t xmutex_rec;
#     define xthread_self tis_self
#     define xthread_fork(func,closure) { pthread_t _tmpxthr; \
        pthread_create(&_tmpxthr,NULL,func,closure); }
#     define xthread_yield() pthread_yield_np()
#     define xthread_exit(v) pthread_exit(v)
#     define xthread_key_create(kp,d) tis_key_create(kp,d)
#     define xthread_key_delete(k) tis_key_delete(k)
#     define xthread_set_specific(k,v) tis_setspecific(k,v)
#     define xthread_get_specific(k,vp) *(vp) = tis_getspecific(k)
#     define XMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#     define xmutex_init(m) tis_mutex_init(m)
#     define xmutex_clear(m) tis_mutex_destroy(m)
#     define xmutex_lock(m) tis_mutex_lock(m)
#     define xmutex_unlock(m) tis_mutex_unlock(m)
#     define xcondition_init(c) tis_cond_init(c)
#     define xcondition_clear(c) tis_cond_destroy(c)
#     define xcondition_wait(c,m) tis_cond_wait(c,m)
#     define xcondition_signal(c) tis_cond_signal(c)
#     define xcondition_broadcast(c) tis_cond_broadcast(c)
#    else
#     ifdef USE_NBSD_THREADLIB
/*
 * NetBSD threadlib support is intended for thread safe libraries.
 * This should not be used for general client programming.
 */
#      include <threadlib.h>
typedef thr_t xthread_t;
typedef thread_key_t xthread_key_t;
typedef cond_t xcondition_rec;
typedef mutex_t xmutex_rec;
#      define xthread_self thr_self
#      define xthread_fork(func,closure) { thr_t _tmpxthr; \
	/* XXX Create it detached?  --thorpej */ \
	thr_create(&_tmpxthr,NULL,func,closure); }
#      define xthread_yield() thr_yield()
#      define xthread_exit(v) thr_exit(v)
#      define xthread_key_create(kp,d) thr_keycreate(kp,d)
#      define xthread_key_delete(k) thr_keydelete(k)
#      define xthread_set_specific(k,v) thr_setspecific(k,v)
#      define xthread_get_specific(k,vp) *(vp) = thr_getspecific(k)
#      define XMUTEX_INITIALIZER MUTEX_INITIALIZER
#      define xmutex_init(m) mutex_init(m, 0)
#      define xmutex_clear(m) mutex_destroy(m)
#      define xmutex_lock(m) mutex_lock(m)
#      define xmutex_unlock(m) mutex_unlock(m)
#      define xcondition_init(c) cond_init(c, 0, 0)
#      define xcondition_clear(c) cond_destroy(c)
#      define xcondition_wait(c,m) cond_wait(c,m)
#      define xcondition_signal(c) cond_signal(c)
#      define xcondition_broadcast(c) cond_broadcast(c)
#     else
#      include <pthread.h>
typedef pthread_t xthread_t;
typedef pthread_key_t xthread_key_t;
typedef pthread_cond_t xcondition_rec;
typedef pthread_mutex_t xmutex_rec;
#      define xthread_self pthread_self
#      define xthread_yield() pthread_yield()
#      define xthread_exit(v) pthread_exit(v)
#      define xthread_set_specific(k,v) pthread_setspecific(k,v)
#      define xmutex_clear(m) pthread_mutex_destroy(m)
#      define xmutex_lock(m) pthread_mutex_lock(m)
#      define xmutex_unlock(m) pthread_mutex_unlock(m)
#      ifndef XPRE_STANDARD_API
#       define xthread_key_create(kp,d) pthread_key_create(kp,d)
#       define xthread_key_delete(k) pthread_key_delete(k)
#       define xthread_get_specific(k,vp) *(vp) = pthread_getspecific(k)
#       define xthread_fork(func,closure) { pthread_t _tmpxthr; \
	pthread_create(&_tmpxthr,NULL,func,closure); }
#       define XMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#       define xmutex_init(m) pthread_mutex_init(m, NULL)
#       define xcondition_init(c) pthread_cond_init(c, NULL)
#      else /* XPRE_STANDARD_API */
#       define xthread_key_create(kp,d) pthread_keycreate(kp,d)
#       define xthread_key_delete(k) 0
#       define xthread_get_specific(k,vp) pthread_getspecific(k,vp)
#       define xthread_fork(func,closure) { pthread_t _tmpxthr; \
	pthread_create(&_tmpxthr,pthread_attr_default,func,closure); }
#       define xmutex_init(m) pthread_mutex_init(m, pthread_mutexattr_default)
#       define xcondition_init(c) pthread_cond_init(c, pthread_condattr_default)
#      endif /* XPRE_STANDARD_API */
#      define xcondition_clear(c) pthread_cond_destroy(c)
#      define xcondition_wait(c,m) pthread_cond_wait(c,m)
#      define xcondition_signal(c) pthread_cond_signal(c)
#      define xcondition_broadcast(c) pthread_cond_broadcast(c)
#      if defined(_DECTHREADS_)
static xthread_t _X_no_thread_id;
#       define xthread_have_id(id) !pthread_equal(id, _X_no_thread_id)
#       define xthread_clear_id(id) id = _X_no_thread_id
#       define xthread_equal(id1,id2) pthread_equal(id1, id2)
#      endif /* _DECTHREADS_ */
#      if defined(__linux__)
#       define xthread_have_id(id) !pthread_equal(id, 0)
#       define xthread_clear_id(id) id = 0
#       define xthread_equal(id1,id2) pthread_equal(id1, id2)
#      endif /* linux */
#      if defined(_CMA_VENDOR_) && defined(_CMA__IBM) && (_CMA_VENDOR_ == _CMA__IBM)
#       ifdef DEBUG		/* too much of a hack to enable normally */
/* see also cma__obj_set_name() */
#        define xmutex_set_name(m,str) ((char**)(m)->field1)[5] = (str)
#        define xcondition_set_name(cv,str) ((char**)(cv)->field1)[5] = (str)
#       endif /* DEBUG */
#      endif /* _CMA_VENDOR_ == _CMA__IBM */
#     endif /* USE_NBSD_THREADLIB */
#    endif /* USE_TIS_SUPPORT */
#   endif /* WIN32 */
#  endif /* SVR4 */
# endif /* CTHREADS */
typedef xcondition_rec *xcondition_t;
typedef xmutex_rec *xmutex_t;
# ifndef xcondition_malloc
#  define xcondition_malloc() (xcondition_t)xmalloc(sizeof(xcondition_rec))
# endif
# ifndef xcondition_free
#  define xcondition_free(c) xfree((char *)c)
# endif
# ifndef xmutex_malloc
#  define xmutex_malloc() (xmutex_t)xmalloc(sizeof(xmutex_rec))
# endif
# ifndef xmutex_free
#  define xmutex_free(m) xfree((char *)m)
# endif
# ifndef xthread_have_id
#  define xthread_have_id(id) id
# endif
# ifndef xthread_clear_id
#  define xthread_clear_id(id) id = 0
# endif
# ifndef xthread_equal
#  define xthread_equal(id1,id2) ((id1) == (id2))
# endif
/* aids understood by some debuggers */
# ifndef xthread_set_name
#  define xthread_set_name(t,str)
# endif
# ifndef xmutex_set_name
#  define xmutex_set_name(m,str)
# endif
# ifndef xcondition_set_name
#  define xcondition_set_name(cv,str)
# endif

#endif /* _XTHREADS_H_ */