]> git.sesse.net Git - rdpsrv/blob - Xserver/include/Xthreads.h
Import X server from vnc-3.3.7.
[rdpsrv] / Xserver / include / Xthreads.h
1 /*
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 $
4  *
5  * 
6 Copyright (c) 1993  X Consortium
7
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:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
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.
24
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.
28  * *
29  */
30
31 #ifndef _XTHREADS_H_
32 #define _XTHREADS_H_
33
34 /* Redefine these to XtMalloc/XtFree or whatever you want before including
35  * this header file.
36  */
37 #ifndef xmalloc
38 #define xmalloc malloc
39 #endif
40 #ifndef xfree
41 #define xfree free
42 #endif
43
44 #ifdef CTHREADS
45 #include <cthreads.h>
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)
66 #else /* !CTHREADS */
67 #if defined(SVR4) && !defined(__sgi)
68 #include <thread.h>
69 #include <synch.h>
70 #ifndef LINE_MAX
71 #define LINE_MAX 2048
72 #endif
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)
82 #ifdef sun
83 #define xthread_key_delete(k) 0
84 #else
85 #define xthread_key_delete(k) thr_keydelete(k)
86 #endif
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)
99 #else /* !SVR4 */
100 #ifdef WIN32
101 #define BOOL wBOOL
102 #ifdef Status
103 #undef Status
104 #define Status wStatus
105 #endif
106 #include <windows.h>
107 #ifdef Status
108 #undef Status
109 #define Status int
110 #endif
111 #undef BOOL
112 typedef DWORD xthread_t;
113 typedef DWORD xthread_key_t;
114 struct _xthread_waiter {
115     HANDLE sem;
116     struct _xthread_waiter *next;
117 };
118 typedef struct {
119     CRITICAL_SECTION cs;
120     struct _xthread_waiter *waiters;
121 } xcondition_rec;
122 typedef CRITICAL_SECTION xmutex_rec;
123 #define xthread_init() _Xthread_init()
124 #define xthread_self GetCurrentThreadId
125 #define xthread_fork(func,closure) { \
126     DWORD _tmptid; \
127     CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, (LPVOID)closure, 0, \
128                  &_tmptid); \
129 }
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; \
144 }
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); \
156 }
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; \
162     } \
163     LeaveCriticalSection(&(cv)->cs); \
164 }
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); \
172 }
173 #else /* !WIN32 */
174 #ifdef USE_TIS_SUPPORT
175 /*
176  * TIS support is intended for thread safe libraries.
177  * This should not be used for general client programming.
178  */
179 #include <tis.h>
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)
203 #else
204 #include <pthread.h>
205 #ifndef LINE_MAX
206 #define LINE_MAX 2048
207 #endif
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)
252 #endif /* DEBUG */
253 #endif /* _CMA_VENDOR_ == _CMA__IBM */
254 #endif /* USE_TIS_SUPPORT */
255 #endif /* WIN32 */
256 #endif /* SVR4 */
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))
262 #endif
263 #ifndef xcondition_free
264 #define xcondition_free(c) xfree((char *)c)
265 #endif
266 #ifndef xmutex_malloc
267 #define xmutex_malloc() (xmutex_t)xmalloc(sizeof(xmutex_rec))
268 #endif
269 #ifndef xmutex_free
270 #define xmutex_free(m) xfree((char *)m)
271 #endif
272 #ifndef xthread_have_id
273 #define xthread_have_id(id) id
274 #endif
275 #ifndef xthread_clear_id
276 #define xthread_clear_id(id) id = 0
277 #endif
278 #ifndef xthread_equal
279 #define xthread_equal(id1,id2) ((id1) == (id2))
280 #endif
281 /* aids understood by some debuggers */
282 #ifndef xthread_set_name
283 #define xthread_set_name(t,str)
284 #endif
285 #ifndef xmutex_set_name
286 #define xmutex_set_name(m,str)
287 #endif
288 #ifndef xcondition_set_name
289 #define xcondition_set_name(cv,str)
290 #endif
291
292 #endif /* _XTHREADS_H_ */