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>
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 #define PTHREAD_MUTEX_INITIALIZER \
87 { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_TIMED_NP) } }
89 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
90 { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_RECURSIVE_NP) } }
91 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
92 { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ERRORCHECK_NP) } }
93 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
94 { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ADAPTIVE_NP) } }
98 /* Read-write lock types. */
99 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
102 PTHREAD_RWLOCK_PREFER_READER_NP
,
103 PTHREAD_RWLOCK_PREFER_WRITER_NP
,
104 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
,
105 PTHREAD_RWLOCK_DEFAULT_NP
= PTHREAD_RWLOCK_PREFER_READER_NP
109 /* Read-write lock initializers. */
110 # define PTHREAD_RWLOCK_INITIALIZER \
111 { { __PTHREAD_RWLOCK_INITIALIZER (PTHREAD_RWLOCK_DEFAULT_NP) } }
113 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
114 { { __PTHREAD_RWLOCK_INITIALIZER (PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) } }
116 #endif /* Unix98 or XOpen2K */
119 /* Scheduler inheritance. */
122 PTHREAD_INHERIT_SCHED
,
123 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
124 PTHREAD_EXPLICIT_SCHED
125 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
129 /* Scope handling. */
132 PTHREAD_SCOPE_SYSTEM
,
133 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
134 PTHREAD_SCOPE_PROCESS
135 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
139 /* Process shared or private flag. */
142 PTHREAD_PROCESS_PRIVATE
,
143 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
144 PTHREAD_PROCESS_SHARED
145 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
150 /* Conditional variable handling. */
151 #define PTHREAD_COND_INITIALIZER { { {0}, {0}, {0, 0}, {0, 0}, 0, 0, {0, 0} } }
154 /* Cleanup buffers */
155 struct _pthread_cleanup_buffer
157 void (*__routine
) (void *); /* Function to call. */
158 void *__arg
; /* Its argument. */
159 int __canceltype
; /* Saved cancellation type. */
160 struct _pthread_cleanup_buffer
*__prev
; /* Chaining of cleanup functions. */
166 PTHREAD_CANCEL_ENABLE
,
167 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
168 PTHREAD_CANCEL_DISABLE
169 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
173 PTHREAD_CANCEL_DEFERRED
,
174 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
175 PTHREAD_CANCEL_ASYNCHRONOUS
176 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
178 #define PTHREAD_CANCELED ((void *) -1)
181 /* Single execution handling. */
182 #define PTHREAD_ONCE_INIT 0
186 /* Value returned by 'pthread_barrier_wait' for one of the threads after
187 the required number of threads have called this function.
188 -1 is distinct from 0 and all errno constants */
189 # define PTHREAD_BARRIER_SERIAL_THREAD -1
195 /* Create a new thread, starting with execution of START-ROUTINE
196 getting passed ARG. Creation attributed come from ATTR. The new
197 handle is stored in *NEWTHREAD. */
198 extern int pthread_create (pthread_t
*__restrict __newthread
,
199 const pthread_attr_t
*__restrict __attr
,
200 void *(*__start_routine
) (void *),
201 void *__restrict __arg
) __THROWNL
__nonnull ((1, 3));
203 /* Terminate calling thread.
205 The registered cleanup handlers are called via exception handling
206 so we cannot mark this function with __THROW.*/
207 extern void pthread_exit (void *__retval
) __attribute__ ((__noreturn__
));
209 /* Make calling thread wait for termination of the thread TH. The
210 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
213 This function is a cancellation point and therefore not marked with
215 extern int pthread_join (pthread_t __th
, void **__thread_return
);
218 /* Check whether thread TH has terminated. If yes return the status of
219 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
220 extern int pthread_tryjoin_np (pthread_t __th
, void **__thread_return
) __THROW
;
222 /* Make calling thread wait for termination of the thread TH, but only
223 until TIMEOUT. The exit status of the thread is stored in
224 *THREAD_RETURN, if THREAD_RETURN is not NULL.
226 This function is a cancellation point and therefore not marked with
228 extern int pthread_timedjoin_np (pthread_t __th
, void **__thread_return
,
229 const struct timespec
*__abstime
);
231 /* Make calling thread wait for termination of the thread TH, but only
232 until TIMEOUT measured against the clock specified by CLOCKID. The
233 exit status of the thread is stored in *THREAD_RETURN, if
234 THREAD_RETURN is not NULL.
236 This function is a cancellation point and therefore not marked with
238 extern int pthread_clockjoin_np (pthread_t __th
, void **__thread_return
,
240 const struct timespec
*__abstime
);
243 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
244 The resources of TH will therefore be freed immediately when it
245 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
247 extern int pthread_detach (pthread_t __th
) __THROW
;
250 /* Obtain the identifier of the current thread. */
251 extern pthread_t
pthread_self (void) __THROW
__attribute__ ((__const__
));
253 /* Compare two thread identifiers. */
254 extern int pthread_equal (pthread_t __thread1
, pthread_t __thread2
)
255 __THROW
__attribute__ ((__const__
));
258 /* Thread attribute handling. */
260 /* Initialize thread attribute *ATTR with default attributes
261 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
262 no user-provided stack). */
263 extern int pthread_attr_init (pthread_attr_t
*__attr
) __THROW
__nonnull ((1));
265 /* Destroy thread attribute *ATTR. */
266 extern int pthread_attr_destroy (pthread_attr_t
*__attr
)
267 __THROW
__nonnull ((1));
269 /* Get detach state attribute. */
270 extern int pthread_attr_getdetachstate (const pthread_attr_t
*__attr
,
272 __THROW
__nonnull ((1, 2));
274 /* Set detach state attribute. */
275 extern int pthread_attr_setdetachstate (pthread_attr_t
*__attr
,
277 __THROW
__nonnull ((1));
280 /* Get the size of the guard area created for stack overflow protection. */
281 extern int pthread_attr_getguardsize (const pthread_attr_t
*__attr
,
283 __THROW
__nonnull ((1, 2));
285 /* Set the size of the guard area created for stack overflow protection. */
286 extern int pthread_attr_setguardsize (pthread_attr_t
*__attr
,
288 __THROW
__nonnull ((1));
291 /* Return in *PARAM the scheduling parameters of *ATTR. */
292 extern int pthread_attr_getschedparam (const pthread_attr_t
*__restrict __attr
,
293 struct sched_param
*__restrict __param
)
294 __THROW
__nonnull ((1, 2));
296 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
297 extern int pthread_attr_setschedparam (pthread_attr_t
*__restrict __attr
,
298 const struct sched_param
*__restrict
299 __param
) __THROW
__nonnull ((1, 2));
301 /* Return in *POLICY the scheduling policy of *ATTR. */
302 extern int pthread_attr_getschedpolicy (const pthread_attr_t
*__restrict
303 __attr
, int *__restrict __policy
)
304 __THROW
__nonnull ((1, 2));
306 /* Set scheduling policy in *ATTR according to POLICY. */
307 extern int pthread_attr_setschedpolicy (pthread_attr_t
*__attr
, int __policy
)
308 __THROW
__nonnull ((1));
310 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
311 extern int pthread_attr_getinheritsched (const pthread_attr_t
*__restrict
312 __attr
, int *__restrict __inherit
)
313 __THROW
__nonnull ((1, 2));
315 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
316 extern int pthread_attr_setinheritsched (pthread_attr_t
*__attr
,
318 __THROW
__nonnull ((1));
321 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
322 extern int pthread_attr_getscope (const pthread_attr_t
*__restrict __attr
,
323 int *__restrict __scope
)
324 __THROW
__nonnull ((1, 2));
326 /* Set scheduling contention scope in *ATTR according to SCOPE. */
327 extern int pthread_attr_setscope (pthread_attr_t
*__attr
, int __scope
)
328 __THROW
__nonnull ((1));
330 /* Return the previously set address for the stack. */
331 extern int pthread_attr_getstackaddr (const pthread_attr_t
*__restrict
332 __attr
, void **__restrict __stackaddr
)
333 __THROW
__nonnull ((1, 2)) __attribute_deprecated__
;
335 /* Set the starting address of the stack of the thread to be created.
336 Depending on whether the stack grows up or down the value must either
337 be higher or lower than all the address in the memory block. The
338 minimal size of the block must be PTHREAD_STACK_MIN. */
339 extern int pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
341 __THROW
__nonnull ((1)) __attribute_deprecated__
;
343 /* Return the currently used minimal stack size. */
344 extern int pthread_attr_getstacksize (const pthread_attr_t
*__restrict
345 __attr
, size_t *__restrict __stacksize
)
346 __THROW
__nonnull ((1, 2));
348 /* Add information about the minimum stack size needed for the thread
349 to be started. This size must never be less than PTHREAD_STACK_MIN
350 and must also not exceed the system limits. */
351 extern int pthread_attr_setstacksize (pthread_attr_t
*__attr
,
353 __THROW
__nonnull ((1));
356 /* Return the previously set address for the stack. */
357 extern int pthread_attr_getstack (const pthread_attr_t
*__restrict __attr
,
358 void **__restrict __stackaddr
,
359 size_t *__restrict __stacksize
)
360 __THROW
__nonnull ((1, 2, 3));
362 /* The following two interfaces are intended to replace the last two. They
363 require setting the address as well as the size since only setting the
364 address will make the implementation on some architectures impossible. */
365 extern int pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
366 size_t __stacksize
) __THROW
__nonnull ((1));
370 /* Thread created with attribute ATTR will be limited to run only on
371 the processors represented in CPUSET. */
372 extern int pthread_attr_setaffinity_np (pthread_attr_t
*__attr
,
374 const cpu_set_t
*__cpuset
)
375 __THROW
__nonnull ((1, 3));
377 /* Get bit set in CPUSET representing the processors threads created with
379 extern int pthread_attr_getaffinity_np (const pthread_attr_t
*__attr
,
382 __THROW
__nonnull ((1, 3));
384 /* Get the default attributes used by pthread_create in this process. */
385 extern int pthread_getattr_default_np (pthread_attr_t
*__attr
)
386 __THROW
__nonnull ((1));
388 /* Set the default attributes to be used by pthread_create in this
390 extern int pthread_setattr_default_np (const pthread_attr_t
*__attr
)
391 __THROW
__nonnull ((1));
393 /* Initialize thread attribute *ATTR with attributes corresponding to the
394 already running thread TH. It shall be called on uninitialized ATTR
395 and destroyed with pthread_attr_destroy when no longer needed. */
396 extern int pthread_getattr_np (pthread_t __th
, pthread_attr_t
*__attr
)
397 __THROW
__nonnull ((2));
401 /* Functions for scheduling control. */
403 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
405 extern int pthread_setschedparam (pthread_t __target_thread
, int __policy
,
406 const struct sched_param
*__param
)
407 __THROW
__nonnull ((3));
409 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
410 extern int pthread_getschedparam (pthread_t __target_thread
,
411 int *__restrict __policy
,
412 struct sched_param
*__restrict __param
)
413 __THROW
__nonnull ((2, 3));
415 /* Set the scheduling priority for TARGET_THREAD. */
416 extern int pthread_setschedprio (pthread_t __target_thread
, int __prio
)
421 /* Get thread name visible in the kernel and its interfaces. */
422 extern int pthread_getname_np (pthread_t __target_thread
, char *__buf
,
424 __THROW
__nonnull ((2));
426 /* Set thread name visible in the kernel and its interfaces. */
427 extern int pthread_setname_np (pthread_t __target_thread
, const char *__name
)
428 __THROW
__nonnull ((2));
433 /* Determine level of concurrency. */
434 extern int pthread_getconcurrency (void) __THROW
;
436 /* Set new concurrency level to LEVEL. */
437 extern int pthread_setconcurrency (int __level
) __THROW
;
441 /* Yield the processor to another thread or process.
442 This function is similar to the POSIX `sched_yield' function but
443 might be differently implemented in the case of a m-on-n thread
445 extern int pthread_yield (void) __THROW
;
448 /* Limit specified thread TH to run only on the processors represented
450 extern int pthread_setaffinity_np (pthread_t __th
, size_t __cpusetsize
,
451 const cpu_set_t
*__cpuset
)
452 __THROW
__nonnull ((3));
454 /* Get bit set in CPUSET representing the processors TH can run on. */
455 extern int pthread_getaffinity_np (pthread_t __th
, size_t __cpusetsize
,
457 __THROW
__nonnull ((3));
461 /* Functions for handling initialization. */
463 /* Guarantee that the initialization function INIT_ROUTINE will be called
464 only once, even if pthread_once is executed several times with the
465 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
466 extern variable initialized to PTHREAD_ONCE_INIT.
468 The initialization functions might throw exception which is why
469 this function is not marked with __THROW. */
470 extern int pthread_once (pthread_once_t
*__once_control
,
471 void (*__init_routine
) (void)) __nonnull ((1, 2));
474 /* Functions for handling cancellation.
476 Note that these functions are explicitly not marked to not throw an
477 exception in C++ code. If cancellation is implemented by unwinding
478 this is necessary to have the compiler generate the unwind information. */
480 /* Set cancelability state of current thread to STATE, returning old
481 state in *OLDSTATE if OLDSTATE is not NULL. */
482 extern int pthread_setcancelstate (int __state
, int *__oldstate
);
484 /* Set cancellation state of current thread to TYPE, returning the old
485 type in *OLDTYPE if OLDTYPE is not NULL. */
486 extern int pthread_setcanceltype (int __type
, int *__oldtype
);
488 /* Cancel THREAD immediately or at the next possibility. */
489 extern int pthread_cancel (pthread_t __th
);
491 /* Test for pending cancellation for the current thread and terminate
492 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
494 extern void pthread_testcancel (void);
497 /* Cancellation handling with integration into exception handling. */
503 __jmp_buf __cancel_jmp_buf
;
504 int __mask_was_saved
;
505 } __cancel_jmp_buf
[1];
507 } __pthread_unwind_buf_t
__attribute__ ((__aligned__
));
509 /* No special attributes by default. */
510 #ifndef __cleanup_fct_attribute
511 # define __cleanup_fct_attribute
515 /* Structure to hold the cleanup handler information. */
516 struct __pthread_cleanup_frame
518 void (*__cancel_routine
) (void *);
524 #if defined __GNUC__ && defined __EXCEPTIONS
526 /* Class to handle cancellation handler invocation. */
527 class __pthread_cleanup_class
529 void (*__cancel_routine
) (void *);
535 __pthread_cleanup_class (void (*__fct
) (void *), void *__arg
)
536 : __cancel_routine (__fct
), __cancel_arg (__arg
), __do_it (1) { }
537 ~__pthread_cleanup_class () { if (__do_it
) __cancel_routine (__cancel_arg
); }
538 void __setdoit (int __newval
) { __do_it
= __newval
; }
539 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
,
541 void __restore () const { pthread_setcanceltype (__cancel_type
, 0); }
544 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
545 when the thread is canceled or calls pthread_exit. ROUTINE will also
546 be called with arguments ARG when the matching pthread_cleanup_pop
547 is executed with non-zero EXECUTE argument.
549 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
550 be used in matching pairs at the same nesting level of braces. */
551 # define pthread_cleanup_push(routine, arg) \
553 __pthread_cleanup_class __clframe (routine, arg)
555 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
556 If EXECUTE is non-zero, the handler function is called. */
557 # define pthread_cleanup_pop(execute) \
558 __clframe.__setdoit (execute); \
562 /* Install a cleanup handler as pthread_cleanup_push does, but also
563 saves the current cancellation type and sets it to deferred
565 # define pthread_cleanup_push_defer_np(routine, arg) \
567 __pthread_cleanup_class __clframe (routine, arg); \
570 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
571 restores the cancellation type that was in effect when the matching
572 pthread_cleanup_push_defer was called. */
573 # define pthread_cleanup_pop_restore_np(execute) \
574 __clframe.__restore (); \
575 __clframe.__setdoit (execute); \
579 /* Function called to call the cleanup handler. As an extern inline
580 function the compiler is free to decide inlining the change when
581 needed or fall back on the copy which must exist somewhere
584 __pthread_cleanup_routine (struct __pthread_cleanup_frame
*__frame
)
586 if (__frame
->__do_it
)
587 __frame
->__cancel_routine (__frame
->__cancel_arg
);
590 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
591 when the thread is canceled or calls pthread_exit. ROUTINE will also
592 be called with arguments ARG when the matching pthread_cleanup_pop
593 is executed with non-zero EXECUTE argument.
595 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
596 be used in matching pairs at the same nesting level of braces. */
597 # define pthread_cleanup_push(routine, arg) \
599 struct __pthread_cleanup_frame __clframe \
600 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
601 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
604 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
605 If EXECUTE is non-zero, the handler function is called. */
606 # define pthread_cleanup_pop(execute) \
607 __clframe.__do_it = (execute); \
611 /* Install a cleanup handler as pthread_cleanup_push does, but also
612 saves the current cancellation type and sets it to deferred
614 # define pthread_cleanup_push_defer_np(routine, arg) \
616 struct __pthread_cleanup_frame __clframe \
617 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
618 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
620 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
621 &__clframe.__cancel_type)
623 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
624 restores the cancellation type that was in effect when the matching
625 pthread_cleanup_push_defer was called. */
626 # define pthread_cleanup_pop_restore_np(execute) \
627 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
628 __clframe.__do_it = (execute); \
633 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
634 when the thread is canceled or calls pthread_exit. ROUTINE will also
635 be called with arguments ARG when the matching pthread_cleanup_pop
636 is executed with non-zero EXECUTE argument.
638 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
639 be used in matching pairs at the same nesting level of braces. */
640 # define pthread_cleanup_push(routine, arg) \
642 __pthread_unwind_buf_t __cancel_buf; \
643 void (*__cancel_routine) (void *) = (routine); \
644 void *__cancel_arg = (arg); \
645 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
646 __cancel_buf.__cancel_jmp_buf, 0); \
647 if (__glibc_unlikely (__not_first_call)) \
649 __cancel_routine (__cancel_arg); \
650 __pthread_unwind_next (&__cancel_buf); \
654 __pthread_register_cancel (&__cancel_buf); \
656 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
657 __cleanup_fct_attribute
;
659 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
660 If EXECUTE is non-zero, the handler function is called. */
661 # define pthread_cleanup_pop(execute) \
662 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
664 __pthread_unregister_cancel (&__cancel_buf); \
666 __cancel_routine (__cancel_arg); \
668 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
669 __cleanup_fct_attribute
;
672 /* Install a cleanup handler as pthread_cleanup_push does, but also
673 saves the current cancellation type and sets it to deferred
675 # define pthread_cleanup_push_defer_np(routine, arg) \
677 __pthread_unwind_buf_t __cancel_buf; \
678 void (*__cancel_routine) (void *) = (routine); \
679 void *__cancel_arg = (arg); \
680 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
681 __cancel_buf.__cancel_jmp_buf, 0); \
682 if (__glibc_unlikely (__not_first_call)) \
684 __cancel_routine (__cancel_arg); \
685 __pthread_unwind_next (&__cancel_buf); \
689 __pthread_register_cancel_defer (&__cancel_buf); \
691 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t
*__buf
)
692 __cleanup_fct_attribute
;
694 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
695 restores the cancellation type that was in effect when the matching
696 pthread_cleanup_push_defer was called. */
697 # define pthread_cleanup_pop_restore_np(execute) \
698 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
700 __pthread_unregister_cancel_restore (&__cancel_buf); \
702 __cancel_routine (__cancel_arg); \
704 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t
*__buf
)
705 __cleanup_fct_attribute
;
708 /* Internal interface to initiate cleanup. */
709 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
710 __cleanup_fct_attribute
__attribute__ ((__noreturn__
))
712 __attribute__ ((__weak__
))
717 /* Function used in the macros. */
718 struct __jmp_buf_tag
;
719 extern int __sigsetjmp (struct __jmp_buf_tag
*__env
, int __savemask
) __THROWNL
;
722 /* Mutex handling. */
724 /* Initialize a mutex. */
725 extern int pthread_mutex_init (pthread_mutex_t
*__mutex
,
726 const pthread_mutexattr_t
*__mutexattr
)
727 __THROW
__nonnull ((1));
729 /* Destroy a mutex. */
730 extern int pthread_mutex_destroy (pthread_mutex_t
*__mutex
)
731 __THROW
__nonnull ((1));
733 /* Try locking a mutex. */
734 extern int pthread_mutex_trylock (pthread_mutex_t
*__mutex
)
735 __THROWNL
__nonnull ((1));
738 extern int pthread_mutex_lock (pthread_mutex_t
*__mutex
)
739 __THROWNL
__nonnull ((1));
742 /* Wait until lock becomes available, or specified time passes. */
743 extern int pthread_mutex_timedlock (pthread_mutex_t
*__restrict __mutex
,
744 const struct timespec
*__restrict
745 __abstime
) __THROWNL
__nonnull ((1, 2));
749 extern int pthread_mutex_clocklock (pthread_mutex_t
*__restrict __mutex
,
751 const struct timespec
*__restrict
752 __abstime
) __THROWNL
__nonnull ((1, 3));
755 /* Unlock a mutex. */
756 extern int pthread_mutex_unlock (pthread_mutex_t
*__mutex
)
757 __THROWNL
__nonnull ((1));
760 /* Get the priority ceiling of MUTEX. */
761 extern int pthread_mutex_getprioceiling (const pthread_mutex_t
*
763 int *__restrict __prioceiling
)
764 __THROW
__nonnull ((1, 2));
766 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
767 priority ceiling value in *OLD_CEILING. */
768 extern int pthread_mutex_setprioceiling (pthread_mutex_t
*__restrict __mutex
,
770 int *__restrict __old_ceiling
)
771 __THROW
__nonnull ((1, 3));
774 #ifdef __USE_XOPEN2K8
775 /* Declare the state protected by MUTEX as consistent. */
776 extern int pthread_mutex_consistent (pthread_mutex_t
*__mutex
)
777 __THROW
__nonnull ((1));
779 extern int pthread_mutex_consistent_np (pthread_mutex_t
*__mutex
)
780 __THROW
__nonnull ((1));
785 /* Functions for handling mutex attributes. */
787 /* Initialize mutex attribute object ATTR with default attributes
788 (kind is PTHREAD_MUTEX_TIMED_NP). */
789 extern int pthread_mutexattr_init (pthread_mutexattr_t
*__attr
)
790 __THROW
__nonnull ((1));
792 /* Destroy mutex attribute object ATTR. */
793 extern int pthread_mutexattr_destroy (pthread_mutexattr_t
*__attr
)
794 __THROW
__nonnull ((1));
796 /* Get the process-shared flag of the mutex attribute ATTR. */
797 extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t
*
799 int *__restrict __pshared
)
800 __THROW
__nonnull ((1, 2));
802 /* Set the process-shared flag of the mutex attribute ATTR. */
803 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t
*__attr
,
805 __THROW
__nonnull ((1));
807 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
808 /* Return in *KIND the mutex kind attribute in *ATTR. */
809 extern int pthread_mutexattr_gettype (const pthread_mutexattr_t
*__restrict
810 __attr
, int *__restrict __kind
)
811 __THROW
__nonnull ((1, 2));
813 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
814 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
815 PTHREAD_MUTEX_DEFAULT). */
816 extern int pthread_mutexattr_settype (pthread_mutexattr_t
*__attr
, int __kind
)
817 __THROW
__nonnull ((1));
820 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
821 extern int pthread_mutexattr_getprotocol (const pthread_mutexattr_t
*
823 int *__restrict __protocol
)
824 __THROW
__nonnull ((1, 2));
826 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
827 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
828 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t
*__attr
,
830 __THROW
__nonnull ((1));
832 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
833 extern int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t
*
835 int *__restrict __prioceiling
)
836 __THROW
__nonnull ((1, 2));
838 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
839 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t
*__attr
,
841 __THROW
__nonnull ((1));
844 /* Get the robustness flag of the mutex attribute ATTR. */
845 extern int pthread_mutexattr_getrobust (const pthread_mutexattr_t
*__attr
,
847 __THROW
__nonnull ((1, 2));
849 extern int pthread_mutexattr_getrobust_np (const pthread_mutexattr_t
*__attr
,
851 __THROW
__nonnull ((1, 2));
854 /* Set the robustness flag of the mutex attribute ATTR. */
855 extern int pthread_mutexattr_setrobust (pthread_mutexattr_t
*__attr
,
857 __THROW
__nonnull ((1));
859 extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t
*__attr
,
861 __THROW
__nonnull ((1));
866 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
867 /* Functions for handling read-write locks. */
869 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
870 the default values if later is NULL. */
871 extern int pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
872 const pthread_rwlockattr_t
*__restrict
873 __attr
) __THROW
__nonnull ((1));
875 /* Destroy read-write lock RWLOCK. */
876 extern int pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
)
877 __THROW
__nonnull ((1));
879 /* Acquire read lock for RWLOCK. */
880 extern int pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
)
881 __THROWNL
__nonnull ((1));
883 /* Try to acquire read lock for RWLOCK. */
884 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
)
885 __THROWNL
__nonnull ((1));
887 # ifdef __USE_XOPEN2K
888 /* Try to acquire read lock for RWLOCK or return after specfied time. */
889 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t
*__restrict __rwlock
,
890 const struct timespec
*__restrict
891 __abstime
) __THROWNL
__nonnull ((1, 2));
895 extern int pthread_rwlock_clockrdlock (pthread_rwlock_t
*__restrict __rwlock
,
897 const struct timespec
*__restrict
898 __abstime
) __THROWNL
__nonnull ((1, 3));
901 /* Acquire write lock for RWLOCK. */
902 extern int pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
)
903 __THROWNL
__nonnull ((1));
905 /* Try to acquire write lock for RWLOCK. */
906 extern int pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
)
907 __THROWNL
__nonnull ((1));
909 # ifdef __USE_XOPEN2K
910 /* Try to acquire write lock for RWLOCK or return after specfied time. */
911 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t
*__restrict __rwlock
,
912 const struct timespec
*__restrict
913 __abstime
) __THROWNL
__nonnull ((1, 2));
917 extern int pthread_rwlock_clockwrlock (pthread_rwlock_t
*__restrict __rwlock
,
919 const struct timespec
*__restrict
920 __abstime
) __THROWNL
__nonnull ((1, 3));
924 extern int pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
)
925 __THROWNL
__nonnull ((1));
928 /* Functions for handling read-write lock attributes. */
930 /* Initialize attribute object ATTR with default values. */
931 extern int pthread_rwlockattr_init (pthread_rwlockattr_t
*__attr
)
932 __THROW
__nonnull ((1));
934 /* Destroy attribute object ATTR. */
935 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t
*__attr
)
936 __THROW
__nonnull ((1));
938 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
939 extern int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t
*
941 int *__restrict __pshared
)
942 __THROW
__nonnull ((1, 2));
944 /* Set process-shared attribute of ATTR to PSHARED. */
945 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t
*__attr
,
947 __THROW
__nonnull ((1));
949 /* Return current setting of reader/writer preference. */
950 extern int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t
*
952 int *__restrict __pref
)
953 __THROW
__nonnull ((1, 2));
955 /* Set reader/write preference. */
956 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t
*__attr
,
957 int __pref
) __THROW
__nonnull ((1));
961 /* Functions for handling conditional variables. */
963 /* Initialize condition variable COND using attributes ATTR, or use
964 the default values if later is NULL. */
965 extern int pthread_cond_init (pthread_cond_t
*__restrict __cond
,
966 const pthread_condattr_t
*__restrict __cond_attr
)
967 __THROW
__nonnull ((1));
969 /* Destroy condition variable COND. */
970 extern int pthread_cond_destroy (pthread_cond_t
*__cond
)
971 __THROW
__nonnull ((1));
973 /* Wake up one thread waiting for condition variable COND. */
974 extern int pthread_cond_signal (pthread_cond_t
*__cond
)
975 __THROWNL
__nonnull ((1));
977 /* Wake up all threads waiting for condition variables COND. */
978 extern int pthread_cond_broadcast (pthread_cond_t
*__cond
)
979 __THROWNL
__nonnull ((1));
981 /* Wait for condition variable COND to be signaled or broadcast.
982 MUTEX is assumed to be locked before.
984 This function is a cancellation point and therefore not marked with
986 extern int pthread_cond_wait (pthread_cond_t
*__restrict __cond
,
987 pthread_mutex_t
*__restrict __mutex
)
990 /* Wait for condition variable COND to be signaled or broadcast until
991 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
992 absolute time specification; zero is the beginning of the epoch
993 (00:00:00 GMT, January 1, 1970).
995 This function is a cancellation point and therefore not marked with
997 extern int pthread_cond_timedwait (pthread_cond_t
*__restrict __cond
,
998 pthread_mutex_t
*__restrict __mutex
,
999 const struct timespec
*__restrict __abstime
)
1000 __nonnull ((1, 2, 3));
1003 /* Wait for condition variable COND to be signaled or broadcast until
1004 ABSTIME measured by the specified clock. MUTEX is assumed to be
1005 locked before. CLOCK is the clock to use. ABSTIME is an absolute
1006 time specification against CLOCK's epoch.
1008 This function is a cancellation point and therefore not marked with
1010 extern int pthread_cond_clockwait (pthread_cond_t
*__restrict __cond
,
1011 pthread_mutex_t
*__restrict __mutex
,
1012 __clockid_t __clock_id
,
1013 const struct timespec
*__restrict __abstime
)
1014 __nonnull ((1, 2, 4));
1017 /* Functions for handling condition variable attributes. */
1019 /* Initialize condition variable attribute ATTR. */
1020 extern int pthread_condattr_init (pthread_condattr_t
*__attr
)
1021 __THROW
__nonnull ((1));
1023 /* Destroy condition variable attribute ATTR. */
1024 extern int pthread_condattr_destroy (pthread_condattr_t
*__attr
)
1025 __THROW
__nonnull ((1));
1027 /* Get the process-shared flag of the condition variable attribute ATTR. */
1028 extern int pthread_condattr_getpshared (const pthread_condattr_t
*
1030 int *__restrict __pshared
)
1031 __THROW
__nonnull ((1, 2));
1033 /* Set the process-shared flag of the condition variable attribute ATTR. */
1034 extern int pthread_condattr_setpshared (pthread_condattr_t
*__attr
,
1035 int __pshared
) __THROW
__nonnull ((1));
1037 #ifdef __USE_XOPEN2K
1038 /* Get the clock selected for the condition variable attribute ATTR. */
1039 extern int pthread_condattr_getclock (const pthread_condattr_t
*
1041 __clockid_t
*__restrict __clock_id
)
1042 __THROW
__nonnull ((1, 2));
1044 /* Set the clock selected for the condition variable attribute ATTR. */
1045 extern int pthread_condattr_setclock (pthread_condattr_t
*__attr
,
1046 __clockid_t __clock_id
)
1047 __THROW
__nonnull ((1));
1051 #ifdef __USE_XOPEN2K
1052 /* Functions to handle spinlocks. */
1054 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
1055 be shared between different processes. */
1056 extern int pthread_spin_init (pthread_spinlock_t
*__lock
, int __pshared
)
1057 __THROW
__nonnull ((1));
1059 /* Destroy the spinlock LOCK. */
1060 extern int pthread_spin_destroy (pthread_spinlock_t
*__lock
)
1061 __THROW
__nonnull ((1));
1063 /* Wait until spinlock LOCK is retrieved. */
1064 extern int pthread_spin_lock (pthread_spinlock_t
*__lock
)
1065 __THROWNL
__nonnull ((1));
1067 /* Try to lock spinlock LOCK. */
1068 extern int pthread_spin_trylock (pthread_spinlock_t
*__lock
)
1069 __THROWNL
__nonnull ((1));
1071 /* Release spinlock LOCK. */
1072 extern int pthread_spin_unlock (pthread_spinlock_t
*__lock
)
1073 __THROWNL
__nonnull ((1));
1076 /* Functions to handle barriers. */
1078 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1079 opened when COUNT waiters arrived. */
1080 extern int pthread_barrier_init (pthread_barrier_t
*__restrict __barrier
,
1081 const pthread_barrierattr_t
*__restrict
1082 __attr
, unsigned int __count
)
1083 __THROW
__nonnull ((1));
1085 /* Destroy a previously dynamically initialized barrier BARRIER. */
1086 extern int pthread_barrier_destroy (pthread_barrier_t
*__barrier
)
1087 __THROW
__nonnull ((1));
1089 /* Wait on barrier BARRIER. */
1090 extern int pthread_barrier_wait (pthread_barrier_t
*__barrier
)
1091 __THROWNL
__nonnull ((1));
1094 /* Initialize barrier attribute ATTR. */
1095 extern int pthread_barrierattr_init (pthread_barrierattr_t
*__attr
)
1096 __THROW
__nonnull ((1));
1098 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1099 extern int pthread_barrierattr_destroy (pthread_barrierattr_t
*__attr
)
1100 __THROW
__nonnull ((1));
1102 /* Get the process-shared flag of the barrier attribute ATTR. */
1103 extern int pthread_barrierattr_getpshared (const pthread_barrierattr_t
*
1105 int *__restrict __pshared
)
1106 __THROW
__nonnull ((1, 2));
1108 /* Set the process-shared flag of the barrier attribute ATTR. */
1109 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t
*__attr
,
1111 __THROW
__nonnull ((1));
1115 /* Functions for handling thread-specific data. */
1117 /* Create a key value identifying a location in the thread-specific
1118 data area. Each thread maintains a distinct thread-specific data
1119 area. DESTR_FUNCTION, if non-NULL, is called with the value
1120 associated to that key when the key is destroyed.
1121 DESTR_FUNCTION is not called if the value associated is NULL when
1122 the key is destroyed. */
1123 extern int pthread_key_create (pthread_key_t
*__key
,
1124 void (*__destr_function
) (void *))
1125 __THROW
__nonnull ((1));
1128 extern int pthread_key_delete (pthread_key_t __key
) __THROW
;
1130 /* Return current value of the thread-specific data slot identified by KEY. */
1131 extern void *pthread_getspecific (pthread_key_t __key
) __THROW
;
1133 /* Store POINTER in the thread-specific data slot identified by KEY. */
1134 extern int pthread_setspecific (pthread_key_t __key
,
1135 const void *__pointer
) __THROW
;
1138 #ifdef __USE_XOPEN2K
1139 /* Get ID of CPU-time clock for thread THREAD_ID. */
1140 extern int pthread_getcpuclockid (pthread_t __thread_id
,
1141 __clockid_t
*__clock_id
)
1142 __THROW
__nonnull ((2));
1146 /* Install handlers to be called when a new process is created with FORK.
1147 The PREPARE handler is called in the parent process just before performing
1148 FORK. The PARENT handler is called in the parent process just after FORK.
1149 The CHILD handler is called in the child process. Each of the three
1150 handlers can be NULL, meaning that no handler needs to be called at that
1152 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1153 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1154 first called before FORK), and the PARENT and CHILD handlers are called
1155 in FIFO (first added, first called). */
1157 extern int pthread_atfork (void (*__prepare
) (void),
1158 void (*__parent
) (void),
1159 void (*__child
) (void)) __THROW
;
1162 #ifdef __USE_EXTERN_INLINES
1163 /* Optimizations. */
1165 __NTH (pthread_equal (pthread_t __thread1
, pthread_t __thread2
))
1167 return __thread1
== __thread2
;
1173 #endif /* pthread.h */