2.5-18.1
[glibc.git] / fedora / libc-lock.h
blob5037caf4da025cda94d169a129775574afac7bb3
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 /* Mutex type. */
27 #ifdef _IO_MTSAFE_IO
28 typedef pthread_mutex_t __libc_lock_t;
29 typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
30 # ifdef __USE_UNIX98
31 typedef pthread_rwlock_t __libc_rwlock_t;
32 # else
33 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
34 # endif
35 typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
36 #else
37 typedef struct __libc_lock_opaque__ __libc_lock_t;
38 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
39 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
40 #endif
42 /* Type for key to thread-specific data. */
43 typedef pthread_key_t __libc_key_t;
45 /* Define a lock variable NAME with storage class CLASS. The lock must be
46 initialized with __libc_lock_init before it can be used (or define it
47 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
48 declare a lock defined in another module. In public structure
49 definitions you must use a pointer to the lock structure (i.e., NAME
50 begins with a `*'), because its storage size will not be known outside
51 of libc. */
52 #define __libc_lock_define(CLASS,NAME) \
53 CLASS __libc_lock_t NAME;
54 #define __libc_rwlock_define(CLASS,NAME) \
55 CLASS __libc_rwlock_t NAME;
56 #define __libc_lock_define_recursive(CLASS,NAME) \
57 CLASS __libc_lock_recursive_t NAME;
58 #define __rtld_lock_define_recursive(CLASS,NAME) \
59 CLASS __rtld_lock_recursive_t NAME;
61 /* Define an initialized lock variable NAME with storage class CLASS.
63 For the C library we take a deeper look at the initializer. For
64 this implementation all fields are initialized to zero. Therefore
65 we don't initialize the variable which allows putting it into the
66 BSS section. (Except on PA-RISC and other odd architectures, where
67 initialized locks must be set to one due to the lack of normal
68 atomic operations.) */
70 #if __LT_SPINLOCK_INIT == 0
71 # define __libc_lock_define_initialized(CLASS,NAME) \
72 CLASS __libc_lock_t NAME;
73 #else
74 # define __libc_lock_define_initialized(CLASS,NAME) \
75 CLASS __libc_lock_t NAME = PTHREAD_MUTEX_INITIALIZER;
76 #endif
78 #define __libc_rwlock_define_initialized(CLASS,NAME) \
79 CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
81 /* Define an initialized recursive lock variable NAME with storage
82 class CLASS. */
83 #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
84 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
85 #define _LIBC_LOCK_RECURSIVE_INITIALIZER \
86 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
88 #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
89 CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
90 #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
91 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
93 #if defined __PIC__
94 # define __libc_maybe_call(FUNC, ARGS, ELSE) \
95 (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
96 _fn != NULL ? (*_fn) ARGS : ELSE; }))
97 #else
98 # define __libc_maybe_call(FUNC, ARGS, ELSE) \
99 (FUNC != NULL ? FUNC ARGS : ELSE)
100 #endif
101 #define __libc_maybe_call2(FUNC, ARGS, ELSE) __libc_maybe_call (__##FUNC, ARGS, ELSE)
103 /* Initialize the named lock variable, leaving it in a consistent, unlocked
104 state. */
105 #define __libc_lock_init(NAME) \
106 (__libc_maybe_call2 (pthread_mutex_init, (&(NAME), NULL), 0))
107 #define __libc_rwlock_init(NAME) \
108 (__libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0));
110 /* Same as last but this time we initialize a recursive mutex. */
111 #define __libc_lock_init_recursive(NAME) \
112 do { \
113 if (__pthread_mutex_init != NULL) \
115 pthread_mutexattr_t __attr; \
116 __pthread_mutexattr_init (&__attr); \
117 __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
118 __pthread_mutex_init (&(NAME).mutex, &__attr); \
119 __pthread_mutexattr_destroy (&__attr); \
121 } while (0);
122 #define __rtld_lock_init_recursive(NAME) \
123 __libc_lock_init_recursive (NAME)
125 /* Finalize the named lock variable, which must be locked. It cannot be
126 used again until __libc_lock_init is called again on it. This must be
127 called on a lock variable before the containing storage is reused. */
128 #define __libc_lock_fini(NAME) \
129 (__libc_maybe_call2 (pthread_mutex_destroy, (&(NAME)), 0));
130 #define __libc_rwlock_fini(NAME) \
131 (__libc_maybe_call (__pthread_rwlock_destroy, (&(NAME)), 0));
133 /* Finalize recursive named lock. */
134 #define __libc_lock_fini_recursive(NAME) __libc_lock_fini ((NAME).mutex)
135 #define __rtld_lock_fini_recursive(NAME) __libc_lock_fini_recursive (NAME)
137 /* Lock the named lock variable. */
138 #define __libc_lock_lock(NAME) \
139 (__libc_maybe_call2 (pthread_mutex_lock, (&(NAME)), 0));
140 #define __libc_rwlock_rdlock(NAME) \
141 (__libc_maybe_call (__pthread_rwlock_rdlock, (&(NAME)), 0));
142 #define __libc_rwlock_wrlock(NAME) \
143 (__libc_maybe_call (__pthread_rwlock_wrlock, (&(NAME)), 0));
145 /* Lock the recursive named lock variable. */
146 #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
148 /* Try to lock the named lock variable. */
149 #define __libc_lock_trylock(NAME) \
150 (__libc_maybe_call2 (pthread_mutex_trylock, (&(NAME)), 0))
151 #define __libc_rwlock_tryrdlock(NAME) \
152 (__libc_maybe_call (__pthread_rwlock_tryrdlock, (&(NAME)), 0))
153 #define __libc_rwlock_trywrlock(NAME) \
154 (__libc_maybe_call (__pthread_rwlock_trywrlock, (&(NAME)), 0))
156 /* Try to lock the recursive named lock variable. */
157 #define __libc_lock_trylock_recursive(NAME) __libc_lock_trylock ((NAME).mutex)
158 #define __rtld_lock_trylock_recursive(NAME) \
159 __libc_lock_trylock_recursive (NAME)
161 /* Unlock the named lock variable. */
162 #define __libc_lock_unlock(NAME) \
163 (__libc_maybe_call2 (pthread_mutex_unlock, (&(NAME)), 0));
164 #define __libc_rwlock_unlock(NAME) \
165 (__libc_maybe_call (__pthread_rwlock_unlock, (&(NAME)), 0));
167 /* Unlock the recursive named lock variable. */
168 #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
170 #define __rtld_lock_lock_recursive(NAME) __libc_lock_lock_recursive (NAME)
171 #define __rtld_lock_unlock_recursive(NAME) __libc_lock_unlock_recursive (NAME)
173 /* Define once control variable. */
174 #if PTHREAD_ONCE_INIT == 0
175 /* Special case for static variables where we can avoid the initialization
176 if it is zero. */
177 # define __libc_once_define(CLASS, NAME) \
178 CLASS pthread_once_t NAME
179 #else
180 # define __libc_once_define(CLASS, NAME) \
181 CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
182 #endif
184 /* Call handler iff the first call. */
185 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
186 do { \
187 if (__pthread_once != NULL) \
188 __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION)); \
189 else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
190 INIT_FUNCTION (); \
191 (ONCE_CONTROL) = 2; \
193 } while (0)
196 /* Start critical region with cleanup. */
197 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
198 { struct _pthread_cleanup_buffer _buffer; \
199 int _avail = (DOIT) && _pthread_cleanup_push_defer != NULL; \
200 if (_avail) { \
201 _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG)); \
204 /* End critical region with cleanup. */
205 #define __libc_cleanup_region_end(DOIT) \
206 if (_avail) { \
207 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
211 /* Sometimes we have to exit the block in the middle. */
212 #define __libc_cleanup_end(DOIT) \
213 if (_avail) { \
214 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
217 #define __libc_cleanup_push(fct, arg) \
218 { struct _pthread_cleanup_buffer _buffer; \
219 __libc_maybe_call (_pthread_cleanup_push, (&_buffer, (fct), (arg)), 0)
221 #define __libc_cleanup_pop(execute) \
222 __libc_maybe_call (_pthread_cleanup_pop, (&_buffer, execute), 0); \
225 /* Create thread-specific key. */
226 #define __libc_key_create(KEY, DESTRUCTOR) \
227 (__libc_maybe_call (__pthread_key_create, (KEY, DESTRUCTOR), 1))
229 /* Get thread-specific data. */
230 #define __libc_getspecific(KEY) \
231 (__libc_maybe_call (__pthread_getspecific, (KEY), NULL))
233 /* Set thread-specific data. */
234 #define __libc_setspecific(KEY, VALUE) \
235 (__libc_maybe_call (__pthread_setspecific, (KEY, VALUE), 0))
238 /* Register handlers to execute before and after `fork'. */
239 #define __libc_atfork(PREPARE, PARENT, CHILD) \
240 (__libc_maybe_call (__pthread_atfork, (PREPARE, PARENT, CHILD), 0))
242 __BEGIN_DECLS
244 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *__buffer,
245 void (*__routine) (void *),
246 void *__arg) __THROW;
248 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *__buffer,
249 int __execute) __THROW;
252 /* Functions that are used by this file and are internal to the GNU C
253 library. */
255 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
256 __const pthread_mutexattr_t *__mutex_attr);
258 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
260 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
262 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
264 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
266 extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
268 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
270 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
271 int __kind);
273 #ifdef __USE_UNIX98
274 extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
275 __const pthread_rwlockattr_t *__attr);
277 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
279 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
281 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
283 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
285 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
287 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
288 #endif
290 extern int __pthread_key_create (pthread_key_t *__key,
291 void (*__destr_function) (void *));
293 extern int __pthread_setspecific (pthread_key_t __key,
294 __const void *__pointer);
296 extern void *__pthread_getspecific (pthread_key_t __key);
298 extern int __pthread_once (pthread_once_t *__once_control,
299 void (*__init_routine) (void));
301 extern int __pthread_atfork (void (*__prepare) (void),
302 void (*__parent) (void),
303 void (*__child) (void));
305 __END_DECLS
307 /* Make the pthread functions weak so that we can elide them from
308 single-threaded processes. */
309 #ifndef __NO_WEAK_PTHREAD_ALIASES
310 # pragma weak __pthread_mutex_init
311 # pragma weak __pthread_mutex_destroy
312 # pragma weak __pthread_mutex_lock
313 # pragma weak __pthread_mutex_trylock
314 # pragma weak __pthread_mutex_unlock
315 # pragma weak __pthread_mutexattr_init
316 # pragma weak __pthread_mutexattr_destroy
317 # pragma weak __pthread_mutexattr_settype
318 # pragma weak __pthread_rwlock_destroy
319 # pragma weak __pthread_rwlock_rdlock
320 # pragma weak __pthread_rwlock_tryrdlock
321 # pragma weak __pthread_rwlock_wrlock
322 # pragma weak __pthread_rwlock_trywrlock
323 # pragma weak __pthread_rwlock_unlock
324 # pragma weak __pthread_key_create
325 # pragma weak __pthread_setspecific
326 # pragma weak __pthread_getspecific
327 # pragma weak __pthread_once
328 # pragma weak __pthread_initialize
329 # pragma weak __pthread_atfork
330 # pragma weak _pthread_cleanup_push_defer
331 # pragma weak _pthread_cleanup_pop_restore
332 # pragma weak _pthread_cleanup_push
333 # pragma weak _pthread_cleanup_pop
334 #endif
336 /* We need portable names for some functions. E.g., when they are
337 used as argument to __libc_cleanup_region_start. */
338 #define __libc_mutex_unlock __pthread_mutex_unlock
340 #endif /* bits/libc-lock.h */