1 /* 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_LOCK_H
20 #define _BITS_LIBC_LOCK_H 1
28 #if defined _LIBC || defined _IO_MTSAFE_IO
29 # if (defined NOT_IN_libc && !defined IS_IN_libpthread) || !defined _LIBC
30 typedef struct { pthread_mutex_t mutex
; } __libc_lock_recursive_t
;
32 typedef struct { int lock
; int cnt
; void *owner
; } __libc_lock_recursive_t
;
35 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t
;
38 /* Define a lock variable NAME with storage class CLASS. The lock must be
39 initialized with __libc_lock_init before it can be used (or define it
40 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
41 declare a lock defined in another module. In public structure
42 definitions you must use a pointer to the lock structure (i.e., NAME
43 begins with a `*'), because its storage size will not be known outside
45 #define __libc_lock_define_recursive(CLASS,NAME) \
46 CLASS __libc_lock_recursive_t NAME;
48 /* Define an initialized recursive lock variable NAME with storage
50 #if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
51 # if LLL_LOCK_INITIALIZER == 0
52 # define __libc_lock_define_initialized_recursive(CLASS,NAME) \
53 CLASS __libc_lock_recursive_t NAME;
55 # define __libc_lock_define_initialized_recursive(CLASS,NAME) \
56 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
58 # define _LIBC_LOCK_RECURSIVE_INITIALIZER \
59 { LLL_LOCK_INITIALIZER, 0, NULL }
61 # define __libc_lock_define_initialized_recursive(CLASS,NAME) \
62 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
63 # define _LIBC_LOCK_RECURSIVE_INITIALIZER \
64 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
67 /* Initialize a recursive mutex. */
68 #if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
69 # define __libc_lock_init_recursive(NAME) \
70 ((NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER, 0)
72 # define __libc_lock_init_recursive(NAME) \
74 if (__pthread_mutex_init != NULL) \
76 pthread_mutexattr_t __attr; \
77 __pthread_mutexattr_init (&__attr); \
78 __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
79 __pthread_mutex_init (&(NAME).mutex, &__attr); \
80 __pthread_mutexattr_destroy (&__attr); \
85 /* Finalize recursive named lock. */
86 #if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
87 # define __libc_lock_fini_recursive(NAME) ((void) 0)
89 # define __libc_lock_fini_recursive(NAME) \
90 __libc_maybe_call (__pthread_mutex_destroy, (&(NAME).mutex), 0)
93 /* Lock the recursive named lock variable. */
94 #if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
95 # define __libc_lock_lock_recursive(NAME) \
97 void *self = THREAD_SELF; \
98 if ((NAME).owner != self) \
100 lll_lock ((NAME).lock, LLL_PRIVATE); \
101 (NAME).owner = self; \
106 # define __libc_lock_lock_recursive(NAME) \
107 __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
110 /* Try to lock the recursive named lock variable. */
111 #if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
112 # define __libc_lock_trylock_recursive(NAME) \
115 void *self = THREAD_SELF; \
116 if ((NAME).owner != self) \
118 if (lll_trylock ((NAME).lock) == 0) \
120 (NAME).owner = self; \
131 # define __libc_lock_trylock_recursive(NAME) \
132 __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0)
135 /* Unlock the recursive named lock variable. */
136 #if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
137 /* We do no error checking here. */
138 # define __libc_lock_unlock_recursive(NAME) \
140 if (--(NAME).cnt == 0) \
142 (NAME).owner = NULL; \
143 lll_unlock ((NAME).lock, LLL_PRIVATE); \
147 # define __libc_lock_unlock_recursive(NAME) \
148 __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0)
151 /* Note that for I/O cleanup handling we are using the old-style
152 cancel handling. It does not have to be integrated with C++ since
153 no C++ code is called in the middle. The old-style handling is
154 faster and the support is not going away. */
155 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer
*buffer
,
156 void (*routine
) (void *), void *arg
);
157 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer
*buffer
,
160 /* Start critical region with cleanup. */
161 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
162 { struct _pthread_cleanup_buffer _buffer; \
165 _avail = PTFAVAIL (_pthread_cleanup_push_defer); \
167 __libc_ptf_call_always (_pthread_cleanup_push_defer, (&_buffer, FCT, \
170 _buffer.__routine = (FCT); \
171 _buffer.__arg = (ARG); \
177 /* End critical region with cleanup. */
178 #define __libc_cleanup_region_end(DOIT) \
180 __libc_ptf_call_always (_pthread_cleanup_pop_restore, (&_buffer, DOIT));\
182 _buffer.__routine (_buffer.__arg); \
186 /* Hide the definitions which are only supposed to be used inside libc in
187 a separate file. This file is not present in the installation! */
189 # include "libc-lockP.h"
192 #endif /* bits/libc-lock.h */