]>
Commit | Line | Data |
---|---|---|
09efc3ba | 1 | /* libc-internal interface for mutex locks. NPTL version. |
04277e02 | 2 | Copyright (C) 1996-2019 Free Software Foundation, Inc. |
76a50749 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
28ddcea0 RM |
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 | |
76a50749 UD |
8 | License, or (at your option) any later version. |
9 | ||
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 | |
28ddcea0 | 13 | Lesser General Public License for more details. |
76a50749 | 14 | |
28ddcea0 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; see the file COPYING.LIB. If |
17 | not, see <http://www.gnu.org/licenses/>. */ | |
76a50749 | 18 | |
ec999b8e JM |
19 | #ifndef _LIBC_LOCK_H |
20 | #define _LIBC_LOCK_H 1 | |
76a50749 UD |
21 | |
22 | #include <pthread.h> | |
23 | #define __need_NULL | |
24 | #include <stddef.h> | |
25 | ||
440d8bc2 | 26 | |
76a50749 | 27 | /* Mutex type. */ |
440d8bc2 | 28 | #if defined _LIBC || defined _IO_MTSAFE_IO |
4f41c682 | 29 | # if (!IS_IN (libc) && !IS_IN (libpthread)) || !defined _LIBC |
d3c9f895 | 30 | typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t; |
440d8bc2 | 31 | # else |
d3c9f895 | 32 | typedef struct { int lock; int cnt; void *owner; } __libc_lock_recursive_t; |
440d8bc2 | 33 | # endif |
76a50749 | 34 | #else |
76a50749 | 35 | typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t; |
76a50749 UD |
36 | #endif |
37 | ||
76a50749 UD |
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. */ | |
76a50749 UD |
45 | #define __libc_lock_define_recursive(CLASS,NAME) \ |
46 | CLASS __libc_lock_recursive_t NAME; | |
76a50749 UD |
47 | |
48 | /* Define an initialized recursive lock variable NAME with storage | |
49 | class CLASS. */ | |
4f41c682 | 50 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) |
c727f032 | 51 | # define __libc_lock_define_initialized_recursive(CLASS, NAME) \ |
d3c9f895 | 52 | CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER; |
d3c9f895 UD |
53 | # define _LIBC_LOCK_RECURSIVE_INITIALIZER \ |
54 | { LLL_LOCK_INITIALIZER, 0, NULL } | |
55 | #else | |
56 | # define __libc_lock_define_initialized_recursive(CLASS,NAME) \ | |
76a50749 | 57 | CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER; |
d3c9f895 | 58 | # define _LIBC_LOCK_RECURSIVE_INITIALIZER \ |
76a50749 | 59 | {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP} |
d3c9f895 | 60 | #endif |
76a50749 | 61 | |
9463518d | 62 | /* Initialize a recursive mutex. */ |
4f41c682 | 63 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) |
d3c9f895 | 64 | # define __libc_lock_init_recursive(NAME) \ |
ab49e763 | 65 | ((void) ((NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER)) |
d3c9f895 UD |
66 | #else |
67 | # define __libc_lock_init_recursive(NAME) \ | |
68 | do { \ | |
69 | if (__pthread_mutex_init != NULL) \ | |
70 | { \ | |
71 | pthread_mutexattr_t __attr; \ | |
72 | __pthread_mutexattr_init (&__attr); \ | |
73 | __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \ | |
74 | __pthread_mutex_init (&(NAME).mutex, &__attr); \ | |
75 | __pthread_mutexattr_destroy (&__attr); \ | |
76 | } \ | |
77 | } while (0) | |
78 | #endif | |
79 | ||
76a50749 | 80 | /* Finalize recursive named lock. */ |
4f41c682 | 81 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) |
d3c9f895 UD |
82 | # define __libc_lock_fini_recursive(NAME) ((void) 0) |
83 | #else | |
84 | # define __libc_lock_fini_recursive(NAME) \ | |
c8886d0a | 85 | __libc_maybe_call (__pthread_mutex_destroy, (&(NAME).mutex), 0) |
d3c9f895 | 86 | #endif |
76a50749 | 87 | |
76a50749 | 88 | /* Lock the recursive named lock variable. */ |
4f41c682 | 89 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) |
d3c9f895 UD |
90 | # define __libc_lock_lock_recursive(NAME) \ |
91 | do { \ | |
92 | void *self = THREAD_SELF; \ | |
93 | if ((NAME).owner != self) \ | |
94 | { \ | |
e51deae7 | 95 | lll_lock ((NAME).lock, LLL_PRIVATE); \ |
d3c9f895 UD |
96 | (NAME).owner = self; \ |
97 | } \ | |
98 | ++(NAME).cnt; \ | |
99 | } while (0) | |
100 | #else | |
101 | # define __libc_lock_lock_recursive(NAME) \ | |
102 | __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0) | |
103 | #endif | |
104 | ||
76a50749 | 105 | /* Try to lock the recursive named lock variable. */ |
4f41c682 | 106 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) |
09efc3ba UD |
107 | # define __libc_lock_trylock_recursive(NAME) \ |
108 | ({ \ | |
d3c9f895 UD |
109 | int result = 0; \ |
110 | void *self = THREAD_SELF; \ | |
111 | if ((NAME).owner != self) \ | |
112 | { \ | |
113 | if (lll_trylock ((NAME).lock) == 0) \ | |
114 | { \ | |
115 | (NAME).owner = self; \ | |
116 | (NAME).cnt = 1; \ | |
117 | } \ | |
118 | else \ | |
119 | result = EBUSY; \ | |
120 | } \ | |
121 | else \ | |
122 | ++(NAME).cnt; \ | |
09efc3ba UD |
123 | result; \ |
124 | }) | |
d3c9f895 UD |
125 | #else |
126 | # define __libc_lock_trylock_recursive(NAME) \ | |
c8886d0a | 127 | __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0) |
d3c9f895 UD |
128 | #endif |
129 | ||
76a50749 | 130 | /* Unlock the recursive named lock variable. */ |
4f41c682 | 131 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) |
d3c9f895 UD |
132 | /* We do no error checking here. */ |
133 | # define __libc_lock_unlock_recursive(NAME) \ | |
134 | do { \ | |
135 | if (--(NAME).cnt == 0) \ | |
136 | { \ | |
137 | (NAME).owner = NULL; \ | |
e51deae7 | 138 | lll_unlock ((NAME).lock, LLL_PRIVATE); \ |
d3c9f895 UD |
139 | } \ |
140 | } while (0) | |
141 | #else | |
142 | # define __libc_lock_unlock_recursive(NAME) \ | |
c8886d0a | 143 | __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0) |
d3c9f895 | 144 | #endif |
76a50749 | 145 | |
09d65ff3 | 146 | /* Note that for I/O cleanup handling we are using the old-style |
9463518d | 147 | cancel handling. It does not have to be integrated with C++ since |
09d65ff3 UD |
148 | no C++ code is called in the middle. The old-style handling is |
149 | faster and the support is not going away. */ | |
09d65ff3 | 150 | extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer, |
a784e502 | 151 | void (*routine) (void *), void *arg); |
09d65ff3 | 152 | extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer, |
a784e502 | 153 | int execute); |
09d65ff3 | 154 | |
76a50749 UD |
155 | /* Start critical region with cleanup. */ |
156 | #define __libc_cleanup_region_start(DOIT, FCT, ARG) \ | |
157 | { struct _pthread_cleanup_buffer _buffer; \ | |
1fc46908 UD |
158 | int _avail; \ |
159 | if (DOIT) { \ | |
ea1533e0 | 160 | _avail = PTFAVAIL (_pthread_cleanup_push_defer); \ |
1fc46908 | 161 | if (_avail) { \ |
ea1533e0 UD |
162 | __libc_ptf_call_always (_pthread_cleanup_push_defer, (&_buffer, FCT, \ |
163 | ARG)); \ | |
1fc46908 UD |
164 | } else { \ |
165 | _buffer.__routine = (FCT); \ | |
166 | _buffer.__arg = (ARG); \ | |
167 | } \ | |
168 | } else { \ | |
169 | _avail = 0; \ | |
76a50749 UD |
170 | } |
171 | ||
172 | /* End critical region with cleanup. */ | |
173 | #define __libc_cleanup_region_end(DOIT) \ | |
174 | if (_avail) { \ | |
ea1533e0 | 175 | __libc_ptf_call_always (_pthread_cleanup_pop_restore, (&_buffer, DOIT));\ |
000160a2 UD |
176 | } else if (DOIT) \ |
177 | _buffer.__routine (_buffer.__arg); \ | |
76a50749 UD |
178 | } |
179 | ||
76a50749 | 180 | |
9463518d UD |
181 | /* Hide the definitions which are only supposed to be used inside libc in |
182 | a separate file. This file is not present in the installation! */ | |
183 | #ifdef _LIBC | |
184 | # include "libc-lockP.h" | |
76a50749 UD |
185 | #endif |
186 | ||
ec999b8e | 187 | #endif /* libc-lock.h */ |