1 /* Copyright (C) 2002-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
25 #include <bits/endian.h>
26 #include <bits/pthreadtypes.h>
27 #include <bits/setjmp.h>
28 #include <bits/wordsize.h>
29 #include <bits/types/struct_timespec.h>
30 #include <bits/types/__sigset_t.h>
31 #include <bits/types/struct___jmp_buf_tag.h>
37 PTHREAD_CREATE_JOINABLE
,
38 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
39 PTHREAD_CREATE_DETACHED
40 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
47 PTHREAD_MUTEX_TIMED_NP
,
48 PTHREAD_MUTEX_RECURSIVE_NP
,
49 PTHREAD_MUTEX_ERRORCHECK_NP
,
50 PTHREAD_MUTEX_ADAPTIVE_NP
51 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
53 PTHREAD_MUTEX_NORMAL
= PTHREAD_MUTEX_TIMED_NP
,
54 PTHREAD_MUTEX_RECURSIVE
= PTHREAD_MUTEX_RECURSIVE_NP
,
55 PTHREAD_MUTEX_ERRORCHECK
= PTHREAD_MUTEX_ERRORCHECK_NP
,
56 PTHREAD_MUTEX_DEFAULT
= PTHREAD_MUTEX_NORMAL
59 /* For compatibility. */
60 , PTHREAD_MUTEX_FAST_NP
= PTHREAD_MUTEX_TIMED_NP
66 /* Robust mutex or not flags. */
69 PTHREAD_MUTEX_STALLED
,
70 PTHREAD_MUTEX_STALLED_NP
= PTHREAD_MUTEX_STALLED
,
72 PTHREAD_MUTEX_ROBUST_NP
= PTHREAD_MUTEX_ROBUST
77 #if defined __USE_POSIX199506 || defined __USE_UNIX98
78 /* Mutex protocols. */
88 #define PTHREAD_MUTEX_INITIALIZER \
89 { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_TIMED_NP) } }
91 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
92 { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_RECURSIVE_NP) } }
93 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
94 { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ERRORCHECK_NP) } }
95 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
96 { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ADAPTIVE_NP) } }
100 /* Read-write lock types. */
101 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
104 PTHREAD_RWLOCK_PREFER_READER_NP
,
105 PTHREAD_RWLOCK_PREFER_WRITER_NP
,
106 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
,
107 PTHREAD_RWLOCK_DEFAULT_NP
= PTHREAD_RWLOCK_PREFER_READER_NP
111 /* Read-write lock initializers. */
112 # define PTHREAD_RWLOCK_INITIALIZER \
113 { { __PTHREAD_RWLOCK_INITIALIZER (PTHREAD_RWLOCK_DEFAULT_NP) } }
115 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
116 { { __PTHREAD_RWLOCK_INITIALIZER (PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) } }
118 #endif /* Unix98 or XOpen2K */
121 /* Scheduler inheritance. */
124 PTHREAD_INHERIT_SCHED
,
125 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
126 PTHREAD_EXPLICIT_SCHED
127 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
131 /* Scope handling. */
134 PTHREAD_SCOPE_SYSTEM
,
135 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
136 PTHREAD_SCOPE_PROCESS
137 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
141 /* Process shared or private flag. */
144 PTHREAD_PROCESS_PRIVATE
,
145 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
146 PTHREAD_PROCESS_SHARED
147 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
152 /* Conditional variable handling. */
153 #define PTHREAD_COND_INITIALIZER { { {0}, {0}, {0, 0}, {0, 0}, 0, 0, {0, 0} } }
156 /* Cleanup buffers */
157 struct _pthread_cleanup_buffer
159 void (*__routine
) (void *); /* Function to call. */
160 void *__arg
; /* Its argument. */
161 int __canceltype
; /* Saved cancellation type. */
162 struct _pthread_cleanup_buffer
*__prev
; /* Chaining of cleanup functions. */
168 PTHREAD_CANCEL_ENABLE
,
169 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
170 PTHREAD_CANCEL_DISABLE
171 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
175 PTHREAD_CANCEL_DEFERRED
,
176 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
177 PTHREAD_CANCEL_ASYNCHRONOUS
178 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
180 #define PTHREAD_CANCELED ((void *) -1)
183 /* Single execution handling. */
184 #define PTHREAD_ONCE_INIT 0
188 /* Value returned by 'pthread_barrier_wait' for one of the threads after
189 the required number of threads have called this function.
190 -1 is distinct from 0 and all errno constants */
191 # define PTHREAD_BARRIER_SERIAL_THREAD -1
197 /* Create a new thread, starting with execution of START-ROUTINE
198 getting passed ARG. Creation attributed come from ATTR. The new
199 handle is stored in *NEWTHREAD. */
200 extern int pthread_create (pthread_t
*__restrict __newthread
,
201 const pthread_attr_t
*__restrict __attr
,
202 void *(*__start_routine
) (void *),
203 void *__restrict __arg
) __THROWNL
__nonnull ((1, 3));
205 /* Terminate calling thread.
207 The registered cleanup handlers are called via exception handling
208 so we cannot mark this function with __THROW.*/
209 extern void pthread_exit (void *__retval
) __attribute__ ((__noreturn__
));
211 /* Make calling thread wait for termination of the thread TH. The
212 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
215 This function is a cancellation point and therefore not marked with
217 extern int pthread_join (pthread_t __th
, void **__thread_return
);
220 /* Check whether thread TH has terminated. If yes return the status of
221 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
222 extern int pthread_tryjoin_np (pthread_t __th
, void **__thread_return
) __THROW
;
224 /* Make calling thread wait for termination of the thread TH, but only
225 until TIMEOUT. The exit status of the thread is stored in
226 *THREAD_RETURN, if THREAD_RETURN is not NULL.
228 This function is a cancellation point and therefore not marked with
230 extern int pthread_timedjoin_np (pthread_t __th
, void **__thread_return
,
231 const struct timespec
*__abstime
);
233 /* Make calling thread wait for termination of the thread TH, but only
234 until TIMEOUT measured against the clock specified by CLOCKID. The
235 exit status of the thread is stored in *THREAD_RETURN, if
236 THREAD_RETURN is not NULL.
238 This function is a cancellation point and therefore not marked with
240 extern int pthread_clockjoin_np (pthread_t __th
, void **__thread_return
,
242 const struct timespec
*__abstime
);
245 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
246 The resources of TH will therefore be freed immediately when it
247 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
249 extern int pthread_detach (pthread_t __th
) __THROW
;
252 /* Obtain the identifier of the current thread. */
253 extern pthread_t
pthread_self (void) __THROW
__attribute__ ((__const__
));
255 /* Compare two thread identifiers. */
256 extern int pthread_equal (pthread_t __thread1
, pthread_t __thread2
)
257 __THROW
__attribute__ ((__const__
));
260 /* Thread attribute handling. */
262 /* Initialize thread attribute *ATTR with default attributes
263 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
264 no user-provided stack). */
265 extern int pthread_attr_init (pthread_attr_t
*__attr
) __THROW
__nonnull ((1));
267 /* Destroy thread attribute *ATTR. */
268 extern int pthread_attr_destroy (pthread_attr_t
*__attr
)
269 __THROW
__nonnull ((1));
271 /* Get detach state attribute. */
272 extern int pthread_attr_getdetachstate (const pthread_attr_t
*__attr
,
274 __THROW
__nonnull ((1, 2));
276 /* Set detach state attribute. */
277 extern int pthread_attr_setdetachstate (pthread_attr_t
*__attr
,
279 __THROW
__nonnull ((1));
282 /* Get the size of the guard area created for stack overflow protection. */
283 extern int pthread_attr_getguardsize (const pthread_attr_t
*__attr
,
285 __THROW
__nonnull ((1, 2));
287 /* Set the size of the guard area created for stack overflow protection. */
288 extern int pthread_attr_setguardsize (pthread_attr_t
*__attr
,
290 __THROW
__nonnull ((1));
293 /* Return in *PARAM the scheduling parameters of *ATTR. */
294 extern int pthread_attr_getschedparam (const pthread_attr_t
*__restrict __attr
,
295 struct sched_param
*__restrict __param
)
296 __THROW
__nonnull ((1, 2));
298 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
299 extern int pthread_attr_setschedparam (pthread_attr_t
*__restrict __attr
,
300 const struct sched_param
*__restrict
301 __param
) __THROW
__nonnull ((1, 2));
303 /* Return in *POLICY the scheduling policy of *ATTR. */
304 extern int pthread_attr_getschedpolicy (const pthread_attr_t
*__restrict
305 __attr
, int *__restrict __policy
)
306 __THROW
__nonnull ((1, 2));
308 /* Set scheduling policy in *ATTR according to POLICY. */
309 extern int pthread_attr_setschedpolicy (pthread_attr_t
*__attr
, int __policy
)
310 __THROW
__nonnull ((1));
312 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
313 extern int pthread_attr_getinheritsched (const pthread_attr_t
*__restrict
314 __attr
, int *__restrict __inherit
)
315 __THROW
__nonnull ((1, 2));
317 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
318 extern int pthread_attr_setinheritsched (pthread_attr_t
*__attr
,
320 __THROW
__nonnull ((1));
323 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
324 extern int pthread_attr_getscope (const pthread_attr_t
*__restrict __attr
,
325 int *__restrict __scope
)
326 __THROW
__nonnull ((1, 2));
328 /* Set scheduling contention scope in *ATTR according to SCOPE. */
329 extern int pthread_attr_setscope (pthread_attr_t
*__attr
, int __scope
)
330 __THROW
__nonnull ((1));
332 /* Return the previously set address for the stack. */
333 extern int pthread_attr_getstackaddr (const pthread_attr_t
*__restrict
334 __attr
, void **__restrict __stackaddr
)
335 __THROW
__nonnull ((1, 2)) __attribute_deprecated__
;
337 /* Set the starting address of the stack of the thread to be created.
338 Depending on whether the stack grows up or down the value must either
339 be higher or lower than all the address in the memory block. The
340 minimal size of the block must be PTHREAD_STACK_MIN. */
341 extern int pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
343 __THROW
__nonnull ((1)) __attribute_deprecated__
;
345 /* Return the currently used minimal stack size. */
346 extern int pthread_attr_getstacksize (const pthread_attr_t
*__restrict
347 __attr
, size_t *__restrict __stacksize
)
348 __THROW
__nonnull ((1, 2));
350 /* Add information about the minimum stack size needed for the thread
351 to be started. This size must never be less than PTHREAD_STACK_MIN
352 and must also not exceed the system limits. */
353 extern int pthread_attr_setstacksize (pthread_attr_t
*__attr
,
355 __THROW
__nonnull ((1));
358 /* Return the previously set address for the stack. */
359 extern int pthread_attr_getstack (const pthread_attr_t
*__restrict __attr
,
360 void **__restrict __stackaddr
,
361 size_t *__restrict __stacksize
)
362 __THROW
__nonnull ((1, 2, 3));
364 /* The following two interfaces are intended to replace the last two. They
365 require setting the address as well as the size since only setting the
366 address will make the implementation on some architectures impossible. */
367 extern int pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
368 size_t __stacksize
) __THROW
__nonnull ((1));
372 /* Thread created with attribute ATTR will be limited to run only on
373 the processors represented in CPUSET. */
374 extern int pthread_attr_setaffinity_np (pthread_attr_t
*__attr
,
376 const cpu_set_t
*__cpuset
)
377 __THROW
__nonnull ((1, 3));
379 /* Get bit set in CPUSET representing the processors threads created with
381 extern int pthread_attr_getaffinity_np (const pthread_attr_t
*__attr
,
384 __THROW
__nonnull ((1, 3));
386 /* Get the default attributes used by pthread_create in this process. */
387 extern int pthread_getattr_default_np (pthread_attr_t
*__attr
)
388 __THROW
__nonnull ((1));
390 /* Store *SIGMASK as the signal mask for the new thread in *ATTR. */
391 extern int pthread_attr_setsigmask_np (pthread_attr_t
*__attr
,
392 const __sigset_t
*sigmask
);
394 /* Store the signal mask of *ATTR in *SIGMASK. If there is no signal
395 mask stored, return PTHREAD_ATTR_NOSIGMASK_NP. Return zero on
397 extern int pthread_attr_getsigmask_np (const pthread_attr_t
*__attr
,
398 __sigset_t
*sigmask
);
400 /* Special return value from pthread_attr_getsigmask_np if the signal
401 mask has not been set. */
402 #define PTHREAD_ATTR_NO_SIGMASK_NP (-1)
404 /* Set the default attributes to be used by pthread_create in this
406 extern int pthread_setattr_default_np (const pthread_attr_t
*__attr
)
407 __THROW
__nonnull ((1));
409 /* Initialize thread attribute *ATTR with attributes corresponding to the
410 already running thread TH. It shall be called on uninitialized ATTR
411 and destroyed with pthread_attr_destroy when no longer needed. */
412 extern int pthread_getattr_np (pthread_t __th
, pthread_attr_t
*__attr
)
413 __THROW
__nonnull ((2));
417 /* Functions for scheduling control. */
419 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
421 extern int pthread_setschedparam (pthread_t __target_thread
, int __policy
,
422 const struct sched_param
*__param
)
423 __THROW
__nonnull ((3));
425 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
426 extern int pthread_getschedparam (pthread_t __target_thread
,
427 int *__restrict __policy
,
428 struct sched_param
*__restrict __param
)
429 __THROW
__nonnull ((2, 3));
431 /* Set the scheduling priority for TARGET_THREAD. */
432 extern int pthread_setschedprio (pthread_t __target_thread
, int __prio
)
437 /* Get thread name visible in the kernel and its interfaces. */
438 extern int pthread_getname_np (pthread_t __target_thread
, char *__buf
,
440 __THROW
__nonnull ((2));
442 /* Set thread name visible in the kernel and its interfaces. */
443 extern int pthread_setname_np (pthread_t __target_thread
, const char *__name
)
444 __THROW
__nonnull ((2));
449 /* Determine level of concurrency. */
450 extern int pthread_getconcurrency (void) __THROW
;
452 /* Set new concurrency level to LEVEL. */
453 extern int pthread_setconcurrency (int __level
) __THROW
;
457 /* Yield the processor to another thread or process.
458 This function is similar to the POSIX `sched_yield' function but
459 might be differently implemented in the case of a m-on-n thread
461 extern int pthread_yield (void) __THROW
;
464 /* Limit specified thread TH to run only on the processors represented
466 extern int pthread_setaffinity_np (pthread_t __th
, size_t __cpusetsize
,
467 const cpu_set_t
*__cpuset
)
468 __THROW
__nonnull ((3));
470 /* Get bit set in CPUSET representing the processors TH can run on. */
471 extern int pthread_getaffinity_np (pthread_t __th
, size_t __cpusetsize
,
473 __THROW
__nonnull ((3));
477 /* Functions for handling initialization. */
479 /* Guarantee that the initialization function INIT_ROUTINE will be called
480 only once, even if pthread_once is executed several times with the
481 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
482 extern variable initialized to PTHREAD_ONCE_INIT.
484 The initialization functions might throw exception which is why
485 this function is not marked with __THROW. */
486 extern int pthread_once (pthread_once_t
*__once_control
,
487 void (*__init_routine
) (void)) __nonnull ((1, 2));
490 /* Functions for handling cancellation.
492 Note that these functions are explicitly not marked to not throw an
493 exception in C++ code. If cancellation is implemented by unwinding
494 this is necessary to have the compiler generate the unwind information. */
496 /* Set cancelability state of current thread to STATE, returning old
497 state in *OLDSTATE if OLDSTATE is not NULL. */
498 extern int pthread_setcancelstate (int __state
, int *__oldstate
);
500 /* Set cancellation state of current thread to TYPE, returning the old
501 type in *OLDTYPE if OLDTYPE is not NULL. */
502 extern int pthread_setcanceltype (int __type
, int *__oldtype
);
504 /* Cancel THREAD immediately or at the next possibility. */
505 extern int pthread_cancel (pthread_t __th
);
507 /* Test for pending cancellation for the current thread and terminate
508 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
510 extern void pthread_testcancel (void);
513 /* Cancellation handling with integration into exception handling. */
515 struct __cancel_jmp_buf_tag
517 __jmp_buf __cancel_jmp_buf
;
518 int __mask_was_saved
;
523 struct __cancel_jmp_buf_tag __cancel_jmp_buf
[1];
525 } __pthread_unwind_buf_t
__attribute__ ((__aligned__
));
527 /* No special attributes by default. */
528 #ifndef __cleanup_fct_attribute
529 # define __cleanup_fct_attribute
533 /* Structure to hold the cleanup handler information. */
534 struct __pthread_cleanup_frame
536 void (*__cancel_routine
) (void *);
542 #if defined __GNUC__ && defined __EXCEPTIONS
544 /* Class to handle cancellation handler invocation. */
545 class __pthread_cleanup_class
547 void (*__cancel_routine
) (void *);
553 __pthread_cleanup_class (void (*__fct
) (void *), void *__arg
)
554 : __cancel_routine (__fct
), __cancel_arg (__arg
), __do_it (1) { }
555 ~__pthread_cleanup_class () { if (__do_it
) __cancel_routine (__cancel_arg
); }
556 void __setdoit (int __newval
) { __do_it
= __newval
; }
557 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
,
559 void __restore () const { pthread_setcanceltype (__cancel_type
, 0); }
562 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
563 when the thread is canceled or calls pthread_exit. ROUTINE will also
564 be called with arguments ARG when the matching pthread_cleanup_pop
565 is executed with non-zero EXECUTE argument.
567 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
568 be used in matching pairs at the same nesting level of braces. */
569 # define pthread_cleanup_push(routine, arg) \
571 __pthread_cleanup_class __clframe (routine, arg)
573 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
574 If EXECUTE is non-zero, the handler function is called. */
575 # define pthread_cleanup_pop(execute) \
576 __clframe.__setdoit (execute); \
580 /* Install a cleanup handler as pthread_cleanup_push does, but also
581 saves the current cancellation type and sets it to deferred
583 # define pthread_cleanup_push_defer_np(routine, arg) \
585 __pthread_cleanup_class __clframe (routine, arg); \
588 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
589 restores the cancellation type that was in effect when the matching
590 pthread_cleanup_push_defer was called. */
591 # define pthread_cleanup_pop_restore_np(execute) \
592 __clframe.__restore (); \
593 __clframe.__setdoit (execute); \
597 /* Function called to call the cleanup handler. As an extern inline
598 function the compiler is free to decide inlining the change when
599 needed or fall back on the copy which must exist somewhere
602 __pthread_cleanup_routine (struct __pthread_cleanup_frame
*__frame
)
604 if (__frame
->__do_it
)
605 __frame
->__cancel_routine (__frame
->__cancel_arg
);
608 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
609 when the thread is canceled or calls pthread_exit. ROUTINE will also
610 be called with arguments ARG when the matching pthread_cleanup_pop
611 is executed with non-zero EXECUTE argument.
613 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
614 be used in matching pairs at the same nesting level of braces. */
615 # define pthread_cleanup_push(routine, arg) \
617 struct __pthread_cleanup_frame __clframe \
618 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
619 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
622 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
623 If EXECUTE is non-zero, the handler function is called. */
624 # define pthread_cleanup_pop(execute) \
625 __clframe.__do_it = (execute); \
629 /* Install a cleanup handler as pthread_cleanup_push does, but also
630 saves the current cancellation type and sets it to deferred
632 # define pthread_cleanup_push_defer_np(routine, arg) \
634 struct __pthread_cleanup_frame __clframe \
635 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
636 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
638 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
639 &__clframe.__cancel_type)
641 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
642 restores the cancellation type that was in effect when the matching
643 pthread_cleanup_push_defer was called. */
644 # define pthread_cleanup_pop_restore_np(execute) \
645 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
646 __clframe.__do_it = (execute); \
651 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
652 when the thread is canceled or calls pthread_exit. ROUTINE will also
653 be called with arguments ARG when the matching pthread_cleanup_pop
654 is executed with non-zero EXECUTE argument.
656 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
657 be used in matching pairs at the same nesting level of braces. */
658 # define pthread_cleanup_push(routine, arg) \
660 __pthread_unwind_buf_t __cancel_buf; \
661 void (*__cancel_routine) (void *) = (routine); \
662 void *__cancel_arg = (arg); \
663 int __not_first_call = __sigsetjmp_cancel (__cancel_buf.__cancel_jmp_buf, \
665 if (__glibc_unlikely (__not_first_call)) \
667 __cancel_routine (__cancel_arg); \
668 __pthread_unwind_next (&__cancel_buf); \
672 __pthread_register_cancel (&__cancel_buf); \
674 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
675 __cleanup_fct_attribute
;
677 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
678 If EXECUTE is non-zero, the handler function is called. */
679 # define pthread_cleanup_pop(execute) \
680 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
682 __pthread_unregister_cancel (&__cancel_buf); \
684 __cancel_routine (__cancel_arg); \
686 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
687 __cleanup_fct_attribute
;
690 /* Install a cleanup handler as pthread_cleanup_push does, but also
691 saves the current cancellation type and sets it to deferred
693 # define pthread_cleanup_push_defer_np(routine, arg) \
695 __pthread_unwind_buf_t __cancel_buf; \
696 void (*__cancel_routine) (void *) = (routine); \
697 void *__cancel_arg = (arg); \
698 int __not_first_call = __sigsetjmp_cancel (__cancel_buf.__cancel_jmp_buf, \
700 if (__glibc_unlikely (__not_first_call)) \
702 __cancel_routine (__cancel_arg); \
703 __pthread_unwind_next (&__cancel_buf); \
707 __pthread_register_cancel_defer (&__cancel_buf); \
709 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t
*__buf
)
710 __cleanup_fct_attribute
;
712 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
713 restores the cancellation type that was in effect when the matching
714 pthread_cleanup_push_defer was called. */
715 # define pthread_cleanup_pop_restore_np(execute) \
716 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
718 __pthread_unregister_cancel_restore (&__cancel_buf); \
720 __cancel_routine (__cancel_arg); \
722 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t
*__buf
)
723 __cleanup_fct_attribute
;
726 /* Internal interface to initiate cleanup. */
727 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
728 __cleanup_fct_attribute
__attribute__ ((__noreturn__
))
730 __attribute__ ((__weak__
))
735 /* Function used in the macros. Calling __sigsetjmp, with its first
736 argument declared as an array, results in a -Wstringop-overflow
737 warning from GCC 11 because struct pthread_unwind_buf is smaller
738 than jmp_buf. The calls from the macros have __SAVEMASK set to 0,
739 so nothing beyond the common prefix is used and this warning is a
740 false positive. Use an alias with its first argument declared to
741 use the type in the macros if possible to avoid this warning. */
742 #if __GNUC_PREREQ (11, 0)
743 extern int __REDIRECT_NTHNL (__sigsetjmp_cancel
,
744 (struct __cancel_jmp_buf_tag __env
[1],
746 __sigsetjmp
) __attribute_returns_twice__
;
748 # define __sigsetjmp_cancel(env, savemask) \
749 __sigsetjmp ((struct __jmp_buf_tag *) (void *) (env), (savemask))
750 extern int __sigsetjmp (struct __jmp_buf_tag __env
[1],
751 int __savemask
) __THROWNL
;
755 /* Mutex handling. */
757 /* Initialize a mutex. */
758 extern int pthread_mutex_init (pthread_mutex_t
*__mutex
,
759 const pthread_mutexattr_t
*__mutexattr
)
760 __THROW
__nonnull ((1));
762 /* Destroy a mutex. */
763 extern int pthread_mutex_destroy (pthread_mutex_t
*__mutex
)
764 __THROW
__nonnull ((1));
766 /* Try locking a mutex. */
767 extern int pthread_mutex_trylock (pthread_mutex_t
*__mutex
)
768 __THROWNL
__nonnull ((1));
771 extern int pthread_mutex_lock (pthread_mutex_t
*__mutex
)
772 __THROWNL
__nonnull ((1));
775 /* Wait until lock becomes available, or specified time passes. */
776 extern int pthread_mutex_timedlock (pthread_mutex_t
*__restrict __mutex
,
777 const struct timespec
*__restrict
778 __abstime
) __THROWNL
__nonnull ((1, 2));
782 extern int pthread_mutex_clocklock (pthread_mutex_t
*__restrict __mutex
,
784 const struct timespec
*__restrict
785 __abstime
) __THROWNL
__nonnull ((1, 3));
788 /* Unlock a mutex. */
789 extern int pthread_mutex_unlock (pthread_mutex_t
*__mutex
)
790 __THROWNL
__nonnull ((1));
793 /* Get the priority ceiling of MUTEX. */
794 extern int pthread_mutex_getprioceiling (const pthread_mutex_t
*
796 int *__restrict __prioceiling
)
797 __THROW
__nonnull ((1, 2));
799 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
800 priority ceiling value in *OLD_CEILING. */
801 extern int pthread_mutex_setprioceiling (pthread_mutex_t
*__restrict __mutex
,
803 int *__restrict __old_ceiling
)
804 __THROW
__nonnull ((1, 3));
807 #ifdef __USE_XOPEN2K8
808 /* Declare the state protected by MUTEX as consistent. */
809 extern int pthread_mutex_consistent (pthread_mutex_t
*__mutex
)
810 __THROW
__nonnull ((1));
812 extern int pthread_mutex_consistent_np (pthread_mutex_t
*__mutex
)
813 __THROW
__nonnull ((1));
818 /* Functions for handling mutex attributes. */
820 /* Initialize mutex attribute object ATTR with default attributes
821 (kind is PTHREAD_MUTEX_TIMED_NP). */
822 extern int pthread_mutexattr_init (pthread_mutexattr_t
*__attr
)
823 __THROW
__nonnull ((1));
825 /* Destroy mutex attribute object ATTR. */
826 extern int pthread_mutexattr_destroy (pthread_mutexattr_t
*__attr
)
827 __THROW
__nonnull ((1));
829 /* Get the process-shared flag of the mutex attribute ATTR. */
830 extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t
*
832 int *__restrict __pshared
)
833 __THROW
__nonnull ((1, 2));
835 /* Set the process-shared flag of the mutex attribute ATTR. */
836 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t
*__attr
,
838 __THROW
__nonnull ((1));
840 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
841 /* Return in *KIND the mutex kind attribute in *ATTR. */
842 extern int pthread_mutexattr_gettype (const pthread_mutexattr_t
*__restrict
843 __attr
, int *__restrict __kind
)
844 __THROW
__nonnull ((1, 2));
846 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
847 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
848 PTHREAD_MUTEX_DEFAULT). */
849 extern int pthread_mutexattr_settype (pthread_mutexattr_t
*__attr
, int __kind
)
850 __THROW
__nonnull ((1));
853 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
854 extern int pthread_mutexattr_getprotocol (const pthread_mutexattr_t
*
856 int *__restrict __protocol
)
857 __THROW
__nonnull ((1, 2));
859 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
860 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
861 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t
*__attr
,
863 __THROW
__nonnull ((1));
865 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
866 extern int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t
*
868 int *__restrict __prioceiling
)
869 __THROW
__nonnull ((1, 2));
871 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
872 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t
*__attr
,
874 __THROW
__nonnull ((1));
877 /* Get the robustness flag of the mutex attribute ATTR. */
878 extern int pthread_mutexattr_getrobust (const pthread_mutexattr_t
*__attr
,
880 __THROW
__nonnull ((1, 2));
882 extern int pthread_mutexattr_getrobust_np (const pthread_mutexattr_t
*__attr
,
884 __THROW
__nonnull ((1, 2));
887 /* Set the robustness flag of the mutex attribute ATTR. */
888 extern int pthread_mutexattr_setrobust (pthread_mutexattr_t
*__attr
,
890 __THROW
__nonnull ((1));
892 extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t
*__attr
,
894 __THROW
__nonnull ((1));
899 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
900 /* Functions for handling read-write locks. */
902 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
903 the default values if later is NULL. */
904 extern int pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
905 const pthread_rwlockattr_t
*__restrict
906 __attr
) __THROW
__nonnull ((1));
908 /* Destroy read-write lock RWLOCK. */
909 extern int pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
)
910 __THROW
__nonnull ((1));
912 /* Acquire read lock for RWLOCK. */
913 extern int pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
)
914 __THROWNL
__nonnull ((1));
916 /* Try to acquire read lock for RWLOCK. */
917 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
)
918 __THROWNL
__nonnull ((1));
920 # ifdef __USE_XOPEN2K
921 /* Try to acquire read lock for RWLOCK or return after specfied time. */
922 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t
*__restrict __rwlock
,
923 const struct timespec
*__restrict
924 __abstime
) __THROWNL
__nonnull ((1, 2));
928 extern int pthread_rwlock_clockrdlock (pthread_rwlock_t
*__restrict __rwlock
,
930 const struct timespec
*__restrict
931 __abstime
) __THROWNL
__nonnull ((1, 3));
934 /* Acquire write lock for RWLOCK. */
935 extern int pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
)
936 __THROWNL
__nonnull ((1));
938 /* Try to acquire write lock for RWLOCK. */
939 extern int pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
)
940 __THROWNL
__nonnull ((1));
942 # ifdef __USE_XOPEN2K
943 /* Try to acquire write lock for RWLOCK or return after specfied time. */
944 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t
*__restrict __rwlock
,
945 const struct timespec
*__restrict
946 __abstime
) __THROWNL
__nonnull ((1, 2));
950 extern int pthread_rwlock_clockwrlock (pthread_rwlock_t
*__restrict __rwlock
,
952 const struct timespec
*__restrict
953 __abstime
) __THROWNL
__nonnull ((1, 3));
957 extern int pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
)
958 __THROWNL
__nonnull ((1));
961 /* Functions for handling read-write lock attributes. */
963 /* Initialize attribute object ATTR with default values. */
964 extern int pthread_rwlockattr_init (pthread_rwlockattr_t
*__attr
)
965 __THROW
__nonnull ((1));
967 /* Destroy attribute object ATTR. */
968 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t
*__attr
)
969 __THROW
__nonnull ((1));
971 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
972 extern int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t
*
974 int *__restrict __pshared
)
975 __THROW
__nonnull ((1, 2));
977 /* Set process-shared attribute of ATTR to PSHARED. */
978 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t
*__attr
,
980 __THROW
__nonnull ((1));
982 /* Return current setting of reader/writer preference. */
983 extern int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t
*
985 int *__restrict __pref
)
986 __THROW
__nonnull ((1, 2));
988 /* Set reader/write preference. */
989 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t
*__attr
,
990 int __pref
) __THROW
__nonnull ((1));
994 /* Functions for handling conditional variables. */
996 /* Initialize condition variable COND using attributes ATTR, or use
997 the default values if later is NULL. */
998 extern int pthread_cond_init (pthread_cond_t
*__restrict __cond
,
999 const pthread_condattr_t
*__restrict __cond_attr
)
1000 __THROW
__nonnull ((1));
1002 /* Destroy condition variable COND. */
1003 extern int pthread_cond_destroy (pthread_cond_t
*__cond
)
1004 __THROW
__nonnull ((1));
1006 /* Wake up one thread waiting for condition variable COND. */
1007 extern int pthread_cond_signal (pthread_cond_t
*__cond
)
1008 __THROWNL
__nonnull ((1));
1010 /* Wake up all threads waiting for condition variables COND. */
1011 extern int pthread_cond_broadcast (pthread_cond_t
*__cond
)
1012 __THROWNL
__nonnull ((1));
1014 /* Wait for condition variable COND to be signaled or broadcast.
1015 MUTEX is assumed to be locked before.
1017 This function is a cancellation point and therefore not marked with
1019 extern int pthread_cond_wait (pthread_cond_t
*__restrict __cond
,
1020 pthread_mutex_t
*__restrict __mutex
)
1023 /* Wait for condition variable COND to be signaled or broadcast until
1024 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
1025 absolute time specification; zero is the beginning of the epoch
1026 (00:00:00 GMT, January 1, 1970).
1028 This function is a cancellation point and therefore not marked with
1030 extern int pthread_cond_timedwait (pthread_cond_t
*__restrict __cond
,
1031 pthread_mutex_t
*__restrict __mutex
,
1032 const struct timespec
*__restrict __abstime
)
1033 __nonnull ((1, 2, 3));
1036 /* Wait for condition variable COND to be signaled or broadcast until
1037 ABSTIME measured by the specified clock. MUTEX is assumed to be
1038 locked before. CLOCK is the clock to use. ABSTIME is an absolute
1039 time specification against CLOCK's epoch.
1041 This function is a cancellation point and therefore not marked with
1043 extern int pthread_cond_clockwait (pthread_cond_t
*__restrict __cond
,
1044 pthread_mutex_t
*__restrict __mutex
,
1045 __clockid_t __clock_id
,
1046 const struct timespec
*__restrict __abstime
)
1047 __nonnull ((1, 2, 4));
1050 /* Functions for handling condition variable attributes. */
1052 /* Initialize condition variable attribute ATTR. */
1053 extern int pthread_condattr_init (pthread_condattr_t
*__attr
)
1054 __THROW
__nonnull ((1));
1056 /* Destroy condition variable attribute ATTR. */
1057 extern int pthread_condattr_destroy (pthread_condattr_t
*__attr
)
1058 __THROW
__nonnull ((1));
1060 /* Get the process-shared flag of the condition variable attribute ATTR. */
1061 extern int pthread_condattr_getpshared (const pthread_condattr_t
*
1063 int *__restrict __pshared
)
1064 __THROW
__nonnull ((1, 2));
1066 /* Set the process-shared flag of the condition variable attribute ATTR. */
1067 extern int pthread_condattr_setpshared (pthread_condattr_t
*__attr
,
1068 int __pshared
) __THROW
__nonnull ((1));
1070 #ifdef __USE_XOPEN2K
1071 /* Get the clock selected for the condition variable attribute ATTR. */
1072 extern int pthread_condattr_getclock (const pthread_condattr_t
*
1074 __clockid_t
*__restrict __clock_id
)
1075 __THROW
__nonnull ((1, 2));
1077 /* Set the clock selected for the condition variable attribute ATTR. */
1078 extern int pthread_condattr_setclock (pthread_condattr_t
*__attr
,
1079 __clockid_t __clock_id
)
1080 __THROW
__nonnull ((1));
1084 #ifdef __USE_XOPEN2K
1085 /* Functions to handle spinlocks. */
1087 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
1088 be shared between different processes. */
1089 extern int pthread_spin_init (pthread_spinlock_t
*__lock
, int __pshared
)
1090 __THROW
__nonnull ((1));
1092 /* Destroy the spinlock LOCK. */
1093 extern int pthread_spin_destroy (pthread_spinlock_t
*__lock
)
1094 __THROW
__nonnull ((1));
1096 /* Wait until spinlock LOCK is retrieved. */
1097 extern int pthread_spin_lock (pthread_spinlock_t
*__lock
)
1098 __THROWNL
__nonnull ((1));
1100 /* Try to lock spinlock LOCK. */
1101 extern int pthread_spin_trylock (pthread_spinlock_t
*__lock
)
1102 __THROWNL
__nonnull ((1));
1104 /* Release spinlock LOCK. */
1105 extern int pthread_spin_unlock (pthread_spinlock_t
*__lock
)
1106 __THROWNL
__nonnull ((1));
1109 /* Functions to handle barriers. */
1111 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1112 opened when COUNT waiters arrived. */
1113 extern int pthread_barrier_init (pthread_barrier_t
*__restrict __barrier
,
1114 const pthread_barrierattr_t
*__restrict
1115 __attr
, unsigned int __count
)
1116 __THROW
__nonnull ((1));
1118 /* Destroy a previously dynamically initialized barrier BARRIER. */
1119 extern int pthread_barrier_destroy (pthread_barrier_t
*__barrier
)
1120 __THROW
__nonnull ((1));
1122 /* Wait on barrier BARRIER. */
1123 extern int pthread_barrier_wait (pthread_barrier_t
*__barrier
)
1124 __THROWNL
__nonnull ((1));
1127 /* Initialize barrier attribute ATTR. */
1128 extern int pthread_barrierattr_init (pthread_barrierattr_t
*__attr
)
1129 __THROW
__nonnull ((1));
1131 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1132 extern int pthread_barrierattr_destroy (pthread_barrierattr_t
*__attr
)
1133 __THROW
__nonnull ((1));
1135 /* Get the process-shared flag of the barrier attribute ATTR. */
1136 extern int pthread_barrierattr_getpshared (const pthread_barrierattr_t
*
1138 int *__restrict __pshared
)
1139 __THROW
__nonnull ((1, 2));
1141 /* Set the process-shared flag of the barrier attribute ATTR. */
1142 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t
*__attr
,
1144 __THROW
__nonnull ((1));
1148 /* Functions for handling thread-specific data. */
1150 /* Create a key value identifying a location in the thread-specific
1151 data area. Each thread maintains a distinct thread-specific data
1152 area. DESTR_FUNCTION, if non-NULL, is called with the value
1153 associated to that key when the key is destroyed.
1154 DESTR_FUNCTION is not called if the value associated is NULL when
1155 the key is destroyed. */
1156 extern int pthread_key_create (pthread_key_t
*__key
,
1157 void (*__destr_function
) (void *))
1158 __THROW
__nonnull ((1));
1161 extern int pthread_key_delete (pthread_key_t __key
) __THROW
;
1163 /* Return current value of the thread-specific data slot identified by KEY. */
1164 extern void *pthread_getspecific (pthread_key_t __key
) __THROW
;
1166 /* Store POINTER in the thread-specific data slot identified by KEY. */
1167 extern int pthread_setspecific (pthread_key_t __key
,
1168 const void *__pointer
) __THROW
;
1171 #ifdef __USE_XOPEN2K
1172 /* Get ID of CPU-time clock for thread THREAD_ID. */
1173 extern int pthread_getcpuclockid (pthread_t __thread_id
,
1174 __clockid_t
*__clock_id
)
1175 __THROW
__nonnull ((2));
1179 /* Install handlers to be called when a new process is created with FORK.
1180 The PREPARE handler is called in the parent process just before performing
1181 FORK. The PARENT handler is called in the parent process just after FORK.
1182 The CHILD handler is called in the child process. Each of the three
1183 handlers can be NULL, meaning that no handler needs to be called at that
1185 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1186 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1187 first called before FORK), and the PARENT and CHILD handlers are called
1188 in FIFO (first added, first called). */
1190 extern int pthread_atfork (void (*__prepare
) (void),
1191 void (*__parent
) (void),
1192 void (*__child
) (void)) __THROW
;
1195 #ifdef __USE_EXTERN_INLINES
1196 /* Optimizations. */
1198 __NTH (pthread_equal (pthread_t __thread1
, pthread_t __thread2
))
1200 return __thread1
== __thread2
;
1206 #endif /* pthread.h */