1 /* libc-internal interface for mutex locks. LinuxThreads version.
2 Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003,2006
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/internals.h>
30 #if defined(_LIBC) || defined(_IO_MTSAFE_IO)
31 typedef pthread_mutex_t __libc_lock_t;
32 typedef struct { pthread_mutex_t mutex; } __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 a recursive mutex. */
135 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
136 #define __libc_lock_init_recursive(NAME) \
138 (NAME).mutex.__m_count = 0; \
139 (NAME).mutex.__m_owner = NULL; \
140 (NAME).mutex.__m_kind = PTHREAD_MUTEX_RECURSIVE_NP; \
141 (NAME).mutex.__m_lock.__status = 0; \
142 (NAME).mutex.__m_lock.__spinlock = __LT_SPINLOCK_INIT; \
145 #define __libc_lock_init_recursive(NAME) \
147 if (__pthread_mutex_init != NULL) \
149 pthread_mutexattr_t __attr; \
150 __pthread_mutexattr_init (&__attr); \
151 __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
152 __pthread_mutex_init (&(NAME).mutex, &__attr); \
153 __pthread_mutexattr_destroy (&__attr); \
157 #define __rtld_lock_init_recursive(NAME) \
158 __libc_lock_init_recursive (NAME)
160 /* Finalize the named lock variable, which must be locked. It cannot be
161 used again until __libc_lock_init is called again on it. This must be
162 called on a lock variable before the containing storage is reused. */
163 #define __libc_lock_fini(NAME) \
164 (__libc_maybe_call2 (pthread_mutex_destroy, (&(NAME)), 0));
165 #define __libc_rwlock_fini(NAME) \
166 (__libc_maybe_call (__pthread_rwlock_destroy, (&(NAME)), 0));
168 /* Finalize recursive named lock. */
169 #define __libc_lock_fini_recursive(NAME) __libc_lock_fini ((NAME).mutex)
170 #define __rtld_lock_fini_recursive(NAME) __libc_lock_fini_recursive (NAME)
172 /* Lock the named lock variable. */
173 #define __libc_lock_lock(NAME) \
174 (__libc_maybe_call2 (pthread_mutex_lock, (&(NAME)), 0));
175 #define __libc_rwlock_rdlock(NAME) \
176 (__libc_maybe_call (__pthread_rwlock_rdlock, (&(NAME)), 0));
177 #define __libc_rwlock_wrlock(NAME) \
178 (__libc_maybe_call (__pthread_rwlock_wrlock, (&(NAME)), 0));
180 /* Lock the recursive named lock variable. */
181 #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
183 /* Try to lock the named lock variable. */
184 #define __libc_lock_trylock(NAME) \
185 (__libc_maybe_call2 (pthread_mutex_trylock, (&(NAME)), 0))
186 #define __libc_rwlock_tryrdlock(NAME) \
187 (__libc_maybe_call (__pthread_rwlock_tryrdlock, (&(NAME)), 0))
188 #define __libc_rwlock_trywrlock(NAME) \
189 (__libc_maybe_call (__pthread_rwlock_trywrlock, (&(NAME)), 0))
191 /* Try to lock the recursive named lock variable. */
192 #define __libc_lock_trylock_recursive(NAME) __libc_lock_trylock ((NAME).mutex)
193 #define __rtld_lock_trylock_recursive(NAME) \
194 __libc_lock_trylock_recursive (NAME)
196 /* Unlock the named lock variable. */
197 #define __libc_lock_unlock(NAME) \
198 (__libc_maybe_call2 (pthread_mutex_unlock, (&(NAME)), 0));
199 #define __libc_rwlock_unlock(NAME) \
200 (__libc_maybe_call (__pthread_rwlock_unlock, (&(NAME)), 0));
202 /* Unlock the recursive named lock variable. */
203 #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
205 #if defined _LIBC && defined SHARED
206 # define __rtld_lock_default_lock_recursive(lock) \
207 ++((pthread_mutex_t *)(lock))->__m_count;
209 # define __rtld_lock_default_unlock_recursive(lock) \
210 --((pthread_mutex_t *)(lock))->__m_count;
212 # define __rtld_lock_lock_recursive(NAME) \
213 GL(dl_rtld_lock_recursive) (&(NAME).mutex)
215 # define __rtld_lock_unlock_recursive(NAME) \
216 GL(dl_rtld_unlock_recursive) (&(NAME).mutex)
218 #define __rtld_lock_lock_recursive(NAME) __libc_lock_lock_recursive (NAME)
219 #define __rtld_lock_unlock_recursive(NAME) __libc_lock_unlock_recursive (NAME)
222 /* Define once control variable. */
223 #if PTHREAD_ONCE_INIT == 0
224 /* Special case for static variables where we can avoid the initialization
226 # define __libc_once_define(CLASS, NAME) \
227 CLASS pthread_once_t NAME
229 # define __libc_once_define(CLASS, NAME) \
230 CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
233 /* Call handler iff the first call. */
234 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
236 if (__pthread_once != NULL) \
237 __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION)); \
238 else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
240 (ONCE_CONTROL) = 2; \
245 /* Start critical region with cleanup. */
246 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
247 { struct _pthread_cleanup_buffer _buffer; \
248 int _avail = (DOIT) && _pthread_cleanup_push_defer != NULL; \
250 _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG)); \
253 /* End critical region with cleanup. */
254 #define __libc_cleanup_region_end(DOIT) \
256 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
260 /* Sometimes we have to exit the block in the middle. */
261 #define __libc_cleanup_end(DOIT) \
263 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
266 #define __libc_cleanup_push(fct, arg) \
267 { struct _pthread_cleanup_buffer _buffer; \
268 __libc_maybe_call (_pthread_cleanup_push, (&_buffer, (fct), (arg)), 0)
270 #define __libc_cleanup_pop(execute) \
271 __libc_maybe_call (_pthread_cleanup_pop, (&_buffer, execute), 0); \
274 /* Create thread-specific key. */
275 #define __libc_key_create(KEY, DESTRUCTOR) \
276 (__libc_maybe_call (__pthread_key_create, (KEY, DESTRUCTOR), 1))
278 /* Get thread-specific data. */
279 #define __libc_getspecific(KEY) \
280 (__libc_maybe_call (__pthread_getspecific, (KEY), NULL))
282 /* Set thread-specific data. */
283 #define __libc_setspecific(KEY, VALUE) \
284 (__libc_maybe_call (__pthread_setspecific, (KEY, VALUE), 0))
287 /* Register handlers to execute before and after `fork'. */
288 #define __libc_atfork(PREPARE, PARENT, CHILD) \
289 (__libc_maybe_call (__pthread_atfork, (PREPARE, PARENT, CHILD), 0))
291 /* Functions that are used by this file and are internal to the GNU C
294 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
295 __const pthread_mutexattr_t *__mutex_attr);
297 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
299 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
301 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
303 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
305 extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
307 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
309 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
313 extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
314 __const pthread_rwlockattr_t *__attr);
316 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
318 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
320 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
322 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
324 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
326 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
329 extern int __pthread_key_create (pthread_key_t *__key,
330 void (*__destr_function) (void *));
332 extern int __pthread_setspecific (pthread_key_t __key,
333 __const void *__pointer);
335 extern void *__pthread_getspecific (pthread_key_t __key);
337 extern int __pthread_once (pthread_once_t *__once_control,
338 void (*__init_routine) (void));
340 extern int __pthread_atfork (void (*__prepare) (void),
341 void (*__parent) (void),
342 void (*__child) (void));
346 /* Make the pthread functions weak so that we can elide them from
347 single-threaded processes. */
348 #ifndef __NO_WEAK_PTHREAD_ALIASES
350 # define BP_SYM(sym) sym
351 weak_extern (BP_SYM (__pthread_mutex_init))
352 weak_extern (BP_SYM (__pthread_mutex_destroy))
353 weak_extern (BP_SYM (__pthread_mutex_lock))
354 weak_extern (BP_SYM (__pthread_mutex_trylock))
355 weak_extern (BP_SYM (__pthread_mutex_unlock))
356 weak_extern (BP_SYM (__pthread_mutexattr_init))
357 weak_extern (BP_SYM (__pthread_mutexattr_destroy))
358 weak_extern (BP_SYM (__pthread_mutexattr_settype))
359 weak_extern (BP_SYM (__pthread_rwlock_init))
360 weak_extern (BP_SYM (__pthread_rwlock_destroy))
361 weak_extern (BP_SYM (__pthread_rwlock_rdlock))
362 weak_extern (BP_SYM (__pthread_rwlock_tryrdlock))
363 weak_extern (BP_SYM (__pthread_rwlock_wrlock))
364 weak_extern (BP_SYM (__pthread_rwlock_trywrlock))
365 weak_extern (BP_SYM (__pthread_rwlock_unlock))
366 weak_extern (BP_SYM (__pthread_key_create))
367 weak_extern (BP_SYM (__pthread_setspecific))
368 weak_extern (BP_SYM (__pthread_getspecific))
369 weak_extern (BP_SYM (__pthread_once))
370 weak_extern (__pthread_initialize)
371 weak_extern (__pthread_atfork)
372 weak_extern (BP_SYM (_pthread_cleanup_push))
373 weak_extern (BP_SYM (_pthread_cleanup_pop))
374 weak_extern (BP_SYM (_pthread_cleanup_push_defer))
375 weak_extern (BP_SYM (_pthread_cleanup_pop_restore))
377 # pragma weak __pthread_mutex_init
378 # pragma weak __pthread_mutex_destroy
379 # pragma weak __pthread_mutex_lock
380 # pragma weak __pthread_mutex_trylock
381 # pragma weak __pthread_mutex_unlock
382 # pragma weak __pthread_mutexattr_init
383 # pragma weak __pthread_mutexattr_destroy
384 # pragma weak __pthread_mutexattr_settype
385 # pragma weak __pthread_rwlock_destroy
386 # pragma weak __pthread_rwlock_rdlock
387 # pragma weak __pthread_rwlock_tryrdlock
388 # pragma weak __pthread_rwlock_wrlock
389 # pragma weak __pthread_rwlock_trywrlock
390 # pragma weak __pthread_rwlock_unlock
391 # pragma weak __pthread_key_create
392 # pragma weak __pthread_setspecific
393 # pragma weak __pthread_getspecific
394 # pragma weak __pthread_once
395 # pragma weak __pthread_initialize
396 # pragma weak __pthread_atfork
397 # pragma weak _pthread_cleanup_push_defer
398 # pragma weak _pthread_cleanup_pop_restore
399 # pragma weak _pthread_cleanup_push
400 # pragma weak _pthread_cleanup_pop
404 /* We need portable names for some functions. E.g., when they are
405 used as argument to __libc_cleanup_region_start. */
406 #define __libc_mutex_unlock __pthread_mutex_unlock
408 #endif /* bits/libc-lock.h */