Debian Codegraph @

nx-X11/include/Xthreads.h

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