OSDN Git Service

drop support for pre ISO-C compilers
[uclinux-h8/uClibc.git] / libpthread / linuxthreads.old / sysdeps / pthread / bits / libc-lock.h
1 /* libc-internal interface for mutex locks.  LinuxThreads version.
2    Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003
3         Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If
18    not, see <http://www.gnu.org/licenses/>.  */
19
20 #ifndef _BITS_LIBC_LOCK_H
21 #define _BITS_LIBC_LOCK_H 1
22
23 #include <pthread.h>
24
25 #if defined _LIBC && !defined NOT_IN_libc
26 #include <linuxthreads.old/internals.h>
27 #endif
28
29 /* Mutex type.  */
30 #if defined(_LIBC) || defined(_IO_MTSAFE_IO)
31 typedef pthread_mutex_t __libc_lock_t;
32 typedef pthread_mutex_t __libc_lock_recursive_t;
33 # ifdef __USE_UNIX98
34 typedef pthread_rwlock_t __libc_rwlock_t;
35 # else
36 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
37 # endif
38 typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
39 #else
40 typedef struct __libc_lock_opaque__ __libc_lock_t;
41 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
42 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
43 #endif
44
45 /* Type for key to thread-specific data.  */
46 typedef pthread_key_t __libc_key_t;
47
48 /* Define a lock variable NAME with storage class CLASS.  The lock must be
49    initialized with __libc_lock_init before it can be used (or define it
50    with __libc_lock_define_initialized, below).  Use `extern' for CLASS to
51    declare a lock defined in another module.  In public structure
52    definitions you must use a pointer to the lock structure (i.e., NAME
53    begins with a `*'), because its storage size will not be known outside
54    of libc.  */
55 #define __libc_lock_define(CLASS,NAME) \
56   CLASS __libc_lock_t NAME;
57 #define __libc_rwlock_define(CLASS,NAME) \
58   CLASS __libc_rwlock_t NAME;
59 #define __libc_lock_define_recursive(CLASS,NAME) \
60   CLASS __libc_lock_recursive_t NAME;
61 #define __rtld_lock_define_recursive(CLASS,NAME) \
62   CLASS __rtld_lock_recursive_t NAME;
63
64 /* Define an initialized lock variable NAME with storage class CLASS.
65
66    For the C library we take a deeper look at the initializer.  For
67    this implementation all fields are initialized to zero.  Therefore
68    we don't initialize the variable which allows putting it into the
69    BSS section.  (Except on PA-RISC and other odd architectures, where
70    initialized locks must be set to one due to the lack of normal
71    atomic operations.) */
72
73 #if __LT_SPINLOCK_INIT == 0
74 #  define __libc_lock_define_initialized(CLASS,NAME) \
75   CLASS __libc_lock_t NAME;
76 #else
77 #  define __libc_lock_define_initialized(CLASS,NAME) \
78   CLASS __libc_lock_t NAME = PTHREAD_MUTEX_INITIALIZER;
79 #endif
80
81 #define __libc_rwlock_define_initialized(CLASS,NAME) \
82   CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
83
84 /* Define an initialized recursive lock variable NAME with storage
85    class CLASS.  */
86 #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
87   CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
88 #define _LIBC_LOCK_RECURSIVE_INITIALIZER \
89   {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
90
91 #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
92   CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
93 #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
94   {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
95
96 #if defined _LIBC && defined IS_IN_libpthread
97 # define __libc_maybe_call(FUNC, ARGS, ELSE) FUNC ARGS
98 #else
99 # if defined __PIC__ || (defined _LIBC && defined SHARED)
100 #  define __libc_maybe_call(FUNC, ARGS, ELSE) \
101   (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
102                     _fn != NULL ? (*_fn) ARGS : ELSE; }))
103 # else
104 #  define __libc_maybe_call(FUNC, ARGS, ELSE) \
105   (FUNC != NULL ? FUNC ARGS : ELSE)
106 # endif
107 #endif
108 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
109 # define __libc_maybe_call2(FUNC, ARGS, ELSE) \
110   ({__builtin_expect (__libc_pthread_functions.ptr_##FUNC != NULL, 0) \
111     ? __libc_pthread_functions.ptr_##FUNC ARGS : ELSE; })
112 #else
113 # define __libc_maybe_call2(FUNC, ARGS, ELSE) __libc_maybe_call (__##FUNC, ARGS, ELSE)
114 #endif
115
116 /* Initialize the named lock variable, leaving it in a consistent, unlocked
117    state.  */
118 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
119 #define __libc_lock_init(NAME) \
120   ({                                                                          \
121     (NAME).__m_count = 0;                                                     \
122     (NAME).__m_owner = NULL;                                                  \
123     (NAME).__m_kind = PTHREAD_MUTEX_TIMED_NP;                                 \
124     (NAME).__m_lock.__status = 0;                                             \
125     (NAME).__m_lock.__spinlock = __LT_SPINLOCK_INIT;                          \
126     0; })
127 #else
128 #define __libc_lock_init(NAME) \
129   (__libc_maybe_call2 (pthread_mutex_init, (&(NAME), NULL), 0))
130 #endif
131 #define __libc_rwlock_init(NAME) \
132   (__libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0));
133
134 /* Same as last but this time we initialize an adaptive mutex.  */
135 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
136 #define __libc_lock_init_adaptive(NAME) \
137   ({                                                                          \
138     (NAME).__m_count = 0;                                                     \
139     (NAME).__m_owner = NULL;                                                  \
140     (NAME).__m_kind = PTHREAD_MUTEX_ADAPTIVE_NP;                              \
141     (NAME).__m_lock.__status = 0;                                             \
142     (NAME).__m_lock.__spinlock = __LT_SPINLOCK_INIT;                          \
143     0; })
144 #else
145 #define __libc_lock_init_adaptive(NAME) \
146   do {                                                                        \
147     if (__pthread_mutex_init != NULL)                                         \
148       {                                                                       \
149         pthread_mutexattr_t __attr;                                           \
150         __pthread_mutexattr_init (&__attr);                                   \
151         __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_ADAPTIVE_NP);     \
152         __pthread_mutex_init (&(NAME), &__attr);                              \
153         __pthread_mutexattr_destroy (&__attr);                                \
154       }                                                                       \
155   } while (0);
156 #endif
157
158 /* Same as last but this time we initialize a recursive mutex.  */
159 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
160 #define __libc_lock_init_recursive(NAME) \
161   ({                                                                          \
162     (NAME).__m_count = 0;                                                     \
163     (NAME).__m_owner = NULL;                                          \
164     (NAME).__m_kind = PTHREAD_MUTEX_RECURSIVE_NP;                             \
165     (NAME).__m_lock.__status = 0;                                             \
166     (NAME).__m_lock.__spinlock = __LT_SPINLOCK_INIT;                  \
167     0; })
168 #else
169 #define __libc_lock_init_recursive(NAME) \
170   do {                                                                        \
171     if (__pthread_mutex_init != NULL)                                         \
172       {                                                                       \
173         pthread_mutexattr_t __attr;                                           \
174         __pthread_mutexattr_init (&__attr);                                   \
175         __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
176         __pthread_mutex_init (&(NAME), &__attr);                              \
177         __pthread_mutexattr_destroy (&__attr);                                \
178       }                                                                       \
179   } while (0);
180 #endif
181 #define __rtld_lock_init_recursive(NAME) \
182   __libc_lock_init_recursive (NAME)
183
184 /* Finalize the named lock variable, which must be locked.  It cannot be
185    used again until __libc_lock_init is called again on it.  This must be
186    called on a lock variable before the containing storage is reused.  */
187 #define __libc_lock_fini(NAME) \
188   (__libc_maybe_call2 (pthread_mutex_destroy, (&(NAME)), 0));
189 #define __libc_rwlock_fini(NAME) \
190   (__libc_maybe_call (__pthread_rwlock_destroy, (&(NAME)), 0));
191
192 /* Finalize recursive named lock.  */
193 #define __libc_lock_fini_recursive(NAME) __libc_lock_fini ((NAME).mutex)
194 #define __rtld_lock_fini_recursive(NAME) __libc_lock_fini_recursive (NAME)
195
196 /* Lock the named lock variable.  */
197 #define __libc_lock_lock(NAME) \
198   (__libc_maybe_call2 (pthread_mutex_lock, (&(NAME)), 0));
199 #define __libc_rwlock_rdlock(NAME) \
200   (__libc_maybe_call (__pthread_rwlock_rdlock, (&(NAME)), 0));
201 #define __libc_rwlock_wrlock(NAME) \
202   (__libc_maybe_call (__pthread_rwlock_wrlock, (&(NAME)), 0));
203
204 /* Lock the recursive named lock variable.  */
205 #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
206
207 /* Try to lock the named lock variable.  */
208 #define __libc_lock_trylock(NAME) \
209   (__libc_maybe_call2 (pthread_mutex_trylock, (&(NAME)), 0))
210 #define __libc_rwlock_tryrdlock(NAME) \
211   (__libc_maybe_call (__pthread_rwlock_tryrdlock, (&(NAME)), 0))
212 #define __libc_rwlock_trywrlock(NAME) \
213   (__libc_maybe_call (__pthread_rwlock_trywrlock, (&(NAME)), 0))
214
215 /* Try to lock the recursive named lock variable.  */
216 #define __libc_lock_trylock_recursive(NAME) __libc_lock_trylock ((NAME).mutex)
217 #define __rtld_lock_trylock_recursive(NAME) \
218   __libc_lock_trylock_recursive (NAME)
219
220 /* Unlock the named lock variable.  */
221 #define __libc_lock_unlock(NAME) \
222   (__libc_maybe_call2 (pthread_mutex_unlock, (&(NAME)), 0));
223 #define __libc_rwlock_unlock(NAME) \
224   (__libc_maybe_call (__pthread_rwlock_unlock, (&(NAME)), 0));
225
226 /* Unlock the recursive named lock variable.  */
227 #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
228
229 /* Define once control variable.  */
230 #if PTHREAD_ONCE_INIT == 0
231 /* Special case for static variables where we can avoid the initialization
232    if it is zero.  */
233 # define __libc_once_define(CLASS, NAME) \
234   CLASS pthread_once_t NAME
235 #else
236 # define __libc_once_define(CLASS, NAME) \
237   CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
238 #endif
239
240 /* Call handler iff the first call.  */
241 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
242   do {                                                                        \
243     if (__pthread_once != NULL)                                               \
244       __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION));                      \
245     else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) {                           \
246       INIT_FUNCTION ();                                                       \
247       (ONCE_CONTROL) = !PTHREAD_ONCE_INIT;                                    \
248     }                                                                         \
249   } while (0)
250
251
252 /* Start critical region with cleanup.  */
253 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
254   { struct _pthread_cleanup_buffer _buffer;                                   \
255     int _avail = (DOIT) && _pthread_cleanup_push_defer != NULL;               \
256     if (_avail) {                                                             \
257       _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG));                   \
258     }
259
260 /* End critical region with cleanup.  */
261 #define __libc_cleanup_region_end(DOIT) \
262     if (_avail) {                                                             \
263       _pthread_cleanup_pop_restore (&_buffer, (DOIT));                        \
264     }                                                                         \
265   }
266
267 /* Sometimes we have to exit the block in the middle.  */
268 #define __libc_cleanup_end(DOIT) \
269     if (_avail) {                                                             \
270       _pthread_cleanup_pop_restore (&_buffer, (DOIT));                        \
271     }
272
273 #if 0
274 #define __libc_cleanup_push(fct, arg) \
275     { struct _pthread_cleanup_buffer _buffer;                                 \
276     __libc_maybe_call (_pthread_cleanup_push, (&_buffer, (fct), (arg)), 0)
277
278 #define __libc_cleanup_pop(execute) \
279     __libc_maybe_call (_pthread_cleanup_pop, (&_buffer, execute), 0);         \
280     }
281 #endif
282
283 /* Create thread-specific key.  */
284 #define __libc_key_create(KEY, DESTRUCTOR) \
285   (__libc_maybe_call (__pthread_key_create, (KEY, DESTRUCTOR), 1))
286
287 /* Get thread-specific data.  */
288 #define __libc_getspecific(KEY) \
289   (__libc_maybe_call (__pthread_getspecific, (KEY), NULL))
290
291 /* Set thread-specific data.  */
292 #define __libc_setspecific(KEY, VALUE) \
293   (__libc_maybe_call (__pthread_setspecific, (KEY, VALUE), 0))
294
295
296 /* Register handlers to execute before and after `fork'.  */
297 #define __libc_atfork(PREPARE, PARENT, CHILD) \
298   (__libc_maybe_call (__pthread_atfork, (PREPARE, PARENT, CHILD), 0))
299
300 /* Functions that are used by this file and are internal to the GNU C
301    library.  */
302
303 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
304                                  const pthread_mutexattr_t *__mutex_attr);
305
306 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
307
308 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
309
310 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
311
312 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
313
314 extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
315
316 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
317
318 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
319                                         int __kind);
320
321 #ifdef __USE_UNIX98
322 extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
323                                   const pthread_rwlockattr_t *__attr);
324
325 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
326
327 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
328
329 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
330
331 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
332
333 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
334
335 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
336 #endif
337
338 extern int __pthread_key_create (pthread_key_t *__key,
339                                  void (*__destr_function) (void *));
340
341 extern int __pthread_setspecific (pthread_key_t __key,
342                                   const void *__pointer);
343
344 extern void *__pthread_getspecific (pthread_key_t __key);
345
346 extern int __pthread_once (pthread_once_t *__once_control,
347                            void (*__init_routine) (void));
348
349 extern int __pthread_atfork (void (*__prepare) (void),
350                              void (*__parent) (void),
351                              void (*__child) (void));
352
353
354
355 /* Make the pthread functions weak so that we can elide them from
356    single-threaded processes.  */
357 #ifndef __NO_WEAK_PTHREAD_ALIASES
358 # ifdef weak_extern
359 #   define BP_SYM(sym) sym
360 weak_extern (BP_SYM (__pthread_mutex_init))
361 weak_extern (BP_SYM (__pthread_mutex_destroy))
362 weak_extern (BP_SYM (__pthread_mutex_lock))
363 weak_extern (BP_SYM (__pthread_mutex_trylock))
364 weak_extern (BP_SYM (__pthread_mutex_unlock))
365 weak_extern (BP_SYM (__pthread_mutexattr_init))
366 weak_extern (BP_SYM (__pthread_mutexattr_destroy))
367 weak_extern (BP_SYM (__pthread_mutexattr_settype))
368 weak_extern (BP_SYM (__pthread_rwlock_init))
369 weak_extern (BP_SYM (__pthread_rwlock_destroy))
370 weak_extern (BP_SYM (__pthread_rwlock_rdlock))
371 weak_extern (BP_SYM (__pthread_rwlock_tryrdlock))
372 weak_extern (BP_SYM (__pthread_rwlock_wrlock))
373 weak_extern (BP_SYM (__pthread_rwlock_trywrlock))
374 weak_extern (BP_SYM (__pthread_rwlock_unlock))
375 weak_extern (BP_SYM (__pthread_key_create))
376 weak_extern (BP_SYM (__pthread_setspecific))
377 weak_extern (BP_SYM (__pthread_getspecific))
378 weak_extern (BP_SYM (__pthread_once))
379 weak_extern (__pthread_atfork)
380 weak_extern (BP_SYM (_pthread_cleanup_push))
381 weak_extern (BP_SYM (_pthread_cleanup_pop))
382 weak_extern (BP_SYM (_pthread_cleanup_push_defer))
383 weak_extern (BP_SYM (_pthread_cleanup_pop_restore))
384 # else
385 #  pragma weak __pthread_mutex_init
386 #  pragma weak __pthread_mutex_destroy
387 #  pragma weak __pthread_mutex_lock
388 #  pragma weak __pthread_mutex_trylock
389 #  pragma weak __pthread_mutex_unlock
390 #  pragma weak __pthread_mutexattr_init
391 #  pragma weak __pthread_mutexattr_destroy
392 #  pragma weak __pthread_mutexattr_settype
393 #  pragma weak __pthread_rwlock_destroy
394 #  pragma weak __pthread_rwlock_rdlock
395 #  pragma weak __pthread_rwlock_tryrdlock
396 #  pragma weak __pthread_rwlock_wrlock
397 #  pragma weak __pthread_rwlock_trywrlock
398 #  pragma weak __pthread_rwlock_unlock
399 #  pragma weak __pthread_key_create
400 #  pragma weak __pthread_setspecific
401 #  pragma weak __pthread_getspecific
402 #  pragma weak __pthread_once
403 #  pragma weak __pthread_atfork
404 #  pragma weak _pthread_cleanup_push_defer
405 #  pragma weak _pthread_cleanup_pop_restore
406 #  pragma weak _pthread_cleanup_push
407 #  pragma weak _pthread_cleanup_pop
408 # endif
409 #endif
410
411 /* We need portable names for some functions.  E.g., when they are
412    used as argument to __libc_cleanup_region_start.  */
413 #define __libc_mutex_unlock __pthread_mutex_unlock
414
415 #endif  /* bits/libc-lock.h */