1 /* Copyright (C) 2002-2013 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
74 #if defined __USE_POSIX199506 || defined __USE_UNIX98
75 /* Mutex protocols. */
85 /* Mutex initializers. */
86 #ifdef __PTHREAD_MUTEX_HAVE_PREV
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 /* Define __PTHREAD_RWLOCK_INT_FLAGS_SHARED to 1 if pthread_rwlock_t
122 has the shared field. All 64-bit architectures have the shared field
123 in pthread_rwlock_t. */
124 #ifndef __PTHREAD_RWLOCK_INT_FLAGS_SHARED
125 # if __WORDSIZE == 64
126 # define __PTHREAD_RWLOCK_INT_FLAGS_SHARED 1
130 /* Read-write lock initializers. */
131 # define PTHREAD_RWLOCK_INITIALIZER \
132 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
134 # ifdef __PTHREAD_RWLOCK_INT_FLAGS_SHARED
135 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
136 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
137 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
139 # if __BYTE_ORDER == __LITTLE_ENDIAN
140 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
141 { { 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, \
144 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
145 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,\
150 #endif /* Unix98 or XOpen2K */
153 /* Scheduler inheritance. */
156 PTHREAD_INHERIT_SCHED
,
157 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
158 PTHREAD_EXPLICIT_SCHED
159 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
163 /* Scope handling. */
166 PTHREAD_SCOPE_SYSTEM
,
167 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
168 PTHREAD_SCOPE_PROCESS
169 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
173 /* Process shared or private flag. */
176 PTHREAD_PROCESS_PRIVATE
,
177 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
178 PTHREAD_PROCESS_SHARED
179 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
184 /* Conditional variable handling. */
185 #define PTHREAD_COND_INITIALIZER { { 0, 0, 0, 0, 0, (void *) 0, 0, 0 } }
188 /* Cleanup buffers */
189 struct _pthread_cleanup_buffer
191 void (*__routine
) (void *); /* Function to call. */
192 void *__arg
; /* Its argument. */
193 int __canceltype
; /* Saved cancellation type. */
194 struct _pthread_cleanup_buffer
*__prev
; /* Chaining of cleanup functions. */
200 PTHREAD_CANCEL_ENABLE
,
201 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
202 PTHREAD_CANCEL_DISABLE
203 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
207 PTHREAD_CANCEL_DEFERRED
,
208 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
209 PTHREAD_CANCEL_ASYNCHRONOUS
210 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
212 #define PTHREAD_CANCELED ((void *) -1)
215 /* Single execution handling. */
216 #define PTHREAD_ONCE_INIT 0
220 /* Value returned by 'pthread_barrier_wait' for one of the threads after
221 the required number of threads have called this function.
222 -1 is distinct from 0 and all errno constants */
223 # define PTHREAD_BARRIER_SERIAL_THREAD -1
229 /* Create a new thread, starting with execution of START-ROUTINE
230 getting passed ARG. Creation attributed come from ATTR. The new
231 handle is stored in *NEWTHREAD. */
232 extern int pthread_create (pthread_t
*__restrict __newthread
,
233 const pthread_attr_t
*__restrict __attr
,
234 void *(*__start_routine
) (void *),
235 void *__restrict __arg
) __THROWNL
__nonnull ((1, 3));
237 /* Terminate calling thread.
239 The registered cleanup handlers are called via exception handling
240 so we cannot mark this function with __THROW.*/
241 extern void pthread_exit (void *__retval
) __attribute__ ((__noreturn__
));
243 /* Make calling thread wait for termination of the thread TH. The
244 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
247 This function is a cancellation point and therefore not marked with
249 extern int pthread_join (pthread_t __th
, void **__thread_return
);
252 /* Check whether thread TH has terminated. If yes return the status of
253 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
254 extern int pthread_tryjoin_np (pthread_t __th
, void **__thread_return
) __THROW
;
256 /* Make calling thread wait for termination of the thread TH, but only
257 until TIMEOUT. The exit status of the thread is stored in
258 *THREAD_RETURN, if THREAD_RETURN is not NULL.
260 This function is a cancellation point and therefore not marked with
262 extern int pthread_timedjoin_np (pthread_t __th
, void **__thread_return
,
263 const struct timespec
*__abstime
);
266 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
267 The resources of TH will therefore be freed immediately when it
268 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
270 extern int pthread_detach (pthread_t __th
) __THROW
;
273 /* Obtain the identifier of the current thread. */
274 extern pthread_t
pthread_self (void) __THROW
__attribute__ ((__const__
));
276 /* Compare two thread identifiers. */
277 extern int pthread_equal (pthread_t __thread1
, pthread_t __thread2
)
278 __THROW
__attribute__ ((__const__
));
281 /* Thread attribute handling. */
283 /* Initialize thread attribute *ATTR with default attributes
284 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
285 no user-provided stack). */
286 extern int pthread_attr_init (pthread_attr_t
*__attr
) __THROW
__nonnull ((1));
288 /* Destroy thread attribute *ATTR. */
289 extern int pthread_attr_destroy (pthread_attr_t
*__attr
)
290 __THROW
__nonnull ((1));
292 /* Get detach state attribute. */
293 extern int pthread_attr_getdetachstate (const pthread_attr_t
*__attr
,
295 __THROW
__nonnull ((1, 2));
297 /* Set detach state attribute. */
298 extern int pthread_attr_setdetachstate (pthread_attr_t
*__attr
,
300 __THROW
__nonnull ((1));
303 /* Get the size of the guard area created for stack overflow protection. */
304 extern int pthread_attr_getguardsize (const pthread_attr_t
*__attr
,
306 __THROW
__nonnull ((1, 2));
308 /* Set the size of the guard area created for stack overflow protection. */
309 extern int pthread_attr_setguardsize (pthread_attr_t
*__attr
,
311 __THROW
__nonnull ((1));
314 /* Return in *PARAM the scheduling parameters of *ATTR. */
315 extern int pthread_attr_getschedparam (const pthread_attr_t
*__restrict __attr
,
316 struct sched_param
*__restrict __param
)
317 __THROW
__nonnull ((1, 2));
319 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
320 extern int pthread_attr_setschedparam (pthread_attr_t
*__restrict __attr
,
321 const struct sched_param
*__restrict
322 __param
) __THROW
__nonnull ((1, 2));
324 /* Return in *POLICY the scheduling policy of *ATTR. */
325 extern int pthread_attr_getschedpolicy (const pthread_attr_t
*__restrict
326 __attr
, int *__restrict __policy
)
327 __THROW
__nonnull ((1, 2));
329 /* Set scheduling policy in *ATTR according to POLICY. */
330 extern int pthread_attr_setschedpolicy (pthread_attr_t
*__attr
, int __policy
)
331 __THROW
__nonnull ((1));
333 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
334 extern int pthread_attr_getinheritsched (const pthread_attr_t
*__restrict
335 __attr
, int *__restrict __inherit
)
336 __THROW
__nonnull ((1, 2));
338 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
339 extern int pthread_attr_setinheritsched (pthread_attr_t
*__attr
,
341 __THROW
__nonnull ((1));
344 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
345 extern int pthread_attr_getscope (const pthread_attr_t
*__restrict __attr
,
346 int *__restrict __scope
)
347 __THROW
__nonnull ((1, 2));
349 /* Set scheduling contention scope in *ATTR according to SCOPE. */
350 extern int pthread_attr_setscope (pthread_attr_t
*__attr
, int __scope
)
351 __THROW
__nonnull ((1));
353 /* Return the previously set address for the stack. */
354 extern int pthread_attr_getstackaddr (const pthread_attr_t
*__restrict
355 __attr
, void **__restrict __stackaddr
)
356 __THROW
__nonnull ((1, 2)) __attribute_deprecated__
;
358 /* Set the starting address of the stack of the thread to be created.
359 Depending on whether the stack grows up or down the value must either
360 be higher or lower than all the address in the memory block. The
361 minimal size of the block must be PTHREAD_STACK_MIN. */
362 extern int pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
364 __THROW
__nonnull ((1)) __attribute_deprecated__
;
366 /* Return the currently used minimal stack size. */
367 extern int pthread_attr_getstacksize (const pthread_attr_t
*__restrict
368 __attr
, size_t *__restrict __stacksize
)
369 __THROW
__nonnull ((1, 2));
371 /* Add information about the minimum stack size needed for the thread
372 to be started. This size must never be less than PTHREAD_STACK_MIN
373 and must also not exceed the system limits. */
374 extern int pthread_attr_setstacksize (pthread_attr_t
*__attr
,
376 __THROW
__nonnull ((1));
379 /* Return the previously set address for the stack. */
380 extern int pthread_attr_getstack (const pthread_attr_t
*__restrict __attr
,
381 void **__restrict __stackaddr
,
382 size_t *__restrict __stacksize
)
383 __THROW
__nonnull ((1, 2, 3));
385 /* The following two interfaces are intended to replace the last two. They
386 require setting the address as well as the size since only setting the
387 address will make the implementation on some architectures impossible. */
388 extern int pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
389 size_t __stacksize
) __THROW
__nonnull ((1));
393 /* Thread created with attribute ATTR will be limited to run only on
394 the processors represented in CPUSET. */
395 extern int pthread_attr_setaffinity_np (pthread_attr_t
*__attr
,
397 const cpu_set_t
*__cpuset
)
398 __THROW
__nonnull ((1, 3));
400 /* Get bit set in CPUSET representing the processors threads created with
402 extern int pthread_attr_getaffinity_np (const pthread_attr_t
*__attr
,
405 __THROW
__nonnull ((1, 3));
407 /* Get the default attributes used by pthread_create in this process. */
408 extern int pthread_getattr_default_np (pthread_attr_t
*__attr
)
409 __THROW
__nonnull ((1));
411 /* Set the default attributes to be used by pthread_create in this
413 extern int pthread_setattr_default_np (const pthread_attr_t
*__attr
)
414 __THROW
__nonnull ((1));
416 /* Initialize thread attribute *ATTR with attributes corresponding to the
417 already running thread TH. It shall be called on uninitialized ATTR
418 and destroyed with pthread_attr_destroy when no longer needed. */
419 extern int pthread_getattr_np (pthread_t __th
, pthread_attr_t
*__attr
)
420 __THROW
__nonnull ((2));
424 /* Functions for scheduling control. */
426 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
428 extern int pthread_setschedparam (pthread_t __target_thread
, int __policy
,
429 const struct sched_param
*__param
)
430 __THROW
__nonnull ((3));
432 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
433 extern int pthread_getschedparam (pthread_t __target_thread
,
434 int *__restrict __policy
,
435 struct sched_param
*__restrict __param
)
436 __THROW
__nonnull ((2, 3));
438 /* Set the scheduling priority for TARGET_THREAD. */
439 extern int pthread_setschedprio (pthread_t __target_thread
, int __prio
)
444 /* Get thread name visible in the kernel and its interfaces. */
445 extern int pthread_getname_np (pthread_t __target_thread
, char *__buf
,
447 __THROW
__nonnull ((2));
449 /* Set thread name visible in the kernel and its interfaces. */
450 extern int pthread_setname_np (pthread_t __target_thread
, const char *__name
)
451 __THROW
__nonnull ((2));
456 /* Determine level of concurrency. */
457 extern int pthread_getconcurrency (void) __THROW
;
459 /* Set new concurrency level to LEVEL. */
460 extern int pthread_setconcurrency (int __level
) __THROW
;
464 /* Yield the processor to another thread or process.
465 This function is similar to the POSIX `sched_yield' function but
466 might be differently implemented in the case of a m-on-n thread
468 extern int pthread_yield (void) __THROW
;
471 /* Limit specified thread TH to run only on the processors represented
473 extern int pthread_setaffinity_np (pthread_t __th
, size_t __cpusetsize
,
474 const cpu_set_t
*__cpuset
)
475 __THROW
__nonnull ((3));
477 /* Get bit set in CPUSET representing the processors TH can run on. */
478 extern int pthread_getaffinity_np (pthread_t __th
, size_t __cpusetsize
,
480 __THROW
__nonnull ((3));
484 /* Functions for handling initialization. */
486 /* Guarantee that the initialization function INIT_ROUTINE will be called
487 only once, even if pthread_once is executed several times with the
488 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
489 extern variable initialized to PTHREAD_ONCE_INIT.
491 The initialization functions might throw exception which is why
492 this function is not marked with __THROW. */
493 extern int pthread_once (pthread_once_t
*__once_control
,
494 void (*__init_routine
) (void)) __nonnull ((1, 2));
497 /* Functions for handling cancellation.
499 Note that these functions are explicitly not marked to not throw an
500 exception in C++ code. If cancellation is implemented by unwinding
501 this is necessary to have the compiler generate the unwind information. */
503 /* Set cancelability state of current thread to STATE, returning old
504 state in *OLDSTATE if OLDSTATE is not NULL. */
505 extern int pthread_setcancelstate (int __state
, int *__oldstate
);
507 /* Set cancellation state of current thread to TYPE, returning the old
508 type in *OLDTYPE if OLDTYPE is not NULL. */
509 extern int pthread_setcanceltype (int __type
, int *__oldtype
);
511 /* Cancel THREAD immediately or at the next possibility. */
512 extern int pthread_cancel (pthread_t __th
);
514 /* Test for pending cancellation for the current thread and terminate
515 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
517 extern void pthread_testcancel (void);
520 /* Cancellation handling with integration into exception handling. */
526 __jmp_buf __cancel_jmp_buf
;
527 int __mask_was_saved
;
528 } __cancel_jmp_buf
[1];
530 } __pthread_unwind_buf_t
__attribute__ ((__aligned__
));
532 /* No special attributes by default. */
533 #ifndef __cleanup_fct_attribute
534 # define __cleanup_fct_attribute
538 /* Structure to hold the cleanup handler information. */
539 struct __pthread_cleanup_frame
541 void (*__cancel_routine
) (void *);
547 #if defined __GNUC__ && defined __EXCEPTIONS
549 /* Class to handle cancellation handler invocation. */
550 class __pthread_cleanup_class
552 void (*__cancel_routine
) (void *);
558 __pthread_cleanup_class (void (*__fct
) (void *), void *__arg
)
559 : __cancel_routine (__fct
), __cancel_arg (__arg
), __do_it (1) { }
560 ~__pthread_cleanup_class () { if (__do_it
) __cancel_routine (__cancel_arg
); }
561 void __setdoit (int __newval
) { __do_it
= __newval
; }
562 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
,
564 void __restore () const { pthread_setcanceltype (__cancel_type
, 0); }
567 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
568 when the thread is canceled or calls pthread_exit. ROUTINE will also
569 be called with arguments ARG when the matching pthread_cleanup_pop
570 is executed with non-zero EXECUTE argument.
572 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
573 be used in matching pairs at the same nesting level of braces. */
574 # define pthread_cleanup_push(routine, arg) \
576 __pthread_cleanup_class __clframe (routine, arg)
578 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
579 If EXECUTE is non-zero, the handler function is called. */
580 # define pthread_cleanup_pop(execute) \
581 __clframe.__setdoit (execute); \
585 /* Install a cleanup handler as pthread_cleanup_push does, but also
586 saves the current cancellation type and sets it to deferred
588 # define pthread_cleanup_push_defer_np(routine, arg) \
590 __pthread_cleanup_class __clframe (routine, arg); \
593 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
594 restores the cancellation type that was in effect when the matching
595 pthread_cleanup_push_defer was called. */
596 # define pthread_cleanup_pop_restore_np(execute) \
597 __clframe.__restore (); \
598 __clframe.__setdoit (execute); \
602 /* Function called to call the cleanup handler. As an extern inline
603 function the compiler is free to decide inlining the change when
604 needed or fall back on the copy which must exist somewhere
607 __pthread_cleanup_routine (struct __pthread_cleanup_frame
*__frame
)
609 if (__frame
->__do_it
)
610 __frame
->__cancel_routine (__frame
->__cancel_arg
);
613 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
614 when the thread is canceled or calls pthread_exit. ROUTINE will also
615 be called with arguments ARG when the matching pthread_cleanup_pop
616 is executed with non-zero EXECUTE argument.
618 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
619 be used in matching pairs at the same nesting level of braces. */
620 # define pthread_cleanup_push(routine, arg) \
622 struct __pthread_cleanup_frame __clframe \
623 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
624 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
627 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
628 If EXECUTE is non-zero, the handler function is called. */
629 # define pthread_cleanup_pop(execute) \
630 __clframe.__do_it = (execute); \
634 /* Install a cleanup handler as pthread_cleanup_push does, but also
635 saves the current cancellation type and sets it to deferred
637 # define pthread_cleanup_push_defer_np(routine, arg) \
639 struct __pthread_cleanup_frame __clframe \
640 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
641 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
643 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
644 &__clframe.__cancel_type)
646 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
647 restores the cancellation type that was in effect when the matching
648 pthread_cleanup_push_defer was called. */
649 # define pthread_cleanup_pop_restore_np(execute) \
650 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
651 __clframe.__do_it = (execute); \
656 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
657 when the thread is canceled or calls pthread_exit. ROUTINE will also
658 be called with arguments ARG when the matching pthread_cleanup_pop
659 is executed with non-zero EXECUTE argument.
661 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
662 be used in matching pairs at the same nesting level of braces. */
663 # define pthread_cleanup_push(routine, arg) \
665 __pthread_unwind_buf_t __cancel_buf; \
666 void (*__cancel_routine) (void *) = (routine); \
667 void *__cancel_arg = (arg); \
668 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
669 __cancel_buf.__cancel_jmp_buf, 0); \
670 if (__glibc_unlikely (__not_first_call)) \
672 __cancel_routine (__cancel_arg); \
673 __pthread_unwind_next (&__cancel_buf); \
677 __pthread_register_cancel (&__cancel_buf); \
679 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
680 __cleanup_fct_attribute
;
682 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
683 If EXECUTE is non-zero, the handler function is called. */
684 # define pthread_cleanup_pop(execute) \
685 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
687 __pthread_unregister_cancel (&__cancel_buf); \
689 __cancel_routine (__cancel_arg); \
691 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
692 __cleanup_fct_attribute
;
695 /* Install a cleanup handler as pthread_cleanup_push does, but also
696 saves the current cancellation type and sets it to deferred
698 # define pthread_cleanup_push_defer_np(routine, arg) \
700 __pthread_unwind_buf_t __cancel_buf; \
701 void (*__cancel_routine) (void *) = (routine); \
702 void *__cancel_arg = (arg); \
703 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
704 __cancel_buf.__cancel_jmp_buf, 0); \
705 if (__glibc_unlikely (__not_first_call)) \
707 __cancel_routine (__cancel_arg); \
708 __pthread_unwind_next (&__cancel_buf); \
712 __pthread_register_cancel_defer (&__cancel_buf); \
714 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t
*__buf
)
715 __cleanup_fct_attribute
;
717 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
718 restores the cancellation type that was in effect when the matching
719 pthread_cleanup_push_defer was called. */
720 # define pthread_cleanup_pop_restore_np(execute) \
721 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
723 __pthread_unregister_cancel_restore (&__cancel_buf); \
725 __cancel_routine (__cancel_arg); \
727 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t
*__buf
)
728 __cleanup_fct_attribute
;
731 /* Internal interface to initiate cleanup. */
732 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
733 __cleanup_fct_attribute
__attribute__ ((__noreturn__
))
735 __attribute__ ((__weak__
))
740 /* Function used in the macros. */
741 struct __jmp_buf_tag
;
742 extern int __sigsetjmp (struct __jmp_buf_tag
*__env
, int __savemask
) __THROWNL
;
745 /* Mutex handling. */
747 /* Initialize a mutex. */
748 extern int pthread_mutex_init (pthread_mutex_t
*__mutex
,
749 const pthread_mutexattr_t
*__mutexattr
)
750 __THROW
__nonnull ((1));
752 /* Destroy a mutex. */
753 extern int pthread_mutex_destroy (pthread_mutex_t
*__mutex
)
754 __THROW
__nonnull ((1));
756 /* Try locking a mutex. */
757 extern int pthread_mutex_trylock (pthread_mutex_t
*__mutex
)
758 __THROWNL
__nonnull ((1));
761 extern int pthread_mutex_lock (pthread_mutex_t
*__mutex
)
762 __THROWNL
__nonnull ((1));
765 /* Wait until lock becomes available, or specified time passes. */
766 extern int pthread_mutex_timedlock (pthread_mutex_t
*__restrict __mutex
,
767 const struct timespec
*__restrict
768 __abstime
) __THROWNL
__nonnull ((1, 2));
771 /* Unlock a mutex. */
772 extern int pthread_mutex_unlock (pthread_mutex_t
*__mutex
)
773 __THROWNL
__nonnull ((1));
776 /* Get the priority ceiling of MUTEX. */
777 extern int pthread_mutex_getprioceiling (const pthread_mutex_t
*
779 int *__restrict __prioceiling
)
780 __THROW
__nonnull ((1, 2));
782 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
783 priority ceiling value in *OLD_CEILING. */
784 extern int pthread_mutex_setprioceiling (pthread_mutex_t
*__restrict __mutex
,
786 int *__restrict __old_ceiling
)
787 __THROW
__nonnull ((1, 3));
790 #ifdef __USE_XOPEN2K8
791 /* Declare the state protected by MUTEX as consistent. */
792 extern int pthread_mutex_consistent (pthread_mutex_t
*__mutex
)
793 __THROW
__nonnull ((1));
795 extern int pthread_mutex_consistent_np (pthread_mutex_t
*__mutex
)
796 __THROW
__nonnull ((1));
801 /* Functions for handling mutex attributes. */
803 /* Initialize mutex attribute object ATTR with default attributes
804 (kind is PTHREAD_MUTEX_TIMED_NP). */
805 extern int pthread_mutexattr_init (pthread_mutexattr_t
*__attr
)
806 __THROW
__nonnull ((1));
808 /* Destroy mutex attribute object ATTR. */
809 extern int pthread_mutexattr_destroy (pthread_mutexattr_t
*__attr
)
810 __THROW
__nonnull ((1));
812 /* Get the process-shared flag of the mutex attribute ATTR. */
813 extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t
*
815 int *__restrict __pshared
)
816 __THROW
__nonnull ((1, 2));
818 /* Set the process-shared flag of the mutex attribute ATTR. */
819 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t
*__attr
,
821 __THROW
__nonnull ((1));
823 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
824 /* Return in *KIND the mutex kind attribute in *ATTR. */
825 extern int pthread_mutexattr_gettype (const pthread_mutexattr_t
*__restrict
826 __attr
, int *__restrict __kind
)
827 __THROW
__nonnull ((1, 2));
829 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
830 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
831 PTHREAD_MUTEX_DEFAULT). */
832 extern int pthread_mutexattr_settype (pthread_mutexattr_t
*__attr
, int __kind
)
833 __THROW
__nonnull ((1));
836 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
837 extern int pthread_mutexattr_getprotocol (const pthread_mutexattr_t
*
839 int *__restrict __protocol
)
840 __THROW
__nonnull ((1, 2));
842 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
843 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
844 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t
*__attr
,
846 __THROW
__nonnull ((1));
848 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
849 extern int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t
*
851 int *__restrict __prioceiling
)
852 __THROW
__nonnull ((1, 2));
854 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
855 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t
*__attr
,
857 __THROW
__nonnull ((1));
860 /* Get the robustness flag of the mutex attribute ATTR. */
861 extern int pthread_mutexattr_getrobust (const pthread_mutexattr_t
*__attr
,
863 __THROW
__nonnull ((1, 2));
865 extern int pthread_mutexattr_getrobust_np (const pthread_mutexattr_t
*__attr
,
867 __THROW
__nonnull ((1, 2));
870 /* Set the robustness flag of the mutex attribute ATTR. */
871 extern int pthread_mutexattr_setrobust (pthread_mutexattr_t
*__attr
,
873 __THROW
__nonnull ((1));
875 extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t
*__attr
,
877 __THROW
__nonnull ((1));
882 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
883 /* Functions for handling read-write locks. */
885 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
886 the default values if later is NULL. */
887 extern int pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
888 const pthread_rwlockattr_t
*__restrict
889 __attr
) __THROW
__nonnull ((1));
891 /* Destroy read-write lock RWLOCK. */
892 extern int pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
)
893 __THROW
__nonnull ((1));
895 /* Acquire read lock for RWLOCK. */
896 extern int pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
)
897 __THROWNL
__nonnull ((1));
899 /* Try to acquire read lock for RWLOCK. */
900 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
)
901 __THROWNL
__nonnull ((1));
903 # ifdef __USE_XOPEN2K
904 /* Try to acquire read lock for RWLOCK or return after specfied time. */
905 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t
*__restrict __rwlock
,
906 const struct timespec
*__restrict
907 __abstime
) __THROWNL
__nonnull ((1, 2));
910 /* Acquire write lock for RWLOCK. */
911 extern int pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
)
912 __THROWNL
__nonnull ((1));
914 /* Try to acquire write lock for RWLOCK. */
915 extern int pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
)
916 __THROWNL
__nonnull ((1));
918 # ifdef __USE_XOPEN2K
919 /* Try to acquire write lock for RWLOCK or return after specfied time. */
920 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t
*__restrict __rwlock
,
921 const struct timespec
*__restrict
922 __abstime
) __THROWNL
__nonnull ((1, 2));
926 extern int pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
)
927 __THROWNL
__nonnull ((1));
930 /* Functions for handling read-write lock attributes. */
932 /* Initialize attribute object ATTR with default values. */
933 extern int pthread_rwlockattr_init (pthread_rwlockattr_t
*__attr
)
934 __THROW
__nonnull ((1));
936 /* Destroy attribute object ATTR. */
937 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t
*__attr
)
938 __THROW
__nonnull ((1));
940 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
941 extern int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t
*
943 int *__restrict __pshared
)
944 __THROW
__nonnull ((1, 2));
946 /* Set process-shared attribute of ATTR to PSHARED. */
947 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t
*__attr
,
949 __THROW
__nonnull ((1));
951 /* Return current setting of reader/writer preference. */
952 extern int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t
*
954 int *__restrict __pref
)
955 __THROW
__nonnull ((1, 2));
957 /* Set reader/write preference. */
958 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t
*__attr
,
959 int __pref
) __THROW
__nonnull ((1));
963 /* Functions for handling conditional variables. */
965 /* Initialize condition variable COND using attributes ATTR, or use
966 the default values if later is NULL. */
967 extern int pthread_cond_init (pthread_cond_t
*__restrict __cond
,
968 const pthread_condattr_t
*__restrict __cond_attr
)
969 __THROW
__nonnull ((1));
971 /* Destroy condition variable COND. */
972 extern int pthread_cond_destroy (pthread_cond_t
*__cond
)
973 __THROW
__nonnull ((1));
975 /* Wake up one thread waiting for condition variable COND. */
976 extern int pthread_cond_signal (pthread_cond_t
*__cond
)
977 __THROWNL
__nonnull ((1));
979 /* Wake up all threads waiting for condition variables COND. */
980 extern int pthread_cond_broadcast (pthread_cond_t
*__cond
)
981 __THROWNL
__nonnull ((1));
983 /* Wait for condition variable COND to be signaled or broadcast.
984 MUTEX is assumed to be locked before.
986 This function is a cancellation point and therefore not marked with
988 extern int pthread_cond_wait (pthread_cond_t
*__restrict __cond
,
989 pthread_mutex_t
*__restrict __mutex
)
992 /* Wait for condition variable COND to be signaled or broadcast until
993 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
994 absolute time specification; zero is the beginning of the epoch
995 (00:00:00 GMT, January 1, 1970).
997 This function is a cancellation point and therefore not marked with
999 extern int pthread_cond_timedwait (pthread_cond_t
*__restrict __cond
,
1000 pthread_mutex_t
*__restrict __mutex
,
1001 const struct timespec
*__restrict __abstime
)
1002 __nonnull ((1, 2, 3));
1004 /* Functions for handling condition variable attributes. */
1006 /* Initialize condition variable attribute ATTR. */
1007 extern int pthread_condattr_init (pthread_condattr_t
*__attr
)
1008 __THROW
__nonnull ((1));
1010 /* Destroy condition variable attribute ATTR. */
1011 extern int pthread_condattr_destroy (pthread_condattr_t
*__attr
)
1012 __THROW
__nonnull ((1));
1014 /* Get the process-shared flag of the condition variable attribute ATTR. */
1015 extern int pthread_condattr_getpshared (const pthread_condattr_t
*
1017 int *__restrict __pshared
)
1018 __THROW
__nonnull ((1, 2));
1020 /* Set the process-shared flag of the condition variable attribute ATTR. */
1021 extern int pthread_condattr_setpshared (pthread_condattr_t
*__attr
,
1022 int __pshared
) __THROW
__nonnull ((1));
1024 #ifdef __USE_XOPEN2K
1025 /* Get the clock selected for the conditon variable attribute ATTR. */
1026 extern int pthread_condattr_getclock (const pthread_condattr_t
*
1028 __clockid_t
*__restrict __clock_id
)
1029 __THROW
__nonnull ((1, 2));
1031 /* Set the clock selected for the conditon variable attribute ATTR. */
1032 extern int pthread_condattr_setclock (pthread_condattr_t
*__attr
,
1033 __clockid_t __clock_id
)
1034 __THROW
__nonnull ((1));
1038 #ifdef __USE_XOPEN2K
1039 /* Functions to handle spinlocks. */
1041 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
1042 be shared between different processes. */
1043 extern int pthread_spin_init (pthread_spinlock_t
*__lock
, int __pshared
)
1044 __THROW
__nonnull ((1));
1046 /* Destroy the spinlock LOCK. */
1047 extern int pthread_spin_destroy (pthread_spinlock_t
*__lock
)
1048 __THROW
__nonnull ((1));
1050 /* Wait until spinlock LOCK is retrieved. */
1051 extern int pthread_spin_lock (pthread_spinlock_t
*__lock
)
1052 __THROWNL
__nonnull ((1));
1054 /* Try to lock spinlock LOCK. */
1055 extern int pthread_spin_trylock (pthread_spinlock_t
*__lock
)
1056 __THROWNL
__nonnull ((1));
1058 /* Release spinlock LOCK. */
1059 extern int pthread_spin_unlock (pthread_spinlock_t
*__lock
)
1060 __THROWNL
__nonnull ((1));
1063 /* Functions to handle barriers. */
1065 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1066 opened when COUNT waiters arrived. */
1067 extern int pthread_barrier_init (pthread_barrier_t
*__restrict __barrier
,
1068 const pthread_barrierattr_t
*__restrict
1069 __attr
, unsigned int __count
)
1070 __THROW
__nonnull ((1));
1072 /* Destroy a previously dynamically initialized barrier BARRIER. */
1073 extern int pthread_barrier_destroy (pthread_barrier_t
*__barrier
)
1074 __THROW
__nonnull ((1));
1076 /* Wait on barrier BARRIER. */
1077 extern int pthread_barrier_wait (pthread_barrier_t
*__barrier
)
1078 __THROWNL
__nonnull ((1));
1081 /* Initialize barrier attribute ATTR. */
1082 extern int pthread_barrierattr_init (pthread_barrierattr_t
*__attr
)
1083 __THROW
__nonnull ((1));
1085 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1086 extern int pthread_barrierattr_destroy (pthread_barrierattr_t
*__attr
)
1087 __THROW
__nonnull ((1));
1089 /* Get the process-shared flag of the barrier attribute ATTR. */
1090 extern int pthread_barrierattr_getpshared (const pthread_barrierattr_t
*
1092 int *__restrict __pshared
)
1093 __THROW
__nonnull ((1, 2));
1095 /* Set the process-shared flag of the barrier attribute ATTR. */
1096 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t
*__attr
,
1098 __THROW
__nonnull ((1));
1102 /* Functions for handling thread-specific data. */
1104 /* Create a key value identifying a location in the thread-specific
1105 data area. Each thread maintains a distinct thread-specific data
1106 area. DESTR_FUNCTION, if non-NULL, is called with the value
1107 associated to that key when the key is destroyed.
1108 DESTR_FUNCTION is not called if the value associated is NULL when
1109 the key is destroyed. */
1110 extern int pthread_key_create (pthread_key_t
*__key
,
1111 void (*__destr_function
) (void *))
1112 __THROW
__nonnull ((1));
1115 extern int pthread_key_delete (pthread_key_t __key
) __THROW
;
1117 /* Return current value of the thread-specific data slot identified by KEY. */
1118 extern void *pthread_getspecific (pthread_key_t __key
) __THROW
;
1120 /* Store POINTER in the thread-specific data slot identified by KEY. */
1121 extern int pthread_setspecific (pthread_key_t __key
,
1122 const void *__pointer
) __THROW
;
1125 #ifdef __USE_XOPEN2K
1126 /* Get ID of CPU-time clock for thread THREAD_ID. */
1127 extern int pthread_getcpuclockid (pthread_t __thread_id
,
1128 __clockid_t
*__clock_id
)
1129 __THROW
__nonnull ((2));
1133 /* Install handlers to be called when a new process is created with FORK.
1134 The PREPARE handler is called in the parent process just before performing
1135 FORK. The PARENT handler is called in the parent process just after FORK.
1136 The CHILD handler is called in the child process. Each of the three
1137 handlers can be NULL, meaning that no handler needs to be called at that
1139 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1140 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1141 first called before FORK), and the PARENT and CHILD handlers are called
1142 in FIFO (first added, first called). */
1144 extern int pthread_atfork (void (*__prepare
) (void),
1145 void (*__parent
) (void),
1146 void (*__child
) (void)) __THROW
;
1149 #ifdef __USE_EXTERN_INLINES
1150 /* Optimizations. */
1152 __NTH (pthread_equal (pthread_t __thread1
, pthread_t __thread2
))
1154 return __thread1
== __thread2
;
1160 #endif /* pthread.h */