Update.
[glibc.git] / malloc / thread-m.h
blob8307f256ff907799b3dfc1b1f67c6d2532b0a8af
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 /* 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. */
26 #ifndef _THREAD_M_H
27 #define _THREAD_M_H
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;
37 /* mutex */
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)
69 /* Assume hurd, with cthreads */
71 /* Cthreads `mutex_t' is a pointer to a mutex, and malloc wants just the
72 mutex itself. */
73 #undef mutex_t
74 #define mutex_t struct mutex
76 #undef mutex_lock
77 #define mutex_lock(m) (__mutex_lock(m), 0)
79 #undef mutex_unlock
80 #define mutex_unlock(m) (__mutex_unlock(m), 0)
82 #define mutex_trylock(m) (!__mutex_trylock(m))
84 #include <hurd/threadvar.h>
86 /* thread specific data */
87 typedef int tsd_key_t;
89 static int tsd_keys_alloced = 0;
91 #define tsd_key_create(key, destr) \
92 (assert (tsd_keys_alloced == 0), tsd_keys_alloced++)
93 #define tsd_setspecific(key, data) \
94 (*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC) = (unsigned long)(data))
95 #define tsd_getspecific(key, vptr) \
96 ((vptr) = (void *)*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC))
98 /* No we're *not* using pthreads. */
99 #define __pthread_initialize ((void (*)(void))0)
101 #else
103 #define NO_THREADS
105 #endif /* MUTEX_INITIALIZER && PTHREAD_MUTEX_INITIALIZER */
107 #elif defined(USE_PTHREADS) /* Posix threads */
109 #include <pthread.h>
111 typedef pthread_t thread_id;
113 /* mutex */
114 typedef pthread_mutex_t mutex_t;
116 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
117 #define mutex_init(m) pthread_mutex_init(m, NULL)
118 #define mutex_lock(m) pthread_mutex_lock(m)
119 #define mutex_trylock(m) pthread_mutex_trylock(m)
120 #define mutex_unlock(m) pthread_mutex_unlock(m)
122 /* thread specific data */
123 typedef pthread_key_t tsd_key_t;
125 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
126 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
127 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
129 #elif USE_THR /* Solaris threads */
131 #include <thread.h>
133 typedef thread_t thread_id;
135 #define MUTEX_INITIALIZER { 0 }
136 #define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
139 * Hack for thread-specific data on Solaris. We can't use thr_setspecific
140 * because that function calls malloc() itself.
142 typedef void *tsd_key_t[256];
143 #define tsd_key_create(key, destr) do { \
144 int i; \
145 for(i=0; i<256; i++) (*key)[i] = 0; \
146 } while(0)
147 #define tsd_setspecific(key, data) (key[(unsigned)thr_self() % 256] = (data))
148 #define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
150 #elif USE_SPROC /* SGI sproc() threads */
152 #include <sys/wait.h>
153 #include <sys/types.h>
154 #include <sys/prctl.h>
155 #include <abi_mutex.h>
157 typedef int thread_id;
159 typedef abilock_t mutex_t;
161 #define MUTEX_INITIALIZER { 0 }
162 #define mutex_init(m) init_lock(m)
163 #define mutex_lock(m) (spin_lock(m), 0)
164 #define mutex_trylock(m) acquire_lock(m)
165 #define mutex_unlock(m) release_lock(m)
167 typedef int tsd_key_t;
168 int tsd_key_next;
169 #define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
170 #define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
171 #define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
173 #else /* no _LIBC or USE_... are defined */
175 #define NO_THREADS
177 #endif /* defined(_LIBC) */
179 #ifdef NO_THREADS /* No threads, provide dummy macros */
181 typedef int thread_id;
183 typedef int mutex_t;
185 #define MUTEX_INITIALIZER 0
186 #define mutex_init(m) (*(m) = 0)
187 #define mutex_lock(m) (0)
188 #define mutex_trylock(m) (0)
189 #define mutex_unlock(m) (0)
191 typedef void *tsd_key_t;
192 #define tsd_key_create(key, destr) do {} while(0)
193 #define tsd_setspecific(key, data) do {} while(0)
194 #define tsd_getspecific(key, vptr) (vptr = NULL)
196 #endif /* defined(NO_THREADS) */
198 #endif /* !defined(_THREAD_M_H) */