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