1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #define __need_sigset_t
30 #include <bits/pthreadtypes.h>
31 #include <bits/setjmp.h>
32 #include <bits/wordsize.h>
33 #if defined _LIBC && ( defined IS_IN_libc || defined NOT_IN_libc )
34 #include <bits/uClibc_pthread.h>
41 PTHREAD_CREATE_JOINABLE,
42 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
43 PTHREAD_CREATE_DETACHED
44 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
51 PTHREAD_MUTEX_TIMED_NP,
52 PTHREAD_MUTEX_RECURSIVE_NP,
53 PTHREAD_MUTEX_ERRORCHECK_NP,
54 PTHREAD_MUTEX_ADAPTIVE_NP
55 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
57 PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_TIMED_NP,
58 PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
59 PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
60 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
63 /* For compatibility. */
64 , PTHREAD_MUTEX_FAST_NP = PTHREAD_MUTEX_TIMED_NP
70 /* Robust mutex or not flags. */
73 PTHREAD_MUTEX_STALLED,
74 PTHREAD_MUTEX_STALLED_NP = PTHREAD_MUTEX_STALLED,
76 PTHREAD_MUTEX_ROBUST_NP = PTHREAD_MUTEX_ROBUST
82 /* Mutex protocols. */
92 /* Mutex initializers. */
94 # define PTHREAD_MUTEX_INITIALIZER \
95 { { 0, 0, 0, 0, 0, 0, { 0, 0 } } }
97 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
98 { { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { 0, 0 } } }
99 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
100 { { 0, 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { 0, 0 } } }
101 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
102 { { 0, 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { 0, 0 } } }
105 # define PTHREAD_MUTEX_INITIALIZER \
106 { { 0, 0, 0, 0, 0, { 0 } } }
108 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
109 { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { 0 } } }
110 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
111 { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { 0 } } }
112 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
113 { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { 0 } } }
118 /* Read-write lock types. */
119 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
122 PTHREAD_RWLOCK_PREFER_READER_NP,
123 PTHREAD_RWLOCK_PREFER_WRITER_NP,
124 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
125 PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_READER_NP
128 /* Read-write lock initializers. */
129 # define PTHREAD_RWLOCK_INITIALIZER \
130 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
132 # if __WORDSIZE == 64
133 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
134 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
135 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
137 # if __BYTE_ORDER == __LITTLE_ENDIAN
138 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
139 { { 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, \
142 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
143 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,\
148 #endif /* Unix98 or XOpen2K */
151 /* Scheduler inheritance. */
154 PTHREAD_INHERIT_SCHED,
155 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
156 PTHREAD_EXPLICIT_SCHED
157 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
161 /* Scope handling. */
164 PTHREAD_SCOPE_SYSTEM,
165 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
166 PTHREAD_SCOPE_PROCESS
167 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
171 /* Process shared or private flag. */
174 PTHREAD_PROCESS_PRIVATE,
175 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
176 PTHREAD_PROCESS_SHARED
177 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
182 /* Conditional variable handling. */
183 #define PTHREAD_COND_INITIALIZER { { 0, 0, 0, 0, 0, (void *) 0, 0, 0 } }
186 /* Cleanup buffers */
187 struct _pthread_cleanup_buffer
189 void (*__routine) (void *); /* Function to call. */
190 void *__arg; /* Its argument. */
191 int __canceltype; /* Saved cancellation type. */
192 struct _pthread_cleanup_buffer *__prev; /* Chaining of cleanup functions. */
198 PTHREAD_CANCEL_ENABLE,
199 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
200 PTHREAD_CANCEL_DISABLE
201 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
205 PTHREAD_CANCEL_DEFERRED,
206 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
207 PTHREAD_CANCEL_ASYNCHRONOUS
208 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
210 #define PTHREAD_CANCELED ((void *) -1)
213 /* Single execution handling. */
214 #define PTHREAD_ONCE_INIT 0
218 /* Value returned by 'pthread_barrier_wait' for one of the threads after
219 the required number of threads have called this function.
220 -1 is distinct from 0 and all errno constants */
221 # define PTHREAD_BARRIER_SERIAL_THREAD -1
227 /* Create a new thread, starting with execution of START-ROUTINE
228 getting passed ARG. Creation attributed come from ATTR. The new
229 handle is stored in *NEWTHREAD. */
230 extern int pthread_create (pthread_t *__restrict __newthread,
231 __const pthread_attr_t *__restrict __attr,
232 void *(*__start_routine) (void *),
233 void *__restrict __arg) __THROW __nonnull ((1, 3));
235 /* Terminate calling thread.
237 The registered cleanup handlers are called via exception handling
238 so we cannot mark this function with __THROW.*/
239 extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__));
241 /* Make calling thread wait for termination of the thread TH. The
242 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
245 This function is a cancellation point and therefore not marked with
247 extern int pthread_join (pthread_t __th, void **__thread_return);
250 /* Check whether thread TH has terminated. If yes return the status of
251 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
252 extern int pthread_tryjoin_np (pthread_t __th, void **__thread_return) __THROW;
254 /* Make calling thread wait for termination of the thread TH, but only
255 until TIMEOUT. The exit status of the thread is stored in
256 *THREAD_RETURN, if THREAD_RETURN is not NULL.
258 This function is a cancellation point and therefore not marked with
260 extern int pthread_timedjoin_np (pthread_t __th, void **__thread_return,
261 __const struct timespec *__abstime);
264 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
265 The resources of TH will therefore be freed immediately when it
266 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
268 extern int pthread_detach (pthread_t __th) __THROW;
271 /* Obtain the identifier of the current thread. */
272 extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__));
274 /* Compare two thread identifiers. */
275 extern int pthread_equal (pthread_t __thread1, pthread_t __thread2) __THROW;
278 /* Thread attribute handling. */
280 /* Initialize thread attribute *ATTR with default attributes
281 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
282 no user-provided stack). */
283 extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1));
285 /* Destroy thread attribute *ATTR. */
286 extern int pthread_attr_destroy (pthread_attr_t *__attr)
287 __THROW __nonnull ((1));
289 /* Get detach state attribute. */
290 extern int pthread_attr_getdetachstate (__const pthread_attr_t *__attr,
292 __THROW __nonnull ((1, 2));
294 /* Set detach state attribute. */
295 extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
297 __THROW __nonnull ((1));
300 /* Get the size of the guard area created for stack overflow protection. */
301 extern int pthread_attr_getguardsize (__const pthread_attr_t *__attr,
303 __THROW __nonnull ((1, 2));
305 /* Set the size of the guard area created for stack overflow protection. */
306 extern int pthread_attr_setguardsize (pthread_attr_t *__attr,
308 __THROW __nonnull ((1));
311 /* Return in *PARAM the scheduling parameters of *ATTR. */
312 extern int pthread_attr_getschedparam (__const pthread_attr_t *__restrict
314 struct sched_param *__restrict __param)
315 __THROW __nonnull ((1, 2));
317 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
318 extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,
319 __const struct sched_param *__restrict
320 __param) __THROW __nonnull ((1, 2));
322 /* Return in *POLICY the scheduling policy of *ATTR. */
323 extern int pthread_attr_getschedpolicy (__const pthread_attr_t *__restrict
324 __attr, int *__restrict __policy)
325 __THROW __nonnull ((1, 2));
327 /* Set scheduling policy in *ATTR according to POLICY. */
328 extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy)
329 __THROW __nonnull ((1));
331 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
332 extern int pthread_attr_getinheritsched (__const pthread_attr_t *__restrict
333 __attr, int *__restrict __inherit)
334 __THROW __nonnull ((1, 2));
336 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
337 extern int pthread_attr_setinheritsched (pthread_attr_t *__attr,
339 __THROW __nonnull ((1));
342 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
343 extern int pthread_attr_getscope (__const pthread_attr_t *__restrict __attr,
344 int *__restrict __scope)
345 __THROW __nonnull ((1, 2));
347 /* Set scheduling contention scope in *ATTR according to SCOPE. */
348 extern int pthread_attr_setscope (pthread_attr_t *__attr, int __scope)
349 __THROW __nonnull ((1));
351 /* Return the previously set address for the stack. */
352 extern int pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
353 __attr, void **__restrict __stackaddr)
354 __THROW __nonnull ((1, 2)) __attribute_deprecated__;
356 /* Set the starting address of the stack of the thread to be created.
357 Depending on whether the stack grows up or down the value must either
358 be higher or lower than all the address in the memory block. The
359 minimal size of the block must be PTHREAD_STACK_MIN. */
360 extern int pthread_attr_setstackaddr (pthread_attr_t *__attr,
362 __THROW __nonnull ((1)) __attribute_deprecated__;
364 /* Return the currently used minimal stack size. */
365 extern int pthread_attr_getstacksize (__const pthread_attr_t *__restrict
366 __attr, size_t *__restrict __stacksize)
367 __THROW __nonnull ((1, 2));
369 /* Add information about the minimum stack size needed for the thread
370 to be started. This size must never be less than PTHREAD_STACK_MIN
371 and must also not exceed the system limits. */
372 extern int pthread_attr_setstacksize (pthread_attr_t *__attr,
374 __THROW __nonnull ((1));
377 /* Return the previously set address for the stack. */
378 extern int pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
379 void **__restrict __stackaddr,
380 size_t *__restrict __stacksize)
381 __THROW __nonnull ((1, 2, 3));
383 /* The following two interfaces are intended to replace the last two. They
384 require setting the address as well as the size since only setting the
385 address will make the implementation on some architectures impossible. */
386 extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
387 size_t __stacksize) __THROW __nonnull ((1));
391 /* Thread created with attribute ATTR will be limited to run only on
392 the processors represented in CPUSET. */
393 extern int pthread_attr_setaffinity_np (pthread_attr_t *__attr,
395 __const cpu_set_t *__cpuset)
396 __THROW __nonnull ((1, 3));
398 /* Get bit set in CPUSET representing the processors threads created with
400 extern int pthread_attr_getaffinity_np (__const pthread_attr_t *__attr,
403 __THROW __nonnull ((1, 3));
406 /* Initialize thread attribute *ATTR with attributes corresponding to the
407 already running thread TH. It shall be called on uninitialized ATTR
408 and destroyed with pthread_attr_destroy when no longer needed. */
409 extern int pthread_getattr_np (pthread_t __th, pthread_attr_t *__attr)
410 __THROW __nonnull ((2));
414 /* Functions for scheduling control. */
416 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
418 extern int pthread_setschedparam (pthread_t __target_thread, int __policy,
419 __const struct sched_param *__param)
420 __THROW __nonnull ((3));
422 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
423 extern int pthread_getschedparam (pthread_t __target_thread,
424 int *__restrict __policy,
425 struct sched_param *__restrict __param)
426 __THROW __nonnull ((2, 3));
428 /* Set the scheduling priority for TARGET_THREAD. */
429 extern int pthread_setschedprio (pthread_t __target_thread, int __prio)
434 /* Determine level of concurrency. */
435 extern int pthread_getconcurrency (void) __THROW;
437 /* Set new concurrency level to LEVEL. */
438 extern int pthread_setconcurrency (int __level) __THROW;
442 /* Yield the processor to another thread or process.
443 This function is similar to the POSIX `sched_yield' function but
444 might be differently implemented in the case of a m-on-n thread
446 extern int pthread_yield (void) __THROW;
449 /* Limit specified thread TH to run only on the processors represented
451 extern int pthread_setaffinity_np (pthread_t __th, size_t __cpusetsize,
452 __const cpu_set_t *__cpuset)
453 __THROW __nonnull ((3));
455 /* Get bit set in CPUSET representing the processors TH can run on. */
456 extern int pthread_getaffinity_np (pthread_t __th, size_t __cpusetsize,
458 __THROW __nonnull ((3));
462 /* Functions for handling initialization. */
464 /* Guarantee that the initialization function INIT_ROUTINE will be called
465 only once, even if pthread_once is executed several times with the
466 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
467 extern variable initialized to PTHREAD_ONCE_INIT.
469 The initialization functions might throw exception which is why
470 this function is not marked with __THROW. */
471 extern int pthread_once (pthread_once_t *__once_control,
472 void (*__init_routine) (void)) __nonnull ((1, 2));
475 /* Functions for handling cancellation.
477 Note that these functions are explicitly not marked to not throw an
478 exception in C++ code. If cancellation is implemented by unwinding
479 this is necessary to have the compiler generate the unwind information. */
481 /* Set cancelability state of current thread to STATE, returning old
482 state in *OLDSTATE if OLDSTATE is not NULL. */
483 extern int pthread_setcancelstate (int __state, int *__oldstate);
485 /* Set cancellation state of current thread to TYPE, returning the old
486 type in *OLDTYPE if OLDTYPE is not NULL. */
487 extern int pthread_setcanceltype (int __type, int *__oldtype);
489 /* Cancel THREAD immediately or at the next possibility. */
490 extern int pthread_cancel (pthread_t __th);
492 /* Test for pending cancellation for the current thread and terminate
493 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
495 extern void pthread_testcancel (void);
498 /* Cancellation handling with integration into exception handling. */
504 __jmp_buf __cancel_jmp_buf;
505 int __mask_was_saved;
506 } __cancel_jmp_buf[1];
508 } __pthread_unwind_buf_t __attribute__ ((__aligned__));
510 /* No special attributes by default. */
511 #ifndef __cleanup_fct_attribute
512 # define __cleanup_fct_attribute
516 /* Structure to hold the cleanup handler information. */
517 struct __pthread_cleanup_frame
519 void (*__cancel_routine) (void *);
525 #if defined __GNUC__ && defined __EXCEPTIONS
527 /* Class to handle cancellation handler invocation. */
528 class __pthread_cleanup_class
530 void (*__cancel_routine) (void *);
536 __pthread_cleanup_class (void (*__fct) (void *), void *__arg)
537 : __cancel_routine (__fct), __cancel_arg (__arg), __do_it (1) { }
538 ~__pthread_cleanup_class () { if (__do_it) __cancel_routine (__cancel_arg); }
539 void __setdoit (int __newval) { __do_it = __newval; }
540 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED,
542 void __restore () const { pthread_setcanceltype (__cancel_type, 0); }
545 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
546 when the thread is canceled or calls pthread_exit. ROUTINE will also
547 be called with arguments ARG when the matching pthread_cleanup_pop
548 is executed with non-zero EXECUTE argument.
550 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
551 be used in matching pairs at the same nesting level of braces. */
552 # define pthread_cleanup_push(routine, arg) \
554 __pthread_cleanup_class __clframe (routine, arg)
556 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
557 If EXECUTE is non-zero, the handler function is called. */
558 # define pthread_cleanup_pop(execute) \
559 __clframe.__setdoit (execute); \
563 /* Install a cleanup handler as pthread_cleanup_push does, but also
564 saves the current cancellation type and sets it to deferred
566 # define pthread_cleanup_push_defer_np(routine, arg) \
568 __pthread_cleanup_class __clframe (routine, arg); \
571 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
572 restores the cancellation type that was in effect when the matching
573 pthread_cleanup_push_defer was called. */
574 # define pthread_cleanup_pop_restore_np(execute) \
575 __clframe.__restore (); \
576 __clframe.__setdoit (execute); \
580 /* Function called to call the cleanup handler. As an extern inline
581 function the compiler is free to decide inlining the change when
582 needed or fall back on the copy which must exist somewhere
585 __pthread_cleanup_routine (struct __pthread_cleanup_frame *__frame)
587 if (__frame->__do_it)
588 __frame->__cancel_routine (__frame->__cancel_arg);
591 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
592 when the thread is canceled or calls pthread_exit. ROUTINE will also
593 be called with arguments ARG when the matching pthread_cleanup_pop
594 is executed with non-zero EXECUTE argument.
596 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
597 be used in matching pairs at the same nesting level of braces. */
598 # define pthread_cleanup_push(routine, arg) \
600 struct __pthread_cleanup_frame __clframe \
601 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
602 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
605 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
606 If EXECUTE is non-zero, the handler function is called. */
607 # define pthread_cleanup_pop(execute) \
608 __clframe.__do_it = (execute); \
612 /* Install a cleanup handler as pthread_cleanup_push does, but also
613 saves the current cancellation type and sets it to deferred
615 # define pthread_cleanup_push_defer_np(routine, arg) \
617 struct __pthread_cleanup_frame __clframe \
618 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
619 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
621 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
622 &__clframe.__cancel_type)
624 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
625 restores the cancellation type that was in effect when the matching
626 pthread_cleanup_push_defer was called. */
627 # define pthread_cleanup_pop_restore_np(execute) \
628 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
629 __clframe.__do_it = (execute); \
634 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
635 when the thread is canceled or calls pthread_exit. ROUTINE will also
636 be called with arguments ARG when the matching pthread_cleanup_pop
637 is executed with non-zero EXECUTE argument.
639 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
640 be used in matching pairs at the same nesting level of braces. */
641 # define pthread_cleanup_push(routine, arg) \
643 __pthread_unwind_buf_t __cancel_buf; \
644 void (*__cancel_routine) (void *) = (routine); \
645 void *__cancel_arg = (arg); \
646 int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
647 __cancel_buf.__cancel_jmp_buf, 0); \
648 if (__builtin_expect (not_first_call, 0)) \
650 __cancel_routine (__cancel_arg); \
651 __pthread_unwind_next (&__cancel_buf); \
655 __pthread_register_cancel (&__cancel_buf); \
657 extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
658 __cleanup_fct_attribute;
660 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
661 If EXECUTE is non-zero, the handler function is called. */
662 # define pthread_cleanup_pop(execute) \
663 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
665 __pthread_unregister_cancel (&__cancel_buf); \
667 __cancel_routine (__cancel_arg); \
669 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
670 __cleanup_fct_attribute;
673 /* Install a cleanup handler as pthread_cleanup_push does, but also
674 saves the current cancellation type and sets it to deferred
676 # define pthread_cleanup_push_defer_np(routine, arg) \
678 __pthread_unwind_buf_t __cancel_buf; \
679 void (*__cancel_routine) (void *) = (routine); \
680 void *__cancel_arg = (arg); \
681 int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
682 __cancel_buf.__cancel_jmp_buf, 0); \
683 if (__builtin_expect (not_first_call, 0)) \
685 __cancel_routine (__cancel_arg); \
686 __pthread_unwind_next (&__cancel_buf); \
690 __pthread_register_cancel_defer (&__cancel_buf); \
692 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t *__buf)
693 __cleanup_fct_attribute;
695 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
696 restores the cancellation type that was in effect when the matching
697 pthread_cleanup_push_defer was called. */
698 # define pthread_cleanup_pop_restore_np(execute) \
699 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
701 __pthread_unregister_cancel_restore (&__cancel_buf); \
703 __cancel_routine (__cancel_arg); \
705 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t *__buf)
706 __cleanup_fct_attribute;
709 /* Internal interface to initiate cleanup. */
710 extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
711 __cleanup_fct_attribute __attribute__ ((__noreturn__))
713 __attribute__ ((__weak__))
718 /* Function used in the macros. */
719 struct __jmp_buf_tag;
720 extern int __sigsetjmp (struct __jmp_buf_tag *__env, int __savemask) __THROW;
723 /* Mutex handling. */
725 /* Initialize a mutex. */
726 extern int pthread_mutex_init (pthread_mutex_t *__mutex,
727 __const pthread_mutexattr_t *__mutexattr)
728 __THROW __nonnull ((1));
730 /* Destroy a mutex. */
731 extern int pthread_mutex_destroy (pthread_mutex_t *__mutex)
732 __THROW __nonnull ((1));
734 /* Try locking a mutex. */
735 extern int pthread_mutex_trylock (pthread_mutex_t *__mutex)
736 __THROW __nonnull ((1));
739 extern int pthread_mutex_lock (pthread_mutex_t *__mutex)
740 __THROW __nonnull ((1));
743 /* Wait until lock becomes available, or specified time passes. */
744 extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
745 __const struct timespec *__restrict
746 __abstime) __THROW __nonnull ((1, 2));
749 /* Unlock a mutex. */
750 extern int pthread_mutex_unlock (pthread_mutex_t *__mutex)
751 __THROW __nonnull ((1));
754 /* Get the priority ceiling of MUTEX. */
755 extern int pthread_mutex_getprioceiling (__const pthread_mutex_t *
757 int *__restrict __prioceiling)
758 __THROW __nonnull ((1, 2));
760 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
761 priority ceiling value in *OLD_CEILING. */
762 extern int pthread_mutex_setprioceiling (pthread_mutex_t *__restrict __mutex,
764 int *__restrict __old_ceiling)
765 __THROW __nonnull ((1, 3));
768 #ifdef __USE_XOPEN2K8
769 /* Declare the state protected by MUTEX as consistent. */
770 extern int pthread_mutex_consistent (pthread_mutex_t *__mutex)
771 __THROW __nonnull ((1));
773 extern int pthread_mutex_consistent_np (pthread_mutex_t *__mutex)
774 __THROW __nonnull ((1));
779 /* Functions for handling mutex attributes. */
781 /* Initialize mutex attribute object ATTR with default attributes
782 (kind is PTHREAD_MUTEX_TIMED_NP). */
783 extern int pthread_mutexattr_init (pthread_mutexattr_t *__attr)
784 __THROW __nonnull ((1));
786 /* Destroy mutex attribute object ATTR. */
787 extern int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr)
788 __THROW __nonnull ((1));
790 /* Get the process-shared flag of the mutex attribute ATTR. */
791 extern int pthread_mutexattr_getpshared (__const pthread_mutexattr_t *
793 int *__restrict __pshared)
794 __THROW __nonnull ((1, 2));
796 /* Set the process-shared flag of the mutex attribute ATTR. */
797 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
799 __THROW __nonnull ((1));
801 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
802 /* Return in *KIND the mutex kind attribute in *ATTR. */
803 extern int pthread_mutexattr_gettype (__const pthread_mutexattr_t *__restrict
804 __attr, int *__restrict __kind)
805 __THROW __nonnull ((1, 2));
807 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
808 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
809 PTHREAD_MUTEX_DEFAULT). */
810 extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
811 __THROW __nonnull ((1));
814 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
815 extern int pthread_mutexattr_getprotocol (__const pthread_mutexattr_t *
817 int *__restrict __protocol)
818 __THROW __nonnull ((1, 2));
820 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
821 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
822 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr,
824 __THROW __nonnull ((1));
826 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
827 extern int pthread_mutexattr_getprioceiling (__const pthread_mutexattr_t *
829 int *__restrict __prioceiling)
830 __THROW __nonnull ((1, 2));
832 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
833 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr,
835 __THROW __nonnull ((1));
838 /* Get the robustness flag of the mutex attribute ATTR. */
839 extern int pthread_mutexattr_getrobust (__const pthread_mutexattr_t *__attr,
841 __THROW __nonnull ((1, 2));
843 extern int pthread_mutexattr_getrobust_np (__const pthread_mutexattr_t *__attr,
845 __THROW __nonnull ((1, 2));
848 /* Set the robustness flag of the mutex attribute ATTR. */
849 extern int pthread_mutexattr_setrobust (pthread_mutexattr_t *__attr,
851 __THROW __nonnull ((1));
853 extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t *__attr,
855 __THROW __nonnull ((1));
860 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
861 /* Functions for handling read-write locks. */
863 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
864 the default values if later is NULL. */
865 extern int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
866 __const pthread_rwlockattr_t *__restrict
867 __attr) __THROW __nonnull ((1));
869 /* Destroy read-write lock RWLOCK. */
870 extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock)
871 __THROW __nonnull ((1));
873 /* Acquire read lock for RWLOCK. */
874 extern int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock)
875 __THROW __nonnull ((1));
877 /* Try to acquire read lock for RWLOCK. */
878 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock)
879 __THROW __nonnull ((1));
881 # ifdef __USE_XOPEN2K
882 /* Try to acquire read lock for RWLOCK or return after specfied time. */
883 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
884 __const struct timespec *__restrict
885 __abstime) __THROW __nonnull ((1, 2));
888 /* Acquire write lock for RWLOCK. */
889 extern int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock)
890 __THROW __nonnull ((1));
892 /* Try to acquire write lock for RWLOCK. */
893 extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock)
894 __THROW __nonnull ((1));
896 # ifdef __USE_XOPEN2K
897 /* Try to acquire write lock for RWLOCK or return after specfied time. */
898 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
899 __const struct timespec *__restrict
900 __abstime) __THROW __nonnull ((1, 2));
904 extern int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock)
905 __THROW __nonnull ((1));
908 /* Functions for handling read-write lock attributes. */
910 /* Initialize attribute object ATTR with default values. */
911 extern int pthread_rwlockattr_init (pthread_rwlockattr_t *__attr)
912 __THROW __nonnull ((1));
914 /* Destroy attribute object ATTR. */
915 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr)
916 __THROW __nonnull ((1));
918 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
919 extern int pthread_rwlockattr_getpshared (__const pthread_rwlockattr_t *
921 int *__restrict __pshared)
922 __THROW __nonnull ((1, 2));
924 /* Set process-shared attribute of ATTR to PSHARED. */
925 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr,
927 __THROW __nonnull ((1));
929 /* Return current setting of reader/writer preference. */
930 extern int pthread_rwlockattr_getkind_np (__const pthread_rwlockattr_t *
932 int *__restrict __pref)
933 __THROW __nonnull ((1, 2));
935 /* Set reader/write preference. */
936 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *__attr,
937 int __pref) __THROW __nonnull ((1));
941 /* Functions for handling conditional variables. */
943 /* Initialize condition variable COND using attributes ATTR, or use
944 the default values if later is NULL. */
945 extern int pthread_cond_init (pthread_cond_t *__restrict __cond,
946 __const pthread_condattr_t *__restrict
947 __cond_attr) __THROW __nonnull ((1));
949 /* Destroy condition variable COND. */
950 extern int pthread_cond_destroy (pthread_cond_t *__cond)
951 __THROW __nonnull ((1));
953 /* Wake up one thread waiting for condition variable COND. */
954 extern int pthread_cond_signal (pthread_cond_t *__cond)
955 __THROW __nonnull ((1));
957 /* Wake up all threads waiting for condition variables COND. */
958 extern int pthread_cond_broadcast (pthread_cond_t *__cond)
959 __THROW __nonnull ((1));
961 /* Wait for condition variable COND to be signaled or broadcast.
962 MUTEX is assumed to be locked before.
964 This function is a cancellation point and therefore not marked with
966 extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,
967 pthread_mutex_t *__restrict __mutex)
970 /* Wait for condition variable COND to be signaled or broadcast until
971 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
972 absolute time specification; zero is the beginning of the epoch
973 (00:00:00 GMT, January 1, 1970).
975 This function is a cancellation point and therefore not marked with
977 extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
978 pthread_mutex_t *__restrict __mutex,
979 __const struct timespec *__restrict
980 __abstime) __nonnull ((1, 2, 3));
982 /* Functions for handling condition variable attributes. */
984 /* Initialize condition variable attribute ATTR. */
985 extern int pthread_condattr_init (pthread_condattr_t *__attr)
986 __THROW __nonnull ((1));
988 /* Destroy condition variable attribute ATTR. */
989 extern int pthread_condattr_destroy (pthread_condattr_t *__attr)
990 __THROW __nonnull ((1));
992 /* Get the process-shared flag of the condition variable attribute ATTR. */
993 extern int pthread_condattr_getpshared (__const pthread_condattr_t *
995 int *__restrict __pshared)
996 __THROW __nonnull ((1, 2));
998 /* Set the process-shared flag of the condition variable attribute ATTR. */
999 extern int pthread_condattr_setpshared (pthread_condattr_t *__attr,
1000 int __pshared) __THROW __nonnull ((1));
1002 #ifdef __USE_XOPEN2K
1003 /* Get the clock selected for the conditon variable attribute ATTR. */
1004 extern int pthread_condattr_getclock (__const pthread_condattr_t *
1006 __clockid_t *__restrict __clock_id)
1007 __THROW __nonnull ((1, 2));
1009 /* Set the clock selected for the conditon variable attribute ATTR. */
1010 extern int pthread_condattr_setclock (pthread_condattr_t *__attr,
1011 __clockid_t __clock_id)
1012 __THROW __nonnull ((1));
1016 #ifdef __USE_XOPEN2K
1017 /* Functions to handle spinlocks. */
1019 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
1020 be shared between different processes. */
1021 extern int pthread_spin_init (pthread_spinlock_t *__lock, int __pshared)
1022 __THROW __nonnull ((1));
1024 /* Destroy the spinlock LOCK. */
1025 extern int pthread_spin_destroy (pthread_spinlock_t *__lock)
1026 __THROW __nonnull ((1));
1028 /* Wait until spinlock LOCK is retrieved. */
1029 extern int pthread_spin_lock (pthread_spinlock_t *__lock)
1030 __THROW __nonnull ((1));
1032 /* Try to lock spinlock LOCK. */
1033 extern int pthread_spin_trylock (pthread_spinlock_t *__lock)
1034 __THROW __nonnull ((1));
1036 /* Release spinlock LOCK. */
1037 extern int pthread_spin_unlock (pthread_spinlock_t *__lock)
1038 __THROW __nonnull ((1));
1041 /* Functions to handle barriers. */
1043 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1044 opened when COUNT waiters arrived. */
1045 extern int pthread_barrier_init (pthread_barrier_t *__restrict __barrier,
1046 __const pthread_barrierattr_t *__restrict
1047 __attr, unsigned int __count)
1048 __THROW __nonnull ((1));
1050 /* Destroy a previously dynamically initialized barrier BARRIER. */
1051 extern int pthread_barrier_destroy (pthread_barrier_t *__barrier)
1052 __THROW __nonnull ((1));
1054 /* Wait on barrier BARRIER. */
1055 extern int pthread_barrier_wait (pthread_barrier_t *__barrier)
1056 __THROW __nonnull ((1));
1059 /* Initialize barrier attribute ATTR. */
1060 extern int pthread_barrierattr_init (pthread_barrierattr_t *__attr)
1061 __THROW __nonnull ((1));
1063 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1064 extern int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr)
1065 __THROW __nonnull ((1));
1067 /* Get the process-shared flag of the barrier attribute ATTR. */
1068 extern int pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
1070 int *__restrict __pshared)
1071 __THROW __nonnull ((1, 2));
1073 /* Set the process-shared flag of the barrier attribute ATTR. */
1074 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr,
1076 __THROW __nonnull ((1));
1080 /* Functions for handling thread-specific data. */
1082 /* Create a key value identifying a location in the thread-specific
1083 data area. Each thread maintains a distinct thread-specific data
1084 area. DESTR_FUNCTION, if non-NULL, is called with the value
1085 associated to that key when the key is destroyed.
1086 DESTR_FUNCTION is not called if the value associated is NULL when
1087 the key is destroyed. */
1088 extern int pthread_key_create (pthread_key_t *__key,
1089 void (*__destr_function) (void *))
1090 __THROW __nonnull ((1));
1093 extern int pthread_key_delete (pthread_key_t __key) __THROW;
1095 /* Return current value of the thread-specific data slot identified by KEY. */
1096 extern void *pthread_getspecific (pthread_key_t __key) __THROW;
1098 /* Store POINTER in the thread-specific data slot identified by KEY. */
1099 extern int pthread_setspecific (pthread_key_t __key,
1100 __const void *__pointer) __THROW ;
1103 #ifdef __USE_XOPEN2K
1104 /* Get ID of CPU-time clock for thread THREAD_ID. */
1105 extern int pthread_getcpuclockid (pthread_t __thread_id,
1106 __clockid_t *__clock_id)
1107 __THROW __nonnull ((2));
1111 /* Install handlers to be called when a new process is created with FORK.
1112 The PREPARE handler is called in the parent process just before performing
1113 FORK. The PARENT handler is called in the parent process just after FORK.
1114 The CHILD handler is called in the child process. Each of the three
1115 handlers can be NULL, meaning that no handler needs to be called at that
1117 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1118 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1119 first called before FORK), and the PARENT and CHILD handlers are called
1120 in FIFO (first added, first called). */
1122 extern int pthread_atfork (void (*__prepare) (void),
1123 void (*__parent) (void),
1124 void (*__child) (void)) __THROW;
1127 #ifdef __USE_EXTERN_INLINES
1128 /* Optimizations. */
1130 __NTH (pthread_equal (pthread_t __thread1, pthread_t __thread2))
1132 return __thread1 == __thread2;
1138 #endif /* pthread.h */