(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / sysdeps / pthread / bits / libc-lock.h
blob7e22166862eab76844fa8fccb6ae63cba3055f4e
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.
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.
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.
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 not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #ifndef _BITS_LIBC_LOCK_H
22 #define _BITS_LIBC_LOCK_H 1
24 #include <pthread.h>
26 #if defined _LIBC && !defined NOT_IN_libc
27 #include <linuxthreads/internals.h>
28 #endif
30 /* Mutex type. */
31 #if defined(_LIBC) || defined(_IO_MTSAFE_IO)
32 typedef pthread_mutex_t __libc_lock_t;
33 typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
34 # ifdef __USE_UNIX98
35 typedef pthread_rwlock_t __libc_rwlock_t;
36 # else
37 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
38 # endif
39 typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
40 #else
41 typedef struct __libc_lock_opaque__ __libc_lock_t;
42 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
43 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
44 #endif
46 /* Type for key to thread-specific data. */
47 typedef pthread_key_t __libc_key_t;
49 /* Define a lock variable NAME with storage class CLASS. The lock must be
50 initialized with __libc_lock_init before it can be used (or define it
51 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
52 declare a lock defined in another module. In public structure
53 definitions you must use a pointer to the lock structure (i.e., NAME
54 begins with a `*'), because its storage size will not be known outside
55 of libc. */
56 #define __libc_lock_define(CLASS,NAME) \
57 CLASS __libc_lock_t NAME;
58 #define __libc_rwlock_define(CLASS,NAME) \
59 CLASS __libc_rwlock_t NAME;
60 #define __libc_lock_define_recursive(CLASS,NAME) \
61 CLASS __libc_lock_recursive_t NAME;
62 #define __rtld_lock_define_recursive(CLASS,NAME) \
63 CLASS __rtld_lock_recursive_t NAME;
65 /* Define an initialized lock variable NAME with storage class CLASS.
67 For the C library we take a deeper look at the initializer. For
68 this implementation all fields are initialized to zero. Therefore
69 we don't initialize the variable which allows putting it into the
70 BSS section. (Except on PA-RISC and other odd architectures, where
71 initialized locks must be set to one due to the lack of normal
72 atomic operations.) */
74 #if __LT_SPINLOCK_INIT == 0
75 # define __libc_lock_define_initialized(CLASS,NAME) \
76 CLASS __libc_lock_t NAME;
77 #else
78 # define __libc_lock_define_initialized(CLASS,NAME) \
79 CLASS __libc_lock_t NAME = PTHREAD_MUTEX_INITIALIZER;
80 #endif
82 #define __libc_rwlock_define_initialized(CLASS,NAME) \
83 CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
85 /* Define an initialized recursive lock variable NAME with storage
86 class CLASS. */
87 #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
88 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
89 #define _LIBC_LOCK_RECURSIVE_INITIALIZER \
90 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
92 #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
93 CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
94 #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
95 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
97 #if defined _LIBC && defined IS_IN_libpthread
98 # define __libc_maybe_call(FUNC, ARGS, ELSE) FUNC ARGS
99 #else
100 # if defined __PIC__ || (defined _LIBC && defined SHARED)
101 # define __libc_maybe_call(FUNC, ARGS, ELSE) \
102 (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
103 _fn != NULL ? (*_fn) ARGS : ELSE; }))
104 # else
105 # define __libc_maybe_call(FUNC, ARGS, ELSE) \
106 (FUNC != NULL ? FUNC ARGS : ELSE)
107 # endif
108 #endif
109 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
110 # define __libc_maybe_call2(FUNC, ARGS, ELSE) \
111 ({__builtin_expect (__libc_pthread_functions.ptr_##FUNC != NULL, 0) \
112 ? __libc_pthread_functions.ptr_##FUNC ARGS : ELSE; })
113 #else
114 # define __libc_maybe_call2(FUNC, ARGS, ELSE) __libc_maybe_call (__##FUNC, ARGS, ELSE)
115 #endif
117 /* Initialize the named lock variable, leaving it in a consistent, unlocked
118 state. */
119 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
120 #define __libc_lock_init(NAME) \
121 ({ \
122 (NAME).__m_count = 0; \
123 (NAME).__m_owner = NULL; \
124 (NAME).__m_kind = PTHREAD_MUTEX_TIMED_NP; \
125 (NAME).__m_lock.__status = 0; \
126 (NAME).__m_lock.__spinlock = __LT_SPINLOCK_INIT; \
127 0; })
128 #else
129 #define __libc_lock_init(NAME) \
130 (__libc_maybe_call2 (pthread_mutex_init, (&(NAME), NULL), 0))
131 #endif
132 #define __libc_rwlock_init(NAME) \
133 (__libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0));
135 /* Same as last but this time we initialize a recursive mutex. */
136 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
137 #define __libc_lock_init_recursive(NAME) \
138 ({ \
139 (NAME).mutex.__m_count = 0; \
140 (NAME).mutex.__m_owner = NULL; \
141 (NAME).mutex.__m_kind = PTHREAD_MUTEX_RECURSIVE_NP; \
142 (NAME).mutex.__m_lock.__status = 0; \
143 (NAME).mutex.__m_lock.__spinlock = __LT_SPINLOCK_INIT; \
144 0; })
145 #else
146 #define __libc_lock_init_recursive(NAME) \
147 do { \
148 if (__pthread_mutex_init != NULL) \
150 pthread_mutexattr_t __attr; \
151 __pthread_mutexattr_init (&__attr); \
152 __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
153 __pthread_mutex_init (&(NAME).mutex, &__attr); \
154 __pthread_mutexattr_destroy (&__attr); \
156 } while (0);
157 #endif
158 #define __rtld_lock_init_recursive(NAME) \
159 __libc_lock_init_recursive (NAME)
161 /* Finalize the named lock variable, which must be locked. It cannot be
162 used again until __libc_lock_init is called again on it. This must be
163 called on a lock variable before the containing storage is reused. */
164 #define __libc_lock_fini(NAME) \
165 (__libc_maybe_call2 (pthread_mutex_destroy, (&(NAME)), 0));
166 #define __libc_rwlock_fini(NAME) \
167 (__libc_maybe_call (__pthread_rwlock_destroy, (&(NAME)), 0));
169 /* Finalize recursive named lock. */
170 #define __libc_lock_fini_recursive(NAME) __libc_lock_fini ((NAME).mutex)
171 #define __rtld_lock_fini_recursive(NAME) __libc_lock_fini_recursive (NAME)
173 /* Lock the named lock variable. */
174 #define __libc_lock_lock(NAME) \
175 (__libc_maybe_call2 (pthread_mutex_lock, (&(NAME)), 0));
176 #define __libc_rwlock_rdlock(NAME) \
177 (__libc_maybe_call (__pthread_rwlock_rdlock, (&(NAME)), 0));
178 #define __libc_rwlock_wrlock(NAME) \
179 (__libc_maybe_call (__pthread_rwlock_wrlock, (&(NAME)), 0));
181 /* Lock the recursive named lock variable. */
182 #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
184 /* Try to lock the named lock variable. */
185 #define __libc_lock_trylock(NAME) \
186 (__libc_maybe_call2 (pthread_mutex_trylock, (&(NAME)), 0))
187 #define __libc_rwlock_tryrdlock(NAME) \
188 (__libc_maybe_call (__pthread_rwlock_tryrdlock, (&(NAME)), 0))
189 #define __libc_rwlock_trywrlock(NAME) \
190 (__libc_maybe_call (__pthread_rwlock_trywrlock, (&(NAME)), 0))
192 /* Try to lock the recursive named lock variable. */
193 #define __libc_lock_trylock_recursive(NAME) __libc_lock_trylock ((NAME).mutex)
194 #define __rtld_lock_trylock_recursive(NAME) \
195 __libc_lock_trylock_recursive (NAME)
197 /* Unlock the named lock variable. */
198 #define __libc_lock_unlock(NAME) \
199 (__libc_maybe_call2 (pthread_mutex_unlock, (&(NAME)), 0));
200 #define __libc_rwlock_unlock(NAME) \
201 (__libc_maybe_call (__pthread_rwlock_unlock, (&(NAME)), 0));
203 /* Unlock the recursive named lock variable. */
204 #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
206 #if defined _LIBC && defined SHARED
207 # define __rtld_lock_default_lock_recursive(lock) \
208 ++((pthread_mutex_t *)(lock))->__m_count;
210 # define __rtld_lock_default_unlock_recursive(lock) \
211 --((pthread_mutex_t *)(lock))->__m_count;
213 # define __rtld_lock_lock_recursive(NAME) \
214 GL(dl_rtld_lock_recursive) (&(NAME).mutex)
216 # define __rtld_lock_unlock_recursive(NAME) \
217 GL(dl_rtld_unlock_recursive) (&(NAME).mutex)
218 #else
219 #define __rtld_lock_lock_recursive(NAME) __libc_lock_lock_recursive (NAME)
220 #define __rtld_lock_unlock_recursive(NAME) __libc_lock_unlock_recursive (NAME)
221 #endif
223 /* Define once control variable. */
224 #if PTHREAD_ONCE_INIT == 0
225 /* Special case for static variables where we can avoid the initialization
226 if it is zero. */
227 # define __libc_once_define(CLASS, NAME) \
228 CLASS pthread_once_t NAME
229 #else
230 # define __libc_once_define(CLASS, NAME) \
231 CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
232 #endif
234 /* Call handler iff the first call. */
235 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
236 do { \
237 if (__pthread_once != NULL) \
238 __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION)); \
239 else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
240 INIT_FUNCTION (); \
241 (ONCE_CONTROL) = 2; \
243 } while (0)
246 /* Start critical region with cleanup. */
247 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
248 { struct _pthread_cleanup_buffer _buffer; \
249 int _avail = (DOIT) && _pthread_cleanup_push_defer != NULL; \
250 if (_avail) { \
251 _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG)); \
254 /* End critical region with cleanup. */
255 #define __libc_cleanup_region_end(DOIT) \
256 if (_avail) { \
257 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
261 /* Sometimes we have to exit the block in the middle. */
262 #define __libc_cleanup_end(DOIT) \
263 if (_avail) { \
264 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
267 #define __libc_cleanup_push(fct, arg) \
268 { struct _pthread_cleanup_buffer _buffer; \
269 __libc_maybe_call (_pthread_cleanup_push, (&_buffer, (fct), (arg)), 0)
271 #define __libc_cleanup_pop(execute) \
272 __libc_maybe_call (_pthread_cleanup_pop, (&_buffer, execute), 0); \
275 /* Create thread-specific key. */
276 #define __libc_key_create(KEY, DESTRUCTOR) \
277 (__libc_maybe_call (__pthread_key_create, (KEY, DESTRUCTOR), 1))
279 /* Get thread-specific data. */
280 #define __libc_getspecific(KEY) \
281 (__libc_maybe_call (__pthread_getspecific, (KEY), NULL))
283 /* Set thread-specific data. */
284 #define __libc_setspecific(KEY, VALUE) \
285 (__libc_maybe_call (__pthread_setspecific, (KEY, VALUE), 0))
288 /* Register handlers to execute before and after `fork'. */
289 #define __libc_atfork(PREPARE, PARENT, CHILD) \
290 (__libc_maybe_call (__pthread_atfork, (PREPARE, PARENT, CHILD), 0))
292 /* Functions that are used by this file and are internal to the GNU C
293 library. */
295 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
296 __const pthread_mutexattr_t *__mutex_attr);
298 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
300 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
302 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
304 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
306 extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
308 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
310 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
311 int __kind);
313 #ifdef __USE_UNIX98
314 extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
315 __const pthread_rwlockattr_t *__attr);
317 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
319 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
321 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
323 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
325 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
327 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
328 #endif
330 extern int __pthread_key_create (pthread_key_t *__key,
331 void (*__destr_function) (void *));
333 extern int __pthread_setspecific (pthread_key_t __key,
334 __const void *__pointer);
336 extern void *__pthread_getspecific (pthread_key_t __key);
338 extern int __pthread_once (pthread_once_t *__once_control,
339 void (*__init_routine) (void));
341 extern int __pthread_atfork (void (*__prepare) (void),
342 void (*__parent) (void),
343 void (*__child) (void));
347 /* Make the pthread functions weak so that we can elide them from
348 single-threaded processes. */
349 #ifndef __NO_WEAK_PTHREAD_ALIASES
350 # ifdef weak_extern
351 # if _LIBC
352 # include <bp-sym.h>
353 # else
354 # define BP_SYM (sym) sym
355 # endif
356 weak_extern (BP_SYM (__pthread_mutex_init))
357 weak_extern (BP_SYM (__pthread_mutex_destroy))
358 weak_extern (BP_SYM (__pthread_mutex_lock))
359 weak_extern (BP_SYM (__pthread_mutex_trylock))
360 weak_extern (BP_SYM (__pthread_mutex_unlock))
361 weak_extern (BP_SYM (__pthread_mutexattr_init))
362 weak_extern (BP_SYM (__pthread_mutexattr_destroy))
363 weak_extern (BP_SYM (__pthread_mutexattr_settype))
364 weak_extern (BP_SYM (__pthread_rwlock_init))
365 weak_extern (BP_SYM (__pthread_rwlock_destroy))
366 weak_extern (BP_SYM (__pthread_rwlock_rdlock))
367 weak_extern (BP_SYM (__pthread_rwlock_tryrdlock))
368 weak_extern (BP_SYM (__pthread_rwlock_wrlock))
369 weak_extern (BP_SYM (__pthread_rwlock_trywrlock))
370 weak_extern (BP_SYM (__pthread_rwlock_unlock))
371 weak_extern (BP_SYM (__pthread_key_create))
372 weak_extern (BP_SYM (__pthread_setspecific))
373 weak_extern (BP_SYM (__pthread_getspecific))
374 weak_extern (BP_SYM (__pthread_once))
375 weak_extern (__pthread_initialize)
376 weak_extern (__pthread_atfork)
377 weak_extern (BP_SYM (_pthread_cleanup_push))
378 weak_extern (BP_SYM (_pthread_cleanup_pop))
379 weak_extern (BP_SYM (_pthread_cleanup_push_defer))
380 weak_extern (BP_SYM (_pthread_cleanup_pop_restore))
381 # else
382 # pragma weak __pthread_mutex_init
383 # pragma weak __pthread_mutex_destroy
384 # pragma weak __pthread_mutex_lock
385 # pragma weak __pthread_mutex_trylock
386 # pragma weak __pthread_mutex_unlock
387 # pragma weak __pthread_mutexattr_init
388 # pragma weak __pthread_mutexattr_destroy
389 # pragma weak __pthread_mutexattr_settype
390 # pragma weak __pthread_rwlock_destroy
391 # pragma weak __pthread_rwlock_rdlock
392 # pragma weak __pthread_rwlock_tryrdlock
393 # pragma weak __pthread_rwlock_wrlock
394 # pragma weak __pthread_rwlock_trywrlock
395 # pragma weak __pthread_rwlock_unlock
396 # pragma weak __pthread_key_create
397 # pragma weak __pthread_setspecific
398 # pragma weak __pthread_getspecific
399 # pragma weak __pthread_once
400 # pragma weak __pthread_initialize
401 # pragma weak __pthread_atfork
402 # pragma weak _pthread_cleanup_push_defer
403 # pragma weak _pthread_cleanup_pop_restore
404 # pragma weak _pthread_cleanup_push
405 # pragma weak _pthread_cleanup_pop
406 # endif
407 #endif
409 /* We need portable names for some functions. E.g., when they are
410 used as argument to __libc_cleanup_region_start. */
411 #define __libc_mutex_unlock __pthread_mutex_unlock
413 #endif /* bits/libc-lock.h */