(main): Update strsep tests from 2.1.
[glibc.git] / malloc / thread-m.h
blob60a87a7850566a3215bc6371ea02caeef89fa826
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 void * tsd_key_t;
43 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
45 #define tsd_key_create(key, destr) ( *(key) = NULL )
46 #define tsd_setspecific(key, data) \
47 if (__libc_internal_tsd_set != NULL) { \
48 __libc_internal_tsd_set(_LIBC_TSD_KEY_MALLOC, data); \
49 } else { (key) = (Void_t *) data; }
50 #define tsd_getspecific(key, vptr) \
51 (vptr = (__libc_internal_tsd_get != NULL \
52 ? __libc_internal_tsd_get(_LIBC_TSD_KEY_MALLOC) : (key)))
54 #define mutex_init(m) \
55 (__pthread_mutex_init != NULL ? __pthread_mutex_init (m, NULL) : 0)
56 #define mutex_lock(m) \
57 (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (m) : 0)
58 #define mutex_trylock(m) \
59 (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (m) : 0)
60 #define mutex_unlock(m) \
61 (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (m) : 0)
63 #elif defined(MUTEX_INITIALIZER)
64 /* Assume hurd, with cthreads */
66 /* Cthreads `mutex_t' is a pointer to a mutex, and malloc wants just the
67 mutex itself. */
68 #undef mutex_t
69 #define mutex_t struct mutex
71 #undef mutex_lock
72 #define mutex_lock(m) (__mutex_lock(m), 0)
74 #undef mutex_unlock
75 #define mutex_unlock(m) (__mutex_unlock(m), 0)
77 #define mutex_trylock(m) (!__mutex_trylock(m))
79 #include <hurd/threadvar.h>
81 /* thread specific data */
82 typedef int tsd_key_t;
84 static int tsd_keys_alloced = 0;
86 #define tsd_key_create(key, destr) \
87 (assert (tsd_keys_alloced == 0), tsd_keys_alloced++)
88 #define tsd_setspecific(key, data) \
89 (*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC) = (unsigned long)(data))
90 #define tsd_getspecific(key, vptr) \
91 ((vptr) = (void *)*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC))
93 /* No we're *not* using pthreads. */
94 #define __pthread_initialize ((void (*)(void))0)
96 #else
98 #define NO_THREADS
100 #endif /* MUTEX_INITIALIZER && PTHREAD_MUTEX_INITIALIZER */
102 #elif defined(USE_PTHREADS) /* Posix threads */
104 #include <pthread.h>
106 typedef pthread_t thread_id;
108 /* mutex */
109 typedef pthread_mutex_t mutex_t;
111 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
112 #define mutex_init(m) pthread_mutex_init(m, NULL)
113 #define mutex_lock(m) pthread_mutex_lock(m)
114 #define mutex_trylock(m) pthread_mutex_trylock(m)
115 #define mutex_unlock(m) pthread_mutex_unlock(m)
117 /* thread specific data */
118 typedef pthread_key_t tsd_key_t;
120 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
121 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
122 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
124 #elif USE_THR /* Solaris threads */
126 #include <thread.h>
128 typedef thread_t thread_id;
130 #define MUTEX_INITIALIZER { 0 }
131 #define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
134 * Hack for thread-specific data on Solaris. We can't use thr_setspecific
135 * because that function calls malloc() itself.
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) (key[(unsigned)thr_self() % 256] = (data))
143 #define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
145 #elif USE_SPROC /* SGI sproc() threads */
147 #include <sys/wait.h>
148 #include <sys/types.h>
149 #include <sys/prctl.h>
150 #include <abi_mutex.h>
152 typedef int thread_id;
154 typedef abilock_t mutex_t;
156 #define MUTEX_INITIALIZER { 0 }
157 #define mutex_init(m) init_lock(m)
158 #define mutex_lock(m) (spin_lock(m), 0)
159 #define mutex_trylock(m) acquire_lock(m)
160 #define mutex_unlock(m) release_lock(m)
162 typedef int tsd_key_t;
163 int tsd_key_next;
164 #define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
165 #define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
166 #define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
168 #else /* no _LIBC or USE_... are defined */
170 #define NO_THREADS
172 #endif /* defined(_LIBC) */
174 #ifdef NO_THREADS /* No threads, provide dummy macros */
176 typedef int thread_id;
178 typedef int mutex_t;
180 #define MUTEX_INITIALIZER 0
181 #define mutex_init(m) (*(m) = 0)
182 #define mutex_lock(m) (0)
183 #define mutex_trylock(m) (0)
184 #define mutex_unlock(m) (0)
186 typedef void *tsd_key_t;
187 #define tsd_key_create(key, destr) do {} while(0)
188 #define tsd_setspecific(key, data) do {} while(0)
189 #define tsd_getspecific(key, vptr) (vptr = NULL)
191 #endif /* defined(NO_THREADS) */
193 #endif /* !defined(_THREAD_M_H) */