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 #include <bits/pthreadtypes.h>
29 #include <bits/setjmp.h>
30 #include <bits/wordsize.h>
36 PTHREAD_CREATE_JOINABLE
,
37 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
38 PTHREAD_CREATE_DETACHED
39 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
46 PTHREAD_MUTEX_TIMED_NP
,
47 PTHREAD_MUTEX_RECURSIVE_NP
,
48 PTHREAD_MUTEX_ERRORCHECK_NP
,
49 PTHREAD_MUTEX_ADAPTIVE_NP
50 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
52 PTHREAD_MUTEX_NORMAL
= PTHREAD_MUTEX_TIMED_NP
,
53 PTHREAD_MUTEX_RECURSIVE
= PTHREAD_MUTEX_RECURSIVE_NP
,
54 PTHREAD_MUTEX_ERRORCHECK
= PTHREAD_MUTEX_ERRORCHECK_NP
,
55 PTHREAD_MUTEX_DEFAULT
= PTHREAD_MUTEX_NORMAL
58 /* For compatibility. */
59 , PTHREAD_MUTEX_FAST_NP
= PTHREAD_MUTEX_TIMED_NP
65 /* Robust mutex or not flags. */
68 PTHREAD_MUTEX_STALLED
,
69 PTHREAD_MUTEX_STALLED_NP
= PTHREAD_MUTEX_STALLED
,
71 PTHREAD_MUTEX_ROBUST_NP
= PTHREAD_MUTEX_ROBUST
77 /* Mutex protocols. */
87 /* Mutex initializers. */
89 # define PTHREAD_MUTEX_INITIALIZER \
90 { { 0, 0, 0, 0, 0, 0, { 0, 0 } } }
92 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
93 { { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { 0, 0 } } }
94 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
95 { { 0, 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { 0, 0 } } }
96 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
97 { { 0, 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { 0, 0 } } }
100 # define PTHREAD_MUTEX_INITIALIZER \
101 { { 0, 0, 0, 0, 0, { 0 } } }
103 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
104 { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { 0 } } }
105 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
106 { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { 0 } } }
107 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
108 { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { 0 } } }
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 /* Read-write lock initializers. */
124 # define PTHREAD_RWLOCK_INITIALIZER \
125 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
127 # if __WORDSIZE == 64
128 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
129 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
130 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
132 # if __BYTE_ORDER == __LITTLE_ENDIAN
133 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
134 { { 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, \
137 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
138 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,\
143 #endif /* Unix98 or XOpen2K */
146 /* Scheduler inheritance. */
149 PTHREAD_INHERIT_SCHED
,
150 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
151 PTHREAD_EXPLICIT_SCHED
152 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
156 /* Scope handling. */
159 PTHREAD_SCOPE_SYSTEM
,
160 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
161 PTHREAD_SCOPE_PROCESS
162 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
166 /* Process shared or private flag. */
169 PTHREAD_PROCESS_PRIVATE
,
170 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
171 PTHREAD_PROCESS_SHARED
172 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
177 /* Conditional variable handling. */
178 #define PTHREAD_COND_INITIALIZER { { 0, 0, 0, 0, 0, (void *) 0, 0, 0 } }
181 /* Cleanup buffers */
182 struct _pthread_cleanup_buffer
184 void (*__routine
) (void *); /* Function to call. */
185 void *__arg
; /* Its argument. */
186 int __canceltype
; /* Saved cancellation type. */
187 struct _pthread_cleanup_buffer
*__prev
; /* Chaining of cleanup functions. */
193 PTHREAD_CANCEL_ENABLE
,
194 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
195 PTHREAD_CANCEL_DISABLE
196 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
200 PTHREAD_CANCEL_DEFERRED
,
201 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
202 PTHREAD_CANCEL_ASYNCHRONOUS
203 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
205 #define PTHREAD_CANCELED ((void *) -1)
208 /* Single execution handling. */
209 #define PTHREAD_ONCE_INIT 0
213 /* Value returned by 'pthread_barrier_wait' for one of the threads after
214 the required number of threads have called this function.
215 -1 is distinct from 0 and all errno constants */
216 # define PTHREAD_BARRIER_SERIAL_THREAD -1
222 /* Create a new thread, starting with execution of START-ROUTINE
223 getting passed ARG. Creation attributed come from ATTR. The new
224 handle is stored in *NEWTHREAD. */
225 extern int pthread_create (pthread_t
*__restrict __newthread
,
226 __const pthread_attr_t
*__restrict __attr
,
227 void *(*__start_routine
) (void *),
228 void *__restrict __arg
) __THROW
__nonnull ((1, 3));
230 /* Terminate calling thread.
232 The registered cleanup handlers are called via exception handling
233 so we cannot mark this function with __THROW.*/
234 extern void pthread_exit (void *__retval
) __attribute__ ((__noreturn__
));
236 /* Make calling thread wait for termination of the thread TH. The
237 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
240 This function is a cancellation point and therefore not marked with
242 extern int pthread_join (pthread_t __th
, void **__thread_return
);
245 /* Check whether thread TH has terminated. If yes return the status of
246 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
247 extern int pthread_tryjoin_np (pthread_t __th
, void **__thread_return
) __THROW
;
249 /* Make calling thread wait for termination of the thread TH, but only
250 until TIMEOUT. The exit status of the thread is stored in
251 *THREAD_RETURN, if THREAD_RETURN is not NULL.
253 This function is a cancellation point and therefore not marked with
255 extern int pthread_timedjoin_np (pthread_t __th
, void **__thread_return
,
256 __const
struct timespec
*__abstime
);
259 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
260 The resources of TH will therefore be freed immediately when it
261 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
263 extern int pthread_detach (pthread_t __th
) __THROW
;
266 /* Obtain the identifier of the current thread. */
267 extern pthread_t
pthread_self (void) __THROW
__attribute__ ((__const__
));
269 /* Compare two thread identifiers. */
270 extern int pthread_equal (pthread_t __thread1
, pthread_t __thread2
) __THROW
;
273 /* Thread attribute handling. */
275 /* Initialize thread attribute *ATTR with default attributes
276 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
277 no user-provided stack). */
278 extern int pthread_attr_init (pthread_attr_t
*__attr
) __THROW
__nonnull ((1));
280 /* Destroy thread attribute *ATTR. */
281 extern int pthread_attr_destroy (pthread_attr_t
*__attr
)
282 __THROW
__nonnull ((1));
284 /* Get detach state attribute. */
285 extern int pthread_attr_getdetachstate (__const pthread_attr_t
*__attr
,
287 __THROW
__nonnull ((1, 2));
289 /* Set detach state attribute. */
290 extern int pthread_attr_setdetachstate (pthread_attr_t
*__attr
,
292 __THROW
__nonnull ((1));
295 /* Get the size of the guard area created for stack overflow protection. */
296 extern int pthread_attr_getguardsize (__const pthread_attr_t
*__attr
,
298 __THROW
__nonnull ((1, 2));
300 /* Set the size of the guard area created for stack overflow protection. */
301 extern int pthread_attr_setguardsize (pthread_attr_t
*__attr
,
303 __THROW
__nonnull ((1));
306 /* Return in *PARAM the scheduling parameters of *ATTR. */
307 extern int pthread_attr_getschedparam (__const pthread_attr_t
*__restrict
309 struct sched_param
*__restrict __param
)
310 __THROW
__nonnull ((1, 2));
312 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
313 extern int pthread_attr_setschedparam (pthread_attr_t
*__restrict __attr
,
314 __const
struct sched_param
*__restrict
315 __param
) __THROW
__nonnull ((1, 2));
317 /* Return in *POLICY the scheduling policy of *ATTR. */
318 extern int pthread_attr_getschedpolicy (__const pthread_attr_t
*__restrict
319 __attr
, int *__restrict __policy
)
320 __THROW
__nonnull ((1, 2));
322 /* Set scheduling policy in *ATTR according to POLICY. */
323 extern int pthread_attr_setschedpolicy (pthread_attr_t
*__attr
, int __policy
)
324 __THROW
__nonnull ((1));
326 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
327 extern int pthread_attr_getinheritsched (__const pthread_attr_t
*__restrict
328 __attr
, int *__restrict __inherit
)
329 __THROW
__nonnull ((1, 2));
331 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
332 extern int pthread_attr_setinheritsched (pthread_attr_t
*__attr
,
334 __THROW
__nonnull ((1));
337 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
338 extern int pthread_attr_getscope (__const pthread_attr_t
*__restrict __attr
,
339 int *__restrict __scope
)
340 __THROW
__nonnull ((1, 2));
342 /* Set scheduling contention scope in *ATTR according to SCOPE. */
343 extern int pthread_attr_setscope (pthread_attr_t
*__attr
, int __scope
)
344 __THROW
__nonnull ((1));
346 /* Return the previously set address for the stack. */
347 extern int pthread_attr_getstackaddr (__const pthread_attr_t
*__restrict
348 __attr
, void **__restrict __stackaddr
)
349 __THROW
__nonnull ((1, 2)) __attribute_deprecated__
;
351 /* Set the starting address of the stack of the thread to be created.
352 Depending on whether the stack grows up or down the value must either
353 be higher or lower than all the address in the memory block. The
354 minimal size of the block must be PTHREAD_STACK_MIN. */
355 extern int pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
357 __THROW
__nonnull ((1)) __attribute_deprecated__
;
359 /* Return the currently used minimal stack size. */
360 extern int pthread_attr_getstacksize (__const pthread_attr_t
*__restrict
361 __attr
, size_t *__restrict __stacksize
)
362 __THROW
__nonnull ((1, 2));
364 /* Add information about the minimum stack size needed for the thread
365 to be started. This size must never be less than PTHREAD_STACK_MIN
366 and must also not exceed the system limits. */
367 extern int pthread_attr_setstacksize (pthread_attr_t
*__attr
,
369 __THROW
__nonnull ((1));
372 /* Return the previously set address for the stack. */
373 extern int pthread_attr_getstack (__const pthread_attr_t
*__restrict __attr
,
374 void **__restrict __stackaddr
,
375 size_t *__restrict __stacksize
)
376 __THROW
__nonnull ((1, 2, 3));
378 /* The following two interfaces are intended to replace the last two. They
379 require setting the address as well as the size since only setting the
380 address will make the implementation on some architectures impossible. */
381 extern int pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
382 size_t __stacksize
) __THROW
__nonnull ((1));
386 /* Thread created with attribute ATTR will be limited to run only on
387 the processors represented in CPUSET. */
388 extern int pthread_attr_setaffinity_np (pthread_attr_t
*__attr
,
390 __const cpu_set_t
*__cpuset
)
391 __THROW
__nonnull ((1, 3));
393 /* Get bit set in CPUSET representing the processors threads created with
395 extern int pthread_attr_getaffinity_np (__const pthread_attr_t
*__attr
,
398 __THROW
__nonnull ((1, 3));
401 /* Initialize thread attribute *ATTR with attributes corresponding to the
402 already running thread TH. It shall be called on uninitialized ATTR
403 and destroyed with pthread_attr_destroy when no longer needed. */
404 extern int pthread_getattr_np (pthread_t __th
, pthread_attr_t
*__attr
)
405 __THROW
__nonnull ((2));
409 /* Functions for scheduling control. */
411 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
413 extern int pthread_setschedparam (pthread_t __target_thread
, int __policy
,
414 __const
struct sched_param
*__param
)
415 __THROW
__nonnull ((3));
417 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
418 extern int pthread_getschedparam (pthread_t __target_thread
,
419 int *__restrict __policy
,
420 struct sched_param
*__restrict __param
)
421 __THROW
__nonnull ((2, 3));
423 /* Set the scheduling priority for TARGET_THREAD. */
424 extern int pthread_setschedprio (pthread_t __target_thread
, int __prio
)
429 /* Determine level of concurrency. */
430 extern int pthread_getconcurrency (void) __THROW
;
432 /* Set new concurrency level to LEVEL. */
433 extern int pthread_setconcurrency (int __level
) __THROW
;
437 /* Yield the processor to another thread or process.
438 This function is similar to the POSIX `sched_yield' function but
439 might be differently implemented in the case of a m-on-n thread
441 extern int pthread_yield (void) __THROW
;
444 /* Limit specified thread TH to run only on the processors represented
446 extern int pthread_setaffinity_np (pthread_t __th
, size_t __cpusetsize
,
447 __const cpu_set_t
*__cpuset
)
448 __THROW
__nonnull ((3));
450 /* Get bit set in CPUSET representing the processors TH can run on. */
451 extern int pthread_getaffinity_np (pthread_t __th
, size_t __cpusetsize
,
453 __THROW
__nonnull ((3));
457 /* Functions for handling initialization. */
459 /* Guarantee that the initialization function INIT_ROUTINE will be called
460 only once, even if pthread_once is executed several times with the
461 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
462 extern variable initialized to PTHREAD_ONCE_INIT.
464 The initialization functions might throw exception which is why
465 this function is not marked with __THROW. */
466 extern int pthread_once (pthread_once_t
*__once_control
,
467 void (*__init_routine
) (void)) __nonnull ((1, 2));
470 /* Functions for handling cancellation.
472 Note that these functions are explicitly not marked to not throw an
473 exception in C++ code. If cancellation is implemented by unwinding
474 this is necessary to have the compiler generate the unwind information. */
476 /* Set cancelability state of current thread to STATE, returning old
477 state in *OLDSTATE if OLDSTATE is not NULL. */
478 extern int pthread_setcancelstate (int __state
, int *__oldstate
);
480 /* Set cancellation state of current thread to TYPE, returning the old
481 type in *OLDTYPE if OLDTYPE is not NULL. */
482 extern int pthread_setcanceltype (int __type
, int *__oldtype
);
484 /* Cancel THREAD immediately or at the next possibility. */
485 extern int pthread_cancel (pthread_t __th
);
487 /* Test for pending cancellation for the current thread and terminate
488 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
490 extern void pthread_testcancel (void);
493 /* Cancellation handling with integration into exception handling. */
499 __jmp_buf __cancel_jmp_buf
;
500 int __mask_was_saved
;
501 } __cancel_jmp_buf
[1];
503 } __pthread_unwind_buf_t
__attribute__ ((__aligned__
));
505 /* No special attributes by default. */
506 #ifndef __cleanup_fct_attribute
507 # define __cleanup_fct_attribute
511 /* Structure to hold the cleanup handler information. */
512 struct __pthread_cleanup_frame
514 void (*__cancel_routine
) (void *);
520 #if defined __GNUC__ && defined __EXCEPTIONS
522 /* Class to handle cancellation handler invocation. */
523 class __pthread_cleanup_class
525 void (*__cancel_routine
) (void *);
531 __pthread_cleanup_class (void (*__fct
) (void *), void *__arg
)
532 : __cancel_routine (__fct
), __cancel_arg (__arg
), __do_it (1) { }
533 ~__pthread_cleanup_class () { if (__do_it
) __cancel_routine (__cancel_arg
); }
534 void __setdoit (int __newval
) { __do_it
= __newval
; }
535 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
,
537 void __restore () const { pthread_setcanceltype (__cancel_type
, 0); }
540 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
541 when the thread is canceled or calls pthread_exit. ROUTINE will also
542 be called with arguments ARG when the matching pthread_cleanup_pop
543 is executed with non-zero EXECUTE argument.
545 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
546 be used in matching pairs at the same nesting level of braces. */
547 # define pthread_cleanup_push(routine, arg) \
549 __pthread_cleanup_class __clframe (routine, arg)
551 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
552 If EXECUTE is non-zero, the handler function is called. */
553 # define pthread_cleanup_pop(execute) \
554 __clframe.__setdoit (execute); \
558 /* Install a cleanup handler as pthread_cleanup_push does, but also
559 saves the current cancellation type and sets it to deferred
561 # define pthread_cleanup_push_defer_np(routine, arg) \
563 __pthread_cleanup_class __clframe (routine, arg); \
566 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
567 restores the cancellation type that was in effect when the matching
568 pthread_cleanup_push_defer was called. */
569 # define pthread_cleanup_pop_restore_np(execute) \
570 __clframe.__restore (); \
571 __clframe.__setdoit (execute); \
575 /* Function called to call the cleanup handler. As an extern inline
576 function the compiler is free to decide inlining the change when
577 needed or fall back on the copy which must exist somewhere
580 __pthread_cleanup_routine (struct __pthread_cleanup_frame
*__frame
)
582 if (__frame
->__do_it
)
583 __frame
->__cancel_routine (__frame
->__cancel_arg
);
586 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
587 when the thread is canceled or calls pthread_exit. ROUTINE will also
588 be called with arguments ARG when the matching pthread_cleanup_pop
589 is executed with non-zero EXECUTE argument.
591 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
592 be used in matching pairs at the same nesting level of braces. */
593 # define pthread_cleanup_push(routine, arg) \
595 struct __pthread_cleanup_frame __clframe \
596 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
597 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
600 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
601 If EXECUTE is non-zero, the handler function is called. */
602 # define pthread_cleanup_pop(execute) \
603 __clframe.__do_it = (execute); \
607 /* Install a cleanup handler as pthread_cleanup_push does, but also
608 saves the current cancellation type and sets it to deferred
610 # define pthread_cleanup_push_defer_np(routine, arg) \
612 struct __pthread_cleanup_frame __clframe \
613 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
614 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
616 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
617 &__clframe.__cancel_type)
619 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
620 restores the cancellation type that was in effect when the matching
621 pthread_cleanup_push_defer was called. */
622 # define pthread_cleanup_pop_restore_np(execute) \
623 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
624 __clframe.__do_it = (execute); \
629 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
630 when the thread is canceled or calls pthread_exit. ROUTINE will also
631 be called with arguments ARG when the matching pthread_cleanup_pop
632 is executed with non-zero EXECUTE argument.
634 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
635 be used in matching pairs at the same nesting level of braces. */
636 # define pthread_cleanup_push(routine, arg) \
638 __pthread_unwind_buf_t __cancel_buf; \
639 void (*__cancel_routine) (void *) = (routine); \
640 void *__cancel_arg = (arg); \
641 int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
642 __cancel_buf.__cancel_jmp_buf, 0); \
643 if (__builtin_expect (not_first_call, 0)) \
645 __cancel_routine (__cancel_arg); \
646 __pthread_unwind_next (&__cancel_buf); \
650 __pthread_register_cancel (&__cancel_buf); \
652 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
653 __cleanup_fct_attribute
;
655 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
656 If EXECUTE is non-zero, the handler function is called. */
657 # define pthread_cleanup_pop(execute) \
658 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
660 __pthread_unregister_cancel (&__cancel_buf); \
662 __cancel_routine (__cancel_arg); \
664 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
665 __cleanup_fct_attribute
;
668 /* Install a cleanup handler as pthread_cleanup_push does, but also
669 saves the current cancellation type and sets it to deferred
671 # define pthread_cleanup_push_defer_np(routine, arg) \
673 __pthread_unwind_buf_t __cancel_buf; \
674 void (*__cancel_routine) (void *) = (routine); \
675 void *__cancel_arg = (arg); \
676 int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
677 __cancel_buf.__cancel_jmp_buf, 0); \
678 if (__builtin_expect (not_first_call, 0)) \
680 __cancel_routine (__cancel_arg); \
681 __pthread_unwind_next (&__cancel_buf); \
685 __pthread_register_cancel_defer (&__cancel_buf); \
687 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t
*__buf
)
688 __cleanup_fct_attribute
;
690 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
691 restores the cancellation type that was in effect when the matching
692 pthread_cleanup_push_defer was called. */
693 # define pthread_cleanup_pop_restore_np(execute) \
694 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
696 __pthread_unregister_cancel_restore (&__cancel_buf); \
698 __cancel_routine (__cancel_arg); \
700 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t
*__buf
)
701 __cleanup_fct_attribute
;
704 /* Internal interface to initiate cleanup. */
705 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
706 __cleanup_fct_attribute
__attribute__ ((__noreturn__
))
708 __attribute__ ((__weak__
))
713 /* Function used in the macros. */
714 struct __jmp_buf_tag
;
715 extern int __sigsetjmp (struct __jmp_buf_tag
*__env
, int __savemask
) __THROW
;
718 /* Mutex handling. */
720 /* Initialize a mutex. */
721 extern int pthread_mutex_init (pthread_mutex_t
*__mutex
,
722 __const pthread_mutexattr_t
*__mutexattr
)
723 __THROW
__nonnull ((1));
725 /* Destroy a mutex. */
726 extern int pthread_mutex_destroy (pthread_mutex_t
*__mutex
)
727 __THROW
__nonnull ((1));
729 /* Try locking a mutex. */
730 extern int pthread_mutex_trylock (pthread_mutex_t
*__mutex
)
731 __THROW
__nonnull ((1));
734 extern int pthread_mutex_lock (pthread_mutex_t
*__mutex
)
735 __THROW
__nonnull ((1));
738 /* Wait until lock becomes available, or specified time passes. */
739 extern int pthread_mutex_timedlock (pthread_mutex_t
*__restrict __mutex
,
740 __const
struct timespec
*__restrict
741 __abstime
) __THROW
__nonnull ((1, 2));
744 /* Unlock a mutex. */
745 extern int pthread_mutex_unlock (pthread_mutex_t
*__mutex
)
746 __THROW
__nonnull ((1));
749 /* Get the priority ceiling of MUTEX. */
750 extern int pthread_mutex_getprioceiling (__const pthread_mutex_t
*
752 int *__restrict __prioceiling
)
753 __THROW
__nonnull ((1, 2));
755 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
756 priority ceiling value in *OLD_CEILING. */
757 extern int pthread_mutex_setprioceiling (pthread_mutex_t
*__restrict __mutex
,
759 int *__restrict __old_ceiling
)
760 __THROW
__nonnull ((1, 3));
763 #ifdef __USE_XOPEN2K8
764 /* Declare the state protected by MUTEX as consistent. */
765 extern int pthread_mutex_consistent (pthread_mutex_t
*__mutex
)
766 __THROW
__nonnull ((1));
768 extern int pthread_mutex_consistent_np (pthread_mutex_t
*__mutex
)
769 __THROW
__nonnull ((1));
774 /* Functions for handling mutex attributes. */
776 /* Initialize mutex attribute object ATTR with default attributes
777 (kind is PTHREAD_MUTEX_TIMED_NP). */
778 extern int pthread_mutexattr_init (pthread_mutexattr_t
*__attr
)
779 __THROW
__nonnull ((1));
781 /* Destroy mutex attribute object ATTR. */
782 extern int pthread_mutexattr_destroy (pthread_mutexattr_t
*__attr
)
783 __THROW
__nonnull ((1));
785 /* Get the process-shared flag of the mutex attribute ATTR. */
786 extern int pthread_mutexattr_getpshared (__const pthread_mutexattr_t
*
788 int *__restrict __pshared
)
789 __THROW
__nonnull ((1, 2));
791 /* Set the process-shared flag of the mutex attribute ATTR. */
792 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t
*__attr
,
794 __THROW
__nonnull ((1));
796 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
797 /* Return in *KIND the mutex kind attribute in *ATTR. */
798 extern int pthread_mutexattr_gettype (__const pthread_mutexattr_t
*__restrict
799 __attr
, int *__restrict __kind
)
800 __THROW
__nonnull ((1, 2));
802 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
803 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
804 PTHREAD_MUTEX_DEFAULT). */
805 extern int pthread_mutexattr_settype (pthread_mutexattr_t
*__attr
, int __kind
)
806 __THROW
__nonnull ((1));
809 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
810 extern int pthread_mutexattr_getprotocol (__const pthread_mutexattr_t
*
812 int *__restrict __protocol
)
813 __THROW
__nonnull ((1, 2));
815 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
816 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
817 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t
*__attr
,
819 __THROW
__nonnull ((1));
821 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
822 extern int pthread_mutexattr_getprioceiling (__const pthread_mutexattr_t
*
824 int *__restrict __prioceiling
)
825 __THROW
__nonnull ((1, 2));
827 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
828 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t
*__attr
,
830 __THROW
__nonnull ((1));
833 /* Get the robustness flag of the mutex attribute ATTR. */
834 extern int pthread_mutexattr_getrobust (__const pthread_mutexattr_t
*__attr
,
836 __THROW
__nonnull ((1, 2));
838 extern int pthread_mutexattr_getrobust_np (__const pthread_mutexattr_t
*__attr
,
840 __THROW
__nonnull ((1, 2));
843 /* Set the robustness flag of the mutex attribute ATTR. */
844 extern int pthread_mutexattr_setrobust (pthread_mutexattr_t
*__attr
,
846 __THROW
__nonnull ((1));
848 extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t
*__attr
,
850 __THROW
__nonnull ((1));
855 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
856 /* Functions for handling read-write locks. */
858 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
859 the default values if later is NULL. */
860 extern int pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
861 __const pthread_rwlockattr_t
*__restrict
862 __attr
) __THROW
__nonnull ((1));
864 /* Destroy read-write lock RWLOCK. */
865 extern int pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
)
866 __THROW
__nonnull ((1));
868 /* Acquire read lock for RWLOCK. */
869 extern int pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
)
870 __THROW
__nonnull ((1));
872 /* Try to acquire read lock for RWLOCK. */
873 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
)
874 __THROW
__nonnull ((1));
876 # ifdef __USE_XOPEN2K
877 /* Try to acquire read lock for RWLOCK or return after specfied time. */
878 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t
*__restrict __rwlock
,
879 __const
struct timespec
*__restrict
880 __abstime
) __THROW
__nonnull ((1, 2));
883 /* Acquire write lock for RWLOCK. */
884 extern int pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
)
885 __THROW
__nonnull ((1));
887 /* Try to acquire write lock for RWLOCK. */
888 extern int pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
)
889 __THROW
__nonnull ((1));
891 # ifdef __USE_XOPEN2K
892 /* Try to acquire write lock for RWLOCK or return after specfied time. */
893 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t
*__restrict __rwlock
,
894 __const
struct timespec
*__restrict
895 __abstime
) __THROW
__nonnull ((1, 2));
899 extern int pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
)
900 __THROW
__nonnull ((1));
903 /* Functions for handling read-write lock attributes. */
905 /* Initialize attribute object ATTR with default values. */
906 extern int pthread_rwlockattr_init (pthread_rwlockattr_t
*__attr
)
907 __THROW
__nonnull ((1));
909 /* Destroy attribute object ATTR. */
910 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t
*__attr
)
911 __THROW
__nonnull ((1));
913 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
914 extern int pthread_rwlockattr_getpshared (__const pthread_rwlockattr_t
*
916 int *__restrict __pshared
)
917 __THROW
__nonnull ((1, 2));
919 /* Set process-shared attribute of ATTR to PSHARED. */
920 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t
*__attr
,
922 __THROW
__nonnull ((1));
924 /* Return current setting of reader/writer preference. */
925 extern int pthread_rwlockattr_getkind_np (__const pthread_rwlockattr_t
*
927 int *__restrict __pref
)
928 __THROW
__nonnull ((1, 2));
930 /* Set reader/write preference. */
931 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t
*__attr
,
932 int __pref
) __THROW
__nonnull ((1));
936 /* Functions for handling conditional variables. */
938 /* Initialize condition variable COND using attributes ATTR, or use
939 the default values if later is NULL. */
940 extern int pthread_cond_init (pthread_cond_t
*__restrict __cond
,
941 __const pthread_condattr_t
*__restrict
942 __cond_attr
) __THROW
__nonnull ((1));
944 /* Destroy condition variable COND. */
945 extern int pthread_cond_destroy (pthread_cond_t
*__cond
)
946 __THROW
__nonnull ((1));
948 /* Wake up one thread waiting for condition variable COND. */
949 extern int pthread_cond_signal (pthread_cond_t
*__cond
)
950 __THROW
__nonnull ((1));
952 /* Wake up all threads waiting for condition variables COND. */
953 extern int pthread_cond_broadcast (pthread_cond_t
*__cond
)
954 __THROW
__nonnull ((1));
956 /* Wait for condition variable COND to be signaled or broadcast.
957 MUTEX is assumed to be locked before.
959 This function is a cancellation point and therefore not marked with
961 extern int pthread_cond_wait (pthread_cond_t
*__restrict __cond
,
962 pthread_mutex_t
*__restrict __mutex
)
965 /* Wait for condition variable COND to be signaled or broadcast until
966 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
967 absolute time specification; zero is the beginning of the epoch
968 (00:00:00 GMT, January 1, 1970).
970 This function is a cancellation point and therefore not marked with
972 extern int pthread_cond_timedwait (pthread_cond_t
*__restrict __cond
,
973 pthread_mutex_t
*__restrict __mutex
,
974 __const
struct timespec
*__restrict
975 __abstime
) __nonnull ((1, 2, 3));
977 /* Functions for handling condition variable attributes. */
979 /* Initialize condition variable attribute ATTR. */
980 extern int pthread_condattr_init (pthread_condattr_t
*__attr
)
981 __THROW
__nonnull ((1));
983 /* Destroy condition variable attribute ATTR. */
984 extern int pthread_condattr_destroy (pthread_condattr_t
*__attr
)
985 __THROW
__nonnull ((1));
987 /* Get the process-shared flag of the condition variable attribute ATTR. */
988 extern int pthread_condattr_getpshared (__const pthread_condattr_t
*
990 int *__restrict __pshared
)
991 __THROW
__nonnull ((1, 2));
993 /* Set the process-shared flag of the condition variable attribute ATTR. */
994 extern int pthread_condattr_setpshared (pthread_condattr_t
*__attr
,
995 int __pshared
) __THROW
__nonnull ((1));
998 /* Get the clock selected for the conditon variable attribute ATTR. */
999 extern int pthread_condattr_getclock (__const pthread_condattr_t
*
1001 __clockid_t
*__restrict __clock_id
)
1002 __THROW
__nonnull ((1, 2));
1004 /* Set the clock selected for the conditon variable attribute ATTR. */
1005 extern int pthread_condattr_setclock (pthread_condattr_t
*__attr
,
1006 __clockid_t __clock_id
)
1007 __THROW
__nonnull ((1));
1011 #ifdef __USE_XOPEN2K
1012 /* Functions to handle spinlocks. */
1014 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
1015 be shared between different processes. */
1016 extern int pthread_spin_init (pthread_spinlock_t
*__lock
, int __pshared
)
1017 __THROW
__nonnull ((1));
1019 /* Destroy the spinlock LOCK. */
1020 extern int pthread_spin_destroy (pthread_spinlock_t
*__lock
)
1021 __THROW
__nonnull ((1));
1023 /* Wait until spinlock LOCK is retrieved. */
1024 extern int pthread_spin_lock (pthread_spinlock_t
*__lock
)
1025 __THROW
__nonnull ((1));
1027 /* Try to lock spinlock LOCK. */
1028 extern int pthread_spin_trylock (pthread_spinlock_t
*__lock
)
1029 __THROW
__nonnull ((1));
1031 /* Release spinlock LOCK. */
1032 extern int pthread_spin_unlock (pthread_spinlock_t
*__lock
)
1033 __THROW
__nonnull ((1));
1036 /* Functions to handle barriers. */
1038 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1039 opened when COUNT waiters arrived. */
1040 extern int pthread_barrier_init (pthread_barrier_t
*__restrict __barrier
,
1041 __const pthread_barrierattr_t
*__restrict
1042 __attr
, unsigned int __count
)
1043 __THROW
__nonnull ((1));
1045 /* Destroy a previously dynamically initialized barrier BARRIER. */
1046 extern int pthread_barrier_destroy (pthread_barrier_t
*__barrier
)
1047 __THROW
__nonnull ((1));
1049 /* Wait on barrier BARRIER. */
1050 extern int pthread_barrier_wait (pthread_barrier_t
*__barrier
)
1051 __THROW
__nonnull ((1));
1054 /* Initialize barrier attribute ATTR. */
1055 extern int pthread_barrierattr_init (pthread_barrierattr_t
*__attr
)
1056 __THROW
__nonnull ((1));
1058 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1059 extern int pthread_barrierattr_destroy (pthread_barrierattr_t
*__attr
)
1060 __THROW
__nonnull ((1));
1062 /* Get the process-shared flag of the barrier attribute ATTR. */
1063 extern int pthread_barrierattr_getpshared (__const pthread_barrierattr_t
*
1065 int *__restrict __pshared
)
1066 __THROW
__nonnull ((1, 2));
1068 /* Set the process-shared flag of the barrier attribute ATTR. */
1069 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t
*__attr
,
1071 __THROW
__nonnull ((1));
1075 /* Functions for handling thread-specific data. */
1077 /* Create a key value identifying a location in the thread-specific
1078 data area. Each thread maintains a distinct thread-specific data
1079 area. DESTR_FUNCTION, if non-NULL, is called with the value
1080 associated to that key when the key is destroyed.
1081 DESTR_FUNCTION is not called if the value associated is NULL when
1082 the key is destroyed. */
1083 extern int pthread_key_create (pthread_key_t
*__key
,
1084 void (*__destr_function
) (void *))
1085 __THROW
__nonnull ((1));
1088 extern int pthread_key_delete (pthread_key_t __key
) __THROW
;
1090 /* Return current value of the thread-specific data slot identified by KEY. */
1091 extern void *pthread_getspecific (pthread_key_t __key
) __THROW
;
1093 /* Store POINTER in the thread-specific data slot identified by KEY. */
1094 extern int pthread_setspecific (pthread_key_t __key
,
1095 __const
void *__pointer
) __THROW
;
1098 #ifdef __USE_XOPEN2K
1099 /* Get ID of CPU-time clock for thread THREAD_ID. */
1100 extern int pthread_getcpuclockid (pthread_t __thread_id
,
1101 __clockid_t
*__clock_id
)
1102 __THROW
__nonnull ((2));
1106 /* Install handlers to be called when a new process is created with FORK.
1107 The PREPARE handler is called in the parent process just before performing
1108 FORK. The PARENT handler is called in the parent process just after FORK.
1109 The CHILD handler is called in the child process. Each of the three
1110 handlers can be NULL, meaning that no handler needs to be called at that
1112 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1113 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1114 first called before FORK), and the PARENT and CHILD handlers are called
1115 in FIFO (first added, first called). */
1117 extern int pthread_atfork (void (*__prepare
) (void),
1118 void (*__parent
) (void),
1119 void (*__child
) (void)) __THROW
;
1122 #ifdef __USE_EXTERN_INLINES
1123 /* Optimizations. */
1125 __NTH (pthread_equal (pthread_t __thread1
, pthread_t __thread2
))
1127 return __thread1
== __thread2
;
1133 #endif /* pthread.h */