]> git.ipfire.org Git - thirdparty/glibc.git/blob - malloc/thread-m.h
Update.
[thirdparty/glibc.git] / malloc / thread-m.h
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.
6
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.
11
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.
16
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. */
21
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. */
25
26 #ifndef _THREAD_M_H
27 #define _THREAD_M_H
28
29 #if defined(_LIBC) /* The GNU C library, a special case of Posix threads */
30
31 #include <bits/libc-lock.h>
32
33 #ifdef PTHREAD_MUTEX_INITIALIZER
34
35 typedef pthread_t thread_id;
36
37 /* mutex */
38 typedef pthread_mutex_t mutex_t;
39
40 /* thread specific data */
41 typedef pthread_key_t tsd_key_t;
42
43 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
44
45 static Void_t *malloc_key_data;
46
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))
58
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)
67
68 #elif defined(MUTEX_INITIALIZER)
69 /* Assume hurd, with cthreads */
70
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
75
76 #undef mutex_lock
77 #define mutex_lock(m) (__mutex_lock(m), 0)
78
79 #undef mutex_unlock
80 #define mutex_unlock(m) (__mutex_unlock(m), 0)
81
82 #define mutex_trylock(m) (!__mutex_trylock(m))
83
84 #include <hurd/threadvar.h>
85
86 /* thread specific data */
87 typedef int tsd_key_t;
88
89 static int tsd_keys_alloced = 0;
90
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))
97
98 /* No we're *not* using pthreads. */
99 #define __pthread_initialize ((void (*)(void))0)
100
101 #else
102
103 #define NO_THREADS
104
105 #endif /* MUTEX_INITIALIZER && PTHREAD_MUTEX_INITIALIZER */
106
107 #elif defined(USE_PTHREADS) /* Posix threads */
108
109 #include <pthread.h>
110
111 typedef pthread_t thread_id;
112
113 /* mutex */
114 typedef pthread_mutex_t mutex_t;
115
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)
121
122 /* thread specific data */
123 typedef pthread_key_t tsd_key_t;
124
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))
128
129 #elif USE_THR /* Solaris threads */
130
131 #include <thread.h>
132
133 typedef thread_t thread_id;
134
135 #define MUTEX_INITIALIZER { 0 }
136 #define mutex_init(m) mutex_init(m, USYNC_THREAD, NULL)
137
138 /*
139 * Hack for thread-specific data on Solaris. We can't use thr_setspecific
140 * because that function calls malloc() itself.
141 */
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])
149
150 #elif USE_SPROC /* SGI sproc() threads */
151
152 #include <sys/wait.h>
153 #include <sys/types.h>
154 #include <sys/prctl.h>
155 #include <abi_mutex.h>
156
157 typedef int thread_id;
158
159 typedef abilock_t mutex_t;
160
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)
166
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])
172
173 #else /* no _LIBC or USE_... are defined */
174
175 #define NO_THREADS
176
177 #endif /* defined(_LIBC) */
178
179 #ifdef NO_THREADS /* No threads, provide dummy macros */
180
181 typedef int thread_id;
182
183 typedef int mutex_t;
184
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)
190
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)
195
196 #endif /* defined(NO_THREADS) */
197
198 #endif /* !defined(_THREAD_M_H) */