]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 2002-2019 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
4 | ||
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 | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the 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 | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
76a50749 UD |
18 | |
19 | #ifndef _PTHREADP_H | |
20 | #define _PTHREADP_H 1 | |
21 | ||
22 | #include <pthread.h> | |
23 | #include <setjmp.h> | |
b758b9cb | 24 | #include <stdbool.h> |
76a50749 UD |
25 | #include <sys/syscall.h> |
26 | #include "descr.h" | |
27 | #include <tls.h> | |
28 | #include <lowlevellock.h> | |
29 | #include <stackinfo.h> | |
30 | #include <internaltypes.h> | |
73e9ae88 | 31 | #include <pthread-functions.h> |
86246935 | 32 | #include <atomic.h> |
0f6699ea | 33 | #include <kernel-features.h> |
da1304bc | 34 | #include <errno.h> |
92aabad9 | 35 | #include <internal-signals.h> |
6310e6be | 36 | #include "pthread_mutex_conf.h" |
23fe486b | 37 | |
76a50749 | 38 | |
a1b5424f UD |
39 | /* Atomic operations on TLS memory. */ |
40 | #ifndef THREAD_ATOMIC_CMPXCHG_VAL | |
41 | # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \ | |
42 | atomic_compare_and_exchange_val_acq (&(descr)->member, new, old) | |
43 | #endif | |
44 | ||
45 | #ifndef THREAD_ATOMIC_BIT_SET | |
46 | # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \ | |
47 | atomic_bit_set (&(descr)->member, bit) | |
48 | #endif | |
49 | ||
50 | ||
6310e6be KW |
51 | static inline short max_adaptive_count (void) |
52 | { | |
53 | #if HAVE_TUNABLES | |
54 | return __mutex_aconf.spin_count; | |
55 | #else | |
56 | return DEFAULT_ADAPTIVE_COUNT; | |
2c0b891a | 57 | #endif |
6310e6be | 58 | } |
2c0b891a UD |
59 | |
60 | ||
1bcfb5a5 | 61 | /* Magic cookie representing robust mutex with dead owner. */ |
683040c3 | 62 | #define PTHREAD_MUTEX_INCONSISTENT INT_MAX |
1bcfb5a5 UD |
63 | /* Magic cookie representing not recoverable robust mutex. */ |
64 | #define PTHREAD_MUTEX_NOTRECOVERABLE (INT_MAX - 1) | |
65 | ||
66 | ||
67 | /* Internal mutex type value. */ | |
68 | enum | |
69 | { | |
df47504c | 70 | PTHREAD_MUTEX_KIND_MASK_NP = 3, |
b023e4ca AK |
71 | |
72 | PTHREAD_MUTEX_ELISION_NP = 256, | |
73 | PTHREAD_MUTEX_NO_ELISION_NP = 512, | |
74 | ||
0f6699ea UD |
75 | PTHREAD_MUTEX_ROBUST_NORMAL_NP = 16, |
76 | PTHREAD_MUTEX_ROBUST_RECURSIVE_NP | |
77 | = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_RECURSIVE_NP, | |
78 | PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP | |
79 | = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ERRORCHECK_NP, | |
80 | PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP | |
81 | = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ADAPTIVE_NP, | |
df47504c UD |
82 | PTHREAD_MUTEX_PRIO_INHERIT_NP = 32, |
83 | PTHREAD_MUTEX_PI_NORMAL_NP | |
84 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_NORMAL, | |
85 | PTHREAD_MUTEX_PI_RECURSIVE_NP | |
86 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_RECURSIVE_NP, | |
87 | PTHREAD_MUTEX_PI_ERRORCHECK_NP | |
88 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ERRORCHECK_NP, | |
89 | PTHREAD_MUTEX_PI_ADAPTIVE_NP | |
90 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ADAPTIVE_NP, | |
91 | PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP | |
92 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_NORMAL_NP, | |
93 | PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP | |
94 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_RECURSIVE_NP, | |
95 | PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP | |
96 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP, | |
97 | PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP | |
98 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP, | |
f17efcb4 UD |
99 | PTHREAD_MUTEX_PRIO_PROTECT_NP = 64, |
100 | PTHREAD_MUTEX_PP_NORMAL_NP | |
101 | = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_NORMAL, | |
102 | PTHREAD_MUTEX_PP_RECURSIVE_NP | |
103 | = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_RECURSIVE_NP, | |
104 | PTHREAD_MUTEX_PP_ERRORCHECK_NP | |
105 | = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ERRORCHECK_NP, | |
106 | PTHREAD_MUTEX_PP_ADAPTIVE_NP | |
b023e4ca AK |
107 | = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ADAPTIVE_NP, |
108 | PTHREAD_MUTEX_ELISION_FLAGS_NP | |
109 | = PTHREAD_MUTEX_ELISION_NP | PTHREAD_MUTEX_NO_ELISION_NP, | |
110 | ||
111 | PTHREAD_MUTEX_TIMED_ELISION_NP = | |
112 | PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_ELISION_NP, | |
113 | PTHREAD_MUTEX_TIMED_NO_ELISION_NP = | |
114 | PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_NO_ELISION_NP, | |
1bcfb5a5 | 115 | }; |
5bd8a249 | 116 | #define PTHREAD_MUTEX_PSHARED_BIT 128 |
f17efcb4 | 117 | |
403b4feb SL |
118 | /* See concurrency notes regarding __kind in struct __pthread_mutex_s |
119 | in sysdeps/nptl/bits/thread-shared-types.h. */ | |
ae1ad3ae | 120 | #define PTHREAD_MUTEX_TYPE(m) \ |
403b4feb | 121 | (atomic_load_relaxed (&((m)->__data.__kind)) & 127) |
e8c659d7 AK |
122 | /* Don't include NO_ELISION, as that type is always the same |
123 | as the underlying lock type. */ | |
b023e4ca | 124 | #define PTHREAD_MUTEX_TYPE_ELISION(m) \ |
403b4feb SL |
125 | (atomic_load_relaxed (&((m)->__data.__kind)) \ |
126 | & (127 | PTHREAD_MUTEX_ELISION_NP)) | |
5bd8a249 UD |
127 | |
128 | #if LLL_PRIVATE == 0 && LLL_SHARED == 128 | |
129 | # define PTHREAD_MUTEX_PSHARED(m) \ | |
403b4feb | 130 | (atomic_load_relaxed (&((m)->__data.__kind)) & 128) |
5bd8a249 UD |
131 | #else |
132 | # define PTHREAD_MUTEX_PSHARED(m) \ | |
403b4feb SL |
133 | ((atomic_load_relaxed (&((m)->__data.__kind)) & 128) \ |
134 | ? LLL_SHARED : LLL_PRIVATE) | |
5bd8a249 UD |
135 | #endif |
136 | ||
137 | /* The kernel when waking robust mutexes on exit never uses | |
138 | FUTEX_PRIVATE_FLAG FUTEX_WAKE. */ | |
139 | #define PTHREAD_ROBUST_MUTEX_PSHARED(m) LLL_SHARED | |
ae1ad3ae | 140 | |
f17efcb4 UD |
141 | /* Ceiling in __data.__lock. __data.__lock is signed, so don't |
142 | use the MSB bit in there, but in the mask also include that bit, | |
143 | so that the compiler can optimize & PTHREAD_MUTEX_PRIO_CEILING_MASK | |
144 | masking if the value is then shifted down by | |
145 | PTHREAD_MUTEX_PRIO_CEILING_SHIFT. */ | |
146 | #define PTHREAD_MUTEX_PRIO_CEILING_SHIFT 19 | |
147 | #define PTHREAD_MUTEX_PRIO_CEILING_MASK 0xfff80000 | |
1bcfb5a5 UD |
148 | |
149 | ||
150 | /* Flags in mutex attr. */ | |
a5f2bd86 RM |
151 | #define PTHREAD_MUTEXATTR_PROTOCOL_SHIFT 28 |
152 | #define PTHREAD_MUTEXATTR_PROTOCOL_MASK 0x30000000 | |
f17efcb4 UD |
153 | #define PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT 12 |
154 | #define PTHREAD_MUTEXATTR_PRIO_CEILING_MASK 0x00fff000 | |
a5f2bd86 RM |
155 | #define PTHREAD_MUTEXATTR_FLAG_ROBUST 0x40000000 |
156 | #define PTHREAD_MUTEXATTR_FLAG_PSHARED 0x80000000 | |
1bcfb5a5 | 157 | #define PTHREAD_MUTEXATTR_FLAG_BITS \ |
a5f2bd86 RM |
158 | (PTHREAD_MUTEXATTR_FLAG_ROBUST | PTHREAD_MUTEXATTR_FLAG_PSHARED \ |
159 | | PTHREAD_MUTEXATTR_PROTOCOL_MASK | PTHREAD_MUTEXATTR_PRIO_CEILING_MASK) | |
1bcfb5a5 UD |
160 | |
161 | ||
cc25c8b4 TR |
162 | /* For the following, see pthread_rwlock_common.c. */ |
163 | #define PTHREAD_RWLOCK_WRPHASE 1 | |
164 | #define PTHREAD_RWLOCK_WRLOCKED 2 | |
165 | #define PTHREAD_RWLOCK_RWAITING 4 | |
166 | #define PTHREAD_RWLOCK_READER_SHIFT 3 | |
167 | #define PTHREAD_RWLOCK_READER_OVERFLOW ((unsigned int) 1 \ | |
168 | << (sizeof (unsigned int) * 8 - 1)) | |
169 | #define PTHREAD_RWLOCK_WRHANDOVER ((unsigned int) 1 \ | |
170 | << (sizeof (unsigned int) * 8 - 1)) | |
171 | #define PTHREAD_RWLOCK_FUTEX_USED 2 | |
546346b6 UD |
172 | |
173 | ||
683040c3 UD |
174 | /* Bits used in robust mutex implementation. */ |
175 | #define FUTEX_WAITERS 0x80000000 | |
176 | #define FUTEX_OWNER_DIED 0x40000000 | |
2035d91c | 177 | #define FUTEX_TID_MASK 0x3fffffff |
683040c3 UD |
178 | |
179 | ||
63668b70 TR |
180 | /* pthread_once definitions. See __pthread_once for how these are used. */ |
181 | #define __PTHREAD_ONCE_INPROGRESS 1 | |
182 | #define __PTHREAD_ONCE_DONE 2 | |
183 | #define __PTHREAD_ONCE_FORK_GEN_INCR 4 | |
184 | ||
ce7528f6 AZ |
185 | /* Attribute to indicate thread creation was issued from C11 thrd_create. */ |
186 | #define ATTR_C11_THREAD ((void*)(uintptr_t)-1) | |
187 | ||
63668b70 | 188 | |
ed19993b TR |
189 | /* Condition variable definitions. See __pthread_cond_wait_common. |
190 | Need to be defined here so there is one place from which | |
191 | nptl_lock_constants can grab them. */ | |
192 | #define __PTHREAD_COND_CLOCK_MONOTONIC_MASK 2 | |
193 | #define __PTHREAD_COND_SHARED_MASK 1 | |
194 | ||
195 | ||
76a50749 UD |
196 | /* Internal variables. */ |
197 | ||
198 | ||
e903a713 SP |
199 | /* Default pthread attributes. */ |
200 | extern struct pthread_attr __default_pthread_attr attribute_hidden; | |
61dd6208 | 201 | extern int __default_pthread_attr_lock attribute_hidden; |
76a50749 UD |
202 | |
203 | /* Size and alignment of static TLS block. */ | |
204 | extern size_t __static_tls_size attribute_hidden; | |
bc6389ad | 205 | extern size_t __static_tls_align_m1 attribute_hidden; |
76a50749 | 206 | |
2c0b891a UD |
207 | /* Flag whether the machine is SMP or not. */ |
208 | extern int __is_smp attribute_hidden; | |
209 | ||
76a50749 | 210 | /* Thread descriptor handling. */ |
fa9a4ff0 RM |
211 | extern list_t __stack_user; |
212 | hidden_proto (__stack_user) | |
76a50749 UD |
213 | |
214 | /* Attribute handling. */ | |
215 | extern struct pthread_attr *__attr_list attribute_hidden; | |
e51deae7 | 216 | extern int __attr_list_lock attribute_hidden; |
76a50749 | 217 | |
76a50749 UD |
218 | /* Concurrency handling. */ |
219 | extern int __concurrency_level attribute_hidden; | |
220 | ||
221 | /* Thread-local data key handling. */ | |
fa9a4ff0 RM |
222 | extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX]; |
223 | hidden_proto (__pthread_keys) | |
76a50749 | 224 | |
47202270 UD |
225 | /* Number of threads running. */ |
226 | extern unsigned int __nptl_nthreads attribute_hidden; | |
227 | ||
0f6699ea UD |
228 | #ifndef __ASSUME_SET_ROBUST_LIST |
229 | /* Negative if we do not have the system call and we can use it. */ | |
230 | extern int __set_robust_list_avail attribute_hidden; | |
231 | #endif | |
232 | ||
f17efcb4 UD |
233 | /* Thread Priority Protection. */ |
234 | extern int __sched_fifo_min_prio attribute_hidden; | |
235 | extern int __sched_fifo_max_prio attribute_hidden; | |
236 | extern void __init_sched_fifo_prio (void) attribute_hidden; | |
237 | extern int __pthread_tpp_change_priority (int prev_prio, int new_prio) | |
238 | attribute_hidden; | |
239 | extern int __pthread_current_priority (void) attribute_hidden; | |
240 | ||
76a50749 UD |
241 | /* The library can run in debugging mode where it performs a lot more |
242 | tests. */ | |
243 | extern int __pthread_debug attribute_hidden; | |
729924a0 UD |
244 | /** For now disable debugging support. */ |
245 | #if 0 | |
246 | # define DEBUGGING_P __builtin_expect (__pthread_debug, 0) | |
8c2e9a29 UD |
247 | # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL) |
248 | # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd) | |
729924a0 UD |
249 | #else |
250 | # define DEBUGGING_P 0 | |
8c2e9a29 UD |
251 | /* Simplified test. This will not catch all invalid descriptors but |
252 | is better than nothing. And if the test triggers the thread | |
253 | descriptor is guaranteed to be invalid. */ | |
254 | # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0) | |
255 | # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0) | |
729924a0 | 256 | #endif |
76a50749 UD |
257 | |
258 | ||
259 | /* Cancellation test. */ | |
260 | #define CANCELLATION_P(self) \ | |
261 | do { \ | |
262 | int cancelhandling = THREAD_GETMEM (self, cancelhandling); \ | |
263 | if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \ | |
264 | { \ | |
265 | THREAD_SETMEM (self, result, PTHREAD_CANCELED); \ | |
9ae0909b | 266 | __do_cancel (); \ |
76a50749 UD |
267 | } \ |
268 | } while (0) | |
269 | ||
6efd4814 UD |
270 | |
271 | extern void __pthread_unwind (__pthread_unwind_buf_t *__buf) | |
272 | __cleanup_fct_attribute __attribute ((__noreturn__)) | |
ce9f10f7 | 273 | #if !defined SHARED && !IS_IN (libpthread) |
6efd4814 UD |
274 | weak_function |
275 | #endif | |
276 | ; | |
da0c02ee UD |
277 | extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf) |
278 | __cleanup_fct_attribute __attribute ((__noreturn__)) | |
279 | #ifndef SHARED | |
280 | weak_function | |
281 | #endif | |
282 | ; | |
283 | extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf) | |
284 | __cleanup_fct_attribute; | |
285 | extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf) | |
286 | __cleanup_fct_attribute; | |
ce9f10f7 | 287 | #if IS_IN (libpthread) |
56421b23 | 288 | hidden_proto (__pthread_unwind) |
da0c02ee UD |
289 | hidden_proto (__pthread_unwind_next) |
290 | hidden_proto (__pthread_register_cancel) | |
291 | hidden_proto (__pthread_unregister_cancel) | |
9d79e037 UD |
292 | # ifdef SHARED |
293 | extern void attribute_hidden pthread_cancel_init (void); | |
294 | # endif | |
2827ab99 | 295 | extern void __nptl_unwind_freeres (void) attribute_hidden; |
56421b23 UD |
296 | #endif |
297 | ||
6efd4814 UD |
298 | |
299 | /* Called when a thread reacts on a cancellation request. */ | |
300 | static inline void | |
dd9423a6 | 301 | __attribute ((noreturn, always_inline)) |
6efd4814 UD |
302 | __do_cancel (void) |
303 | { | |
304 | struct pthread *self = THREAD_SELF; | |
305 | ||
306 | /* Make sure we get no more cancellations. */ | |
307 | THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT); | |
308 | ||
309 | __pthread_unwind ((__pthread_unwind_buf_t *) | |
310 | THREAD_GETMEM (self, cleanup_jmp_buf)); | |
311 | } | |
312 | ||
313 | ||
76a50749 | 314 | /* Set cancellation mode to asynchronous. */ |
bdb04f92 UD |
315 | #define CANCEL_ASYNC() \ |
316 | __pthread_enable_asynccancel () | |
76a50749 UD |
317 | /* Reset to previous cancellation mode. */ |
318 | #define CANCEL_RESET(oldtype) \ | |
bdb04f92 | 319 | __pthread_disable_asynccancel (oldtype) |
76a50749 | 320 | |
4f41c682 | 321 | #if IS_IN (libc) |
9ae0909b | 322 | /* Same as CANCEL_ASYNC, but for use in libc.so. */ |
9634cf9d | 323 | # define LIBC_CANCEL_ASYNC() \ |
9ae0909b UD |
324 | __libc_enable_asynccancel () |
325 | /* Same as CANCEL_RESET, but for use in libc.so. */ | |
9634cf9d | 326 | # define LIBC_CANCEL_RESET(oldtype) \ |
9ae0909b | 327 | __libc_disable_asynccancel (oldtype) |
ce9f10f7 | 328 | #elif IS_IN (libpthread) |
9634cf9d UD |
329 | # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC () |
330 | # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val) | |
016afc75 | 331 | #elif IS_IN (librt) |
1fde494e UD |
332 | # define LIBC_CANCEL_ASYNC() \ |
333 | __librt_enable_asynccancel () | |
334 | # define LIBC_CANCEL_RESET(val) \ | |
335 | __librt_disable_asynccancel (val) | |
9634cf9d UD |
336 | #else |
337 | # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */ | |
338 | # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */ | |
339 | #endif | |
9ae0909b | 340 | |
2edb61e3 | 341 | |
76a50749 UD |
342 | /* Internal prototypes. */ |
343 | ||
344 | /* Thread list handling. */ | |
345 | extern struct pthread *__find_in_stack_list (struct pthread *pd) | |
83b09837 | 346 | attribute_hidden; |
76a50749 UD |
347 | |
348 | /* Deallocate a thread's stack after optionally making sure the thread | |
349 | descriptor is still valid. */ | |
83b09837 | 350 | extern void __free_tcb (struct pthread *pd) attribute_hidden; |
76a50749 UD |
351 | |
352 | /* Free allocated stack. */ | |
83b09837 | 353 | extern void __deallocate_stack (struct pthread *pd) attribute_hidden; |
76a50749 UD |
354 | |
355 | /* Mark all the stacks except for the current one as available. This | |
356 | function also re-initializes the lock for the stack cache. */ | |
357 | extern void __reclaim_stacks (void) attribute_hidden; | |
358 | ||
54ee14b3 | 359 | /* Make all threads's stacks executable. */ |
e1d2ae8d | 360 | extern int __make_stacks_executable (void **stack_endp) attribute_hidden; |
54ee14b3 | 361 | |
76a50749 UD |
362 | /* longjmp handling. */ |
363 | extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe); | |
ce9f10f7 | 364 | #if IS_IN (libpthread) |
334fcf2a UD |
365 | hidden_proto (__pthread_cleanup_upto) |
366 | #endif | |
76a50749 UD |
367 | |
368 | ||
369 | /* Functions with versioned interfaces. */ | |
370 | extern int __pthread_create_2_1 (pthread_t *newthread, | |
371 | const pthread_attr_t *attr, | |
372 | void *(*start_routine) (void *), void *arg); | |
373 | extern int __pthread_create_2_0 (pthread_t *newthread, | |
374 | const pthread_attr_t *attr, | |
375 | void *(*start_routine) (void *), void *arg); | |
376 | extern int __pthread_attr_init_2_1 (pthread_attr_t *attr); | |
377 | extern int __pthread_attr_init_2_0 (pthread_attr_t *attr); | |
378 | ||
379 | ||
380 | /* Event handlers for libthread_db interface. */ | |
fa9a4ff0 RM |
381 | extern void __nptl_create_event (void); |
382 | extern void __nptl_death_event (void); | |
383 | hidden_proto (__nptl_create_event) | |
384 | hidden_proto (__nptl_death_event) | |
76a50749 | 385 | |
8454830b | 386 | /* Register the generation counter in the libpthread with the libc. */ |
5a03acfe | 387 | #ifdef TLS_MULTIPLE_THREADS_IN_TCB |
8454830b UD |
388 | extern void __libc_pthread_init (unsigned long int *ptr, |
389 | void (*reclaim) (void), | |
83b09837 | 390 | const struct pthread_functions *functions); |
5a03acfe UD |
391 | #else |
392 | extern int *__libc_pthread_init (unsigned long int *ptr, | |
393 | void (*reclaim) (void), | |
83b09837 | 394 | const struct pthread_functions *functions); |
5a03acfe | 395 | |
439bf404 SP |
396 | /* Variable set to a nonzero value either if more than one thread runs or ran, |
397 | or if a single-threaded process is trying to cancel itself. See | |
398 | nptl/descr.h for more context on the single-threaded process case. */ | |
5a03acfe UD |
399 | extern int __pthread_multiple_threads attribute_hidden; |
400 | /* Pointer to the corresponding variable in libc. */ | |
401 | extern int *__libc_multiple_threads_ptr attribute_hidden; | |
402 | #endif | |
8454830b | 403 | |
4165d44d | 404 | /* Find a thread given its TID. */ |
b639d0c9 UD |
405 | extern struct pthread *__find_thread_by_id (pid_t tid) attribute_hidden |
406 | #ifdef SHARED | |
407 | ; | |
408 | #else | |
409 | weak_function; | |
410 | #define __find_thread_by_id(tid) \ | |
411 | (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL) | |
412 | #endif | |
4165d44d | 413 | |
adc12574 UD |
414 | extern void __pthread_init_static_tls (struct link_map *) attribute_hidden; |
415 | ||
2c1094bd | 416 | extern size_t __pthread_get_minstack (const pthread_attr_t *attr); |
76a50749 UD |
417 | |
418 | /* Namespace save aliases. */ | |
8454830b UD |
419 | extern int __pthread_getschedparam (pthread_t thread_id, int *policy, |
420 | struct sched_param *param); | |
421 | extern int __pthread_setschedparam (pthread_t thread_id, int policy, | |
422 | const struct sched_param *param); | |
423 | extern int __pthread_setcancelstate (int state, int *oldstate); | |
76a50749 | 424 | extern int __pthread_mutex_init (pthread_mutex_t *__mutex, |
a784e502 | 425 | const pthread_mutexattr_t *__mutexattr); |
76a50749 UD |
426 | extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex); |
427 | extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex); | |
428 | extern int __pthread_mutex_lock (pthread_mutex_t *__mutex); | |
fa872e1b AZ |
429 | extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex, |
430 | const struct timespec *__abstime); | |
69431c9a | 431 | extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex) |
83b09837 | 432 | attribute_hidden; |
b0948ffd | 433 | extern void __pthread_mutex_cond_lock_adjust (pthread_mutex_t *__mutex) |
83b09837 | 434 | attribute_hidden; |
76a50749 | 435 | extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex); |
b758b9cb | 436 | extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex, |
83b09837 | 437 | int __decr) attribute_hidden; |
76a50749 UD |
438 | extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr); |
439 | extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr); | |
440 | extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind); | |
8454830b UD |
441 | extern int __pthread_attr_destroy (pthread_attr_t *attr); |
442 | extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr, | |
443 | int *detachstate); | |
444 | extern int __pthread_attr_setdetachstate (pthread_attr_t *attr, | |
445 | int detachstate); | |
446 | extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr, | |
447 | int *inherit); | |
448 | extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit); | |
449 | extern int __pthread_attr_getschedparam (const pthread_attr_t *attr, | |
450 | struct sched_param *param); | |
451 | extern int __pthread_attr_setschedparam (pthread_attr_t *attr, | |
452 | const struct sched_param *param); | |
453 | extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr, | |
454 | int *policy); | |
455 | extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy); | |
456 | extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope); | |
457 | extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope); | |
a784e502 | 458 | extern int __pthread_attr_getstackaddr (const pthread_attr_t *__restrict |
76a50749 UD |
459 | __attr, void **__restrict __stackaddr); |
460 | extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr, | |
461 | void *__stackaddr); | |
a784e502 | 462 | extern int __pthread_attr_getstacksize (const pthread_attr_t *__restrict |
76a50749 UD |
463 | __attr, |
464 | size_t *__restrict __stacksize); | |
465 | extern int __pthread_attr_setstacksize (pthread_attr_t *__attr, | |
466 | size_t __stacksize); | |
a784e502 | 467 | extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr, |
76a50749 UD |
468 | void **__restrict __stackaddr, |
469 | size_t *__restrict __stacksize); | |
470 | extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr, | |
471 | size_t __stacksize); | |
472 | extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock, | |
a784e502 | 473 | const pthread_rwlockattr_t *__restrict |
76a50749 UD |
474 | __attr); |
475 | extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock); | |
476 | extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock); | |
477 | extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock); | |
478 | extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock); | |
479 | extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock); | |
480 | extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock); | |
8454830b UD |
481 | extern int __pthread_cond_broadcast (pthread_cond_t *cond); |
482 | extern int __pthread_cond_destroy (pthread_cond_t *cond); | |
483 | extern int __pthread_cond_init (pthread_cond_t *cond, | |
484 | const pthread_condattr_t *cond_attr); | |
485 | extern int __pthread_cond_signal (pthread_cond_t *cond); | |
486 | extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex); | |
b5facfda UD |
487 | extern int __pthread_cond_timedwait (pthread_cond_t *cond, |
488 | pthread_mutex_t *mutex, | |
489 | const struct timespec *abstime); | |
8454830b UD |
490 | extern int __pthread_condattr_destroy (pthread_condattr_t *attr); |
491 | extern int __pthread_condattr_init (pthread_condattr_t *attr); | |
76a50749 | 492 | extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *)); |
fa872e1b | 493 | extern int __pthread_key_delete (pthread_key_t key); |
76a50749 UD |
494 | extern void *__pthread_getspecific (pthread_key_t key); |
495 | extern int __pthread_setspecific (pthread_key_t key, const void *value); | |
496 | extern int __pthread_once (pthread_once_t *once_control, | |
497 | void (*init_routine) (void)); | |
498 | extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void), | |
499 | void (*child) (void)); | |
8454830b UD |
500 | extern pthread_t __pthread_self (void); |
501 | extern int __pthread_equal (pthread_t thread1, pthread_t thread2); | |
fa872e1b AZ |
502 | extern int __pthread_detach (pthread_t th); |
503 | extern int __pthread_cancel (pthread_t th); | |
09efc3ba | 504 | extern int __pthread_kill (pthread_t threadid, int signo); |
356fa562 | 505 | extern void __pthread_exit (void *value) __attribute__ ((__noreturn__)); |
fa872e1b | 506 | extern int __pthread_join (pthread_t threadid, void **thread_return); |
09efc3ba | 507 | extern int __pthread_setcanceltype (int type, int *oldtype); |
bdb04f92 | 508 | extern int __pthread_enable_asynccancel (void) attribute_hidden; |
83b09837 | 509 | extern void __pthread_disable_asynccancel (int oldtype) attribute_hidden; |
47677f2e | 510 | extern void __pthread_testcancel (void); |
4735850f AZ |
511 | extern int __pthread_timedjoin_ex (pthread_t, void **, const struct timespec *, |
512 | bool); | |
9ae0909b | 513 | |
ce9f10f7 | 514 | #if IS_IN (libpthread) |
4d17e683 AS |
515 | hidden_proto (__pthread_mutex_init) |
516 | hidden_proto (__pthread_mutex_destroy) | |
517 | hidden_proto (__pthread_mutex_lock) | |
fa872e1b | 518 | hidden_proto (__pthread_mutex_trylock) |
4d17e683 AS |
519 | hidden_proto (__pthread_mutex_unlock) |
520 | hidden_proto (__pthread_rwlock_rdlock) | |
521 | hidden_proto (__pthread_rwlock_wrlock) | |
522 | hidden_proto (__pthread_rwlock_unlock) | |
523 | hidden_proto (__pthread_key_create) | |
524 | hidden_proto (__pthread_getspecific) | |
525 | hidden_proto (__pthread_setspecific) | |
526 | hidden_proto (__pthread_once) | |
e5d19c08 | 527 | hidden_proto (__pthread_setcancelstate) |
47677f2e | 528 | hidden_proto (__pthread_testcancel) |
fa872e1b AZ |
529 | hidden_proto (__pthread_mutexattr_init) |
530 | hidden_proto (__pthread_mutexattr_settype) | |
4735850f | 531 | hidden_proto (__pthread_timedjoin_ex) |
4d17e683 AS |
532 | #endif |
533 | ||
997256dd UD |
534 | extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond); |
535 | extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond); | |
536 | extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond, | |
05df18c3 | 537 | const pthread_condattr_t *cond_attr); |
997256dd UD |
538 | extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond); |
539 | extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond, | |
05df18c3 UD |
540 | pthread_mutex_t *mutex, |
541 | const struct timespec *abstime); | |
997256dd | 542 | extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond, |
05df18c3 UD |
543 | pthread_mutex_t *mutex); |
544 | ||
439ff07b UD |
545 | extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize, |
546 | cpu_set_t *cpuset); | |
73e9ae88 | 547 | |
9ae0909b UD |
548 | /* The two functions are in libc.so and not exported. */ |
549 | extern int __libc_enable_asynccancel (void) attribute_hidden; | |
83b09837 | 550 | extern void __libc_disable_asynccancel (int oldtype) attribute_hidden; |
09efc3ba | 551 | |
1fde494e UD |
552 | |
553 | /* The two functions are in librt.so and not exported. */ | |
554 | extern int __librt_enable_asynccancel (void) attribute_hidden; | |
83b09837 | 555 | extern void __librt_disable_asynccancel (int oldtype) attribute_hidden; |
1fde494e | 556 | |
ce9f10f7 | 557 | #if IS_IN (libpthread) |
09efc3ba | 558 | /* Special versions which use non-exported functions. */ |
73e9ae88 UD |
559 | extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer, |
560 | void (*routine) (void *), void *arg) | |
09efc3ba | 561 | attribute_hidden; |
09efc3ba | 562 | |
203c1a89 MS |
563 | /* Replace cleanup macros defined in <pthread.h> with internal |
564 | versions that don't depend on unwind info and better support | |
565 | cancellation. */ | |
566 | # undef pthread_cleanup_push | |
567 | # define pthread_cleanup_push(routine,arg) \ | |
568 | { struct _pthread_cleanup_buffer _buffer; \ | |
569 | __pthread_cleanup_push (&_buffer, (routine), (arg)); | |
570 | ||
73e9ae88 UD |
571 | extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer, |
572 | int execute) attribute_hidden; | |
203c1a89 MS |
573 | # undef pthread_cleanup_pop |
574 | # define pthread_cleanup_pop(execute) \ | |
575 | __pthread_cleanup_pop (&_buffer, (execute)); } | |
1e506629 | 576 | #endif |
76a50749 | 577 | |
73e9ae88 UD |
578 | extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer, |
579 | void (*routine) (void *), void *arg); | |
580 | extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer, | |
581 | int execute); | |
582 | ||
6efd4814 UD |
583 | /* Old cleanup interfaces, still used in libc.so. */ |
584 | extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer, | |
6f8326ca | 585 | void (*routine) (void *), void *arg); |
6efd4814 | 586 | extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer, |
6f8326ca | 587 | int execute); |
6efd4814 | 588 | extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer, |
6f8326ca | 589 | void (*routine) (void *), void *arg); |
6efd4814 | 590 | extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer, |
6f8326ca | 591 | int execute); |
6efd4814 | 592 | |
3fa21fd8 UD |
593 | extern void __nptl_deallocate_tsd (void) attribute_hidden; |
594 | ||
771eb141 FW |
595 | extern void __nptl_setxid_error (struct xid_command *cmdp, int error) |
596 | attribute_hidden; | |
ccd8de9a | 597 | extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden; |
6f8326ca UD |
598 | #ifndef SHARED |
599 | extern void __nptl_set_robust (struct pthread *self); | |
600 | #endif | |
2edb61e3 | 601 | |
2827ab99 CD |
602 | extern void __nptl_stacks_freeres (void) attribute_hidden; |
603 | extern void __shm_directory_freeres (void) attribute_hidden; | |
ba408f84 | 604 | |
df94b641 UD |
605 | extern void __wait_lookup_done (void) attribute_hidden; |
606 | ||
b639d0c9 UD |
607 | #ifdef SHARED |
608 | # define PTHREAD_STATIC_FN_REQUIRE(name) | |
609 | #else | |
610 | # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name); | |
611 | #endif | |
612 | ||
da1304bc SP |
613 | /* Returns 0 if POL is a valid scheduling policy. */ |
614 | static inline int | |
615 | check_sched_policy_attr (int pol) | |
616 | { | |
617 | if (pol == SCHED_OTHER || pol == SCHED_FIFO || pol == SCHED_RR) | |
618 | return 0; | |
619 | ||
620 | return EINVAL; | |
621 | } | |
622 | ||
623 | /* Returns 0 if PR is within the accepted range of priority values for | |
624 | the scheduling policy POL or EINVAL otherwise. */ | |
625 | static inline int | |
626 | check_sched_priority_attr (int pr, int pol) | |
627 | { | |
628 | int min = __sched_get_priority_min (pol); | |
629 | int max = __sched_get_priority_max (pol); | |
630 | ||
631 | if (min >= 0 && max >= 0 && pr >= min && pr <= max) | |
632 | return 0; | |
633 | ||
634 | return EINVAL; | |
635 | } | |
636 | ||
637 | /* Returns 0 if ST is a valid stack size for a thread stack and EINVAL | |
638 | otherwise. */ | |
639 | static inline int | |
640 | check_stacksize_attr (size_t st) | |
641 | { | |
642 | if (st >= PTHREAD_STACK_MIN) | |
643 | return 0; | |
644 | ||
645 | return EINVAL; | |
646 | } | |
647 | ||
72b3c6ee AZ |
648 | #define ASSERT_TYPE_SIZE(type, size) \ |
649 | _Static_assert (sizeof (type) == size, \ | |
650 | "sizeof (" #type ") != " #size) | |
651 | ||
652 | #define ASSERT_PTHREAD_INTERNAL_SIZE(type, internal) \ | |
a3aaa598 | 653 | _Static_assert (sizeof ((type) { { 0 } }).__size >= sizeof (internal),\ |
72b3c6ee AZ |
654 | "sizeof (" #type ".__size) < sizeof (" #internal ")") |
655 | ||
dff91cd4 AZ |
656 | #define ASSERT_PTHREAD_STRING(x) __STRING (x) |
657 | #define ASSERT_PTHREAD_INTERNAL_OFFSET(type, member, offset) \ | |
658 | _Static_assert (offsetof (type, member) == offset, \ | |
659 | "offset of " #member " field of " #type " != " \ | |
660 | ASSERT_PTHREAD_STRING (offset)) | |
661 | ||
76a50749 | 662 | #endif /* pthreadP.h */ |