Update.
[glibc.git] / malloc / thread-m.h
blobd9e17397cec198a09b1b9238125719d0a0f0e338
1 /* Basic platform-independent macro definitions for mutexes and
2 thread-specific data.
3 Copyright (C) 1996, 1997, 1998 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 /* $Id$
23 One out of _LIBC, USE_PTHREADS, USE_THR or USE_SPROC should be
24 defined, otherwise the token NO_THREADS and dummy implementations
25 of the macros will be defined. */
27 #ifndef _THREAD_M_H
28 #define _THREAD_M_H
30 #undef thread_atfork_static
32 #if defined(_LIBC) /* The GNU C library, a special case of Posix threads */
34 #include <bits/libc-lock.h>
36 #ifdef PTHREAD_MUTEX_INITIALIZER
38 typedef pthread_t thread_id;
40 /* mutex */
41 typedef pthread_mutex_t mutex_t;
43 /* thread specific data */
44 typedef void * tsd_key_t;
46 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
48 #define tsd_key_create(key, destr) ( *(key) = NULL )
49 #define tsd_setspecific(key, data) \
50 if (__libc_internal_tsd_set != NULL) { \
51 __libc_internal_tsd_set(_LIBC_TSD_KEY_MALLOC, data); \
52 } else { (key) = (Void_t *) data; }
53 #define tsd_getspecific(key, vptr) \
54 (vptr = (__libc_internal_tsd_get != NULL \
55 ? __libc_internal_tsd_get(_LIBC_TSD_KEY_MALLOC) : (key)))
57 #define mutex_init(m) \
58 (__pthread_mutex_init != NULL ? __pthread_mutex_init (m, NULL) : 0)
59 #define mutex_lock(m) \
60 (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (m) : 0)
61 #define mutex_trylock(m) \
62 (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (m) : 0)
63 #define mutex_unlock(m) \
64 (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (m) : 0)
66 #define thread_atfork(prepare, parent, child) \
67 (__pthread_atfork != NULL ? __pthread_atfork(prepare, parent, child) : 0)
69 #elif defined(MUTEX_INITIALIZER)
70 /* Assume hurd, with cthreads */
72 /* Cthreads `mutex_t' is a pointer to a mutex, and malloc wants just the
73 mutex itself. */
74 #undef mutex_t
75 #define mutex_t struct mutex
77 #undef mutex_init
78 #define mutex_init(m) (__mutex_init(m), 0)
80 #undef mutex_lock
81 #define mutex_lock(m) (__mutex_lock(m), 0)
83 #undef mutex_unlock
84 #define mutex_unlock(m) (__mutex_unlock(m), 0)
86 #define mutex_trylock(m) (!__mutex_trylock(m))
88 #include <hurd/threadvar.h>
90 /* thread specific data */
91 typedef int tsd_key_t;
93 static int tsd_keys_alloced = 0;
95 #define tsd_key_create(key, destr) \
96 (assert (tsd_keys_alloced == 0), tsd_keys_alloced++)
97 #define tsd_setspecific(key, data) \
98 (*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC) = (unsigned long)(data))
99 #define tsd_getspecific(key, vptr) \
100 ((vptr) = (void *)*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC))
102 #define thread_atfork(prepare, parent, child) do {} while(0)
103 #define thread_atfork_static(prepare, parent, child) \
104 text_set_element(_hurd_fork_prepare_hook, prepare); \
105 text_set_element(_hurd_fork_parent_hook, parent); \
106 text_set_element(_hurd_fork_child_hook, child);
108 /* No we're *not* using pthreads. */
109 #define __pthread_initialize ((void (*)(void))0)
111 #else
113 #define NO_THREADS
115 #endif /* MUTEX_INITIALIZER && PTHREAD_MUTEX_INITIALIZER */
117 #elif defined(USE_PTHREADS) /* Posix threads */
119 #include <pthread.h>
121 typedef pthread_t thread_id;
123 /* mutex */
124 typedef pthread_mutex_t mutex_t;
126 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
127 #define mutex_init(m) pthread_mutex_init(m, NULL)
128 #define mutex_lock(m) pthread_mutex_lock(m)
129 #define mutex_trylock(m) pthread_mutex_trylock(m)
130 #define mutex_unlock(m) pthread_mutex_unlock(m)
132 /* thread specific data */
133 #if defined(__sgi) || defined(USE_TSD_DATA_HACK)
135 /* Hack for thread-specific data, e.g. on Irix 6.x. We can't use
136 pthread_setspecific because that function calls malloc() itself.
137 The hack only works when pthread_t can be converted to an integral
138 type. */
140 typedef void *tsd_key_t[256];
141 #define tsd_key_create(key, destr) do { \
142 int i; \
143 for(i=0; i<256; i++) (*key)[i] = 0; \
144 } while(0)
145 #define tsd_setspecific(key, data) \
146 (key[(unsigned)pthread_self() % 256] = (data))
147 #define tsd_getspecific(key, vptr) \
148 (vptr = key[(unsigned)pthread_self() % 256])
150 #else
152 typedef pthread_key_t tsd_key_t;
154 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
155 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
156 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
158 #endif
160 /* at fork */
161 #define thread_atfork(prepare, parent, child) \
162 pthread_atfork(prepare, parent, child)
164 #elif USE_THR /* Solaris threads */
166 #include <thread.h>
168 typedef thread_t thread_id;
170 #define MUTEX_INITIALIZER { 0 }
171 #define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
174 * Hack for thread-specific data on Solaris. We can't use thr_setspecific
175 * because that function calls malloc() itself.
177 typedef void *tsd_key_t[256];
178 #define tsd_key_create(key, destr) do { \
179 int i; \
180 for(i=0; i<256; i++) (*key)[i] = 0; \
181 } while(0)
182 #define tsd_setspecific(key, data) (key[(unsigned)thr_self() % 256] = (data))
183 #define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
185 #define thread_atfork(prepare, parent, child) do {} while(0)
187 #elif USE_SPROC /* SGI sproc() threads */
189 #include <sys/wait.h>
190 #include <sys/types.h>
191 #include <sys/prctl.h>
192 #include <abi_mutex.h>
194 typedef int thread_id;
196 typedef abilock_t mutex_t;
198 #define MUTEX_INITIALIZER { 0 }
199 #define mutex_init(m) init_lock(m)
200 #define mutex_lock(m) (spin_lock(m), 0)
201 #define mutex_trylock(m) acquire_lock(m)
202 #define mutex_unlock(m) release_lock(m)
204 typedef int tsd_key_t;
205 int tsd_key_next;
206 #define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
207 #define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
208 #define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
210 #define thread_atfork(prepare, parent, child) do {} while(0)
212 #else /* no _LIBC or USE_... are defined */
214 #define NO_THREADS
216 #endif /* defined(_LIBC) */
218 #ifdef NO_THREADS /* No threads, provide dummy macros */
220 typedef int thread_id;
222 typedef int mutex_t;
224 #define MUTEX_INITIALIZER 0
225 #define mutex_init(m) (*(m) = 0)
226 #define mutex_lock(m) (0)
227 #define mutex_trylock(m) (0)
228 #define mutex_unlock(m) (0)
230 typedef void *tsd_key_t;
231 #define tsd_key_create(key, destr) do {} while(0)
232 #define tsd_setspecific(key, data) do {} while(0)
233 #define tsd_getspecific(key, vptr) (vptr = NULL)
235 #define thread_atfork(prepare, parent, child) do {} while(0)
237 #endif /* defined(NO_THREADS) */
239 #endif /* !defined(_THREAD_M_H) */