1 /* Copyright (C) 2002-2011, 2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
26 #include <bits/pthreadtypes.h>
27 #include <bits/setjmp.h>
28 #include <bits/wordsize.h>
34 PTHREAD_CREATE_JOINABLE
,
35 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
36 PTHREAD_CREATE_DETACHED
37 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
44 PTHREAD_MUTEX_TIMED_NP
,
45 PTHREAD_MUTEX_RECURSIVE_NP
,
46 PTHREAD_MUTEX_ERRORCHECK_NP
,
47 PTHREAD_MUTEX_ADAPTIVE_NP
48 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
50 PTHREAD_MUTEX_NORMAL
= PTHREAD_MUTEX_TIMED_NP
,
51 PTHREAD_MUTEX_RECURSIVE
= PTHREAD_MUTEX_RECURSIVE_NP
,
52 PTHREAD_MUTEX_ERRORCHECK
= PTHREAD_MUTEX_ERRORCHECK_NP
,
53 PTHREAD_MUTEX_DEFAULT
= PTHREAD_MUTEX_NORMAL
56 /* For compatibility. */
57 , PTHREAD_MUTEX_FAST_NP
= PTHREAD_MUTEX_TIMED_NP
63 /* Robust mutex or not flags. */
66 PTHREAD_MUTEX_STALLED
,
67 PTHREAD_MUTEX_STALLED_NP
= PTHREAD_MUTEX_STALLED
,
69 PTHREAD_MUTEX_ROBUST_NP
= PTHREAD_MUTEX_ROBUST
75 /* Mutex protocols. */
85 /* Mutex initializers. */
87 # define PTHREAD_MUTEX_INITIALIZER \
88 { { 0, 0, 0, 0, 0, 0, { 0, 0 } } }
90 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
91 { { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { 0, 0 } } }
92 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
93 { { 0, 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { 0, 0 } } }
94 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
95 { { 0, 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { 0, 0 } } }
98 # define PTHREAD_MUTEX_INITIALIZER \
99 { { 0, 0, 0, 0, 0, { 0 } } }
101 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
102 { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { 0 } } }
103 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
104 { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { 0 } } }
105 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
106 { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { 0 } } }
111 /* Read-write lock types. */
112 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
115 PTHREAD_RWLOCK_PREFER_READER_NP
,
116 PTHREAD_RWLOCK_PREFER_WRITER_NP
,
117 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
,
118 PTHREAD_RWLOCK_DEFAULT_NP
= PTHREAD_RWLOCK_PREFER_READER_NP
121 /* Read-write lock initializers. */
122 # define PTHREAD_RWLOCK_INITIALIZER \
123 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
125 # if __WORDSIZE == 64
126 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
127 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
128 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
130 # if __BYTE_ORDER == __LITTLE_ENDIAN
131 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
132 { { 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, \
135 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
136 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,\
141 #endif /* Unix98 or XOpen2K */
144 /* Scheduler inheritance. */
147 PTHREAD_INHERIT_SCHED
,
148 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
149 PTHREAD_EXPLICIT_SCHED
150 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
154 /* Scope handling. */
157 PTHREAD_SCOPE_SYSTEM
,
158 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
159 PTHREAD_SCOPE_PROCESS
160 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
164 /* Process shared or private flag. */
167 PTHREAD_PROCESS_PRIVATE
,
168 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
169 PTHREAD_PROCESS_SHARED
170 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
175 /* Conditional variable handling. */
176 #define PTHREAD_COND_INITIALIZER { { 0, 0, 0, 0, 0, (void *) 0, 0, 0 } }
179 /* Cleanup buffers */
180 struct _pthread_cleanup_buffer
182 void (*__routine
) (void *); /* Function to call. */
183 void *__arg
; /* Its argument. */
184 int __canceltype
; /* Saved cancellation type. */
185 struct _pthread_cleanup_buffer
*__prev
; /* Chaining of cleanup functions. */
191 PTHREAD_CANCEL_ENABLE
,
192 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
193 PTHREAD_CANCEL_DISABLE
194 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
198 PTHREAD_CANCEL_DEFERRED
,
199 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
200 PTHREAD_CANCEL_ASYNCHRONOUS
201 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
203 #define PTHREAD_CANCELED ((void *) -1)
206 /* Single execution handling. */
207 #define PTHREAD_ONCE_INIT 0
211 /* Value returned by 'pthread_barrier_wait' for one of the threads after
212 the required number of threads have called this function.
213 -1 is distinct from 0 and all errno constants */
214 # define PTHREAD_BARRIER_SERIAL_THREAD -1
220 /* Create a new thread, starting with execution of START-ROUTINE
221 getting passed ARG. Creation attributed come from ATTR. The new
222 handle is stored in *NEWTHREAD. */
223 extern int pthread_create (pthread_t
*__restrict __newthread
,
224 const pthread_attr_t
*__restrict __attr
,
225 void *(*__start_routine
) (void *),
226 void *__restrict __arg
) __THROWNL
__nonnull ((1, 3));
228 /* Terminate calling thread.
230 The registered cleanup handlers are called via exception handling
231 so we cannot mark this function with __THROW.*/
232 extern void pthread_exit (void *__retval
) __attribute__ ((__noreturn__
));
234 /* Make calling thread wait for termination of the thread TH. The
235 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
238 This function is a cancellation point and therefore not marked with
240 extern int pthread_join (pthread_t __th
, void **__thread_return
);
243 /* Check whether thread TH has terminated. If yes return the status of
244 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
245 extern int pthread_tryjoin_np (pthread_t __th
, void **__thread_return
) __THROW
;
247 /* Make calling thread wait for termination of the thread TH, but only
248 until TIMEOUT. The exit status of the thread is stored in
249 *THREAD_RETURN, if THREAD_RETURN is not NULL.
251 This function is a cancellation point and therefore not marked with
253 extern int pthread_timedjoin_np (pthread_t __th
, void **__thread_return
,
254 const struct timespec
*__abstime
);
257 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
258 The resources of TH will therefore be freed immediately when it
259 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
261 extern int pthread_detach (pthread_t __th
) __THROW
;
264 /* Obtain the identifier of the current thread. */
265 extern pthread_t
pthread_self (void) __THROW
__attribute__ ((__const__
));
267 /* Compare two thread identifiers. */
268 extern int pthread_equal (pthread_t __thread1
, pthread_t __thread2
)
269 __THROW
__attribute__ ((__const__
));
272 /* Thread attribute handling. */
274 /* Initialize thread attribute *ATTR with default attributes
275 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
276 no user-provided stack). */
277 extern int pthread_attr_init (pthread_attr_t
*__attr
) __THROW
__nonnull ((1));
279 /* Destroy thread attribute *ATTR. */
280 extern int pthread_attr_destroy (pthread_attr_t
*__attr
)
281 __THROW
__nonnull ((1));
283 /* Get detach state attribute. */
284 extern int pthread_attr_getdetachstate (const pthread_attr_t
*__attr
,
286 __THROW
__nonnull ((1, 2));
288 /* Set detach state attribute. */
289 extern int pthread_attr_setdetachstate (pthread_attr_t
*__attr
,
291 __THROW
__nonnull ((1));
294 /* Get the size of the guard area created for stack overflow protection. */
295 extern int pthread_attr_getguardsize (const pthread_attr_t
*__attr
,
297 __THROW
__nonnull ((1, 2));
299 /* Set the size of the guard area created for stack overflow protection. */
300 extern int pthread_attr_setguardsize (pthread_attr_t
*__attr
,
302 __THROW
__nonnull ((1));
305 /* Return in *PARAM the scheduling parameters of *ATTR. */
306 extern int pthread_attr_getschedparam (const pthread_attr_t
*__restrict __attr
,
307 struct sched_param
*__restrict __param
)
308 __THROW
__nonnull ((1, 2));
310 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
311 extern int pthread_attr_setschedparam (pthread_attr_t
*__restrict __attr
,
312 const struct sched_param
*__restrict
313 __param
) __THROW
__nonnull ((1, 2));
315 /* Return in *POLICY the scheduling policy of *ATTR. */
316 extern int pthread_attr_getschedpolicy (const pthread_attr_t
*__restrict
317 __attr
, int *__restrict __policy
)
318 __THROW
__nonnull ((1, 2));
320 /* Set scheduling policy in *ATTR according to POLICY. */
321 extern int pthread_attr_setschedpolicy (pthread_attr_t
*__attr
, int __policy
)
322 __THROW
__nonnull ((1));
324 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
325 extern int pthread_attr_getinheritsched (const pthread_attr_t
*__restrict
326 __attr
, int *__restrict __inherit
)
327 __THROW
__nonnull ((1, 2));
329 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
330 extern int pthread_attr_setinheritsched (pthread_attr_t
*__attr
,
332 __THROW
__nonnull ((1));
335 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
336 extern int pthread_attr_getscope (const pthread_attr_t
*__restrict __attr
,
337 int *__restrict __scope
)
338 __THROW
__nonnull ((1, 2));
340 /* Set scheduling contention scope in *ATTR according to SCOPE. */
341 extern int pthread_attr_setscope (pthread_attr_t
*__attr
, int __scope
)
342 __THROW
__nonnull ((1));
344 /* Return the previously set address for the stack. */
345 extern int pthread_attr_getstackaddr (const pthread_attr_t
*__restrict
346 __attr
, void **__restrict __stackaddr
)
347 __THROW
__nonnull ((1, 2)) __attribute_deprecated__
;
349 /* Set the starting address of the stack of the thread to be created.
350 Depending on whether the stack grows up or down the value must either
351 be higher or lower than all the address in the memory block. The
352 minimal size of the block must be PTHREAD_STACK_MIN. */
353 extern int pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
355 __THROW
__nonnull ((1)) __attribute_deprecated__
;
357 /* Return the currently used minimal stack size. */
358 extern int pthread_attr_getstacksize (const pthread_attr_t
*__restrict
359 __attr
, size_t *__restrict __stacksize
)
360 __THROW
__nonnull ((1, 2));
362 /* Add information about the minimum stack size needed for the thread
363 to be started. This size must never be less than PTHREAD_STACK_MIN
364 and must also not exceed the system limits. */
365 extern int pthread_attr_setstacksize (pthread_attr_t
*__attr
,
367 __THROW
__nonnull ((1));
370 /* Return the previously set address for the stack. */
371 extern int pthread_attr_getstack (const pthread_attr_t
*__restrict __attr
,
372 void **__restrict __stackaddr
,
373 size_t *__restrict __stacksize
)
374 __THROW
__nonnull ((1, 2, 3));
376 /* The following two interfaces are intended to replace the last two. They
377 require setting the address as well as the size since only setting the
378 address will make the implementation on some architectures impossible. */
379 extern int pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
380 size_t __stacksize
) __THROW
__nonnull ((1));
384 /* Thread created with attribute ATTR will be limited to run only on
385 the processors represented in CPUSET. */
386 extern int pthread_attr_setaffinity_np (pthread_attr_t
*__attr
,
388 const cpu_set_t
*__cpuset
)
389 __THROW
__nonnull ((1, 3));
391 /* Get bit set in CPUSET representing the processors threads created with
393 extern int pthread_attr_getaffinity_np (const pthread_attr_t
*__attr
,
396 __THROW
__nonnull ((1, 3));
399 /* Initialize thread attribute *ATTR with attributes corresponding to the
400 already running thread TH. It shall be called on uninitialized ATTR
401 and destroyed with pthread_attr_destroy when no longer needed. */
402 extern int pthread_getattr_np (pthread_t __th
, pthread_attr_t
*__attr
)
403 __THROW
__nonnull ((2));
407 /* Functions for scheduling control. */
409 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
411 extern int pthread_setschedparam (pthread_t __target_thread
, int __policy
,
412 const struct sched_param
*__param
)
413 __THROW
__nonnull ((3));
415 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
416 extern int pthread_getschedparam (pthread_t __target_thread
,
417 int *__restrict __policy
,
418 struct sched_param
*__restrict __param
)
419 __THROW
__nonnull ((2, 3));
421 /* Set the scheduling priority for TARGET_THREAD. */
422 extern int pthread_setschedprio (pthread_t __target_thread
, int __prio
)
427 /* Get thread name visible in the kernel and its interfaces. */
428 extern int pthread_getname_np (pthread_t __target_thread
, char *__buf
,
430 __THROW
__nonnull ((2));
432 /* Set thread name visible in the kernel and its interfaces. */
433 extern int pthread_setname_np (pthread_t __target_thread
, const char *__name
)
434 __THROW
__nonnull ((2));
439 /* Determine level of concurrency. */
440 extern int pthread_getconcurrency (void) __THROW
;
442 /* Set new concurrency level to LEVEL. */
443 extern int pthread_setconcurrency (int __level
) __THROW
;
447 /* Yield the processor to another thread or process.
448 This function is similar to the POSIX `sched_yield' function but
449 might be differently implemented in the case of a m-on-n thread
451 extern int pthread_yield (void) __THROW
;
454 /* Limit specified thread TH to run only on the processors represented
456 extern int pthread_setaffinity_np (pthread_t __th
, size_t __cpusetsize
,
457 const cpu_set_t
*__cpuset
)
458 __THROW
__nonnull ((3));
460 /* Get bit set in CPUSET representing the processors TH can run on. */
461 extern int pthread_getaffinity_np (pthread_t __th
, size_t __cpusetsize
,
463 __THROW
__nonnull ((3));
467 /* Functions for handling initialization. */
469 /* Guarantee that the initialization function INIT_ROUTINE will be called
470 only once, even if pthread_once is executed several times with the
471 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
472 extern variable initialized to PTHREAD_ONCE_INIT.
474 The initialization functions might throw exception which is why
475 this function is not marked with __THROW. */
476 extern int pthread_once (pthread_once_t
*__once_control
,
477 void (*__init_routine
) (void)) __nonnull ((1, 2));
480 /* Functions for handling cancellation.
482 Note that these functions are explicitly not marked to not throw an
483 exception in C++ code. If cancellation is implemented by unwinding
484 this is necessary to have the compiler generate the unwind information. */
486 /* Set cancelability state of current thread to STATE, returning old
487 state in *OLDSTATE if OLDSTATE is not NULL. */
488 extern int pthread_setcancelstate (int __state
, int *__oldstate
);
490 /* Set cancellation state of current thread to TYPE, returning the old
491 type in *OLDTYPE if OLDTYPE is not NULL. */
492 extern int pthread_setcanceltype (int __type
, int *__oldtype
);
494 /* Cancel THREAD immediately or at the next possibility. */
495 extern int pthread_cancel (pthread_t __th
);
497 /* Test for pending cancellation for the current thread and terminate
498 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
500 extern void pthread_testcancel (void);
503 /* Cancellation handling with integration into exception handling. */
509 __jmp_buf __cancel_jmp_buf
;
510 int __mask_was_saved
;
511 } __cancel_jmp_buf
[1];
513 } __pthread_unwind_buf_t
__attribute__ ((__aligned__
));
515 /* No special attributes by default. */
516 #ifndef __cleanup_fct_attribute
517 # define __cleanup_fct_attribute
521 /* Structure to hold the cleanup handler information. */
522 struct __pthread_cleanup_frame
524 void (*__cancel_routine
) (void *);
530 #if defined __GNUC__ && defined __EXCEPTIONS
532 /* Class to handle cancellation handler invocation. */
533 class __pthread_cleanup_class
535 void (*__cancel_routine
) (void *);
541 __pthread_cleanup_class (void (*__fct
) (void *), void *__arg
)
542 : __cancel_routine (__fct
), __cancel_arg (__arg
), __do_it (1) { }
543 ~__pthread_cleanup_class () { if (__do_it
) __cancel_routine (__cancel_arg
); }
544 void __setdoit (int __newval
) { __do_it
= __newval
; }
545 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
,
547 void __restore () const { pthread_setcanceltype (__cancel_type
, 0); }
550 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
551 when the thread is canceled or calls pthread_exit. ROUTINE will also
552 be called with arguments ARG when the matching pthread_cleanup_pop
553 is executed with non-zero EXECUTE argument.
555 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
556 be used in matching pairs at the same nesting level of braces. */
557 # define pthread_cleanup_push(routine, arg) \
559 __pthread_cleanup_class __clframe (routine, arg)
561 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
562 If EXECUTE is non-zero, the handler function is called. */
563 # define pthread_cleanup_pop(execute) \
564 __clframe.__setdoit (execute); \
568 /* Install a cleanup handler as pthread_cleanup_push does, but also
569 saves the current cancellation type and sets it to deferred
571 # define pthread_cleanup_push_defer_np(routine, arg) \
573 __pthread_cleanup_class __clframe (routine, arg); \
576 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
577 restores the cancellation type that was in effect when the matching
578 pthread_cleanup_push_defer was called. */
579 # define pthread_cleanup_pop_restore_np(execute) \
580 __clframe.__restore (); \
581 __clframe.__setdoit (execute); \
585 /* Function called to call the cleanup handler. As an extern inline
586 function the compiler is free to decide inlining the change when
587 needed or fall back on the copy which must exist somewhere
590 __pthread_cleanup_routine (struct __pthread_cleanup_frame
*__frame
)
592 if (__frame
->__do_it
)
593 __frame
->__cancel_routine (__frame
->__cancel_arg
);
596 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
597 when the thread is canceled or calls pthread_exit. ROUTINE will also
598 be called with arguments ARG when the matching pthread_cleanup_pop
599 is executed with non-zero EXECUTE argument.
601 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
602 be used in matching pairs at the same nesting level of braces. */
603 # define pthread_cleanup_push(routine, arg) \
605 struct __pthread_cleanup_frame __clframe \
606 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
607 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
610 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
611 If EXECUTE is non-zero, the handler function is called. */
612 # define pthread_cleanup_pop(execute) \
613 __clframe.__do_it = (execute); \
617 /* Install a cleanup handler as pthread_cleanup_push does, but also
618 saves the current cancellation type and sets it to deferred
620 # define pthread_cleanup_push_defer_np(routine, arg) \
622 struct __pthread_cleanup_frame __clframe \
623 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
624 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
626 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
627 &__clframe.__cancel_type)
629 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
630 restores the cancellation type that was in effect when the matching
631 pthread_cleanup_push_defer was called. */
632 # define pthread_cleanup_pop_restore_np(execute) \
633 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
634 __clframe.__do_it = (execute); \
639 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
640 when the thread is canceled or calls pthread_exit. ROUTINE will also
641 be called with arguments ARG when the matching pthread_cleanup_pop
642 is executed with non-zero EXECUTE argument.
644 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
645 be used in matching pairs at the same nesting level of braces. */
646 # define pthread_cleanup_push(routine, arg) \
648 __pthread_unwind_buf_t __cancel_buf; \
649 void (*__cancel_routine) (void *) = (routine); \
650 void *__cancel_arg = (arg); \
651 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
652 __cancel_buf.__cancel_jmp_buf, 0); \
653 if (__builtin_expect (__not_first_call, 0)) \
655 __cancel_routine (__cancel_arg); \
656 __pthread_unwind_next (&__cancel_buf); \
660 __pthread_register_cancel (&__cancel_buf); \
662 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
663 __cleanup_fct_attribute
;
665 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
666 If EXECUTE is non-zero, the handler function is called. */
667 # define pthread_cleanup_pop(execute) \
668 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
670 __pthread_unregister_cancel (&__cancel_buf); \
672 __cancel_routine (__cancel_arg); \
674 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
675 __cleanup_fct_attribute
;
678 /* Install a cleanup handler as pthread_cleanup_push does, but also
679 saves the current cancellation type and sets it to deferred
681 # define pthread_cleanup_push_defer_np(routine, arg) \
683 __pthread_unwind_buf_t __cancel_buf; \
684 void (*__cancel_routine) (void *) = (routine); \
685 void *__cancel_arg = (arg); \
686 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
687 __cancel_buf.__cancel_jmp_buf, 0); \
688 if (__builtin_expect (__not_first_call, 0)) \
690 __cancel_routine (__cancel_arg); \
691 __pthread_unwind_next (&__cancel_buf); \
695 __pthread_register_cancel_defer (&__cancel_buf); \
697 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t
*__buf
)
698 __cleanup_fct_attribute
;
700 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
701 restores the cancellation type that was in effect when the matching
702 pthread_cleanup_push_defer was called. */
703 # define pthread_cleanup_pop_restore_np(execute) \
704 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
706 __pthread_unregister_cancel_restore (&__cancel_buf); \
708 __cancel_routine (__cancel_arg); \
710 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t
*__buf
)
711 __cleanup_fct_attribute
;
714 /* Internal interface to initiate cleanup. */
715 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
716 __cleanup_fct_attribute
__attribute__ ((__noreturn__
))
718 __attribute__ ((__weak__
))
723 /* Function used in the macros. */
724 struct __jmp_buf_tag
;
725 extern int __sigsetjmp (struct __jmp_buf_tag
*__env
, int __savemask
) __THROW
;
728 /* Mutex handling. */
730 /* Initialize a mutex. */
731 extern int pthread_mutex_init (pthread_mutex_t
*__mutex
,
732 const pthread_mutexattr_t
*__mutexattr
)
733 __THROW
__nonnull ((1));
735 /* Destroy a mutex. */
736 extern int pthread_mutex_destroy (pthread_mutex_t
*__mutex
)
737 __THROW
__nonnull ((1));
739 /* Try locking a mutex. */
740 extern int pthread_mutex_trylock (pthread_mutex_t
*__mutex
)
741 __THROWNL
__nonnull ((1));
744 extern int pthread_mutex_lock (pthread_mutex_t
*__mutex
)
745 __THROWNL
__nonnull ((1));
748 /* Wait until lock becomes available, or specified time passes. */
749 extern int pthread_mutex_timedlock (pthread_mutex_t
*__restrict __mutex
,
750 const struct timespec
*__restrict
751 __abstime
) __THROWNL
__nonnull ((1, 2));
754 /* Unlock a mutex. */
755 extern int pthread_mutex_unlock (pthread_mutex_t
*__mutex
)
756 __THROWNL
__nonnull ((1));
759 /* Get the priority ceiling of MUTEX. */
760 extern int pthread_mutex_getprioceiling (const pthread_mutex_t
*
762 int *__restrict __prioceiling
)
763 __THROW
__nonnull ((1, 2));
765 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
766 priority ceiling value in *OLD_CEILING. */
767 extern int pthread_mutex_setprioceiling (pthread_mutex_t
*__restrict __mutex
,
769 int *__restrict __old_ceiling
)
770 __THROW
__nonnull ((1, 3));
773 #ifdef __USE_XOPEN2K8
774 /* Declare the state protected by MUTEX as consistent. */
775 extern int pthread_mutex_consistent (pthread_mutex_t
*__mutex
)
776 __THROW
__nonnull ((1));
778 extern int pthread_mutex_consistent_np (pthread_mutex_t
*__mutex
)
779 __THROW
__nonnull ((1));
784 /* Functions for handling mutex attributes. */
786 /* Initialize mutex attribute object ATTR with default attributes
787 (kind is PTHREAD_MUTEX_TIMED_NP). */
788 extern int pthread_mutexattr_init (pthread_mutexattr_t
*__attr
)
789 __THROW
__nonnull ((1));
791 /* Destroy mutex attribute object ATTR. */
792 extern int pthread_mutexattr_destroy (pthread_mutexattr_t
*__attr
)
793 __THROW
__nonnull ((1));
795 /* Get the process-shared flag of the mutex attribute ATTR. */
796 extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t
*
798 int *__restrict __pshared
)
799 __THROW
__nonnull ((1, 2));
801 /* Set the process-shared flag of the mutex attribute ATTR. */
802 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t
*__attr
,
804 __THROW
__nonnull ((1));
806 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
807 /* Return in *KIND the mutex kind attribute in *ATTR. */
808 extern int pthread_mutexattr_gettype (const pthread_mutexattr_t
*__restrict
809 __attr
, int *__restrict __kind
)
810 __THROW
__nonnull ((1, 2));
812 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
813 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
814 PTHREAD_MUTEX_DEFAULT). */
815 extern int pthread_mutexattr_settype (pthread_mutexattr_t
*__attr
, int __kind
)
816 __THROW
__nonnull ((1));
819 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
820 extern int pthread_mutexattr_getprotocol (const pthread_mutexattr_t
*
822 int *__restrict __protocol
)
823 __THROW
__nonnull ((1, 2));
825 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
826 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
827 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t
*__attr
,
829 __THROW
__nonnull ((1));
831 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
832 extern int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t
*
834 int *__restrict __prioceiling
)
835 __THROW
__nonnull ((1, 2));
837 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
838 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t
*__attr
,
840 __THROW
__nonnull ((1));
843 /* Get the robustness flag of the mutex attribute ATTR. */
844 extern int pthread_mutexattr_getrobust (const pthread_mutexattr_t
*__attr
,
846 __THROW
__nonnull ((1, 2));
848 extern int pthread_mutexattr_getrobust_np (const pthread_mutexattr_t
*__attr
,
850 __THROW
__nonnull ((1, 2));
853 /* Set the robustness flag of the mutex attribute ATTR. */
854 extern int pthread_mutexattr_setrobust (pthread_mutexattr_t
*__attr
,
856 __THROW
__nonnull ((1));
858 extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t
*__attr
,
860 __THROW
__nonnull ((1));
865 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
866 /* Functions for handling read-write locks. */
868 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
869 the default values if later is NULL. */
870 extern int pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
871 const pthread_rwlockattr_t
*__restrict
872 __attr
) __THROW
__nonnull ((1));
874 /* Destroy read-write lock RWLOCK. */
875 extern int pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
)
876 __THROW
__nonnull ((1));
878 /* Acquire read lock for RWLOCK. */
879 extern int pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
)
880 __THROWNL
__nonnull ((1));
882 /* Try to acquire read lock for RWLOCK. */
883 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
)
884 __THROWNL
__nonnull ((1));
886 # ifdef __USE_XOPEN2K
887 /* Try to acquire read lock for RWLOCK or return after specfied time. */
888 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t
*__restrict __rwlock
,
889 const struct timespec
*__restrict
890 __abstime
) __THROWNL
__nonnull ((1, 2));
893 /* Acquire write lock for RWLOCK. */
894 extern int pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
)
895 __THROWNL
__nonnull ((1));
897 /* Try to acquire write lock for RWLOCK. */
898 extern int pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
)
899 __THROWNL
__nonnull ((1));
901 # ifdef __USE_XOPEN2K
902 /* Try to acquire write lock for RWLOCK or return after specfied time. */
903 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t
*__restrict __rwlock
,
904 const struct timespec
*__restrict
905 __abstime
) __THROWNL
__nonnull ((1, 2));
909 extern int pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
)
910 __THROWNL
__nonnull ((1));
913 /* Functions for handling read-write lock attributes. */
915 /* Initialize attribute object ATTR with default values. */
916 extern int pthread_rwlockattr_init (pthread_rwlockattr_t
*__attr
)
917 __THROW
__nonnull ((1));
919 /* Destroy attribute object ATTR. */
920 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t
*__attr
)
921 __THROW
__nonnull ((1));
923 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
924 extern int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t
*
926 int *__restrict __pshared
)
927 __THROW
__nonnull ((1, 2));
929 /* Set process-shared attribute of ATTR to PSHARED. */
930 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t
*__attr
,
932 __THROW
__nonnull ((1));
934 /* Return current setting of reader/writer preference. */
935 extern int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t
*
937 int *__restrict __pref
)
938 __THROW
__nonnull ((1, 2));
940 /* Set reader/write preference. */
941 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t
*__attr
,
942 int __pref
) __THROW
__nonnull ((1));
946 /* Functions for handling conditional variables. */
948 /* Initialize condition variable COND using attributes ATTR, or use
949 the default values if later is NULL. */
950 extern int pthread_cond_init (pthread_cond_t
*__restrict __cond
,
951 const pthread_condattr_t
*__restrict __cond_attr
)
952 __THROW
__nonnull ((1));
954 /* Destroy condition variable COND. */
955 extern int pthread_cond_destroy (pthread_cond_t
*__cond
)
956 __THROW
__nonnull ((1));
958 /* Wake up one thread waiting for condition variable COND. */
959 extern int pthread_cond_signal (pthread_cond_t
*__cond
)
960 __THROWNL
__nonnull ((1));
962 /* Wake up all threads waiting for condition variables COND. */
963 extern int pthread_cond_broadcast (pthread_cond_t
*__cond
)
964 __THROWNL
__nonnull ((1));
966 /* Wait for condition variable COND to be signaled or broadcast.
967 MUTEX is assumed to be locked before.
969 This function is a cancellation point and therefore not marked with
971 extern int pthread_cond_wait (pthread_cond_t
*__restrict __cond
,
972 pthread_mutex_t
*__restrict __mutex
)
975 /* Wait for condition variable COND to be signaled or broadcast until
976 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
977 absolute time specification; zero is the beginning of the epoch
978 (00:00:00 GMT, January 1, 1970).
980 This function is a cancellation point and therefore not marked with
982 extern int pthread_cond_timedwait (pthread_cond_t
*__restrict __cond
,
983 pthread_mutex_t
*__restrict __mutex
,
984 const struct timespec
*__restrict __abstime
)
985 __nonnull ((1, 2, 3));
987 /* Functions for handling condition variable attributes. */
989 /* Initialize condition variable attribute ATTR. */
990 extern int pthread_condattr_init (pthread_condattr_t
*__attr
)
991 __THROW
__nonnull ((1));
993 /* Destroy condition variable attribute ATTR. */
994 extern int pthread_condattr_destroy (pthread_condattr_t
*__attr
)
995 __THROW
__nonnull ((1));
997 /* Get the process-shared flag of the condition variable attribute ATTR. */
998 extern int pthread_condattr_getpshared (const pthread_condattr_t
*
1000 int *__restrict __pshared
)
1001 __THROW
__nonnull ((1, 2));
1003 /* Set the process-shared flag of the condition variable attribute ATTR. */
1004 extern int pthread_condattr_setpshared (pthread_condattr_t
*__attr
,
1005 int __pshared
) __THROW
__nonnull ((1));
1007 #ifdef __USE_XOPEN2K
1008 /* Get the clock selected for the conditon variable attribute ATTR. */
1009 extern int pthread_condattr_getclock (const pthread_condattr_t
*
1011 __clockid_t
*__restrict __clock_id
)
1012 __THROW
__nonnull ((1, 2));
1014 /* Set the clock selected for the conditon variable attribute ATTR. */
1015 extern int pthread_condattr_setclock (pthread_condattr_t
*__attr
,
1016 __clockid_t __clock_id
)
1017 __THROW
__nonnull ((1));
1021 #ifdef __USE_XOPEN2K
1022 /* Functions to handle spinlocks. */
1024 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
1025 be shared between different processes. */
1026 extern int pthread_spin_init (pthread_spinlock_t
*__lock
, int __pshared
)
1027 __THROW
__nonnull ((1));
1029 /* Destroy the spinlock LOCK. */
1030 extern int pthread_spin_destroy (pthread_spinlock_t
*__lock
)
1031 __THROW
__nonnull ((1));
1033 /* Wait until spinlock LOCK is retrieved. */
1034 extern int pthread_spin_lock (pthread_spinlock_t
*__lock
)
1035 __THROWNL
__nonnull ((1));
1037 /* Try to lock spinlock LOCK. */
1038 extern int pthread_spin_trylock (pthread_spinlock_t
*__lock
)
1039 __THROWNL
__nonnull ((1));
1041 /* Release spinlock LOCK. */
1042 extern int pthread_spin_unlock (pthread_spinlock_t
*__lock
)
1043 __THROWNL
__nonnull ((1));
1046 /* Functions to handle barriers. */
1048 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1049 opened when COUNT waiters arrived. */
1050 extern int pthread_barrier_init (pthread_barrier_t
*__restrict __barrier
,
1051 const pthread_barrierattr_t
*__restrict
1052 __attr
, unsigned int __count
)
1053 __THROW
__nonnull ((1));
1055 /* Destroy a previously dynamically initialized barrier BARRIER. */
1056 extern int pthread_barrier_destroy (pthread_barrier_t
*__barrier
)
1057 __THROW
__nonnull ((1));
1059 /* Wait on barrier BARRIER. */
1060 extern int pthread_barrier_wait (pthread_barrier_t
*__barrier
)
1061 __THROWNL
__nonnull ((1));
1064 /* Initialize barrier attribute ATTR. */
1065 extern int pthread_barrierattr_init (pthread_barrierattr_t
*__attr
)
1066 __THROW
__nonnull ((1));
1068 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1069 extern int pthread_barrierattr_destroy (pthread_barrierattr_t
*__attr
)
1070 __THROW
__nonnull ((1));
1072 /* Get the process-shared flag of the barrier attribute ATTR. */
1073 extern int pthread_barrierattr_getpshared (const pthread_barrierattr_t
*
1075 int *__restrict __pshared
)
1076 __THROW
__nonnull ((1, 2));
1078 /* Set the process-shared flag of the barrier attribute ATTR. */
1079 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t
*__attr
,
1081 __THROW
__nonnull ((1));
1085 /* Functions for handling thread-specific data. */
1087 /* Create a key value identifying a location in the thread-specific
1088 data area. Each thread maintains a distinct thread-specific data
1089 area. DESTR_FUNCTION, if non-NULL, is called with the value
1090 associated to that key when the key is destroyed.
1091 DESTR_FUNCTION is not called if the value associated is NULL when
1092 the key is destroyed. */
1093 extern int pthread_key_create (pthread_key_t
*__key
,
1094 void (*__destr_function
) (void *))
1095 __THROW
__nonnull ((1));
1098 extern int pthread_key_delete (pthread_key_t __key
) __THROW
;
1100 /* Return current value of the thread-specific data slot identified by KEY. */
1101 extern void *pthread_getspecific (pthread_key_t __key
) __THROW
;
1103 /* Store POINTER in the thread-specific data slot identified by KEY. */
1104 extern int pthread_setspecific (pthread_key_t __key
,
1105 const void *__pointer
) __THROW
;
1108 #ifdef __USE_XOPEN2K
1109 /* Get ID of CPU-time clock for thread THREAD_ID. */
1110 extern int pthread_getcpuclockid (pthread_t __thread_id
,
1111 __clockid_t
*__clock_id
)
1112 __THROW
__nonnull ((2));
1116 /* Install handlers to be called when a new process is created with FORK.
1117 The PREPARE handler is called in the parent process just before performing
1118 FORK. The PARENT handler is called in the parent process just after FORK.
1119 The CHILD handler is called in the child process. Each of the three
1120 handlers can be NULL, meaning that no handler needs to be called at that
1122 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1123 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1124 first called before FORK), and the PARENT and CHILD handlers are called
1125 in FIFO (first added, first called). */
1127 extern int pthread_atfork (void (*__prepare
) (void),
1128 void (*__parent
) (void),
1129 void (*__child
) (void)) __THROW
;
1132 #ifdef __USE_EXTERN_INLINES
1133 /* Optimizations. */
1135 __NTH (pthread_equal (pthread_t __thread1
, pthread_t __thread2
))
1137 return __thread1
== __thread2
;
1143 #endif /* pthread.h */