Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / pthread / bits / libc-lock.h
blobcddf11a3820c6a0519e9be70e504af19311ab10b
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
22 #include <pthread.h>
23 #define __need_NULL
24 #include <stddef.h>
27 /* Mutex type. */
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;
31 # else
32 typedef struct { int lock; int cnt; void *owner; } __libc_lock_recursive_t;
33 # endif
34 #else
35 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
36 #endif
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
44 of libc. */
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
49 class CLASS. */
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;
54 # else
55 # define __libc_lock_define_initialized_recursive(CLASS,NAME) \
56 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
57 # endif
58 # define _LIBC_LOCK_RECURSIVE_INITIALIZER \
59 { LLL_LOCK_INITIALIZER, 0, NULL }
60 #else
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}
65 #endif
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)
71 #else
72 # define __libc_lock_init_recursive(NAME) \
73 do { \
74 if (__pthread_mutex_init != NULL) \
75 { \
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); \
81 } \
82 } while (0)
83 #endif
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)
88 #else
89 # define __libc_lock_fini_recursive(NAME) \
90 __libc_maybe_call (__pthread_mutex_destroy, (&(NAME).mutex), 0)
91 #endif
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) \
96 do { \
97 void *self = THREAD_SELF; \
98 if ((NAME).owner != self) \
99 { \
100 lll_lock ((NAME).lock, LLL_PRIVATE); \
101 (NAME).owner = self; \
103 ++(NAME).cnt; \
104 } while (0)
105 #else
106 # define __libc_lock_lock_recursive(NAME) \
107 __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
108 #endif
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) \
113 ({ \
114 int result = 0; \
115 void *self = THREAD_SELF; \
116 if ((NAME).owner != self) \
118 if (lll_trylock ((NAME).lock) == 0) \
120 (NAME).owner = self; \
121 (NAME).cnt = 1; \
123 else \
124 result = EBUSY; \
126 else \
127 ++(NAME).cnt; \
128 result; \
130 #else
131 # define __libc_lock_trylock_recursive(NAME) \
132 __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0)
133 #endif
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) \
139 do { \
140 if (--(NAME).cnt == 0) \
142 (NAME).owner = NULL; \
143 lll_unlock ((NAME).lock, LLL_PRIVATE); \
145 } while (0)
146 #else
147 # define __libc_lock_unlock_recursive(NAME) \
148 __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0)
149 #endif
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,
158 int execute);
160 /* Start critical region with cleanup. */
161 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
162 { struct _pthread_cleanup_buffer _buffer; \
163 int _avail; \
164 if (DOIT) { \
165 _avail = PTFAVAIL (_pthread_cleanup_push_defer); \
166 if (_avail) { \
167 __libc_ptf_call_always (_pthread_cleanup_push_defer, (&_buffer, FCT, \
168 ARG)); \
169 } else { \
170 _buffer.__routine = (FCT); \
171 _buffer.__arg = (ARG); \
173 } else { \
174 _avail = 0; \
177 /* End critical region with cleanup. */
178 #define __libc_cleanup_region_end(DOIT) \
179 if (_avail) { \
180 __libc_ptf_call_always (_pthread_cleanup_pop_restore, (&_buffer, DOIT));\
181 } else if (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! */
188 #ifdef _LIBC
189 # include "libc-lockP.h"
190 #endif
192 #endif /* bits/libc-lock.h */