Update.
[glibc.git] / malloc / thread-m.h
blobf9367c6782beef6425e01b746e0e4ecd74af1a4b
1 /* Basic platform-independent macro definitions for mutexes and
2 thread-specific data.
3 Copyright (C) 1996, 1997 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_lock
78 #define mutex_lock(m) (__mutex_lock(m), 0)
80 #undef mutex_unlock
81 #define mutex_unlock(m) (__mutex_unlock(m), 0)
83 #define mutex_trylock(m) (!__mutex_trylock(m))
85 #include <hurd/threadvar.h>
87 /* thread specific data */
88 typedef int tsd_key_t;
90 static int tsd_keys_alloced = 0;
92 #define tsd_key_create(key, destr) \
93 (assert (tsd_keys_alloced == 0), tsd_keys_alloced++)
94 #define tsd_setspecific(key, data) \
95 (*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC) = (unsigned long)(data))
96 #define tsd_getspecific(key, vptr) \
97 ((vptr) = (void *)*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC))
99 #define thread_atfork(prepare, parent, child) do {} while(0)
100 #define thread_atfork_static(prepare, parent, child) \
101 text_set_element(_hurd_fork_prepare_hook, prepare); \
102 text_set_element(_hurd_fork_parent_hook, parent); \
103 text_set_element(_hurd_fork_child_hook, child);
105 /* No we're *not* using pthreads. */
106 #define __pthread_initialize ((void (*)(void))0)
108 #else
110 #define NO_THREADS
112 #endif /* MUTEX_INITIALIZER && PTHREAD_MUTEX_INITIALIZER */
114 #elif defined(USE_PTHREADS) /* Posix threads */
116 #include <pthread.h>
118 typedef pthread_t thread_id;
120 /* mutex */
121 typedef pthread_mutex_t mutex_t;
123 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
124 #define mutex_init(m) pthread_mutex_init(m, NULL)
125 #define mutex_lock(m) pthread_mutex_lock(m)
126 #define mutex_trylock(m) pthread_mutex_trylock(m)
127 #define mutex_unlock(m) pthread_mutex_unlock(m)
129 /* thread specific data */
130 #if defined(__sgi) || defined(USE_TSD_DATA_HACK)
132 /* Hack for thread-specific data, e.g. on Irix 6.x. We can't use
133 pthread_setspecific because that function calls malloc() itself.
134 The hack only works when pthread_t can be converted to an integral
135 type. */
137 typedef void *tsd_key_t[256];
138 #define tsd_key_create(key, destr) do { \
139 int i; \
140 for(i=0; i<256; i++) (*key)[i] = 0; \
141 } while(0)
142 #define tsd_setspecific(key, data) \
143 (key[(unsigned)pthread_self() % 256] = (data))
144 #define tsd_getspecific(key, vptr) \
145 (vptr = key[(unsigned)pthread_self() % 256])
147 #else
149 typedef pthread_key_t tsd_key_t;
151 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
152 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
153 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
155 #endif
157 /* at fork */
158 #define thread_atfork(prepare, parent, child) \
159 pthread_atfork(prepare, parent, child)
161 #elif USE_THR /* Solaris threads */
163 #include <thread.h>
165 typedef thread_t thread_id;
167 #define MUTEX_INITIALIZER { 0 }
168 #define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
171 * Hack for thread-specific data on Solaris. We can't use thr_setspecific
172 * because that function calls malloc() itself.
174 typedef void *tsd_key_t[256];
175 #define tsd_key_create(key, destr) do { \
176 int i; \
177 for(i=0; i<256; i++) (*key)[i] = 0; \
178 } while(0)
179 #define tsd_setspecific(key, data) (key[(unsigned)thr_self() % 256] = (data))
180 #define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
182 #define thread_atfork(prepare, parent, child) do {} while(0)
184 #elif USE_SPROC /* SGI sproc() threads */
186 #include <sys/wait.h>
187 #include <sys/types.h>
188 #include <sys/prctl.h>
189 #include <abi_mutex.h>
191 typedef int thread_id;
193 typedef abilock_t mutex_t;
195 #define MUTEX_INITIALIZER { 0 }
196 #define mutex_init(m) init_lock(m)
197 #define mutex_lock(m) (spin_lock(m), 0)
198 #define mutex_trylock(m) acquire_lock(m)
199 #define mutex_unlock(m) release_lock(m)
201 typedef int tsd_key_t;
202 int tsd_key_next;
203 #define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
204 #define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
205 #define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
207 #define thread_atfork(prepare, parent, child) do {} while(0)
209 #else /* no _LIBC or USE_... are defined */
211 #define NO_THREADS
213 #endif /* defined(_LIBC) */
215 #ifdef NO_THREADS /* No threads, provide dummy macros */
217 typedef int thread_id;
219 typedef int mutex_t;
221 #define MUTEX_INITIALIZER 0
222 #define mutex_init(m) (*(m) = 0)
223 #define mutex_lock(m) (0)
224 #define mutex_trylock(m) (0)
225 #define mutex_unlock(m) (0)
227 typedef void *tsd_key_t;
228 #define tsd_key_create(key, destr) do {} while(0)
229 #define tsd_setspecific(key, data) do {} while(0)
230 #define tsd_getspecific(key, vptr) (vptr = NULL)
232 #define thread_atfork(prepare, parent, child) do {} while(0)
234 #endif /* defined(NO_THREADS) */
236 #endif /* !defined(_THREAD_M_H) */