Update.
[glibc.git] / malloc / thread-m.h
blob1598db8ee772dff67bd089929aba8a312648a159
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 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
45 #define mutex_init(m) \
46 (__pthread_mutex_init != NULL ? __pthread_mutex_init (m, NULL) : 0)
47 #define mutex_lock(m) \
48 (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (m) : 0)
49 #define mutex_trylock(m) \
50 (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (m) : 0)
51 #define mutex_unlock(m) \
52 (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (m) : 0)
54 #define thread_atfork(prepare, parent, child) \
55 (__pthread_atfork != NULL ? __pthread_atfork(prepare, parent, child) : 0)
57 #elif defined(MUTEX_INITIALIZER)
58 /* Assume hurd, with cthreads */
60 /* Cthreads `mutex_t' is a pointer to a mutex, and malloc wants just the
61 mutex itself. */
62 #undef mutex_t
63 #define mutex_t struct mutex
65 #undef mutex_init
66 #define mutex_init(m) (__mutex_init(m), 0)
68 #undef mutex_lock
69 #define mutex_lock(m) (__mutex_lock(m), 0)
71 #undef mutex_unlock
72 #define mutex_unlock(m) (__mutex_unlock(m), 0)
74 #define mutex_trylock(m) (!__mutex_trylock(m))
76 #define thread_atfork(prepare, parent, child) do {} while(0)
77 #define thread_atfork_static(prepare, parent, child) \
78 text_set_element(_hurd_fork_prepare_hook, prepare); \
79 text_set_element(_hurd_fork_parent_hook, parent); \
80 text_set_element(_hurd_fork_child_hook, child);
82 /* No we're *not* using pthreads. */
83 #define __pthread_initialize ((void (*)(void))0)
85 #else
87 #define NO_THREADS
89 #endif /* MUTEX_INITIALIZER && PTHREAD_MUTEX_INITIALIZER */
91 #ifndef NO_THREADS
93 /* thread specific data for glibc */
95 #include <bits/libc-tsd.h>
97 typedef int tsd_key_t[0]; /* no key data structure, libc magic does it */
98 __libc_tsd_define (, MALLOC) /* declaration/common definition */
99 #define tsd_key_create(key, destr) ((void) (key))
100 #define tsd_setspecific(key, data) __libc_tsd_set (MALLOC, (data))
101 #define tsd_getspecific(key, vptr) ((vptr) = __libc_tsd_get (MALLOC))
103 #endif
105 #elif defined(USE_PTHREADS) /* Posix threads */
107 #include <pthread.h>
109 typedef pthread_t thread_id;
111 /* mutex */
112 typedef pthread_mutex_t mutex_t;
114 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
115 #define mutex_init(m) pthread_mutex_init(m, NULL)
116 #define mutex_lock(m) pthread_mutex_lock(m)
117 #define mutex_trylock(m) pthread_mutex_trylock(m)
118 #define mutex_unlock(m) pthread_mutex_unlock(m)
120 /* thread specific data */
121 #if defined(__sgi) || defined(USE_TSD_DATA_HACK)
123 /* Hack for thread-specific data, e.g. on Irix 6.x. We can't use
124 pthread_setspecific because that function calls malloc() itself.
125 The hack only works when pthread_t can be converted to an integral
126 type. */
128 typedef void *tsd_key_t[256];
129 #define tsd_key_create(key, destr) do { \
130 int i; \
131 for(i=0; i<256; i++) (*key)[i] = 0; \
132 } while(0)
133 #define tsd_setspecific(key, data) \
134 (key[(unsigned)pthread_self() % 256] = (data))
135 #define tsd_getspecific(key, vptr) \
136 (vptr = key[(unsigned)pthread_self() % 256])
138 #else
140 typedef pthread_key_t tsd_key_t;
142 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
143 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
144 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
146 #endif
148 /* at fork */
149 #define thread_atfork(prepare, parent, child) \
150 pthread_atfork(prepare, parent, child)
152 #elif USE_THR /* Solaris threads */
154 #include <thread.h>
156 typedef thread_t thread_id;
158 #define MUTEX_INITIALIZER { 0 }
159 #define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
162 * Hack for thread-specific data on Solaris. We can't use thr_setspecific
163 * because that function calls malloc() itself.
165 typedef void *tsd_key_t[256];
166 #define tsd_key_create(key, destr) do { \
167 int i; \
168 for(i=0; i<256; i++) (*key)[i] = 0; \
169 } while(0)
170 #define tsd_setspecific(key, data) (key[(unsigned)thr_self() % 256] = (data))
171 #define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
173 #define thread_atfork(prepare, parent, child) do {} while(0)
175 #elif USE_SPROC /* SGI sproc() threads */
177 #include <sys/wait.h>
178 #include <sys/types.h>
179 #include <sys/prctl.h>
180 #include <abi_mutex.h>
182 typedef int thread_id;
184 typedef abilock_t mutex_t;
186 #define MUTEX_INITIALIZER { 0 }
187 #define mutex_init(m) init_lock(m)
188 #define mutex_lock(m) (spin_lock(m), 0)
189 #define mutex_trylock(m) acquire_lock(m)
190 #define mutex_unlock(m) release_lock(m)
192 typedef int tsd_key_t;
193 int tsd_key_next;
194 #define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
195 #define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
196 #define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
198 #define thread_atfork(prepare, parent, child) do {} while(0)
200 #else /* no _LIBC or USE_... are defined */
202 #define NO_THREADS
204 #endif /* defined(_LIBC) */
206 #ifdef NO_THREADS /* No threads, provide dummy macros */
208 typedef int thread_id;
210 typedef int mutex_t;
212 #define MUTEX_INITIALIZER 0
213 #define mutex_init(m) (*(m) = 0)
214 #define mutex_lock(m) (0)
215 #define mutex_trylock(m) (0)
216 #define mutex_unlock(m) (0)
218 typedef void *tsd_key_t;
219 #define tsd_key_create(key, destr) do {} while(0)
220 #define tsd_setspecific(key, data) do {} while(0)
221 #define tsd_getspecific(key, vptr) (vptr = NULL)
223 #define thread_atfork(prepare, parent, child) do {} while(0)
225 #endif /* defined(NO_THREADS) */
227 #endif /* !defined(_THREAD_M_H) */