1 /* Basic platform-independent macro definitions for mutexes and
3 Copyright (C) 1996 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Wolfram Gloger <wmglo@dent.med.uni-muenchen.de>, 1996.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* One out of _LIBC, USE_PTHREADS, USE_THR or USE_SPROC should be
23 defined, otherwise the token NO_THREADS and dummy implementations
24 of the macros will be defined. */
29 #if defined(_LIBC) /* The GNU C library, a special case of Posix threads */
31 #include <libc-lock.h>
33 #ifdef PTHREAD_MUTEX_INITIALIZER
35 typedef pthread_t thread_id
;
38 typedef pthread_mutex_t mutex_t
;
40 /* thread specific data */
41 typedef pthread_key_t tsd_key_t
;
43 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
45 static Void_t
*malloc_key_data
;
47 #define tsd_key_create(key, destr) \
48 if (__pthread_key_create != NULL) { \
49 __pthread_key_create(key, destr); \
50 } else { *(key) = (tsd_key_t) 0; }
51 #define tsd_setspecific(key, data) \
52 if (__pthread_setspecific != NULL) { \
53 __pthread_setspecific(key, data); \
54 } else { malloc_key_data = (Void_t *) data; }
55 #define tsd_getspecific(key, vptr) \
56 (vptr = (__pthread_getspecific != NULL \
57 ? __pthread_getspecific(key) : malloc_key_data))
59 #define mutex_init(m) \
60 (__pthread_mutex_init != NULL ? __pthread_mutex_init (m, NULL) : 0)
61 #define mutex_lock(m) \
62 (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (m) : 0)
63 #define mutex_trylock(m) \
64 (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (m) : 0)
65 #define mutex_unlock(m) \
66 (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (m) : 0)
68 #elif defined(MUTEX_INITIALIZER)
70 typedef thread_t thread_id
;
73 typedef mutex_t mutex_t
;
75 /* thread specific data */
76 typedef pthread_key_t tsd_key_t
;
78 #define mutex_init(m) __mutex_init (m)
79 #define mutex_lock(m) __mutex_lock (m)
80 #define mutex_trylock(m) __mutex_trylock (m)
81 #define mutex_unlock(m) __mutex_unlock (m)
87 #endif /* MUTEX_INITIALIZER && PTHREAD_MUTEX_INITIALIZER */
89 #elif defined(USE_PTHREADS) /* Posix threads */
93 typedef pthread_t thread_id
;
96 typedef pthread_mutex_t mutex_t
;
98 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
99 #define mutex_init(m) pthread_mutex_init(m, NULL)
100 #define mutex_lock(m) pthread_mutex_lock(m)
101 #define mutex_trylock(m) pthread_mutex_trylock(m)
102 #define mutex_unlock(m) pthread_mutex_unlock(m)
104 /* thread specific data */
105 typedef pthread_key_t tsd_key_t
;
107 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
108 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
109 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
111 #elif USE_THR /* Solaris threads */
115 typedef thread_t thread_id
;
117 #define MUTEX_INITIALIZER { 0 }
118 #define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
121 * Hack for thread-specific data on Solaris. We can't use thr_setspecific
122 * because that function calls malloc() itself.
124 typedef void *tsd_key_t
[256];
125 #define tsd_key_create(key, destr) do { \
127 for(i=0; i<256; i++) (*key)[i] = 0; \
129 #define tsd_setspecific(key, data) (key[(unsigned)thr_self() % 256] = (data))
130 #define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
132 #elif USE_SPROC /* SGI sproc() threads */
134 #include <sys/wait.h>
135 #include <sys/types.h>
136 #include <sys/prctl.h>
137 #include <abi_mutex.h>
139 typedef int thread_id
;
141 typedef abilock_t mutex_t
;
143 #define MUTEX_INITIALIZER { 0 }
144 #define mutex_init(m) init_lock(m)
145 #define mutex_lock(m) (spin_lock(m), 0)
146 #define mutex_trylock(m) acquire_lock(m)
147 #define mutex_unlock(m) release_lock(m)
149 typedef int tsd_key_t
;
151 #define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
152 #define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
153 #define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
155 #else /* no _LIBC or USE_... are defined */
159 #endif /* defined(_LIBC) */
161 #ifdef NO_THREADS /* No threads, provide dummy macros */
163 typedef int thread_id
;
167 #define MUTEX_INITIALIZER 0
168 #define mutex_init(m) (*(m) = 0)
169 #define mutex_lock(m) (0)
170 #define mutex_trylock(m) (0)
171 #define mutex_unlock(m) (0)
173 typedef void *tsd_key_t
;
174 #define tsd_key_create(key, destr) do {} while(0)
175 #define tsd_setspecific(key, data) do {} while(0)
176 #define tsd_getspecific(key, vptr) (vptr = NULL)
178 #endif /* defined(NO_THREADS) */
180 #endif /* !defined(_THREAD_M_H) */