1 /* Copyright (C) 2002-2021 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 extern int pthread_yield (void) __THROW
;
458 # ifdef __REDIRECT_NTH
459 extern int __REDIRECT_NTH (pthread_yield
, (void), sched_yield
)
460 __attribute_deprecated_msg__ ("\
461 pthread_yield is deprecated, use sched_yield instead");
463 # define pthread_yield sched_yield
467 /* Limit specified thread TH to run only on the processors represented
469 extern int pthread_setaffinity_np (pthread_t __th
, size_t __cpusetsize
,
470 const cpu_set_t
*__cpuset
)
471 __THROW
__nonnull ((3));
473 /* Get bit set in CPUSET representing the processors TH can run on. */
474 extern int pthread_getaffinity_np (pthread_t __th
, size_t __cpusetsize
,
476 __THROW
__nonnull ((3));
480 /* Functions for handling initialization. */
482 /* Guarantee that the initialization function INIT_ROUTINE will be called
483 only once, even if pthread_once is executed several times with the
484 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
485 extern variable initialized to PTHREAD_ONCE_INIT.
487 The initialization functions might throw exception which is why
488 this function is not marked with __THROW. */
489 extern int pthread_once (pthread_once_t
*__once_control
,
490 void (*__init_routine
) (void)) __nonnull ((1, 2));
493 /* Functions for handling cancellation.
495 Note that these functions are explicitly not marked to not throw an
496 exception in C++ code. If cancellation is implemented by unwinding
497 this is necessary to have the compiler generate the unwind information. */
499 /* Set cancelability state of current thread to STATE, returning old
500 state in *OLDSTATE if OLDSTATE is not NULL. */
501 extern int pthread_setcancelstate (int __state
, int *__oldstate
);
503 /* Set cancellation state of current thread to TYPE, returning the old
504 type in *OLDTYPE if OLDTYPE is not NULL. */
505 extern int pthread_setcanceltype (int __type
, int *__oldtype
);
507 /* Cancel THREAD immediately or at the next possibility. */
508 extern int pthread_cancel (pthread_t __th
);
510 /* Test for pending cancellation for the current thread and terminate
511 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
513 extern void pthread_testcancel (void);
516 /* Cancellation handling with integration into exception handling. */
518 struct __cancel_jmp_buf_tag
520 __jmp_buf __cancel_jmp_buf
;
521 int __mask_was_saved
;
526 struct __cancel_jmp_buf_tag __cancel_jmp_buf
[1];
528 } __pthread_unwind_buf_t
__attribute__ ((__aligned__
));
530 /* No special attributes by default. */
531 #ifndef __cleanup_fct_attribute
532 # define __cleanup_fct_attribute
536 /* Structure to hold the cleanup handler information. */
537 struct __pthread_cleanup_frame
539 void (*__cancel_routine
) (void *);
545 #if defined __GNUC__ && defined __EXCEPTIONS
547 /* Class to handle cancellation handler invocation. */
548 class __pthread_cleanup_class
550 void (*__cancel_routine
) (void *);
556 __pthread_cleanup_class (void (*__fct
) (void *), void *__arg
)
557 : __cancel_routine (__fct
), __cancel_arg (__arg
), __do_it (1) { }
558 ~__pthread_cleanup_class () { if (__do_it
) __cancel_routine (__cancel_arg
); }
559 void __setdoit (int __newval
) { __do_it
= __newval
; }
560 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
,
562 void __restore () const { pthread_setcanceltype (__cancel_type
, 0); }
565 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
566 when the thread is canceled or calls pthread_exit. ROUTINE will also
567 be called with arguments ARG when the matching pthread_cleanup_pop
568 is executed with non-zero EXECUTE argument.
570 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
571 be used in matching pairs at the same nesting level of braces. */
572 # define pthread_cleanup_push(routine, arg) \
574 __pthread_cleanup_class __clframe (routine, arg)
576 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
577 If EXECUTE is non-zero, the handler function is called. */
578 # define pthread_cleanup_pop(execute) \
579 __clframe.__setdoit (execute); \
583 /* Install a cleanup handler as pthread_cleanup_push does, but also
584 saves the current cancellation type and sets it to deferred
586 # define pthread_cleanup_push_defer_np(routine, arg) \
588 __pthread_cleanup_class __clframe (routine, arg); \
591 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
592 restores the cancellation type that was in effect when the matching
593 pthread_cleanup_push_defer was called. */
594 # define pthread_cleanup_pop_restore_np(execute) \
595 __clframe.__restore (); \
596 __clframe.__setdoit (execute); \
600 /* Function called to call the cleanup handler. As an extern inline
601 function the compiler is free to decide inlining the change when
602 needed or fall back on the copy which must exist somewhere
605 __pthread_cleanup_routine (struct __pthread_cleanup_frame
*__frame
)
607 if (__frame
->__do_it
)
608 __frame
->__cancel_routine (__frame
->__cancel_arg
);
611 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
612 when the thread is canceled or calls pthread_exit. ROUTINE will also
613 be called with arguments ARG when the matching pthread_cleanup_pop
614 is executed with non-zero EXECUTE argument.
616 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
617 be used in matching pairs at the same nesting level of braces. */
618 # define pthread_cleanup_push(routine, arg) \
620 struct __pthread_cleanup_frame __clframe \
621 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
622 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
625 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
626 If EXECUTE is non-zero, the handler function is called. */
627 # define pthread_cleanup_pop(execute) \
628 __clframe.__do_it = (execute); \
632 /* Install a cleanup handler as pthread_cleanup_push does, but also
633 saves the current cancellation type and sets it to deferred
635 # define pthread_cleanup_push_defer_np(routine, arg) \
637 struct __pthread_cleanup_frame __clframe \
638 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
639 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
641 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
642 &__clframe.__cancel_type)
644 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
645 restores the cancellation type that was in effect when the matching
646 pthread_cleanup_push_defer was called. */
647 # define pthread_cleanup_pop_restore_np(execute) \
648 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
649 __clframe.__do_it = (execute); \
654 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
655 when the thread is canceled or calls pthread_exit. ROUTINE will also
656 be called with arguments ARG when the matching pthread_cleanup_pop
657 is executed with non-zero EXECUTE argument.
659 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
660 be used in matching pairs at the same nesting level of braces. */
661 # define pthread_cleanup_push(routine, arg) \
663 __pthread_unwind_buf_t __cancel_buf; \
664 void (*__cancel_routine) (void *) = (routine); \
665 void *__cancel_arg = (arg); \
666 int __not_first_call = __sigsetjmp_cancel (__cancel_buf.__cancel_jmp_buf, \
668 if (__glibc_unlikely (__not_first_call)) \
670 __cancel_routine (__cancel_arg); \
671 __pthread_unwind_next (&__cancel_buf); \
675 __pthread_register_cancel (&__cancel_buf); \
677 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
678 __cleanup_fct_attribute
;
680 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
681 If EXECUTE is non-zero, the handler function is called. */
682 # define pthread_cleanup_pop(execute) \
683 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
685 __pthread_unregister_cancel (&__cancel_buf); \
687 __cancel_routine (__cancel_arg); \
689 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
690 __cleanup_fct_attribute
;
693 /* Install a cleanup handler as pthread_cleanup_push does, but also
694 saves the current cancellation type and sets it to deferred
696 # define pthread_cleanup_push_defer_np(routine, arg) \
698 __pthread_unwind_buf_t __cancel_buf; \
699 void (*__cancel_routine) (void *) = (routine); \
700 void *__cancel_arg = (arg); \
701 int __not_first_call = __sigsetjmp_cancel (__cancel_buf.__cancel_jmp_buf, \
703 if (__glibc_unlikely (__not_first_call)) \
705 __cancel_routine (__cancel_arg); \
706 __pthread_unwind_next (&__cancel_buf); \
710 __pthread_register_cancel_defer (&__cancel_buf); \
712 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t
*__buf
)
713 __cleanup_fct_attribute
;
715 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
716 restores the cancellation type that was in effect when the matching
717 pthread_cleanup_push_defer was called. */
718 # define pthread_cleanup_pop_restore_np(execute) \
719 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
721 __pthread_unregister_cancel_restore (&__cancel_buf); \
723 __cancel_routine (__cancel_arg); \
725 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t
*__buf
)
726 __cleanup_fct_attribute
;
729 /* Internal interface to initiate cleanup. */
730 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
731 __cleanup_fct_attribute
__attribute__ ((__noreturn__
))
733 __attribute__ ((__weak__
))
738 /* Function used in the macros. Calling __sigsetjmp, with its first
739 argument declared as an array, results in a -Wstringop-overflow
740 warning from GCC 11 because struct pthread_unwind_buf is smaller
741 than jmp_buf. The calls from the macros have __SAVEMASK set to 0,
742 so nothing beyond the common prefix is used and this warning is a
743 false positive. Use an alias with its first argument declared to
744 use the type in the macros if possible to avoid this warning. */
745 #if __GNUC_PREREQ (11, 0)
746 extern int __REDIRECT_NTHNL (__sigsetjmp_cancel
,
747 (struct __cancel_jmp_buf_tag __env
[1],
749 __sigsetjmp
) __attribute_returns_twice__
;
751 # define __sigsetjmp_cancel(env, savemask) \
752 __sigsetjmp ((struct __jmp_buf_tag *) (void *) (env), (savemask))
753 extern int __sigsetjmp (struct __jmp_buf_tag __env
[1],
754 int __savemask
) __THROWNL
;
758 /* Mutex handling. */
760 /* Initialize a mutex. */
761 extern int pthread_mutex_init (pthread_mutex_t
*__mutex
,
762 const pthread_mutexattr_t
*__mutexattr
)
763 __THROW
__nonnull ((1));
765 /* Destroy a mutex. */
766 extern int pthread_mutex_destroy (pthread_mutex_t
*__mutex
)
767 __THROW
__nonnull ((1));
769 /* Try locking a mutex. */
770 extern int pthread_mutex_trylock (pthread_mutex_t
*__mutex
)
771 __THROWNL
__nonnull ((1));
774 extern int pthread_mutex_lock (pthread_mutex_t
*__mutex
)
775 __THROWNL
__nonnull ((1));
778 /* Wait until lock becomes available, or specified time passes. */
779 extern int pthread_mutex_timedlock (pthread_mutex_t
*__restrict __mutex
,
780 const struct timespec
*__restrict
781 __abstime
) __THROWNL
__nonnull ((1, 2));
785 extern int pthread_mutex_clocklock (pthread_mutex_t
*__restrict __mutex
,
787 const struct timespec
*__restrict
788 __abstime
) __THROWNL
__nonnull ((1, 3));
791 /* Unlock a mutex. */
792 extern int pthread_mutex_unlock (pthread_mutex_t
*__mutex
)
793 __THROWNL
__nonnull ((1));
796 /* Get the priority ceiling of MUTEX. */
797 extern int pthread_mutex_getprioceiling (const pthread_mutex_t
*
799 int *__restrict __prioceiling
)
800 __THROW
__nonnull ((1, 2));
802 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
803 priority ceiling value in *OLD_CEILING. */
804 extern int pthread_mutex_setprioceiling (pthread_mutex_t
*__restrict __mutex
,
806 int *__restrict __old_ceiling
)
807 __THROW
__nonnull ((1, 3));
810 #ifdef __USE_XOPEN2K8
811 /* Declare the state protected by MUTEX as consistent. */
812 extern int pthread_mutex_consistent (pthread_mutex_t
*__mutex
)
813 __THROW
__nonnull ((1));
815 # ifdef __REDIRECT_NTH
816 extern int __REDIRECT_NTH (pthread_mutex_consistent_np
, (pthread_mutex_t
*),
817 pthread_mutex_consistent
) __nonnull ((1))
818 __attribute_deprecated_msg__ ("\
819 pthread_mutex_consistent_np is deprecated, use pthread_mutex_consistent");
821 # define pthread_mutex_consistent_np pthread_mutex_consistent
827 /* Functions for handling mutex attributes. */
829 /* Initialize mutex attribute object ATTR with default attributes
830 (kind is PTHREAD_MUTEX_TIMED_NP). */
831 extern int pthread_mutexattr_init (pthread_mutexattr_t
*__attr
)
832 __THROW
__nonnull ((1));
834 /* Destroy mutex attribute object ATTR. */
835 extern int pthread_mutexattr_destroy (pthread_mutexattr_t
*__attr
)
836 __THROW
__nonnull ((1));
838 /* Get the process-shared flag of the mutex attribute ATTR. */
839 extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t
*
841 int *__restrict __pshared
)
842 __THROW
__nonnull ((1, 2));
844 /* Set the process-shared flag of the mutex attribute ATTR. */
845 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t
*__attr
,
847 __THROW
__nonnull ((1));
849 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
850 /* Return in *KIND the mutex kind attribute in *ATTR. */
851 extern int pthread_mutexattr_gettype (const pthread_mutexattr_t
*__restrict
852 __attr
, int *__restrict __kind
)
853 __THROW
__nonnull ((1, 2));
855 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
856 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
857 PTHREAD_MUTEX_DEFAULT). */
858 extern int pthread_mutexattr_settype (pthread_mutexattr_t
*__attr
, int __kind
)
859 __THROW
__nonnull ((1));
862 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
863 extern int pthread_mutexattr_getprotocol (const pthread_mutexattr_t
*
865 int *__restrict __protocol
)
866 __THROW
__nonnull ((1, 2));
868 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
869 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
870 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t
*__attr
,
872 __THROW
__nonnull ((1));
874 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
875 extern int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t
*
877 int *__restrict __prioceiling
)
878 __THROW
__nonnull ((1, 2));
880 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
881 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t
*__attr
,
883 __THROW
__nonnull ((1));
886 /* Get the robustness flag of the mutex attribute ATTR. */
887 extern int pthread_mutexattr_getrobust (const pthread_mutexattr_t
*__attr
,
889 __THROW
__nonnull ((1, 2));
891 # ifdef __REDIRECT_NTH
892 extern int __REDIRECT_NTH (pthread_mutexattr_getrobust_np
,
893 (pthread_mutex_t
*, int *),
894 pthread_mutexattr_getrobust
) __nonnull ((1))
895 __attribute_deprecated_msg__ ("\
896 pthread_mutexattr_getrobust_np is deprecated, use pthread_mutexattr_getrobust");
898 # define pthread_mutexattr_getrobust_np pthread_mutexattr_getrobust
902 /* Set the robustness flag of the mutex attribute ATTR. */
903 extern int pthread_mutexattr_setrobust (pthread_mutexattr_t
*__attr
,
905 __THROW
__nonnull ((1));
907 # ifdef __REDIRECT_NTH
908 extern int __REDIRECT_NTH (pthread_mutexattr_setrobust_np
,
909 (pthread_mutex_t
*, int),
910 pthread_mutexattr_setrobust
) __nonnull ((1))
911 __attribute_deprecated_msg__ ("\
912 pthread_mutexattr_setrobust_np is deprecated, use pthread_mutexattr_setrobust");
914 # define pthread_mutexattr_setrobust_np pthread_mutexattr_setrobust
919 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
920 /* Functions for handling read-write locks. */
922 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
923 the default values if later is NULL. */
924 extern int pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
925 const pthread_rwlockattr_t
*__restrict
926 __attr
) __THROW
__nonnull ((1));
928 /* Destroy read-write lock RWLOCK. */
929 extern int pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
)
930 __THROW
__nonnull ((1));
932 /* Acquire read lock for RWLOCK. */
933 extern int pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
)
934 __THROWNL
__nonnull ((1));
936 /* Try to acquire read lock for RWLOCK. */
937 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
)
938 __THROWNL
__nonnull ((1));
940 # ifdef __USE_XOPEN2K
941 /* Try to acquire read lock for RWLOCK or return after specfied time. */
942 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t
*__restrict __rwlock
,
943 const struct timespec
*__restrict
944 __abstime
) __THROWNL
__nonnull ((1, 2));
948 extern int pthread_rwlock_clockrdlock (pthread_rwlock_t
*__restrict __rwlock
,
950 const struct timespec
*__restrict
951 __abstime
) __THROWNL
__nonnull ((1, 3));
954 /* Acquire write lock for RWLOCK. */
955 extern int pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
)
956 __THROWNL
__nonnull ((1));
958 /* Try to acquire write lock for RWLOCK. */
959 extern int pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
)
960 __THROWNL
__nonnull ((1));
962 # ifdef __USE_XOPEN2K
963 /* Try to acquire write lock for RWLOCK or return after specfied time. */
964 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t
*__restrict __rwlock
,
965 const struct timespec
*__restrict
966 __abstime
) __THROWNL
__nonnull ((1, 2));
970 extern int pthread_rwlock_clockwrlock (pthread_rwlock_t
*__restrict __rwlock
,
972 const struct timespec
*__restrict
973 __abstime
) __THROWNL
__nonnull ((1, 3));
977 extern int pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
)
978 __THROWNL
__nonnull ((1));
981 /* Functions for handling read-write lock attributes. */
983 /* Initialize attribute object ATTR with default values. */
984 extern int pthread_rwlockattr_init (pthread_rwlockattr_t
*__attr
)
985 __THROW
__nonnull ((1));
987 /* Destroy attribute object ATTR. */
988 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t
*__attr
)
989 __THROW
__nonnull ((1));
991 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
992 extern int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t
*
994 int *__restrict __pshared
)
995 __THROW
__nonnull ((1, 2));
997 /* Set process-shared attribute of ATTR to PSHARED. */
998 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t
*__attr
,
1000 __THROW
__nonnull ((1));
1002 /* Return current setting of reader/writer preference. */
1003 extern int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t
*
1005 int *__restrict __pref
)
1006 __THROW
__nonnull ((1, 2));
1008 /* Set reader/write preference. */
1009 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t
*__attr
,
1010 int __pref
) __THROW
__nonnull ((1));
1014 /* Functions for handling conditional variables. */
1016 /* Initialize condition variable COND using attributes ATTR, or use
1017 the default values if later is NULL. */
1018 extern int pthread_cond_init (pthread_cond_t
*__restrict __cond
,
1019 const pthread_condattr_t
*__restrict __cond_attr
)
1020 __THROW
__nonnull ((1));
1022 /* Destroy condition variable COND. */
1023 extern int pthread_cond_destroy (pthread_cond_t
*__cond
)
1024 __THROW
__nonnull ((1));
1026 /* Wake up one thread waiting for condition variable COND. */
1027 extern int pthread_cond_signal (pthread_cond_t
*__cond
)
1028 __THROWNL
__nonnull ((1));
1030 /* Wake up all threads waiting for condition variables COND. */
1031 extern int pthread_cond_broadcast (pthread_cond_t
*__cond
)
1032 __THROWNL
__nonnull ((1));
1034 /* Wait for condition variable COND to be signaled or broadcast.
1035 MUTEX is assumed to be locked before.
1037 This function is a cancellation point and therefore not marked with
1039 extern int pthread_cond_wait (pthread_cond_t
*__restrict __cond
,
1040 pthread_mutex_t
*__restrict __mutex
)
1043 /* Wait for condition variable COND to be signaled or broadcast until
1044 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
1045 absolute time specification; zero is the beginning of the epoch
1046 (00:00:00 GMT, January 1, 1970).
1048 This function is a cancellation point and therefore not marked with
1050 extern int pthread_cond_timedwait (pthread_cond_t
*__restrict __cond
,
1051 pthread_mutex_t
*__restrict __mutex
,
1052 const struct timespec
*__restrict __abstime
)
1053 __nonnull ((1, 2, 3));
1056 /* Wait for condition variable COND to be signaled or broadcast until
1057 ABSTIME measured by the specified clock. MUTEX is assumed to be
1058 locked before. CLOCK is the clock to use. ABSTIME is an absolute
1059 time specification against CLOCK's epoch.
1061 This function is a cancellation point and therefore not marked with
1063 extern int pthread_cond_clockwait (pthread_cond_t
*__restrict __cond
,
1064 pthread_mutex_t
*__restrict __mutex
,
1065 __clockid_t __clock_id
,
1066 const struct timespec
*__restrict __abstime
)
1067 __nonnull ((1, 2, 4));
1070 /* Functions for handling condition variable attributes. */
1072 /* Initialize condition variable attribute ATTR. */
1073 extern int pthread_condattr_init (pthread_condattr_t
*__attr
)
1074 __THROW
__nonnull ((1));
1076 /* Destroy condition variable attribute ATTR. */
1077 extern int pthread_condattr_destroy (pthread_condattr_t
*__attr
)
1078 __THROW
__nonnull ((1));
1080 /* Get the process-shared flag of the condition variable attribute ATTR. */
1081 extern int pthread_condattr_getpshared (const pthread_condattr_t
*
1083 int *__restrict __pshared
)
1084 __THROW
__nonnull ((1, 2));
1086 /* Set the process-shared flag of the condition variable attribute ATTR. */
1087 extern int pthread_condattr_setpshared (pthread_condattr_t
*__attr
,
1088 int __pshared
) __THROW
__nonnull ((1));
1090 #ifdef __USE_XOPEN2K
1091 /* Get the clock selected for the condition variable attribute ATTR. */
1092 extern int pthread_condattr_getclock (const pthread_condattr_t
*
1094 __clockid_t
*__restrict __clock_id
)
1095 __THROW
__nonnull ((1, 2));
1097 /* Set the clock selected for the condition variable attribute ATTR. */
1098 extern int pthread_condattr_setclock (pthread_condattr_t
*__attr
,
1099 __clockid_t __clock_id
)
1100 __THROW
__nonnull ((1));
1104 #ifdef __USE_XOPEN2K
1105 /* Functions to handle spinlocks. */
1107 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
1108 be shared between different processes. */
1109 extern int pthread_spin_init (pthread_spinlock_t
*__lock
, int __pshared
)
1110 __THROW
__nonnull ((1));
1112 /* Destroy the spinlock LOCK. */
1113 extern int pthread_spin_destroy (pthread_spinlock_t
*__lock
)
1114 __THROW
__nonnull ((1));
1116 /* Wait until spinlock LOCK is retrieved. */
1117 extern int pthread_spin_lock (pthread_spinlock_t
*__lock
)
1118 __THROWNL
__nonnull ((1));
1120 /* Try to lock spinlock LOCK. */
1121 extern int pthread_spin_trylock (pthread_spinlock_t
*__lock
)
1122 __THROWNL
__nonnull ((1));
1124 /* Release spinlock LOCK. */
1125 extern int pthread_spin_unlock (pthread_spinlock_t
*__lock
)
1126 __THROWNL
__nonnull ((1));
1129 /* Functions to handle barriers. */
1131 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1132 opened when COUNT waiters arrived. */
1133 extern int pthread_barrier_init (pthread_barrier_t
*__restrict __barrier
,
1134 const pthread_barrierattr_t
*__restrict
1135 __attr
, unsigned int __count
)
1136 __THROW
__nonnull ((1));
1138 /* Destroy a previously dynamically initialized barrier BARRIER. */
1139 extern int pthread_barrier_destroy (pthread_barrier_t
*__barrier
)
1140 __THROW
__nonnull ((1));
1142 /* Wait on barrier BARRIER. */
1143 extern int pthread_barrier_wait (pthread_barrier_t
*__barrier
)
1144 __THROWNL
__nonnull ((1));
1147 /* Initialize barrier attribute ATTR. */
1148 extern int pthread_barrierattr_init (pthread_barrierattr_t
*__attr
)
1149 __THROW
__nonnull ((1));
1151 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1152 extern int pthread_barrierattr_destroy (pthread_barrierattr_t
*__attr
)
1153 __THROW
__nonnull ((1));
1155 /* Get the process-shared flag of the barrier attribute ATTR. */
1156 extern int pthread_barrierattr_getpshared (const pthread_barrierattr_t
*
1158 int *__restrict __pshared
)
1159 __THROW
__nonnull ((1, 2));
1161 /* Set the process-shared flag of the barrier attribute ATTR. */
1162 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t
*__attr
,
1164 __THROW
__nonnull ((1));
1168 /* Functions for handling thread-specific data. */
1170 /* Create a key value identifying a location in the thread-specific
1171 data area. Each thread maintains a distinct thread-specific data
1172 area. DESTR_FUNCTION, if non-NULL, is called with the value
1173 associated to that key when the key is destroyed.
1174 DESTR_FUNCTION is not called if the value associated is NULL when
1175 the key is destroyed. */
1176 extern int pthread_key_create (pthread_key_t
*__key
,
1177 void (*__destr_function
) (void *))
1178 __THROW
__nonnull ((1));
1181 extern int pthread_key_delete (pthread_key_t __key
) __THROW
;
1183 /* Return current value of the thread-specific data slot identified by KEY. */
1184 extern void *pthread_getspecific (pthread_key_t __key
) __THROW
;
1186 /* Store POINTER in the thread-specific data slot identified by KEY. */
1187 extern int pthread_setspecific (pthread_key_t __key
,
1188 const void *__pointer
)
1189 __THROW
__attr_access_none (2);
1192 #ifdef __USE_XOPEN2K
1193 /* Get ID of CPU-time clock for thread THREAD_ID. */
1194 extern int pthread_getcpuclockid (pthread_t __thread_id
,
1195 __clockid_t
*__clock_id
)
1196 __THROW
__nonnull ((2));
1200 /* Install handlers to be called when a new process is created with FORK.
1201 The PREPARE handler is called in the parent process just before performing
1202 FORK. The PARENT handler is called in the parent process just after FORK.
1203 The CHILD handler is called in the child process. Each of the three
1204 handlers can be NULL, meaning that no handler needs to be called at that
1206 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1207 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1208 first called before FORK), and the PARENT and CHILD handlers are called
1209 in FIFO (first added, first called). */
1211 extern int pthread_atfork (void (*__prepare
) (void),
1212 void (*__parent
) (void),
1213 void (*__child
) (void)) __THROW
;
1216 #ifdef __USE_EXTERN_INLINES
1217 /* Optimizations. */
1219 __NTH (pthread_equal (pthread_t __thread1
, pthread_t __thread2
))
1221 return __thread1
== __thread2
;
1227 #endif /* pthread.h */