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