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
18 not, see <http://www.gnu.org/licenses/>. */
20 #ifndef _BITS_LIBC_LOCK_H
21 #define _BITS_LIBC_LOCK_H 1
25 #if defined _LIBC && !defined NOT_IN_libc
26 #include <linuxthreads.old/internals.h>
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;
34 typedef pthread_rwlock_t __libc_rwlock_t;
36 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
38 typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
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;
45 /* Type for key to thread-specific data. */
46 typedef pthread_key_t __libc_key_t;
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
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;
64 /* Define an initialized lock variable NAME with storage class CLASS.
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.) */
73 #if __LT_SPINLOCK_INIT == 0
74 # define __libc_lock_define_initialized(CLASS,NAME) \
75 CLASS __libc_lock_t NAME;
77 # define __libc_lock_define_initialized(CLASS,NAME) \
78 CLASS __libc_lock_t NAME = PTHREAD_MUTEX_INITIALIZER;
81 #define __libc_rwlock_define_initialized(CLASS,NAME) \
82 CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
84 /* Define an initialized recursive lock variable NAME with storage
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}
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}
96 #if defined _LIBC && defined IS_IN_libpthread
97 # define __libc_maybe_call(FUNC, ARGS, ELSE) FUNC ARGS
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; }))
104 # define __libc_maybe_call(FUNC, ARGS, ELSE) \
105 (FUNC != NULL ? FUNC ARGS : ELSE)
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; })
113 # define __libc_maybe_call2(FUNC, ARGS, ELSE) __libc_maybe_call (__##FUNC, ARGS, ELSE)
116 /* Initialize the named lock variable, leaving it in a consistent, unlocked
118 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
119 #define __libc_lock_init(NAME) \
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; \
128 #define __libc_lock_init(NAME) \
129 (__libc_maybe_call2 (pthread_mutex_init, (&(NAME), NULL), 0))
131 #define __libc_rwlock_init(NAME) \
132 (__libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0));
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) \
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; \
145 #define __libc_lock_init_adaptive(NAME) \
147 if (__pthread_mutex_init != NULL) \
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); \
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) \
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; \
169 #define __libc_lock_init_recursive(NAME) \
171 if (__pthread_mutex_init != NULL) \
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); \
181 #define __rtld_lock_init_recursive(NAME) \
182 __libc_lock_init_recursive (NAME)
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));
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)
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));
204 /* Lock the recursive named lock variable. */
205 #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
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))
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)
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));
226 /* Unlock the recursive named lock variable. */
227 #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
229 /* Define once control variable. */
230 #if PTHREAD_ONCE_INIT == 0
231 /* Special case for static variables where we can avoid the initialization
233 # define __libc_once_define(CLASS, NAME) \
234 CLASS pthread_once_t NAME
236 # define __libc_once_define(CLASS, NAME) \
237 CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
240 /* Call handler iff the first call. */
241 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
243 if (__pthread_once != NULL) \
244 __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION)); \
245 else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
247 (ONCE_CONTROL) = !PTHREAD_ONCE_INIT; \
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; \
257 _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG)); \
260 /* End critical region with cleanup. */
261 #define __libc_cleanup_region_end(DOIT) \
263 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
267 /* Sometimes we have to exit the block in the middle. */
268 #define __libc_cleanup_end(DOIT) \
270 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
274 #define __libc_cleanup_push(fct, arg) \
275 { struct _pthread_cleanup_buffer _buffer; \
276 __libc_maybe_call (_pthread_cleanup_push, (&_buffer, (fct), (arg)), 0)
278 #define __libc_cleanup_pop(execute) \
279 __libc_maybe_call (_pthread_cleanup_pop, (&_buffer, execute), 0); \
283 /* Create thread-specific key. */
284 #define __libc_key_create(KEY, DESTRUCTOR) \
285 (__libc_maybe_call (__pthread_key_create, (KEY, DESTRUCTOR), 1))
287 /* Get thread-specific data. */
288 #define __libc_getspecific(KEY) \
289 (__libc_maybe_call (__pthread_getspecific, (KEY), NULL))
291 /* Set thread-specific data. */
292 #define __libc_setspecific(KEY, VALUE) \
293 (__libc_maybe_call (__pthread_setspecific, (KEY, VALUE), 0))
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))
300 /* Functions that are used by this file and are internal to the GNU C
303 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
304 const pthread_mutexattr_t *__mutex_attr);
306 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
308 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
310 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
312 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
314 extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
316 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
318 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
322 extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
323 const pthread_rwlockattr_t *__attr);
325 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
327 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
329 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
331 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
333 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
335 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
338 extern int __pthread_key_create (pthread_key_t *__key,
339 void (*__destr_function) (void *));
341 extern int __pthread_setspecific (pthread_key_t __key,
342 const void *__pointer);
344 extern void *__pthread_getspecific (pthread_key_t __key);
346 extern int __pthread_once (pthread_once_t *__once_control,
347 void (*__init_routine) (void));
349 extern int __pthread_atfork (void (*__prepare) (void),
350 void (*__parent) (void),
351 void (*__child) (void));
355 /* Make the pthread functions weak so that we can elide them from
356 single-threaded processes. */
357 #ifndef __NO_WEAK_PTHREAD_ALIASES
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))
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
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
415 #endif /* bits/libc-lock.h */