OSDN Git Service

drop support for pre ISO-C compilers
[uclinux-h8/uClibc.git] / libpthread / linuxthreads / sysdeps / pthread / bits / libc-lock.h
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.
5
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.
10
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.
15
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/>.  */
19
20 #ifndef _BITS_LIBC_LOCK_H
21 #define _BITS_LIBC_LOCK_H 1
22
23 #include <pthread.h>
24
25 #if defined _LIBC && !defined NOT_IN_libc
26 #include <linuxthreads/internals.h>
27 #endif
28
29 /* Mutex type.  */
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;
33 # ifdef __USE_UNIX98
34 typedef pthread_rwlock_t __libc_rwlock_t;
35 # else
36 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
37 # endif
38 typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
39 #else
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;
43 #endif
44
45 /* Type for key to thread-specific data.  */
46 typedef pthread_key_t __libc_key_t;
47
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
54    of libc.  */
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;
63
64 /* Define an initialized lock variable NAME with storage class CLASS.
65
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.) */
72
73 #if __LT_SPINLOCK_INIT == 0
74 #  define __libc_lock_define_initialized(CLASS,NAME) \
75   CLASS __libc_lock_t NAME;
76 #else
77 #  define __libc_lock_define_initialized(CLASS,NAME) \
78   CLASS __libc_lock_t NAME = PTHREAD_MUTEX_INITIALIZER;
79 #endif
80
81 #define __libc_rwlock_define_initialized(CLASS,NAME) \
82   CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
83
84 /* Define an initialized recursive lock variable NAME with storage
85    class CLASS.  */
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}
90
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}
95
96 #if defined _LIBC && defined IS_IN_libpthread
97 # define __libc_maybe_call(FUNC, ARGS, ELSE) FUNC ARGS
98 #else
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; }))
103 # else
104 #  define __libc_maybe_call(FUNC, ARGS, ELSE) \
105   (FUNC != NULL ? FUNC ARGS : ELSE)
106 # endif
107 #endif
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; })
112 #else
113 # define __libc_maybe_call2(FUNC, ARGS, ELSE) __libc_maybe_call (__##FUNC, ARGS, ELSE)
114 #endif
115
116 /* Initialize the named lock variable, leaving it in a consistent, unlocked
117    state.  */
118 #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
119 #define __libc_lock_init(NAME) \
120   ({                                                                          \
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;                          \
126     0; })
127 #else
128 #define __libc_lock_init(NAME) \
129   (__libc_maybe_call2 (pthread_mutex_init, (&(NAME), NULL), 0))
130 #endif
131 #define __libc_rwlock_init(NAME) \
132   (__libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0));
133
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) \
137   ({                                                                          \
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;                    \
143     0; })
144 #else
145 #define __libc_lock_init_recursive(NAME) \
146   do {                                                                        \
147     if (__pthread_mutex_init != NULL)                                         \
148       {                                                                       \
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);                                \
154       }                                                                       \
155   } while (0);
156 #endif
157 #define __rtld_lock_init_recursive(NAME) \
158   __libc_lock_init_recursive (NAME)
159
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));
167
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)
171
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));
179
180 /* Lock the recursive named lock variable.  */
181 #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
182
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))
190
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)
195
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));
201
202 /* Unlock the recursive named lock variable.  */
203 #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
204
205 #if defined _LIBC && defined SHARED
206 # define __rtld_lock_default_lock_recursive(lock) \
207   ++((pthread_mutex_t *)(lock))->__m_count;
208
209 # define __rtld_lock_default_unlock_recursive(lock) \
210   --((pthread_mutex_t *)(lock))->__m_count;
211
212 # define __rtld_lock_lock_recursive(NAME) \
213   GL(dl_rtld_lock_recursive) (&(NAME).mutex)
214
215 # define __rtld_lock_unlock_recursive(NAME) \
216   GL(dl_rtld_unlock_recursive) (&(NAME).mutex)
217 #else
218 #define __rtld_lock_lock_recursive(NAME) __libc_lock_lock_recursive (NAME)
219 #define __rtld_lock_unlock_recursive(NAME) __libc_lock_unlock_recursive (NAME)
220 #endif
221
222 /* Define once control variable.  */
223 #if PTHREAD_ONCE_INIT == 0
224 /* Special case for static variables where we can avoid the initialization
225    if it is zero.  */
226 # define __libc_once_define(CLASS, NAME) \
227   CLASS pthread_once_t NAME
228 #else
229 # define __libc_once_define(CLASS, NAME) \
230   CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
231 #endif
232
233 /* Call handler iff the first call.  */
234 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
235   do {                                                                        \
236     if (__pthread_once != NULL)                                               \
237       __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION));                      \
238     else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) {                           \
239       INIT_FUNCTION ();                                                       \
240       (ONCE_CONTROL) = 2;                                                     \
241     }                                                                         \
242   } while (0)
243
244
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;               \
249     if (_avail) {                                                             \
250       _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG));                   \
251     }
252
253 /* End critical region with cleanup.  */
254 #define __libc_cleanup_region_end(DOIT) \
255     if (_avail) {                                                             \
256       _pthread_cleanup_pop_restore (&_buffer, (DOIT));                        \
257     }                                                                         \
258   }
259
260 /* Sometimes we have to exit the block in the middle.  */
261 #define __libc_cleanup_end(DOIT) \
262     if (_avail) {                                                             \
263       _pthread_cleanup_pop_restore (&_buffer, (DOIT));                        \
264     }
265
266 #define __libc_cleanup_push(fct, arg) \
267     { struct _pthread_cleanup_buffer _buffer;                                 \
268     __libc_maybe_call (_pthread_cleanup_push, (&_buffer, (fct), (arg)), 0)
269
270 #define __libc_cleanup_pop(execute) \
271     __libc_maybe_call (_pthread_cleanup_pop, (&_buffer, execute), 0);         \
272     }
273
274 /* Create thread-specific key.  */
275 #define __libc_key_create(KEY, DESTRUCTOR) \
276   (__libc_maybe_call (__pthread_key_create, (KEY, DESTRUCTOR), 1))
277
278 /* Get thread-specific data.  */
279 #define __libc_getspecific(KEY) \
280   (__libc_maybe_call (__pthread_getspecific, (KEY), NULL))
281
282 /* Set thread-specific data.  */
283 #define __libc_setspecific(KEY, VALUE) \
284   (__libc_maybe_call (__pthread_setspecific, (KEY, VALUE), 0))
285
286
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))
290
291 /* Functions that are used by this file and are internal to the GNU C
292    library.  */
293
294 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
295                                  const pthread_mutexattr_t *__mutex_attr);
296
297 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
298
299 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
300
301 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
302
303 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
304
305 extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
306
307 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
308
309 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
310                                         int __kind);
311
312 #ifdef __USE_UNIX98
313 extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
314                                   const pthread_rwlockattr_t *__attr);
315
316 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
317
318 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
319
320 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
321
322 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
323
324 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
325
326 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
327 #endif
328
329 extern int __pthread_key_create (pthread_key_t *__key,
330                                  void (*__destr_function) (void *));
331
332 extern int __pthread_setspecific (pthread_key_t __key,
333                                   const void *__pointer);
334
335 extern void *__pthread_getspecific (pthread_key_t __key);
336
337 extern int __pthread_once (pthread_once_t *__once_control,
338                            void (*__init_routine) (void));
339
340 extern int __pthread_atfork (void (*__prepare) (void),
341                              void (*__parent) (void),
342                              void (*__child) (void));
343
344
345
346 /* Make the pthread functions weak so that we can elide them from
347    single-threaded processes.  */
348 #ifndef __NO_WEAK_PTHREAD_ALIASES
349 # ifdef weak_extern
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))
376 # else
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
401 # endif
402 #endif
403
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
407
408 #endif  /* bits/libc-lock.h */