Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / pthread / bits / libc-lockP.h
blobbacc678abd6515c9ffe9d776af984cdf3c0cb4df
1 /* Private libc-internal interface for mutex locks. NPTL version.
2 Copyright (C) 1996-2014 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 Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
19 #ifndef _BITS_LIBC_LOCKP_H
20 #define _BITS_LIBC_LOCKP_H 1
22 #include <pthread.h>
23 #define __need_NULL
24 #include <stddef.h>
27 /* Fortunately Linux now has a mean to do locking which is realtime
28 safe without the aid of the thread library. We also need no fancy
29 options like error checking mutexes etc. We only need simple
30 locks, maybe recursive. This can be easily and cheaply implemented
31 using futexes. We will use them everywhere except in ld.so since
32 ld.so might be used on old kernels with a different libc.so. */
33 #include <lowlevellock.h>
34 #include <tls.h>
35 #include <pthread-functions.h>
37 /* Mutex type. */
38 #if defined NOT_IN_libc && !defined IS_IN_libpthread
39 typedef pthread_mutex_t __libc_lock_t;
40 #else
41 typedef int __libc_lock_t;
42 #endif
43 typedef struct { pthread_mutex_t mutex; } __rtld_lock_recursive_t;
44 typedef pthread_rwlock_t __libc_rwlock_t;
46 /* Type for key to thread-specific data. */
47 typedef pthread_key_t __libc_key_t;
49 /* Define a lock variable NAME with storage class CLASS. The lock must be
50 initialized with __libc_lock_init before it can be used (or define it
51 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
52 declare a lock defined in another module. In public structure
53 definitions you must use a pointer to the lock structure (i.e., NAME
54 begins with a `*'), because its storage size will not be known outside
55 of libc. */
56 #define __libc_lock_define(CLASS,NAME) \
57 CLASS __libc_lock_t NAME;
58 #define __libc_rwlock_define(CLASS,NAME) \
59 CLASS __libc_rwlock_t NAME;
60 #define __rtld_lock_define_recursive(CLASS,NAME) \
61 CLASS __rtld_lock_recursive_t NAME;
63 /* Define an initialized lock variable NAME with storage class CLASS.
65 For the C library we take a deeper look at the initializer. For
66 this implementation all fields are initialized to zero. Therefore
67 we don't initialize the variable which allows putting it into the
68 BSS section. (Except on PA-RISC and other odd architectures, where
69 initialized locks must be set to one due to the lack of normal
70 atomic operations.) */
72 #if !defined NOT_IN_libc || defined IS_IN_libpthread
73 # if LLL_LOCK_INITIALIZER == 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 = LLL_LOCK_INITIALIZER;
79 # endif
80 #else
81 # if __LT_SPINLOCK_INIT == 0
82 # define __libc_lock_define_initialized(CLASS,NAME) \
83 CLASS __libc_lock_t NAME;
84 # else
85 # define __libc_lock_define_initialized(CLASS,NAME) \
86 CLASS __libc_lock_t NAME = PTHREAD_MUTEX_INITIALIZER;
87 # endif
88 #endif
90 #define __libc_rwlock_define_initialized(CLASS,NAME) \
91 CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
93 #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
94 CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
95 #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
96 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
98 #define __rtld_lock_initialize(NAME) \
99 (void) ((NAME) = (__rtld_lock_recursive_t) _RTLD_LOCK_RECURSIVE_INITIALIZER)
101 /* If we check for a weakly referenced symbol and then perform a
102 normal jump to it te code generated for some platforms in case of
103 PIC is unnecessarily slow. What would happen is that the function
104 is first referenced as data and then it is called indirectly
105 through the PLT. We can make this a direct jump. */
106 #ifdef __PIC__
107 # define __libc_maybe_call(FUNC, ARGS, ELSE) \
108 (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
109 _fn != NULL ? (*_fn) ARGS : ELSE; }))
110 #else
111 # define __libc_maybe_call(FUNC, ARGS, ELSE) \
112 (FUNC != NULL ? FUNC ARGS : ELSE)
113 #endif
115 /* Call thread functions through the function pointer table. */
116 #if defined SHARED && !defined NOT_IN_libc
117 # define PTFAVAIL(NAME) __libc_pthread_functions_init
118 # define __libc_ptf_call(FUNC, ARGS, ELSE) \
119 (__libc_pthread_functions_init ? PTHFCT_CALL (ptr_##FUNC, ARGS) : ELSE)
120 # define __libc_ptf_call_always(FUNC, ARGS) \
121 PTHFCT_CALL (ptr_##FUNC, ARGS)
122 #else
123 # define PTFAVAIL(NAME) (NAME != NULL)
124 # define __libc_ptf_call(FUNC, ARGS, ELSE) \
125 __libc_maybe_call (FUNC, ARGS, ELSE)
126 # define __libc_ptf_call_always(FUNC, ARGS) \
127 FUNC ARGS
128 #endif
131 /* Initialize the named lock variable, leaving it in a consistent, unlocked
132 state. */
133 #if !defined NOT_IN_libc || defined IS_IN_libpthread
134 # define __libc_lock_init(NAME) ((NAME) = LLL_LOCK_INITIALIZER, 0)
135 #else
136 # define __libc_lock_init(NAME) \
137 __libc_maybe_call (__pthread_mutex_init, (&(NAME), NULL), 0)
138 #endif
139 #if defined SHARED && !defined NOT_IN_libc
140 /* ((NAME) = (__libc_rwlock_t) PTHREAD_RWLOCK_INITIALIZER, 0) is
141 inefficient. */
142 # define __libc_rwlock_init(NAME) \
143 (__builtin_memset (&(NAME), '\0', sizeof (NAME)), 0)
144 #else
145 # define __libc_rwlock_init(NAME) \
146 __libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0)
147 #endif
149 /* Finalize the named lock variable, which must be locked. It cannot be
150 used again until __libc_lock_init is called again on it. This must be
151 called on a lock variable before the containing storage is reused. */
152 #if !defined NOT_IN_libc || defined IS_IN_libpthread
153 # define __libc_lock_fini(NAME) ((void) 0)
154 #else
155 # define __libc_lock_fini(NAME) \
156 __libc_maybe_call (__pthread_mutex_destroy, (&(NAME)), 0)
157 #endif
158 #if defined SHARED && !defined NOT_IN_libc
159 # define __libc_rwlock_fini(NAME) ((void) 0)
160 #else
161 # define __libc_rwlock_fini(NAME) \
162 __libc_maybe_call (__pthread_rwlock_destroy, (&(NAME)), 0)
163 #endif
165 /* Lock the named lock variable. */
166 #if !defined NOT_IN_libc || defined IS_IN_libpthread
167 # ifndef __libc_lock_lock
168 # define __libc_lock_lock(NAME) \
169 ({ lll_lock (NAME, LLL_PRIVATE); 0; })
170 # endif
171 #else
172 # undef __libc_lock_lock
173 # define __libc_lock_lock(NAME) \
174 __libc_maybe_call (__pthread_mutex_lock, (&(NAME)), 0)
175 #endif
176 #define __libc_rwlock_rdlock(NAME) \
177 __libc_ptf_call (__pthread_rwlock_rdlock, (&(NAME)), 0)
178 #define __libc_rwlock_wrlock(NAME) \
179 __libc_ptf_call (__pthread_rwlock_wrlock, (&(NAME)), 0)
181 /* Try to lock the named lock variable. */
182 #if !defined NOT_IN_libc || defined IS_IN_libpthread
183 # ifndef __libc_lock_trylock
184 # define __libc_lock_trylock(NAME) \
185 lll_trylock (NAME)
186 # endif
187 #else
188 # undef __libc_lock_trylock
189 # define __libc_lock_trylock(NAME) \
190 __libc_maybe_call (__pthread_mutex_trylock, (&(NAME)), 0)
191 #endif
192 #define __libc_rwlock_tryrdlock(NAME) \
193 __libc_maybe_call (__pthread_rwlock_tryrdlock, (&(NAME)), 0)
194 #define __libc_rwlock_trywrlock(NAME) \
195 __libc_maybe_call (__pthread_rwlock_trywrlock, (&(NAME)), 0)
197 #define __rtld_lock_trylock_recursive(NAME) \
198 __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0)
200 /* Unlock the named lock variable. */
201 #if !defined NOT_IN_libc || defined IS_IN_libpthread
202 # define __libc_lock_unlock(NAME) \
203 lll_unlock (NAME, LLL_PRIVATE)
204 #else
205 # define __libc_lock_unlock(NAME) \
206 __libc_maybe_call (__pthread_mutex_unlock, (&(NAME)), 0)
207 #endif
208 #define __libc_rwlock_unlock(NAME) \
209 __libc_ptf_call (__pthread_rwlock_unlock, (&(NAME)), 0)
211 #ifdef SHARED
212 # define __rtld_lock_default_lock_recursive(lock) \
213 ++((pthread_mutex_t *)(lock))->__data.__count;
215 # define __rtld_lock_default_unlock_recursive(lock) \
216 --((pthread_mutex_t *)(lock))->__data.__count;
218 # define __rtld_lock_lock_recursive(NAME) \
219 GL(dl_rtld_lock_recursive) (&(NAME).mutex)
221 # define __rtld_lock_unlock_recursive(NAME) \
222 GL(dl_rtld_unlock_recursive) (&(NAME).mutex)
223 #else
224 # define __rtld_lock_lock_recursive(NAME) \
225 __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
227 # define __rtld_lock_unlock_recursive(NAME) \
228 __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0)
229 #endif
231 /* Define once control variable. */
232 #if PTHREAD_ONCE_INIT == 0
233 /* Special case for static variables where we can avoid the initialization
234 if it is zero. */
235 # define __libc_once_define(CLASS, NAME) \
236 CLASS pthread_once_t NAME
237 #else
238 # define __libc_once_define(CLASS, NAME) \
239 CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
240 #endif
242 /* Call handler iff the first call. */
243 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
244 do { \
245 if (PTFAVAIL (__pthread_once)) \
246 __libc_ptf_call_always (__pthread_once, (&(ONCE_CONTROL), \
247 INIT_FUNCTION)); \
248 else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
249 INIT_FUNCTION (); \
250 (ONCE_CONTROL) |= 2; \
252 } while (0)
254 /* Get once control variable. */
255 #define __libc_once_get(ONCE_CONTROL) ((ONCE_CONTROL) != PTHREAD_ONCE_INIT)
257 /* Note that for I/O cleanup handling we are using the old-style
258 cancel handling. It does not have to be integrated with C++ snce
259 no C++ code is called in the middle. The old-style handling is
260 faster and the support is not going away. */
261 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
262 void (*routine) (void *), void *arg);
263 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
264 int execute);
265 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
266 void (*routine) (void *), void *arg);
267 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
268 int execute);
270 /* Sometimes we have to exit the block in the middle. */
271 #define __libc_cleanup_end(DOIT) \
272 if (_avail) { \
273 __libc_ptf_call_always (_pthread_cleanup_pop_restore, (&_buffer, DOIT));\
274 } else if (DOIT) \
275 _buffer.__routine (_buffer.__arg)
278 /* Normal cleanup handling, based on C cleanup attribute. */
279 __extern_inline void
280 __libc_cleanup_routine (struct __pthread_cleanup_frame *f)
282 if (f->__do_it)
283 f->__cancel_routine (f->__cancel_arg);
286 #define __libc_cleanup_push(fct, arg) \
287 do { \
288 struct __pthread_cleanup_frame __clframe \
289 __attribute__ ((__cleanup__ (__libc_cleanup_routine))) \
290 = { .__cancel_routine = (fct), .__cancel_arg = (arg), \
291 .__do_it = 1 };
293 #define __libc_cleanup_pop(execute) \
294 __clframe.__do_it = (execute); \
295 } while (0)
298 /* Create thread-specific key. */
299 #define __libc_key_create(KEY, DESTRUCTOR) \
300 __libc_ptf_call (__pthread_key_create, (KEY, DESTRUCTOR), 1)
302 /* Get thread-specific data. */
303 #define __libc_getspecific(KEY) \
304 __libc_ptf_call (__pthread_getspecific, (KEY), NULL)
306 /* Set thread-specific data. */
307 #define __libc_setspecific(KEY, VALUE) \
308 __libc_ptf_call (__pthread_setspecific, (KEY, VALUE), 0)
311 /* Register handlers to execute before and after `fork'. Note that the
312 last parameter is NULL. The handlers registered by the libc are
313 never removed so this is OK. */
314 #define __libc_atfork(PREPARE, PARENT, CHILD) \
315 __register_atfork (PREPARE, PARENT, CHILD, NULL)
316 extern int __register_atfork (void (*__prepare) (void),
317 void (*__parent) (void),
318 void (*__child) (void),
319 void *__dso_handle);
321 /* Functions that are used by this file and are internal to the GNU C
322 library. */
324 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
325 const pthread_mutexattr_t *__mutex_attr);
327 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
329 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
331 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
333 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
335 extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
337 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
339 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
340 int __kind);
342 extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
343 const pthread_rwlockattr_t *__attr);
345 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
347 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
349 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
351 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
353 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
355 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
357 extern int __pthread_key_create (pthread_key_t *__key,
358 void (*__destr_function) (void *));
360 extern int __pthread_setspecific (pthread_key_t __key,
361 const void *__pointer);
363 extern void *__pthread_getspecific (pthread_key_t __key);
365 extern int __pthread_once (pthread_once_t *__once_control,
366 void (*__init_routine) (void));
368 extern int __pthread_atfork (void (*__prepare) (void),
369 void (*__parent) (void),
370 void (*__child) (void));
374 /* Make the pthread functions weak so that we can elide them from
375 single-threaded processes. */
376 #ifndef __NO_WEAK_PTHREAD_ALIASES
377 # ifdef weak_extern
378 weak_extern (__pthread_mutex_init)
379 weak_extern (__pthread_mutex_destroy)
380 weak_extern (__pthread_mutex_lock)
381 weak_extern (__pthread_mutex_trylock)
382 weak_extern (__pthread_mutex_unlock)
383 weak_extern (__pthread_mutexattr_init)
384 weak_extern (__pthread_mutexattr_destroy)
385 weak_extern (__pthread_mutexattr_settype)
386 weak_extern (__pthread_rwlock_init)
387 weak_extern (__pthread_rwlock_destroy)
388 weak_extern (__pthread_rwlock_rdlock)
389 weak_extern (__pthread_rwlock_tryrdlock)
390 weak_extern (__pthread_rwlock_wrlock)
391 weak_extern (__pthread_rwlock_trywrlock)
392 weak_extern (__pthread_rwlock_unlock)
393 weak_extern (__pthread_key_create)
394 weak_extern (__pthread_setspecific)
395 weak_extern (__pthread_getspecific)
396 weak_extern (__pthread_once)
397 weak_extern (__pthread_initialize)
398 weak_extern (__pthread_atfork)
399 weak_extern (_pthread_cleanup_push_defer)
400 weak_extern (_pthread_cleanup_pop_restore)
401 weak_extern (pthread_setcancelstate)
402 # else
403 # pragma weak __pthread_mutex_init
404 # pragma weak __pthread_mutex_destroy
405 # pragma weak __pthread_mutex_lock
406 # pragma weak __pthread_mutex_trylock
407 # pragma weak __pthread_mutex_unlock
408 # pragma weak __pthread_mutexattr_init
409 # pragma weak __pthread_mutexattr_destroy
410 # pragma weak __pthread_mutexattr_settype
411 # pragma weak __pthread_rwlock_destroy
412 # pragma weak __pthread_rwlock_rdlock
413 # pragma weak __pthread_rwlock_tryrdlock
414 # pragma weak __pthread_rwlock_wrlock
415 # pragma weak __pthread_rwlock_trywrlock
416 # pragma weak __pthread_rwlock_unlock
417 # pragma weak __pthread_key_create
418 # pragma weak __pthread_setspecific
419 # pragma weak __pthread_getspecific
420 # pragma weak __pthread_once
421 # pragma weak __pthread_initialize
422 # pragma weak __pthread_atfork
423 # pragma weak _pthread_cleanup_push_defer
424 # pragma weak _pthread_cleanup_pop_restore
425 # pragma weak pthread_setcancelstate
426 # endif
427 #endif
429 #endif /* bits/libc-lockP.h */