1 /* Copyright (C) 2002-2018 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 <http://www.gnu.org/licenses/>. */
26 #include <bits/pthreadtypes.h>
27 #include <bits/setjmp.h>
28 #include <bits/wordsize.h>
29 #include <bits/types/struct_timespec.h>
35 PTHREAD_CREATE_JOINABLE
,
36 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
37 PTHREAD_CREATE_DETACHED
38 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
45 PTHREAD_MUTEX_TIMED_NP
,
46 PTHREAD_MUTEX_RECURSIVE_NP
,
47 PTHREAD_MUTEX_ERRORCHECK_NP
,
48 PTHREAD_MUTEX_ADAPTIVE_NP
49 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
51 PTHREAD_MUTEX_NORMAL
= PTHREAD_MUTEX_TIMED_NP
,
52 PTHREAD_MUTEX_RECURSIVE
= PTHREAD_MUTEX_RECURSIVE_NP
,
53 PTHREAD_MUTEX_ERRORCHECK
= PTHREAD_MUTEX_ERRORCHECK_NP
,
54 PTHREAD_MUTEX_DEFAULT
= PTHREAD_MUTEX_NORMAL
57 /* For compatibility. */
58 , PTHREAD_MUTEX_FAST_NP
= PTHREAD_MUTEX_TIMED_NP
64 /* Robust mutex or not flags. */
67 PTHREAD_MUTEX_STALLED
,
68 PTHREAD_MUTEX_STALLED_NP
= PTHREAD_MUTEX_STALLED
,
70 PTHREAD_MUTEX_ROBUST_NP
= PTHREAD_MUTEX_ROBUST
75 #if defined __USE_POSIX199506 || defined __USE_UNIX98
76 /* Mutex protocols. */
86 #if __PTHREAD_MUTEX_HAVE_PREV
87 # define PTHREAD_MUTEX_INITIALIZER \
88 { { 0, 0, 0, 0, 0, __PTHREAD_SPINS, { 0, 0 } } }
90 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
91 { { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, __PTHREAD_SPINS, { 0, 0 } } }
92 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
93 { { 0, 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, __PTHREAD_SPINS, { 0, 0 } } }
94 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
95 { { 0, 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, __PTHREAD_SPINS, { 0, 0 } } }
99 # define PTHREAD_MUTEX_INITIALIZER \
100 { { 0, 0, 0, 0, 0, { __PTHREAD_SPINS } } }
102 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
103 { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { __PTHREAD_SPINS } } }
104 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
105 { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { __PTHREAD_SPINS } } }
106 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
107 { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { __PTHREAD_SPINS } } }
113 /* Read-write lock types. */
114 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
117 PTHREAD_RWLOCK_PREFER_READER_NP
,
118 PTHREAD_RWLOCK_PREFER_WRITER_NP
,
119 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
,
120 PTHREAD_RWLOCK_DEFAULT_NP
= PTHREAD_RWLOCK_PREFER_READER_NP
123 /* Define __PTHREAD_RWLOCK_INT_FLAGS_SHARED to 1 if pthread_rwlock_t
124 has the shared field. All 64-bit architectures have the shared field
125 in pthread_rwlock_t. */
126 #ifndef __PTHREAD_RWLOCK_INT_FLAGS_SHARED
127 # if __WORDSIZE == 64
128 # define __PTHREAD_RWLOCK_INT_FLAGS_SHARED 1
132 /* Read-write lock initializers. */
133 # define PTHREAD_RWLOCK_INITIALIZER \
134 { { 0, 0, 0, 0, 0, 0, 0, 0, __PTHREAD_RWLOCK_ELISION_EXTRA, 0, 0 } }
136 # ifdef __PTHREAD_RWLOCK_INT_FLAGS_SHARED
137 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
138 { { 0, 0, 0, 0, 0, 0, 0, 0, __PTHREAD_RWLOCK_ELISION_EXTRA, 0, \
139 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
141 # if __BYTE_ORDER == __LITTLE_ENDIAN
142 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
143 { { 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, \
144 0, __PTHREAD_RWLOCK_ELISION_EXTRA, 0, 0 } }
146 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
147 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,\
152 #endif /* Unix98 or XOpen2K */
155 /* Scheduler inheritance. */
158 PTHREAD_INHERIT_SCHED
,
159 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
160 PTHREAD_EXPLICIT_SCHED
161 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
165 /* Scope handling. */
168 PTHREAD_SCOPE_SYSTEM
,
169 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
170 PTHREAD_SCOPE_PROCESS
171 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
175 /* Process shared or private flag. */
178 PTHREAD_PROCESS_PRIVATE
,
179 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
180 PTHREAD_PROCESS_SHARED
181 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
186 /* Conditional variable handling. */
187 #define PTHREAD_COND_INITIALIZER { { {0}, {0}, {0, 0}, {0, 0}, 0, 0, {0, 0} } }
190 /* Cleanup buffers */
191 struct _pthread_cleanup_buffer
193 void (*__routine
) (void *); /* Function to call. */
194 void *__arg
; /* Its argument. */
195 int __canceltype
; /* Saved cancellation type. */
196 struct _pthread_cleanup_buffer
*__prev
; /* Chaining of cleanup functions. */
202 PTHREAD_CANCEL_ENABLE
,
203 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
204 PTHREAD_CANCEL_DISABLE
205 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
209 PTHREAD_CANCEL_DEFERRED
,
210 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
211 PTHREAD_CANCEL_ASYNCHRONOUS
212 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
214 #define PTHREAD_CANCELED ((void *) -1)
217 /* Single execution handling. */
218 #define PTHREAD_ONCE_INIT 0
222 /* Value returned by 'pthread_barrier_wait' for one of the threads after
223 the required number of threads have called this function.
224 -1 is distinct from 0 and all errno constants */
225 # define PTHREAD_BARRIER_SERIAL_THREAD -1
231 /* Create a new thread, starting with execution of START-ROUTINE
232 getting passed ARG. Creation attributed come from ATTR. The new
233 handle is stored in *NEWTHREAD. */
234 extern int pthread_create (pthread_t
*__restrict __newthread
,
235 const pthread_attr_t
*__restrict __attr
,
236 void *(*__start_routine
) (void *),
237 void *__restrict __arg
) __THROWNL
__nonnull ((1, 3));
239 /* Terminate calling thread.
241 The registered cleanup handlers are called via exception handling
242 so we cannot mark this function with __THROW.*/
243 extern void pthread_exit (void *__retval
) __attribute__ ((__noreturn__
));
245 /* Make calling thread wait for termination of the thread TH. The
246 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
249 This function is a cancellation point and therefore not marked with
251 extern int pthread_join (pthread_t __th
, void **__thread_return
);
254 /* Check whether thread TH has terminated. If yes return the status of
255 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
256 extern int pthread_tryjoin_np (pthread_t __th
, void **__thread_return
) __THROW
;
258 /* Make calling thread wait for termination of the thread TH, but only
259 until TIMEOUT. The exit status of the thread is stored in
260 *THREAD_RETURN, if THREAD_RETURN is not NULL.
262 This function is a cancellation point and therefore not marked with
264 extern int pthread_timedjoin_np (pthread_t __th
, void **__thread_return
,
265 const struct timespec
*__abstime
);
268 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
269 The resources of TH will therefore be freed immediately when it
270 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
272 extern int pthread_detach (pthread_t __th
) __THROW
;
275 /* Obtain the identifier of the current thread. */
276 extern pthread_t
pthread_self (void) __THROW
__attribute__ ((__const__
));
278 /* Compare two thread identifiers. */
279 extern int pthread_equal (pthread_t __thread1
, pthread_t __thread2
)
280 __THROW
__attribute__ ((__const__
));
283 /* Thread attribute handling. */
285 /* Initialize thread attribute *ATTR with default attributes
286 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
287 no user-provided stack). */
288 extern int pthread_attr_init (pthread_attr_t
*__attr
) __THROW
__nonnull ((1));
290 /* Destroy thread attribute *ATTR. */
291 extern int pthread_attr_destroy (pthread_attr_t
*__attr
)
292 __THROW
__nonnull ((1));
294 /* Get detach state attribute. */
295 extern int pthread_attr_getdetachstate (const pthread_attr_t
*__attr
,
297 __THROW
__nonnull ((1, 2));
299 /* Set detach state attribute. */
300 extern int pthread_attr_setdetachstate (pthread_attr_t
*__attr
,
302 __THROW
__nonnull ((1));
305 /* Get the size of the guard area created for stack overflow protection. */
306 extern int pthread_attr_getguardsize (const pthread_attr_t
*__attr
,
308 __THROW
__nonnull ((1, 2));
310 /* Set the size of the guard area created for stack overflow protection. */
311 extern int pthread_attr_setguardsize (pthread_attr_t
*__attr
,
313 __THROW
__nonnull ((1));
316 /* Return in *PARAM the scheduling parameters of *ATTR. */
317 extern int pthread_attr_getschedparam (const pthread_attr_t
*__restrict __attr
,
318 struct sched_param
*__restrict __param
)
319 __THROW
__nonnull ((1, 2));
321 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
322 extern int pthread_attr_setschedparam (pthread_attr_t
*__restrict __attr
,
323 const struct sched_param
*__restrict
324 __param
) __THROW
__nonnull ((1, 2));
326 /* Return in *POLICY the scheduling policy of *ATTR. */
327 extern int pthread_attr_getschedpolicy (const pthread_attr_t
*__restrict
328 __attr
, int *__restrict __policy
)
329 __THROW
__nonnull ((1, 2));
331 /* Set scheduling policy in *ATTR according to POLICY. */
332 extern int pthread_attr_setschedpolicy (pthread_attr_t
*__attr
, int __policy
)
333 __THROW
__nonnull ((1));
335 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
336 extern int pthread_attr_getinheritsched (const pthread_attr_t
*__restrict
337 __attr
, int *__restrict __inherit
)
338 __THROW
__nonnull ((1, 2));
340 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
341 extern int pthread_attr_setinheritsched (pthread_attr_t
*__attr
,
343 __THROW
__nonnull ((1));
346 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
347 extern int pthread_attr_getscope (const pthread_attr_t
*__restrict __attr
,
348 int *__restrict __scope
)
349 __THROW
__nonnull ((1, 2));
351 /* Set scheduling contention scope in *ATTR according to SCOPE. */
352 extern int pthread_attr_setscope (pthread_attr_t
*__attr
, int __scope
)
353 __THROW
__nonnull ((1));
355 /* Return the previously set address for the stack. */
356 extern int pthread_attr_getstackaddr (const pthread_attr_t
*__restrict
357 __attr
, void **__restrict __stackaddr
)
358 __THROW
__nonnull ((1, 2)) __attribute_deprecated__
;
360 /* Set the starting address of the stack of the thread to be created.
361 Depending on whether the stack grows up or down the value must either
362 be higher or lower than all the address in the memory block. The
363 minimal size of the block must be PTHREAD_STACK_MIN. */
364 extern int pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
366 __THROW
__nonnull ((1)) __attribute_deprecated__
;
368 /* Return the currently used minimal stack size. */
369 extern int pthread_attr_getstacksize (const pthread_attr_t
*__restrict
370 __attr
, size_t *__restrict __stacksize
)
371 __THROW
__nonnull ((1, 2));
373 /* Add information about the minimum stack size needed for the thread
374 to be started. This size must never be less than PTHREAD_STACK_MIN
375 and must also not exceed the system limits. */
376 extern int pthread_attr_setstacksize (pthread_attr_t
*__attr
,
378 __THROW
__nonnull ((1));
381 /* Return the previously set address for the stack. */
382 extern int pthread_attr_getstack (const pthread_attr_t
*__restrict __attr
,
383 void **__restrict __stackaddr
,
384 size_t *__restrict __stacksize
)
385 __THROW
__nonnull ((1, 2, 3));
387 /* The following two interfaces are intended to replace the last two. They
388 require setting the address as well as the size since only setting the
389 address will make the implementation on some architectures impossible. */
390 extern int pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
391 size_t __stacksize
) __THROW
__nonnull ((1));
395 /* Thread created with attribute ATTR will be limited to run only on
396 the processors represented in CPUSET. */
397 extern int pthread_attr_setaffinity_np (pthread_attr_t
*__attr
,
399 const cpu_set_t
*__cpuset
)
400 __THROW
__nonnull ((1, 3));
402 /* Get bit set in CPUSET representing the processors threads created with
404 extern int pthread_attr_getaffinity_np (const pthread_attr_t
*__attr
,
407 __THROW
__nonnull ((1, 3));
409 /* Get the default attributes used by pthread_create in this process. */
410 extern int pthread_getattr_default_np (pthread_attr_t
*__attr
)
411 __THROW
__nonnull ((1));
413 /* Set the default attributes to be used by pthread_create in this
415 extern int pthread_setattr_default_np (const pthread_attr_t
*__attr
)
416 __THROW
__nonnull ((1));
418 /* Initialize thread attribute *ATTR with attributes corresponding to the
419 already running thread TH. It shall be called on uninitialized ATTR
420 and destroyed with pthread_attr_destroy when no longer needed. */
421 extern int pthread_getattr_np (pthread_t __th
, pthread_attr_t
*__attr
)
422 __THROW
__nonnull ((2));
426 /* Functions for scheduling control. */
428 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
430 extern int pthread_setschedparam (pthread_t __target_thread
, int __policy
,
431 const struct sched_param
*__param
)
432 __THROW
__nonnull ((3));
434 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
435 extern int pthread_getschedparam (pthread_t __target_thread
,
436 int *__restrict __policy
,
437 struct sched_param
*__restrict __param
)
438 __THROW
__nonnull ((2, 3));
440 /* Set the scheduling priority for TARGET_THREAD. */
441 extern int pthread_setschedprio (pthread_t __target_thread
, int __prio
)
446 /* Get thread name visible in the kernel and its interfaces. */
447 extern int pthread_getname_np (pthread_t __target_thread
, char *__buf
,
449 __THROW
__nonnull ((2));
451 /* Set thread name visible in the kernel and its interfaces. */
452 extern int pthread_setname_np (pthread_t __target_thread
, const char *__name
)
453 __THROW
__nonnull ((2));
458 /* Determine level of concurrency. */
459 extern int pthread_getconcurrency (void) __THROW
;
461 /* Set new concurrency level to LEVEL. */
462 extern int pthread_setconcurrency (int __level
) __THROW
;
466 /* Yield the processor to another thread or process.
467 This function is similar to the POSIX `sched_yield' function but
468 might be differently implemented in the case of a m-on-n thread
470 extern int pthread_yield (void) __THROW
;
473 /* Limit specified thread TH to run only on the processors represented
475 extern int pthread_setaffinity_np (pthread_t __th
, size_t __cpusetsize
,
476 const cpu_set_t
*__cpuset
)
477 __THROW
__nonnull ((3));
479 /* Get bit set in CPUSET representing the processors TH can run on. */
480 extern int pthread_getaffinity_np (pthread_t __th
, size_t __cpusetsize
,
482 __THROW
__nonnull ((3));
486 /* Functions for handling initialization. */
488 /* Guarantee that the initialization function INIT_ROUTINE will be called
489 only once, even if pthread_once is executed several times with the
490 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
491 extern variable initialized to PTHREAD_ONCE_INIT.
493 The initialization functions might throw exception which is why
494 this function is not marked with __THROW. */
495 extern int pthread_once (pthread_once_t
*__once_control
,
496 void (*__init_routine
) (void)) __nonnull ((1, 2));
499 /* Functions for handling cancellation.
501 Note that these functions are explicitly not marked to not throw an
502 exception in C++ code. If cancellation is implemented by unwinding
503 this is necessary to have the compiler generate the unwind information. */
505 /* Set cancelability state of current thread to STATE, returning old
506 state in *OLDSTATE if OLDSTATE is not NULL. */
507 extern int pthread_setcancelstate (int __state
, int *__oldstate
);
509 /* Set cancellation state of current thread to TYPE, returning the old
510 type in *OLDTYPE if OLDTYPE is not NULL. */
511 extern int pthread_setcanceltype (int __type
, int *__oldtype
);
513 /* Cancel THREAD immediately or at the next possibility. */
514 extern int pthread_cancel (pthread_t __th
);
516 /* Test for pending cancellation for the current thread and terminate
517 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
519 extern void pthread_testcancel (void);
522 /* Cancellation handling with integration into exception handling. */
528 __jmp_buf __cancel_jmp_buf
;
529 int __mask_was_saved
;
530 } __cancel_jmp_buf
[1];
532 } __pthread_unwind_buf_t
__attribute__ ((__aligned__
));
534 /* No special attributes by default. */
535 #ifndef __cleanup_fct_attribute
536 # define __cleanup_fct_attribute
540 /* Structure to hold the cleanup handler information. */
541 struct __pthread_cleanup_frame
543 void (*__cancel_routine
) (void *);
549 #if defined __GNUC__ && defined __EXCEPTIONS
551 /* Class to handle cancellation handler invocation. */
552 class __pthread_cleanup_class
554 void (*__cancel_routine
) (void *);
560 __pthread_cleanup_class (void (*__fct
) (void *), void *__arg
)
561 : __cancel_routine (__fct
), __cancel_arg (__arg
), __do_it (1) { }
562 ~__pthread_cleanup_class () { if (__do_it
) __cancel_routine (__cancel_arg
); }
563 void __setdoit (int __newval
) { __do_it
= __newval
; }
564 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
,
566 void __restore () const { pthread_setcanceltype (__cancel_type
, 0); }
569 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
570 when the thread is canceled or calls pthread_exit. ROUTINE will also
571 be called with arguments ARG when the matching pthread_cleanup_pop
572 is executed with non-zero EXECUTE argument.
574 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
575 be used in matching pairs at the same nesting level of braces. */
576 # define pthread_cleanup_push(routine, arg) \
578 __pthread_cleanup_class __clframe (routine, arg)
580 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
581 If EXECUTE is non-zero, the handler function is called. */
582 # define pthread_cleanup_pop(execute) \
583 __clframe.__setdoit (execute); \
587 /* Install a cleanup handler as pthread_cleanup_push does, but also
588 saves the current cancellation type and sets it to deferred
590 # define pthread_cleanup_push_defer_np(routine, arg) \
592 __pthread_cleanup_class __clframe (routine, arg); \
595 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
596 restores the cancellation type that was in effect when the matching
597 pthread_cleanup_push_defer was called. */
598 # define pthread_cleanup_pop_restore_np(execute) \
599 __clframe.__restore (); \
600 __clframe.__setdoit (execute); \
604 /* Function called to call the cleanup handler. As an extern inline
605 function the compiler is free to decide inlining the change when
606 needed or fall back on the copy which must exist somewhere
609 __pthread_cleanup_routine (struct __pthread_cleanup_frame
*__frame
)
611 if (__frame
->__do_it
)
612 __frame
->__cancel_routine (__frame
->__cancel_arg
);
615 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
616 when the thread is canceled or calls pthread_exit. ROUTINE will also
617 be called with arguments ARG when the matching pthread_cleanup_pop
618 is executed with non-zero EXECUTE argument.
620 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
621 be used in matching pairs at the same nesting level of braces. */
622 # define pthread_cleanup_push(routine, arg) \
624 struct __pthread_cleanup_frame __clframe \
625 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
626 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
629 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
630 If EXECUTE is non-zero, the handler function is called. */
631 # define pthread_cleanup_pop(execute) \
632 __clframe.__do_it = (execute); \
636 /* Install a cleanup handler as pthread_cleanup_push does, but also
637 saves the current cancellation type and sets it to deferred
639 # define pthread_cleanup_push_defer_np(routine, arg) \
641 struct __pthread_cleanup_frame __clframe \
642 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
643 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
645 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
646 &__clframe.__cancel_type)
648 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
649 restores the cancellation type that was in effect when the matching
650 pthread_cleanup_push_defer was called. */
651 # define pthread_cleanup_pop_restore_np(execute) \
652 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
653 __clframe.__do_it = (execute); \
658 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
659 when the thread is canceled or calls pthread_exit. ROUTINE will also
660 be called with arguments ARG when the matching pthread_cleanup_pop
661 is executed with non-zero EXECUTE argument.
663 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
664 be used in matching pairs at the same nesting level of braces. */
665 # define pthread_cleanup_push(routine, arg) \
667 __pthread_unwind_buf_t __cancel_buf; \
668 void (*__cancel_routine) (void *) = (routine); \
669 void *__cancel_arg = (arg); \
670 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
671 __cancel_buf.__cancel_jmp_buf, 0); \
672 if (__glibc_unlikely (__not_first_call)) \
674 __cancel_routine (__cancel_arg); \
675 __pthread_unwind_next (&__cancel_buf); \
679 __pthread_register_cancel (&__cancel_buf); \
681 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
682 __cleanup_fct_attribute
;
684 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
685 If EXECUTE is non-zero, the handler function is called. */
686 # define pthread_cleanup_pop(execute) \
687 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
689 __pthread_unregister_cancel (&__cancel_buf); \
691 __cancel_routine (__cancel_arg); \
693 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
694 __cleanup_fct_attribute
;
697 /* Install a cleanup handler as pthread_cleanup_push does, but also
698 saves the current cancellation type and sets it to deferred
700 # define pthread_cleanup_push_defer_np(routine, arg) \
702 __pthread_unwind_buf_t __cancel_buf; \
703 void (*__cancel_routine) (void *) = (routine); \
704 void *__cancel_arg = (arg); \
705 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
706 __cancel_buf.__cancel_jmp_buf, 0); \
707 if (__glibc_unlikely (__not_first_call)) \
709 __cancel_routine (__cancel_arg); \
710 __pthread_unwind_next (&__cancel_buf); \
714 __pthread_register_cancel_defer (&__cancel_buf); \
716 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t
*__buf
)
717 __cleanup_fct_attribute
;
719 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
720 restores the cancellation type that was in effect when the matching
721 pthread_cleanup_push_defer was called. */
722 # define pthread_cleanup_pop_restore_np(execute) \
723 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
725 __pthread_unregister_cancel_restore (&__cancel_buf); \
727 __cancel_routine (__cancel_arg); \
729 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t
*__buf
)
730 __cleanup_fct_attribute
;
733 /* Internal interface to initiate cleanup. */
734 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
735 __cleanup_fct_attribute
__attribute__ ((__noreturn__
))
737 __attribute__ ((__weak__
))
742 /* Function used in the macros. */
743 struct __jmp_buf_tag
;
744 extern int __sigsetjmp (struct __jmp_buf_tag
*__env
, int __savemask
) __THROWNL
;
747 /* Mutex handling. */
749 /* Initialize a mutex. */
750 extern int pthread_mutex_init (pthread_mutex_t
*__mutex
,
751 const pthread_mutexattr_t
*__mutexattr
)
752 __THROW
__nonnull ((1));
754 /* Destroy a mutex. */
755 extern int pthread_mutex_destroy (pthread_mutex_t
*__mutex
)
756 __THROW
__nonnull ((1));
758 /* Try locking a mutex. */
759 extern int pthread_mutex_trylock (pthread_mutex_t
*__mutex
)
760 __THROWNL
__nonnull ((1));
763 extern int pthread_mutex_lock (pthread_mutex_t
*__mutex
)
764 __THROWNL
__nonnull ((1));
767 /* Wait until lock becomes available, or specified time passes. */
768 extern int pthread_mutex_timedlock (pthread_mutex_t
*__restrict __mutex
,
769 const struct timespec
*__restrict
770 __abstime
) __THROWNL
__nonnull ((1, 2));
773 /* Unlock a mutex. */
774 extern int pthread_mutex_unlock (pthread_mutex_t
*__mutex
)
775 __THROWNL
__nonnull ((1));
778 /* Get the priority ceiling of MUTEX. */
779 extern int pthread_mutex_getprioceiling (const pthread_mutex_t
*
781 int *__restrict __prioceiling
)
782 __THROW
__nonnull ((1, 2));
784 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
785 priority ceiling value in *OLD_CEILING. */
786 extern int pthread_mutex_setprioceiling (pthread_mutex_t
*__restrict __mutex
,
788 int *__restrict __old_ceiling
)
789 __THROW
__nonnull ((1, 3));
792 #ifdef __USE_XOPEN2K8
793 /* Declare the state protected by MUTEX as consistent. */
794 extern int pthread_mutex_consistent (pthread_mutex_t
*__mutex
)
795 __THROW
__nonnull ((1));
797 extern int pthread_mutex_consistent_np (pthread_mutex_t
*__mutex
)
798 __THROW
__nonnull ((1));
803 /* Functions for handling mutex attributes. */
805 /* Initialize mutex attribute object ATTR with default attributes
806 (kind is PTHREAD_MUTEX_TIMED_NP). */
807 extern int pthread_mutexattr_init (pthread_mutexattr_t
*__attr
)
808 __THROW
__nonnull ((1));
810 /* Destroy mutex attribute object ATTR. */
811 extern int pthread_mutexattr_destroy (pthread_mutexattr_t
*__attr
)
812 __THROW
__nonnull ((1));
814 /* Get the process-shared flag of the mutex attribute ATTR. */
815 extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t
*
817 int *__restrict __pshared
)
818 __THROW
__nonnull ((1, 2));
820 /* Set the process-shared flag of the mutex attribute ATTR. */
821 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t
*__attr
,
823 __THROW
__nonnull ((1));
825 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
826 /* Return in *KIND the mutex kind attribute in *ATTR. */
827 extern int pthread_mutexattr_gettype (const pthread_mutexattr_t
*__restrict
828 __attr
, int *__restrict __kind
)
829 __THROW
__nonnull ((1, 2));
831 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
832 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
833 PTHREAD_MUTEX_DEFAULT). */
834 extern int pthread_mutexattr_settype (pthread_mutexattr_t
*__attr
, int __kind
)
835 __THROW
__nonnull ((1));
838 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
839 extern int pthread_mutexattr_getprotocol (const pthread_mutexattr_t
*
841 int *__restrict __protocol
)
842 __THROW
__nonnull ((1, 2));
844 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
845 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
846 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t
*__attr
,
848 __THROW
__nonnull ((1));
850 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
851 extern int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t
*
853 int *__restrict __prioceiling
)
854 __THROW
__nonnull ((1, 2));
856 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
857 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t
*__attr
,
859 __THROW
__nonnull ((1));
862 /* Get the robustness flag of the mutex attribute ATTR. */
863 extern int pthread_mutexattr_getrobust (const pthread_mutexattr_t
*__attr
,
865 __THROW
__nonnull ((1, 2));
867 extern int pthread_mutexattr_getrobust_np (const pthread_mutexattr_t
*__attr
,
869 __THROW
__nonnull ((1, 2));
872 /* Set the robustness flag of the mutex attribute ATTR. */
873 extern int pthread_mutexattr_setrobust (pthread_mutexattr_t
*__attr
,
875 __THROW
__nonnull ((1));
877 extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t
*__attr
,
879 __THROW
__nonnull ((1));
884 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
885 /* Functions for handling read-write locks. */
887 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
888 the default values if later is NULL. */
889 extern int pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
890 const pthread_rwlockattr_t
*__restrict
891 __attr
) __THROW
__nonnull ((1));
893 /* Destroy read-write lock RWLOCK. */
894 extern int pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
)
895 __THROW
__nonnull ((1));
897 /* Acquire read lock for RWLOCK. */
898 extern int pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
)
899 __THROWNL
__nonnull ((1));
901 /* Try to acquire read lock for RWLOCK. */
902 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
)
903 __THROWNL
__nonnull ((1));
905 # ifdef __USE_XOPEN2K
906 /* Try to acquire read lock for RWLOCK or return after specfied time. */
907 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t
*__restrict __rwlock
,
908 const struct timespec
*__restrict
909 __abstime
) __THROWNL
__nonnull ((1, 2));
912 /* Acquire write lock for RWLOCK. */
913 extern int pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
)
914 __THROWNL
__nonnull ((1));
916 /* Try to acquire write lock for RWLOCK. */
917 extern int pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
)
918 __THROWNL
__nonnull ((1));
920 # ifdef __USE_XOPEN2K
921 /* Try to acquire write lock for RWLOCK or return after specfied time. */
922 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t
*__restrict __rwlock
,
923 const struct timespec
*__restrict
924 __abstime
) __THROWNL
__nonnull ((1, 2));
928 extern int pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
)
929 __THROWNL
__nonnull ((1));
932 /* Functions for handling read-write lock attributes. */
934 /* Initialize attribute object ATTR with default values. */
935 extern int pthread_rwlockattr_init (pthread_rwlockattr_t
*__attr
)
936 __THROW
__nonnull ((1));
938 /* Destroy attribute object ATTR. */
939 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t
*__attr
)
940 __THROW
__nonnull ((1));
942 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
943 extern int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t
*
945 int *__restrict __pshared
)
946 __THROW
__nonnull ((1, 2));
948 /* Set process-shared attribute of ATTR to PSHARED. */
949 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t
*__attr
,
951 __THROW
__nonnull ((1));
953 /* Return current setting of reader/writer preference. */
954 extern int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t
*
956 int *__restrict __pref
)
957 __THROW
__nonnull ((1, 2));
959 /* Set reader/write preference. */
960 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t
*__attr
,
961 int __pref
) __THROW
__nonnull ((1));
965 /* Functions for handling conditional variables. */
967 /* Initialize condition variable COND using attributes ATTR, or use
968 the default values if later is NULL. */
969 extern int pthread_cond_init (pthread_cond_t
*__restrict __cond
,
970 const pthread_condattr_t
*__restrict __cond_attr
)
971 __THROW
__nonnull ((1));
973 /* Destroy condition variable COND. */
974 extern int pthread_cond_destroy (pthread_cond_t
*__cond
)
975 __THROW
__nonnull ((1));
977 /* Wake up one thread waiting for condition variable COND. */
978 extern int pthread_cond_signal (pthread_cond_t
*__cond
)
979 __THROWNL
__nonnull ((1));
981 /* Wake up all threads waiting for condition variables COND. */
982 extern int pthread_cond_broadcast (pthread_cond_t
*__cond
)
983 __THROWNL
__nonnull ((1));
985 /* Wait for condition variable COND to be signaled or broadcast.
986 MUTEX is assumed to be locked before.
988 This function is a cancellation point and therefore not marked with
990 extern int pthread_cond_wait (pthread_cond_t
*__restrict __cond
,
991 pthread_mutex_t
*__restrict __mutex
)
994 /* Wait for condition variable COND to be signaled or broadcast until
995 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
996 absolute time specification; zero is the beginning of the epoch
997 (00:00:00 GMT, January 1, 1970).
999 This function is a cancellation point and therefore not marked with
1001 extern int pthread_cond_timedwait (pthread_cond_t
*__restrict __cond
,
1002 pthread_mutex_t
*__restrict __mutex
,
1003 const struct timespec
*__restrict __abstime
)
1004 __nonnull ((1, 2, 3));
1006 /* Functions for handling condition variable attributes. */
1008 /* Initialize condition variable attribute ATTR. */
1009 extern int pthread_condattr_init (pthread_condattr_t
*__attr
)
1010 __THROW
__nonnull ((1));
1012 /* Destroy condition variable attribute ATTR. */
1013 extern int pthread_condattr_destroy (pthread_condattr_t
*__attr
)
1014 __THROW
__nonnull ((1));
1016 /* Get the process-shared flag of the condition variable attribute ATTR. */
1017 extern int pthread_condattr_getpshared (const pthread_condattr_t
*
1019 int *__restrict __pshared
)
1020 __THROW
__nonnull ((1, 2));
1022 /* Set the process-shared flag of the condition variable attribute ATTR. */
1023 extern int pthread_condattr_setpshared (pthread_condattr_t
*__attr
,
1024 int __pshared
) __THROW
__nonnull ((1));
1026 #ifdef __USE_XOPEN2K
1027 /* Get the clock selected for the condition variable attribute ATTR. */
1028 extern int pthread_condattr_getclock (const pthread_condattr_t
*
1030 __clockid_t
*__restrict __clock_id
)
1031 __THROW
__nonnull ((1, 2));
1033 /* Set the clock selected for the condition variable attribute ATTR. */
1034 extern int pthread_condattr_setclock (pthread_condattr_t
*__attr
,
1035 __clockid_t __clock_id
)
1036 __THROW
__nonnull ((1));
1040 #ifdef __USE_XOPEN2K
1041 /* Functions to handle spinlocks. */
1043 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
1044 be shared between different processes. */
1045 extern int pthread_spin_init (pthread_spinlock_t
*__lock
, int __pshared
)
1046 __THROW
__nonnull ((1));
1048 /* Destroy the spinlock LOCK. */
1049 extern int pthread_spin_destroy (pthread_spinlock_t
*__lock
)
1050 __THROW
__nonnull ((1));
1052 /* Wait until spinlock LOCK is retrieved. */
1053 extern int pthread_spin_lock (pthread_spinlock_t
*__lock
)
1054 __THROWNL
__nonnull ((1));
1056 /* Try to lock spinlock LOCK. */
1057 extern int pthread_spin_trylock (pthread_spinlock_t
*__lock
)
1058 __THROWNL
__nonnull ((1));
1060 /* Release spinlock LOCK. */
1061 extern int pthread_spin_unlock (pthread_spinlock_t
*__lock
)
1062 __THROWNL
__nonnull ((1));
1065 /* Functions to handle barriers. */
1067 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1068 opened when COUNT waiters arrived. */
1069 extern int pthread_barrier_init (pthread_barrier_t
*__restrict __barrier
,
1070 const pthread_barrierattr_t
*__restrict
1071 __attr
, unsigned int __count
)
1072 __THROW
__nonnull ((1));
1074 /* Destroy a previously dynamically initialized barrier BARRIER. */
1075 extern int pthread_barrier_destroy (pthread_barrier_t
*__barrier
)
1076 __THROW
__nonnull ((1));
1078 /* Wait on barrier BARRIER. */
1079 extern int pthread_barrier_wait (pthread_barrier_t
*__barrier
)
1080 __THROWNL
__nonnull ((1));
1083 /* Initialize barrier attribute ATTR. */
1084 extern int pthread_barrierattr_init (pthread_barrierattr_t
*__attr
)
1085 __THROW
__nonnull ((1));
1087 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1088 extern int pthread_barrierattr_destroy (pthread_barrierattr_t
*__attr
)
1089 __THROW
__nonnull ((1));
1091 /* Get the process-shared flag of the barrier attribute ATTR. */
1092 extern int pthread_barrierattr_getpshared (const pthread_barrierattr_t
*
1094 int *__restrict __pshared
)
1095 __THROW
__nonnull ((1, 2));
1097 /* Set the process-shared flag of the barrier attribute ATTR. */
1098 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t
*__attr
,
1100 __THROW
__nonnull ((1));
1104 /* Functions for handling thread-specific data. */
1106 /* Create a key value identifying a location in the thread-specific
1107 data area. Each thread maintains a distinct thread-specific data
1108 area. DESTR_FUNCTION, if non-NULL, is called with the value
1109 associated to that key when the key is destroyed.
1110 DESTR_FUNCTION is not called if the value associated is NULL when
1111 the key is destroyed. */
1112 extern int pthread_key_create (pthread_key_t
*__key
,
1113 void (*__destr_function
) (void *))
1114 __THROW
__nonnull ((1));
1117 extern int pthread_key_delete (pthread_key_t __key
) __THROW
;
1119 /* Return current value of the thread-specific data slot identified by KEY. */
1120 extern void *pthread_getspecific (pthread_key_t __key
) __THROW
;
1122 /* Store POINTER in the thread-specific data slot identified by KEY. */
1123 extern int pthread_setspecific (pthread_key_t __key
,
1124 const void *__pointer
) __THROW
;
1127 #ifdef __USE_XOPEN2K
1128 /* Get ID of CPU-time clock for thread THREAD_ID. */
1129 extern int pthread_getcpuclockid (pthread_t __thread_id
,
1130 __clockid_t
*__clock_id
)
1131 __THROW
__nonnull ((2));
1135 /* Install handlers to be called when a new process is created with FORK.
1136 The PREPARE handler is called in the parent process just before performing
1137 FORK. The PARENT handler is called in the parent process just after FORK.
1138 The CHILD handler is called in the child process. Each of the three
1139 handlers can be NULL, meaning that no handler needs to be called at that
1141 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1142 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1143 first called before FORK), and the PARENT and CHILD handlers are called
1144 in FIFO (first added, first called). */
1146 extern int pthread_atfork (void (*__prepare
) (void),
1147 void (*__parent
) (void),
1148 void (*__child
) (void)) __THROW
;
1151 #ifdef __USE_EXTERN_INLINES
1152 /* Optimizations. */
1154 __NTH (pthread_equal (pthread_t __thread1
, pthread_t __thread2
))
1156 return __thread1
== __thread2
;
1162 #endif /* pthread.h */