1 /* libc-internal interface for mutex locks. LinuxThreads version.
2 Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #ifndef _BITS_LIBC_LOCK_H
21 #define _BITS_LIBC_LOCK_H 1
27 typedef pthread_mutex_t __libc_lock_t;
28 typedef pthread_rwlock_t __libc_rwlock_t;
29 typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
31 typedef struct __libc_lock_opaque__ __libc_lock_t;
32 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
33 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
36 /* Type for key to thread-specific data. */
37 typedef pthread_key_t __libc_key_t;
39 /* Define a lock variable NAME with storage class CLASS. The lock must be
40 initialized with __libc_lock_init before it can be used (or define it
41 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
42 declare a lock defined in another module. In public structure
43 definitions you must use a pointer to the lock structure (i.e., NAME
44 begins with a `*'), because its storage size will not be known outside
46 #define __libc_lock_define(CLASS,NAME) \
47 CLASS __libc_lock_t NAME;
48 #define __libc_rwlock_define(CLASS,NAME) \
49 CLASS __libc_rwlock_t NAME;
50 #define __libc_lock_define_recursive(CLASS,NAME) \
51 CLASS __libc_lock_recursive_t NAME;
53 /* Define an initialized lock variable NAME with storage class CLASS.
55 For the C library we take a deeper look at the initializer. For
56 this implementation all fields are initialized to zero. Therefore
57 we don't initialize the variable which allows putting it into the
58 BSS section. (Except on PA-RISC and other odd architectures, where
59 initialized locks must be set to one due to the lack of normal
60 atomic operations.) */
62 #if __LT_SPINLOCK_INIT == 0
63 # define __libc_lock_define_initialized(CLASS,NAME) \
64 CLASS __libc_lock_t NAME;
66 # define __libc_lock_define_initialized(CLASS,NAME) \
67 CLASS __libc_lock_t NAME = PTHREAD_MUTEX_INITIALIZER;
70 #define __libc_rwlock_define_initialized(CLASS,NAME) \
71 CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
73 /* Define an initialized recursive lock variable NAME with storage
75 #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
76 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
77 #define _LIBC_LOCK_RECURSIVE_INITIALIZER \
78 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
80 /* Initialize the named lock variable, leaving it in a consistent, unlocked
82 #define __libc_lock_init(NAME) \
83 (__pthread_mutex_init != NULL ? __pthread_mutex_init (&(NAME), NULL) : 0);
84 #define __libc_rwlock_init(NAME) \
85 (__pthread_rwlock_init != NULL ? __pthread_rwlock_init (&(NAME), NULL) : 0);
87 /* Same as last but this time we initialize a recursive mutex. */
88 #define __libc_lock_init_recursive(NAME) \
90 if (__pthread_mutex_init != NULL) \
92 pthread_mutexattr_t __attr; \
93 __pthread_mutexattr_init (&__attr); \
94 __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
95 __pthread_mutex_init (&(NAME).mutex, &__attr); \
96 __pthread_mutexattr_destroy (&__attr); \
100 /* Finalize the named lock variable, which must be locked. It cannot be
101 used again until __libc_lock_init is called again on it. This must be
102 called on a lock variable before the containing storage is reused. */
103 #define __libc_lock_fini(NAME) \
104 (__pthread_mutex_destroy != NULL ? __pthread_mutex_destroy (&(NAME)) : 0);
105 #define __libc_rwlock_fini(NAME) \
106 (__pthread_rwlock_destroy != NULL ? __pthread_rwlock_destroy (&(NAME)) : 0);
108 /* Finalize recursive named lock. */
109 #define __libc_lock_fini_recursive(NAME) __libc_lock_fini ((NAME).mutex)
111 /* Lock the named lock variable. */
112 #define __libc_lock_lock(NAME) \
113 (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (&(NAME)) : 0);
114 #define __libc_rwlock_rdlock(NAME) \
115 (__pthread_rwlock_rdlock != NULL ? __pthread_rwlock_rdlock (&(NAME)) : 0);
116 #define __libc_rwlock_wrlock(NAME) \
117 (__pthread_rwlock_wrlock != NULL ? __pthread_rwlock_wrlock (&(NAME)) : 0);
119 /* Lock the recursive named lock variable. */
120 #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
122 /* Try to lock the named lock variable. */
123 #define __libc_lock_trylock(NAME) \
124 (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (&(NAME)) : 0)
125 #define __libc_rwlock_tryrdlock(NAME) \
126 (__pthread_rwlock_tryrdlock != NULL \
127 ? __pthread_rwlock_tryrdlock (&(NAME)) : 0)
128 #define __libc_rwlock_trywrlock(NAME) \
129 (__pthread_rwlock_trywrlock != NULL \
130 ? __pthread_rwlock_trywrlock (&(NAME)) : 0)
132 /* Try to lock the recursive named lock variable. */
133 #define __libc_lock_trylock_recursive(NAME) __libc_lock_trylock ((NAME).mutex)
135 /* Unlock the named lock variable. */
136 #define __libc_lock_unlock(NAME) \
137 (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (&(NAME)) : 0);
138 #define __libc_rwlock_unlock(NAME) \
139 (__pthread_rwlock_unlock != NULL ? __pthread_rwlock_unlock (&(NAME)) : 0);
141 /* Unlock the recursive named lock variable. */
142 #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
145 /* Define once control variable. */
146 #if PTHREAD_ONCE_INIT == 0
147 /* Special case for static variables where we can avoid the initialization
149 # define __libc_once_define(CLASS, NAME) \
150 CLASS pthread_once_t NAME
152 # define __libc_once_define(CLASS, NAME) \
153 CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
156 /* Call handler iff the first call. */
157 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
159 if (__pthread_once != NULL) \
160 __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION)); \
161 else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
163 (ONCE_CONTROL) = !PTHREAD_ONCE_INIT; \
168 /* Start critical region with cleanup. */
169 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
170 { struct _pthread_cleanup_buffer _buffer; \
171 int _avail = (DOIT) && _pthread_cleanup_push_defer != NULL; \
173 _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG)); \
176 /* End critical region with cleanup. */
177 #define __libc_cleanup_region_end(DOIT) \
179 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
183 /* Sometimes we have to exit the block in the middle. */
184 #define __libc_cleanup_end(DOIT) \
186 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
189 /* Create thread-specific key. */
190 #define __libc_key_create(KEY, DESTRUCTOR) \
191 (__pthread_key_create != NULL ? __pthread_key_create (KEY, DESTRUCTOR) : 1)
193 /* Get thread-specific data. */
194 #define __libc_getspecific(KEY) \
195 (__pthread_getspecific != NULL ? __pthread_getspecific (KEY) : NULL)
197 /* Set thread-specific data. */
198 #define __libc_setspecific(KEY, VALUE) \
199 (__pthread_setspecific != NULL ? __pthread_setspecific (KEY, VALUE) : 0)
202 /* Register handlers to execute before and after `fork'. */
203 #define __libc_atfork(PREPARE, PARENT, CHILD) \
204 (__pthread_atfork != NULL ? __pthread_atfork (PREPARE, PARENT, CHILD) : 0)
206 /* Functions that are used by this file and are internal to the GNU C
209 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
210 __const pthread_mutexattr_t *__mutex_attr);
212 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
214 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
216 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
218 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
220 extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
222 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
224 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
228 extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
229 __const pthread_rwlockattr_t *__attr);
231 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
233 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
235 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
237 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
239 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
241 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
244 extern int __pthread_key_create (pthread_key_t *__key,
245 void (*__destr_function) (void *));
247 extern int __pthread_setspecific (pthread_key_t __key,
248 __const void *__pointer);
250 extern void *__pthread_getspecific (pthread_key_t __key);
252 extern int __pthread_once (pthread_once_t *__once_control,
253 void (*__init_routine) (void));
255 extern int __pthread_atfork (void (*__prepare) (void),
256 void (*__parent) (void),
257 void (*__child) (void));
261 /* Make the pthread functions weak so that we can elide them from
262 single-threaded processes. */
263 #ifndef __NO_WEAK_PTHREAD_ALIASES
268 # define BP_SYM (sym) sym
270 weak_extern (BP_SYM (__pthread_mutex_init))
271 weak_extern (BP_SYM (__pthread_mutex_destroy))
272 weak_extern (BP_SYM (__pthread_mutex_lock))
273 weak_extern (BP_SYM (__pthread_mutex_trylock))
274 weak_extern (BP_SYM (__pthread_mutex_unlock))
275 weak_extern (BP_SYM (__pthread_mutexattr_init))
276 weak_extern (BP_SYM (__pthread_mutexattr_destroy))
277 weak_extern (BP_SYM (__pthread_mutexattr_settype))
278 weak_extern (BP_SYM (__pthread_rwlock_init))
279 weak_extern (BP_SYM (__pthread_rwlock_destroy))
280 weak_extern (BP_SYM (__pthread_rwlock_rdlock))
281 weak_extern (BP_SYM (__pthread_rwlock_tryrdlock))
282 weak_extern (BP_SYM (__pthread_rwlock_wrlock))
283 weak_extern (BP_SYM (__pthread_rwlock_trywrlock))
284 weak_extern (BP_SYM (__pthread_rwlock_unlock))
285 weak_extern (BP_SYM (__pthread_key_create))
286 weak_extern (BP_SYM (__pthread_setspecific))
287 weak_extern (BP_SYM (__pthread_getspecific))
288 weak_extern (BP_SYM (__pthread_once))
289 weak_extern (__pthread_initialize)
290 weak_extern (__pthread_atfork)
291 weak_extern (BP_SYM (_pthread_cleanup_push_defer))
292 weak_extern (BP_SYM (_pthread_cleanup_pop_restore))
294 # pragma weak __pthread_mutex_init
295 # pragma weak __pthread_mutex_destroy
296 # pragma weak __pthread_mutex_lock
297 # pragma weak __pthread_mutex_trylock
298 # pragma weak __pthread_mutex_unlock
299 # pragma weak __pthread_mutexattr_init
300 # pragma weak __pthread_mutexattr_destroy
301 # pragma weak __pthread_mutexattr_settype
302 # pragma weak __pthread_rwlock_destroy
303 # pragma weak __pthread_rwlock_rdlock
304 # pragma weak __pthread_rwlock_tryrdlock
305 # pragma weak __pthread_rwlock_wrlock
306 # pragma weak __pthread_rwlock_trywrlock
307 # pragma weak __pthread_rwlock_unlock
308 # pragma weak __pthread_key_create
309 # pragma weak __pthread_setspecific
310 # pragma weak __pthread_getspecific
311 # pragma weak __pthread_once
312 # pragma weak __pthread_initialize
313 # pragma weak __pthread_atfork
314 # pragma weak _pthread_cleanup_push_defer
315 # pragma weak _pthread_cleanup_pop_restore
319 /* We need portable names for some functions. E.g., when they are
320 used as argument to __libc_cleanup_region_start. */
321 #define __libc_mutex_unlock __pthread_mutex_unlock
323 #endif /* bits/libc-lock.h */