Update.
[glibc.git] / malloc / thread-m.h
blobbb9cba599e3a30ffb1122adb0d588073bc049ade
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 */
92 /* thread specific data */
94 #include <bits/libc-tsd.h>
96 typedef int tsd_key_t[0]; /* no key data structure, libc magic does it */
97 __libc_tsd_define (, MALLOC) /* declaration/common definition */
98 #define tsd_key_create(key, destr) ((void) (key))
99 #define tsd_setspecific(key, data) __libc_tsd_set (MALLOC, (data))
100 #define tsd_getspecific(key, vptr) ((vptr) = __libc_tsd_get (MALLOC))
103 #elif defined(USE_PTHREADS) /* Posix threads */
105 #include <pthread.h>
107 typedef pthread_t thread_id;
109 /* mutex */
110 typedef pthread_mutex_t mutex_t;
112 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
113 #define mutex_init(m) pthread_mutex_init(m, NULL)
114 #define mutex_lock(m) pthread_mutex_lock(m)
115 #define mutex_trylock(m) pthread_mutex_trylock(m)
116 #define mutex_unlock(m) pthread_mutex_unlock(m)
118 /* thread specific data */
119 #if defined(__sgi) || defined(USE_TSD_DATA_HACK)
121 /* Hack for thread-specific data, e.g. on Irix 6.x. We can't use
122 pthread_setspecific because that function calls malloc() itself.
123 The hack only works when pthread_t can be converted to an integral
124 type. */
126 typedef void *tsd_key_t[256];
127 #define tsd_key_create(key, destr) do { \
128 int i; \
129 for(i=0; i<256; i++) (*key)[i] = 0; \
130 } while(0)
131 #define tsd_setspecific(key, data) \
132 (key[(unsigned)pthread_self() % 256] = (data))
133 #define tsd_getspecific(key, vptr) \
134 (vptr = key[(unsigned)pthread_self() % 256])
136 #else
138 typedef pthread_key_t tsd_key_t;
140 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
141 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
142 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
144 #endif
146 /* at fork */
147 #define thread_atfork(prepare, parent, child) \
148 pthread_atfork(prepare, parent, child)
150 #elif USE_THR /* Solaris threads */
152 #include <thread.h>
154 typedef thread_t thread_id;
156 #define MUTEX_INITIALIZER { 0 }
157 #define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
160 * Hack for thread-specific data on Solaris. We can't use thr_setspecific
161 * because that function calls malloc() itself.
163 typedef void *tsd_key_t[256];
164 #define tsd_key_create(key, destr) do { \
165 int i; \
166 for(i=0; i<256; i++) (*key)[i] = 0; \
167 } while(0)
168 #define tsd_setspecific(key, data) (key[(unsigned)thr_self() % 256] = (data))
169 #define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
171 #define thread_atfork(prepare, parent, child) do {} while(0)
173 #elif USE_SPROC /* SGI sproc() threads */
175 #include <sys/wait.h>
176 #include <sys/types.h>
177 #include <sys/prctl.h>
178 #include <abi_mutex.h>
180 typedef int thread_id;
182 typedef abilock_t mutex_t;
184 #define MUTEX_INITIALIZER { 0 }
185 #define mutex_init(m) init_lock(m)
186 #define mutex_lock(m) (spin_lock(m), 0)
187 #define mutex_trylock(m) acquire_lock(m)
188 #define mutex_unlock(m) release_lock(m)
190 typedef int tsd_key_t;
191 int tsd_key_next;
192 #define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
193 #define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
194 #define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
196 #define thread_atfork(prepare, parent, child) do {} while(0)
198 #else /* no _LIBC or USE_... are defined */
200 #define NO_THREADS
202 #endif /* defined(_LIBC) */
204 #ifdef NO_THREADS /* No threads, provide dummy macros */
206 typedef int thread_id;
208 typedef int mutex_t;
210 #define MUTEX_INITIALIZER 0
211 #define mutex_init(m) (*(m) = 0)
212 #define mutex_lock(m) (0)
213 #define mutex_trylock(m) (0)
214 #define mutex_unlock(m) (0)
216 typedef void *tsd_key_t;
217 #define tsd_key_create(key, destr) do {} while(0)
218 #define tsd_setspecific(key, data) do {} while(0)
219 #define tsd_getspecific(key, vptr) (vptr = NULL)
221 #define thread_atfork(prepare, parent, child) do {} while(0)
223 #endif /* defined(NO_THREADS) */
225 #endif /* !defined(_THREAD_M_H) */