Linux/x86: Update cancel_jmp_buf to match __jmp_buf_tag [BZ #22563]
[glibc.git] / sysdeps / nptl / pthread.h
blob787ac6e4cd627b6c87fdc4e55883f9f374b7a1d1
1 /* Copyright (C) 2002-2017 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/>. */
18 #ifndef _PTHREAD_H
19 #define _PTHREAD_H 1
21 #include <features.h>
22 #include <endian.h>
23 #include <sched.h>
24 #include <time.h>
26 #include <bits/pthreadtypes.h>
27 #include <bits/setjmp.h>
28 #include <bits/wordsize.h>
29 #include <bits/types/struct_timespec.h>
30 #include <bits/types/__cancel_jmp_buf_tag.h>
33 /* Detach state. */
34 enum
36 PTHREAD_CREATE_JOINABLE,
37 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
38 PTHREAD_CREATE_DETACHED
39 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
43 /* Mutex types. */
44 enum
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
56 #endif
57 #ifdef __USE_GNU
58 /* For compatibility. */
59 , PTHREAD_MUTEX_FAST_NP = PTHREAD_MUTEX_TIMED_NP
60 #endif
64 #ifdef __USE_XOPEN2K
65 /* Robust mutex or not flags. */
66 enum
68 PTHREAD_MUTEX_STALLED,
69 PTHREAD_MUTEX_STALLED_NP = PTHREAD_MUTEX_STALLED,
70 PTHREAD_MUTEX_ROBUST,
71 PTHREAD_MUTEX_ROBUST_NP = PTHREAD_MUTEX_ROBUST
73 #endif
76 #if defined __USE_POSIX199506 || defined __USE_UNIX98
77 /* Mutex protocols. */
78 enum
80 PTHREAD_PRIO_NONE,
81 PTHREAD_PRIO_INHERIT,
82 PTHREAD_PRIO_PROTECT
84 #endif
87 #if __PTHREAD_MUTEX_HAVE_PREV
88 # define PTHREAD_MUTEX_INITIALIZER \
89 { { 0, 0, 0, 0, 0, __PTHREAD_SPINS, { 0, 0 } } }
90 # ifdef __USE_GNU
91 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
92 { { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, __PTHREAD_SPINS, { 0, 0 } } }
93 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
94 { { 0, 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, __PTHREAD_SPINS, { 0, 0 } } }
95 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
96 { { 0, 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, __PTHREAD_SPINS, { 0, 0 } } }
98 # endif
99 #else
100 # define PTHREAD_MUTEX_INITIALIZER \
101 { { 0, 0, 0, 0, 0, { __PTHREAD_SPINS } } }
102 # ifdef __USE_GNU
103 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
104 { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { __PTHREAD_SPINS } } }
105 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
106 { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { __PTHREAD_SPINS } } }
107 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
108 { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { __PTHREAD_SPINS } } }
110 # endif
111 #endif
114 /* Read-write lock types. */
115 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
116 enum
118 PTHREAD_RWLOCK_PREFER_READER_NP,
119 PTHREAD_RWLOCK_PREFER_WRITER_NP,
120 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
121 PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_READER_NP
124 /* Define __PTHREAD_RWLOCK_INT_FLAGS_SHARED to 1 if pthread_rwlock_t
125 has the shared field. All 64-bit architectures have the shared field
126 in pthread_rwlock_t. */
127 #ifndef __PTHREAD_RWLOCK_INT_FLAGS_SHARED
128 # if __WORDSIZE == 64
129 # define __PTHREAD_RWLOCK_INT_FLAGS_SHARED 1
130 # endif
131 #endif
133 /* Read-write lock initializers. */
134 # define PTHREAD_RWLOCK_INITIALIZER \
135 { { 0, 0, 0, 0, 0, 0, 0, 0, __PTHREAD_RWLOCK_ELISION_EXTRA, 0, 0 } }
136 # ifdef __USE_GNU
137 # ifdef __PTHREAD_RWLOCK_INT_FLAGS_SHARED
138 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
139 { { 0, 0, 0, 0, 0, 0, 0, 0, __PTHREAD_RWLOCK_ELISION_EXTRA, 0, \
140 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
141 # else
142 # if __BYTE_ORDER == __LITTLE_ENDIAN
143 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
144 { { 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, \
145 0, __PTHREAD_RWLOCK_ELISION_EXTRA, 0, 0 } }
146 # else
147 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
148 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,\
149 0 } }
150 # endif
151 # endif
152 # endif
153 #endif /* Unix98 or XOpen2K */
156 /* Scheduler inheritance. */
157 enum
159 PTHREAD_INHERIT_SCHED,
160 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
161 PTHREAD_EXPLICIT_SCHED
162 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
166 /* Scope handling. */
167 enum
169 PTHREAD_SCOPE_SYSTEM,
170 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
171 PTHREAD_SCOPE_PROCESS
172 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
176 /* Process shared or private flag. */
177 enum
179 PTHREAD_PROCESS_PRIVATE,
180 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
181 PTHREAD_PROCESS_SHARED
182 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
187 /* Conditional variable handling. */
188 #define PTHREAD_COND_INITIALIZER { { {0}, {0}, {0, 0}, {0, 0}, 0, 0, {0, 0} } }
191 /* Cleanup buffers */
192 struct _pthread_cleanup_buffer
194 void (*__routine) (void *); /* Function to call. */
195 void *__arg; /* Its argument. */
196 int __canceltype; /* Saved cancellation type. */
197 struct _pthread_cleanup_buffer *__prev; /* Chaining of cleanup functions. */
200 /* Cancellation */
201 enum
203 PTHREAD_CANCEL_ENABLE,
204 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
205 PTHREAD_CANCEL_DISABLE
206 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
208 enum
210 PTHREAD_CANCEL_DEFERRED,
211 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
212 PTHREAD_CANCEL_ASYNCHRONOUS
213 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
215 #define PTHREAD_CANCELED ((void *) -1)
218 /* Single execution handling. */
219 #define PTHREAD_ONCE_INIT 0
222 #ifdef __USE_XOPEN2K
223 /* Value returned by 'pthread_barrier_wait' for one of the threads after
224 the required number of threads have called this function.
225 -1 is distinct from 0 and all errno constants */
226 # define PTHREAD_BARRIER_SERIAL_THREAD -1
227 #endif
230 __BEGIN_DECLS
232 /* Create a new thread, starting with execution of START-ROUTINE
233 getting passed ARG. Creation attributed come from ATTR. The new
234 handle is stored in *NEWTHREAD. */
235 extern int pthread_create (pthread_t *__restrict __newthread,
236 const pthread_attr_t *__restrict __attr,
237 void *(*__start_routine) (void *),
238 void *__restrict __arg) __THROWNL __nonnull ((1, 3));
240 /* Terminate calling thread.
242 The registered cleanup handlers are called via exception handling
243 so we cannot mark this function with __THROW.*/
244 extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__));
246 /* Make calling thread wait for termination of the thread TH. The
247 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
248 is not NULL.
250 This function is a cancellation point and therefore not marked with
251 __THROW. */
252 extern int pthread_join (pthread_t __th, void **__thread_return);
254 #ifdef __USE_GNU
255 /* Check whether thread TH has terminated. If yes return the status of
256 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
257 extern int pthread_tryjoin_np (pthread_t __th, void **__thread_return) __THROW;
259 /* Make calling thread wait for termination of the thread TH, but only
260 until TIMEOUT. The exit status of the thread is stored in
261 *THREAD_RETURN, if THREAD_RETURN is not NULL.
263 This function is a cancellation point and therefore not marked with
264 __THROW. */
265 extern int pthread_timedjoin_np (pthread_t __th, void **__thread_return,
266 const struct timespec *__abstime);
267 #endif
269 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
270 The resources of TH will therefore be freed immediately when it
271 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
272 on it. */
273 extern int pthread_detach (pthread_t __th) __THROW;
276 /* Obtain the identifier of the current thread. */
277 extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__));
279 /* Compare two thread identifiers. */
280 extern int pthread_equal (pthread_t __thread1, pthread_t __thread2)
281 __THROW __attribute__ ((__const__));
284 /* Thread attribute handling. */
286 /* Initialize thread attribute *ATTR with default attributes
287 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
288 no user-provided stack). */
289 extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1));
291 /* Destroy thread attribute *ATTR. */
292 extern int pthread_attr_destroy (pthread_attr_t *__attr)
293 __THROW __nonnull ((1));
295 /* Get detach state attribute. */
296 extern int pthread_attr_getdetachstate (const pthread_attr_t *__attr,
297 int *__detachstate)
298 __THROW __nonnull ((1, 2));
300 /* Set detach state attribute. */
301 extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
302 int __detachstate)
303 __THROW __nonnull ((1));
306 /* Get the size of the guard area created for stack overflow protection. */
307 extern int pthread_attr_getguardsize (const pthread_attr_t *__attr,
308 size_t *__guardsize)
309 __THROW __nonnull ((1, 2));
311 /* Set the size of the guard area created for stack overflow protection. */
312 extern int pthread_attr_setguardsize (pthread_attr_t *__attr,
313 size_t __guardsize)
314 __THROW __nonnull ((1));
317 /* Return in *PARAM the scheduling parameters of *ATTR. */
318 extern int pthread_attr_getschedparam (const pthread_attr_t *__restrict __attr,
319 struct sched_param *__restrict __param)
320 __THROW __nonnull ((1, 2));
322 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
323 extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,
324 const struct sched_param *__restrict
325 __param) __THROW __nonnull ((1, 2));
327 /* Return in *POLICY the scheduling policy of *ATTR. */
328 extern int pthread_attr_getschedpolicy (const pthread_attr_t *__restrict
329 __attr, int *__restrict __policy)
330 __THROW __nonnull ((1, 2));
332 /* Set scheduling policy in *ATTR according to POLICY. */
333 extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy)
334 __THROW __nonnull ((1));
336 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
337 extern int pthread_attr_getinheritsched (const pthread_attr_t *__restrict
338 __attr, int *__restrict __inherit)
339 __THROW __nonnull ((1, 2));
341 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
342 extern int pthread_attr_setinheritsched (pthread_attr_t *__attr,
343 int __inherit)
344 __THROW __nonnull ((1));
347 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
348 extern int pthread_attr_getscope (const pthread_attr_t *__restrict __attr,
349 int *__restrict __scope)
350 __THROW __nonnull ((1, 2));
352 /* Set scheduling contention scope in *ATTR according to SCOPE. */
353 extern int pthread_attr_setscope (pthread_attr_t *__attr, int __scope)
354 __THROW __nonnull ((1));
356 /* Return the previously set address for the stack. */
357 extern int pthread_attr_getstackaddr (const pthread_attr_t *__restrict
358 __attr, void **__restrict __stackaddr)
359 __THROW __nonnull ((1, 2)) __attribute_deprecated__;
361 /* Set the starting address of the stack of the thread to be created.
362 Depending on whether the stack grows up or down the value must either
363 be higher or lower than all the address in the memory block. The
364 minimal size of the block must be PTHREAD_STACK_MIN. */
365 extern int pthread_attr_setstackaddr (pthread_attr_t *__attr,
366 void *__stackaddr)
367 __THROW __nonnull ((1)) __attribute_deprecated__;
369 /* Return the currently used minimal stack size. */
370 extern int pthread_attr_getstacksize (const pthread_attr_t *__restrict
371 __attr, size_t *__restrict __stacksize)
372 __THROW __nonnull ((1, 2));
374 /* Add information about the minimum stack size needed for the thread
375 to be started. This size must never be less than PTHREAD_STACK_MIN
376 and must also not exceed the system limits. */
377 extern int pthread_attr_setstacksize (pthread_attr_t *__attr,
378 size_t __stacksize)
379 __THROW __nonnull ((1));
381 #ifdef __USE_XOPEN2K
382 /* Return the previously set address for the stack. */
383 extern int pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
384 void **__restrict __stackaddr,
385 size_t *__restrict __stacksize)
386 __THROW __nonnull ((1, 2, 3));
388 /* The following two interfaces are intended to replace the last two. They
389 require setting the address as well as the size since only setting the
390 address will make the implementation on some architectures impossible. */
391 extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
392 size_t __stacksize) __THROW __nonnull ((1));
393 #endif
395 #ifdef __USE_GNU
396 /* Thread created with attribute ATTR will be limited to run only on
397 the processors represented in CPUSET. */
398 extern int pthread_attr_setaffinity_np (pthread_attr_t *__attr,
399 size_t __cpusetsize,
400 const cpu_set_t *__cpuset)
401 __THROW __nonnull ((1, 3));
403 /* Get bit set in CPUSET representing the processors threads created with
404 ATTR can run on. */
405 extern int pthread_attr_getaffinity_np (const pthread_attr_t *__attr,
406 size_t __cpusetsize,
407 cpu_set_t *__cpuset)
408 __THROW __nonnull ((1, 3));
410 /* Get the default attributes used by pthread_create in this process. */
411 extern int pthread_getattr_default_np (pthread_attr_t *__attr)
412 __THROW __nonnull ((1));
414 /* Set the default attributes to be used by pthread_create in this
415 process. */
416 extern int pthread_setattr_default_np (const pthread_attr_t *__attr)
417 __THROW __nonnull ((1));
419 /* Initialize thread attribute *ATTR with attributes corresponding to the
420 already running thread TH. It shall be called on uninitialized ATTR
421 and destroyed with pthread_attr_destroy when no longer needed. */
422 extern int pthread_getattr_np (pthread_t __th, pthread_attr_t *__attr)
423 __THROW __nonnull ((2));
424 #endif
427 /* Functions for scheduling control. */
429 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
430 and *PARAM. */
431 extern int pthread_setschedparam (pthread_t __target_thread, int __policy,
432 const struct sched_param *__param)
433 __THROW __nonnull ((3));
435 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
436 extern int pthread_getschedparam (pthread_t __target_thread,
437 int *__restrict __policy,
438 struct sched_param *__restrict __param)
439 __THROW __nonnull ((2, 3));
441 /* Set the scheduling priority for TARGET_THREAD. */
442 extern int pthread_setschedprio (pthread_t __target_thread, int __prio)
443 __THROW;
446 #ifdef __USE_GNU
447 /* Get thread name visible in the kernel and its interfaces. */
448 extern int pthread_getname_np (pthread_t __target_thread, char *__buf,
449 size_t __buflen)
450 __THROW __nonnull ((2));
452 /* Set thread name visible in the kernel and its interfaces. */
453 extern int pthread_setname_np (pthread_t __target_thread, const char *__name)
454 __THROW __nonnull ((2));
455 #endif
458 #ifdef __USE_UNIX98
459 /* Determine level of concurrency. */
460 extern int pthread_getconcurrency (void) __THROW;
462 /* Set new concurrency level to LEVEL. */
463 extern int pthread_setconcurrency (int __level) __THROW;
464 #endif
466 #ifdef __USE_GNU
467 /* Yield the processor to another thread or process.
468 This function is similar to the POSIX `sched_yield' function but
469 might be differently implemented in the case of a m-on-n thread
470 implementation. */
471 extern int pthread_yield (void) __THROW;
474 /* Limit specified thread TH to run only on the processors represented
475 in CPUSET. */
476 extern int pthread_setaffinity_np (pthread_t __th, size_t __cpusetsize,
477 const cpu_set_t *__cpuset)
478 __THROW __nonnull ((3));
480 /* Get bit set in CPUSET representing the processors TH can run on. */
481 extern int pthread_getaffinity_np (pthread_t __th, size_t __cpusetsize,
482 cpu_set_t *__cpuset)
483 __THROW __nonnull ((3));
484 #endif
487 /* Functions for handling initialization. */
489 /* Guarantee that the initialization function INIT_ROUTINE will be called
490 only once, even if pthread_once is executed several times with the
491 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
492 extern variable initialized to PTHREAD_ONCE_INIT.
494 The initialization functions might throw exception which is why
495 this function is not marked with __THROW. */
496 extern int pthread_once (pthread_once_t *__once_control,
497 void (*__init_routine) (void)) __nonnull ((1, 2));
500 /* Functions for handling cancellation.
502 Note that these functions are explicitly not marked to not throw an
503 exception in C++ code. If cancellation is implemented by unwinding
504 this is necessary to have the compiler generate the unwind information. */
506 /* Set cancelability state of current thread to STATE, returning old
507 state in *OLDSTATE if OLDSTATE is not NULL. */
508 extern int pthread_setcancelstate (int __state, int *__oldstate);
510 /* Set cancellation state of current thread to TYPE, returning the old
511 type in *OLDTYPE if OLDTYPE is not NULL. */
512 extern int pthread_setcanceltype (int __type, int *__oldtype);
514 /* Cancel THREAD immediately or at the next possibility. */
515 extern int pthread_cancel (pthread_t __th);
517 /* Test for pending cancellation for the current thread and terminate
518 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
519 cancelled. */
520 extern void pthread_testcancel (void);
523 /* Cancellation handling with integration into exception handling. */
525 typedef struct
527 struct __cancel_jmp_buf_tag __cancel_jmp_buf[1];
528 void *__pad[4];
529 } __pthread_unwind_buf_t __attribute__ ((__aligned__));
531 /* No special attributes by default. */
532 #ifndef __cleanup_fct_attribute
533 # define __cleanup_fct_attribute
534 #endif
537 /* Structure to hold the cleanup handler information. */
538 struct __pthread_cleanup_frame
540 void (*__cancel_routine) (void *);
541 void *__cancel_arg;
542 int __do_it;
543 int __cancel_type;
546 #if defined __GNUC__ && defined __EXCEPTIONS
547 # ifdef __cplusplus
548 /* Class to handle cancellation handler invocation. */
549 class __pthread_cleanup_class
551 void (*__cancel_routine) (void *);
552 void *__cancel_arg;
553 int __do_it;
554 int __cancel_type;
556 public:
557 __pthread_cleanup_class (void (*__fct) (void *), void *__arg)
558 : __cancel_routine (__fct), __cancel_arg (__arg), __do_it (1) { }
559 ~__pthread_cleanup_class () { if (__do_it) __cancel_routine (__cancel_arg); }
560 void __setdoit (int __newval) { __do_it = __newval; }
561 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED,
562 &__cancel_type); }
563 void __restore () const { pthread_setcanceltype (__cancel_type, 0); }
566 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
567 when the thread is canceled or calls pthread_exit. ROUTINE will also
568 be called with arguments ARG when the matching pthread_cleanup_pop
569 is executed with non-zero EXECUTE argument.
571 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
572 be used in matching pairs at the same nesting level of braces. */
573 # define pthread_cleanup_push(routine, arg) \
574 do { \
575 __pthread_cleanup_class __clframe (routine, arg)
577 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
578 If EXECUTE is non-zero, the handler function is called. */
579 # define pthread_cleanup_pop(execute) \
580 __clframe.__setdoit (execute); \
581 } while (0)
583 # ifdef __USE_GNU
584 /* Install a cleanup handler as pthread_cleanup_push does, but also
585 saves the current cancellation type and sets it to deferred
586 cancellation. */
587 # define pthread_cleanup_push_defer_np(routine, arg) \
588 do { \
589 __pthread_cleanup_class __clframe (routine, arg); \
590 __clframe.__defer ()
592 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
593 restores the cancellation type that was in effect when the matching
594 pthread_cleanup_push_defer was called. */
595 # define pthread_cleanup_pop_restore_np(execute) \
596 __clframe.__restore (); \
597 __clframe.__setdoit (execute); \
598 } while (0)
599 # endif
600 # else
601 /* Function called to call the cleanup handler. As an extern inline
602 function the compiler is free to decide inlining the change when
603 needed or fall back on the copy which must exist somewhere
604 else. */
605 __extern_inline void
606 __pthread_cleanup_routine (struct __pthread_cleanup_frame *__frame)
608 if (__frame->__do_it)
609 __frame->__cancel_routine (__frame->__cancel_arg);
612 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
613 when the thread is canceled or calls pthread_exit. ROUTINE will also
614 be called with arguments ARG when the matching pthread_cleanup_pop
615 is executed with non-zero EXECUTE argument.
617 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
618 be used in matching pairs at the same nesting level of braces. */
619 # define pthread_cleanup_push(routine, arg) \
620 do { \
621 struct __pthread_cleanup_frame __clframe \
622 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
623 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
624 .__do_it = 1 };
626 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
627 If EXECUTE is non-zero, the handler function is called. */
628 # define pthread_cleanup_pop(execute) \
629 __clframe.__do_it = (execute); \
630 } while (0)
632 # ifdef __USE_GNU
633 /* Install a cleanup handler as pthread_cleanup_push does, but also
634 saves the current cancellation type and sets it to deferred
635 cancellation. */
636 # define pthread_cleanup_push_defer_np(routine, arg) \
637 do { \
638 struct __pthread_cleanup_frame __clframe \
639 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
640 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
641 .__do_it = 1 }; \
642 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
643 &__clframe.__cancel_type)
645 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
646 restores the cancellation type that was in effect when the matching
647 pthread_cleanup_push_defer was called. */
648 # define pthread_cleanup_pop_restore_np(execute) \
649 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
650 __clframe.__do_it = (execute); \
651 } while (0)
652 # endif
653 # endif
654 #else
655 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
656 when the thread is canceled or calls pthread_exit. ROUTINE will also
657 be called with arguments ARG when the matching pthread_cleanup_pop
658 is executed with non-zero EXECUTE argument.
660 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
661 be used in matching pairs at the same nesting level of braces. */
662 # define pthread_cleanup_push(routine, arg) \
663 do { \
664 __pthread_unwind_buf_t __cancel_buf; \
665 void (*__cancel_routine) (void *) = (routine); \
666 void *__cancel_arg = (arg); \
667 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
668 __cancel_buf.__cancel_jmp_buf, 0); \
669 if (__glibc_unlikely (__not_first_call)) \
671 __cancel_routine (__cancel_arg); \
672 __pthread_unwind_next (&__cancel_buf); \
673 /* NOTREACHED */ \
676 __pthread_register_cancel (&__cancel_buf); \
677 do {
678 extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
679 __cleanup_fct_attribute;
681 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
682 If EXECUTE is non-zero, the handler function is called. */
683 # define pthread_cleanup_pop(execute) \
684 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
685 } while (0); \
686 __pthread_unregister_cancel (&__cancel_buf); \
687 if (execute) \
688 __cancel_routine (__cancel_arg); \
689 } while (0)
690 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
691 __cleanup_fct_attribute;
693 # ifdef __USE_GNU
694 /* Install a cleanup handler as pthread_cleanup_push does, but also
695 saves the current cancellation type and sets it to deferred
696 cancellation. */
697 # define pthread_cleanup_push_defer_np(routine, arg) \
698 do { \
699 __pthread_unwind_buf_t __cancel_buf; \
700 void (*__cancel_routine) (void *) = (routine); \
701 void *__cancel_arg = (arg); \
702 int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *) \
703 __cancel_buf.__cancel_jmp_buf, 0); \
704 if (__glibc_unlikely (__not_first_call)) \
706 __cancel_routine (__cancel_arg); \
707 __pthread_unwind_next (&__cancel_buf); \
708 /* NOTREACHED */ \
711 __pthread_register_cancel_defer (&__cancel_buf); \
712 do {
713 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t *__buf)
714 __cleanup_fct_attribute;
716 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
717 restores the cancellation type that was in effect when the matching
718 pthread_cleanup_push_defer was called. */
719 # define pthread_cleanup_pop_restore_np(execute) \
720 do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
721 } while (0); \
722 __pthread_unregister_cancel_restore (&__cancel_buf); \
723 if (execute) \
724 __cancel_routine (__cancel_arg); \
725 } while (0)
726 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t *__buf)
727 __cleanup_fct_attribute;
728 # endif
730 /* Internal interface to initiate cleanup. */
731 extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
732 __cleanup_fct_attribute __attribute__ ((__noreturn__))
733 # ifndef SHARED
734 __attribute__ ((__weak__))
735 # endif
737 #endif
739 /* Function used in the macros. */
740 struct __jmp_buf_tag;
741 extern int __sigsetjmp (struct __jmp_buf_tag *__env, int __savemask) __THROWNL;
744 /* Mutex handling. */
746 /* Initialize a mutex. */
747 extern int pthread_mutex_init (pthread_mutex_t *__mutex,
748 const pthread_mutexattr_t *__mutexattr)
749 __THROW __nonnull ((1));
751 /* Destroy a mutex. */
752 extern int pthread_mutex_destroy (pthread_mutex_t *__mutex)
753 __THROW __nonnull ((1));
755 /* Try locking a mutex. */
756 extern int pthread_mutex_trylock (pthread_mutex_t *__mutex)
757 __THROWNL __nonnull ((1));
759 /* Lock a mutex. */
760 extern int pthread_mutex_lock (pthread_mutex_t *__mutex)
761 __THROWNL __nonnull ((1));
763 #ifdef __USE_XOPEN2K
764 /* Wait until lock becomes available, or specified time passes. */
765 extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
766 const struct timespec *__restrict
767 __abstime) __THROWNL __nonnull ((1, 2));
768 #endif
770 /* Unlock a mutex. */
771 extern int pthread_mutex_unlock (pthread_mutex_t *__mutex)
772 __THROWNL __nonnull ((1));
775 /* Get the priority ceiling of MUTEX. */
776 extern int pthread_mutex_getprioceiling (const pthread_mutex_t *
777 __restrict __mutex,
778 int *__restrict __prioceiling)
779 __THROW __nonnull ((1, 2));
781 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
782 priority ceiling value in *OLD_CEILING. */
783 extern int pthread_mutex_setprioceiling (pthread_mutex_t *__restrict __mutex,
784 int __prioceiling,
785 int *__restrict __old_ceiling)
786 __THROW __nonnull ((1, 3));
789 #ifdef __USE_XOPEN2K8
790 /* Declare the state protected by MUTEX as consistent. */
791 extern int pthread_mutex_consistent (pthread_mutex_t *__mutex)
792 __THROW __nonnull ((1));
793 # ifdef __USE_GNU
794 extern int pthread_mutex_consistent_np (pthread_mutex_t *__mutex)
795 __THROW __nonnull ((1));
796 # endif
797 #endif
800 /* Functions for handling mutex attributes. */
802 /* Initialize mutex attribute object ATTR with default attributes
803 (kind is PTHREAD_MUTEX_TIMED_NP). */
804 extern int pthread_mutexattr_init (pthread_mutexattr_t *__attr)
805 __THROW __nonnull ((1));
807 /* Destroy mutex attribute object ATTR. */
808 extern int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr)
809 __THROW __nonnull ((1));
811 /* Get the process-shared flag of the mutex attribute ATTR. */
812 extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t *
813 __restrict __attr,
814 int *__restrict __pshared)
815 __THROW __nonnull ((1, 2));
817 /* Set the process-shared flag of the mutex attribute ATTR. */
818 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
819 int __pshared)
820 __THROW __nonnull ((1));
822 #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
823 /* Return in *KIND the mutex kind attribute in *ATTR. */
824 extern int pthread_mutexattr_gettype (const pthread_mutexattr_t *__restrict
825 __attr, int *__restrict __kind)
826 __THROW __nonnull ((1, 2));
828 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
829 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
830 PTHREAD_MUTEX_DEFAULT). */
831 extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
832 __THROW __nonnull ((1));
833 #endif
835 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
836 extern int pthread_mutexattr_getprotocol (const pthread_mutexattr_t *
837 __restrict __attr,
838 int *__restrict __protocol)
839 __THROW __nonnull ((1, 2));
841 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
842 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
843 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr,
844 int __protocol)
845 __THROW __nonnull ((1));
847 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
848 extern int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *
849 __restrict __attr,
850 int *__restrict __prioceiling)
851 __THROW __nonnull ((1, 2));
853 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
854 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr,
855 int __prioceiling)
856 __THROW __nonnull ((1));
858 #ifdef __USE_XOPEN2K
859 /* Get the robustness flag of the mutex attribute ATTR. */
860 extern int pthread_mutexattr_getrobust (const pthread_mutexattr_t *__attr,
861 int *__robustness)
862 __THROW __nonnull ((1, 2));
863 # ifdef __USE_GNU
864 extern int pthread_mutexattr_getrobust_np (const pthread_mutexattr_t *__attr,
865 int *__robustness)
866 __THROW __nonnull ((1, 2));
867 # endif
869 /* Set the robustness flag of the mutex attribute ATTR. */
870 extern int pthread_mutexattr_setrobust (pthread_mutexattr_t *__attr,
871 int __robustness)
872 __THROW __nonnull ((1));
873 # ifdef __USE_GNU
874 extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t *__attr,
875 int __robustness)
876 __THROW __nonnull ((1));
877 # endif
878 #endif
881 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
882 /* Functions for handling read-write locks. */
884 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
885 the default values if later is NULL. */
886 extern int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
887 const pthread_rwlockattr_t *__restrict
888 __attr) __THROW __nonnull ((1));
890 /* Destroy read-write lock RWLOCK. */
891 extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock)
892 __THROW __nonnull ((1));
894 /* Acquire read lock for RWLOCK. */
895 extern int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock)
896 __THROWNL __nonnull ((1));
898 /* Try to acquire read lock for RWLOCK. */
899 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock)
900 __THROWNL __nonnull ((1));
902 # ifdef __USE_XOPEN2K
903 /* Try to acquire read lock for RWLOCK or return after specfied time. */
904 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
905 const struct timespec *__restrict
906 __abstime) __THROWNL __nonnull ((1, 2));
907 # endif
909 /* Acquire write lock for RWLOCK. */
910 extern int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock)
911 __THROWNL __nonnull ((1));
913 /* Try to acquire write lock for RWLOCK. */
914 extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock)
915 __THROWNL __nonnull ((1));
917 # ifdef __USE_XOPEN2K
918 /* Try to acquire write lock for RWLOCK or return after specfied time. */
919 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
920 const struct timespec *__restrict
921 __abstime) __THROWNL __nonnull ((1, 2));
922 # endif
924 /* Unlock RWLOCK. */
925 extern int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock)
926 __THROWNL __nonnull ((1));
929 /* Functions for handling read-write lock attributes. */
931 /* Initialize attribute object ATTR with default values. */
932 extern int pthread_rwlockattr_init (pthread_rwlockattr_t *__attr)
933 __THROW __nonnull ((1));
935 /* Destroy attribute object ATTR. */
936 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr)
937 __THROW __nonnull ((1));
939 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
940 extern int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *
941 __restrict __attr,
942 int *__restrict __pshared)
943 __THROW __nonnull ((1, 2));
945 /* Set process-shared attribute of ATTR to PSHARED. */
946 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr,
947 int __pshared)
948 __THROW __nonnull ((1));
950 /* Return current setting of reader/writer preference. */
951 extern int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *
952 __restrict __attr,
953 int *__restrict __pref)
954 __THROW __nonnull ((1, 2));
956 /* Set reader/write preference. */
957 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *__attr,
958 int __pref) __THROW __nonnull ((1));
959 #endif
962 /* Functions for handling conditional variables. */
964 /* Initialize condition variable COND using attributes ATTR, or use
965 the default values if later is NULL. */
966 extern int pthread_cond_init (pthread_cond_t *__restrict __cond,
967 const pthread_condattr_t *__restrict __cond_attr)
968 __THROW __nonnull ((1));
970 /* Destroy condition variable COND. */
971 extern int pthread_cond_destroy (pthread_cond_t *__cond)
972 __THROW __nonnull ((1));
974 /* Wake up one thread waiting for condition variable COND. */
975 extern int pthread_cond_signal (pthread_cond_t *__cond)
976 __THROWNL __nonnull ((1));
978 /* Wake up all threads waiting for condition variables COND. */
979 extern int pthread_cond_broadcast (pthread_cond_t *__cond)
980 __THROWNL __nonnull ((1));
982 /* Wait for condition variable COND to be signaled or broadcast.
983 MUTEX is assumed to be locked before.
985 This function is a cancellation point and therefore not marked with
986 __THROW. */
987 extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,
988 pthread_mutex_t *__restrict __mutex)
989 __nonnull ((1, 2));
991 /* Wait for condition variable COND to be signaled or broadcast until
992 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
993 absolute time specification; zero is the beginning of the epoch
994 (00:00:00 GMT, January 1, 1970).
996 This function is a cancellation point and therefore not marked with
997 __THROW. */
998 extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
999 pthread_mutex_t *__restrict __mutex,
1000 const struct timespec *__restrict __abstime)
1001 __nonnull ((1, 2, 3));
1003 /* Functions for handling condition variable attributes. */
1005 /* Initialize condition variable attribute ATTR. */
1006 extern int pthread_condattr_init (pthread_condattr_t *__attr)
1007 __THROW __nonnull ((1));
1009 /* Destroy condition variable attribute ATTR. */
1010 extern int pthread_condattr_destroy (pthread_condattr_t *__attr)
1011 __THROW __nonnull ((1));
1013 /* Get the process-shared flag of the condition variable attribute ATTR. */
1014 extern int pthread_condattr_getpshared (const pthread_condattr_t *
1015 __restrict __attr,
1016 int *__restrict __pshared)
1017 __THROW __nonnull ((1, 2));
1019 /* Set the process-shared flag of the condition variable attribute ATTR. */
1020 extern int pthread_condattr_setpshared (pthread_condattr_t *__attr,
1021 int __pshared) __THROW __nonnull ((1));
1023 #ifdef __USE_XOPEN2K
1024 /* Get the clock selected for the condition variable attribute ATTR. */
1025 extern int pthread_condattr_getclock (const pthread_condattr_t *
1026 __restrict __attr,
1027 __clockid_t *__restrict __clock_id)
1028 __THROW __nonnull ((1, 2));
1030 /* Set the clock selected for the condition variable attribute ATTR. */
1031 extern int pthread_condattr_setclock (pthread_condattr_t *__attr,
1032 __clockid_t __clock_id)
1033 __THROW __nonnull ((1));
1034 #endif
1037 #ifdef __USE_XOPEN2K
1038 /* Functions to handle spinlocks. */
1040 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
1041 be shared between different processes. */
1042 extern int pthread_spin_init (pthread_spinlock_t *__lock, int __pshared)
1043 __THROW __nonnull ((1));
1045 /* Destroy the spinlock LOCK. */
1046 extern int pthread_spin_destroy (pthread_spinlock_t *__lock)
1047 __THROW __nonnull ((1));
1049 /* Wait until spinlock LOCK is retrieved. */
1050 extern int pthread_spin_lock (pthread_spinlock_t *__lock)
1051 __THROWNL __nonnull ((1));
1053 /* Try to lock spinlock LOCK. */
1054 extern int pthread_spin_trylock (pthread_spinlock_t *__lock)
1055 __THROWNL __nonnull ((1));
1057 /* Release spinlock LOCK. */
1058 extern int pthread_spin_unlock (pthread_spinlock_t *__lock)
1059 __THROWNL __nonnull ((1));
1062 /* Functions to handle barriers. */
1064 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1065 opened when COUNT waiters arrived. */
1066 extern int pthread_barrier_init (pthread_barrier_t *__restrict __barrier,
1067 const pthread_barrierattr_t *__restrict
1068 __attr, unsigned int __count)
1069 __THROW __nonnull ((1));
1071 /* Destroy a previously dynamically initialized barrier BARRIER. */
1072 extern int pthread_barrier_destroy (pthread_barrier_t *__barrier)
1073 __THROW __nonnull ((1));
1075 /* Wait on barrier BARRIER. */
1076 extern int pthread_barrier_wait (pthread_barrier_t *__barrier)
1077 __THROWNL __nonnull ((1));
1080 /* Initialize barrier attribute ATTR. */
1081 extern int pthread_barrierattr_init (pthread_barrierattr_t *__attr)
1082 __THROW __nonnull ((1));
1084 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1085 extern int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr)
1086 __THROW __nonnull ((1));
1088 /* Get the process-shared flag of the barrier attribute ATTR. */
1089 extern int pthread_barrierattr_getpshared (const pthread_barrierattr_t *
1090 __restrict __attr,
1091 int *__restrict __pshared)
1092 __THROW __nonnull ((1, 2));
1094 /* Set the process-shared flag of the barrier attribute ATTR. */
1095 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr,
1096 int __pshared)
1097 __THROW __nonnull ((1));
1098 #endif
1101 /* Functions for handling thread-specific data. */
1103 /* Create a key value identifying a location in the thread-specific
1104 data area. Each thread maintains a distinct thread-specific data
1105 area. DESTR_FUNCTION, if non-NULL, is called with the value
1106 associated to that key when the key is destroyed.
1107 DESTR_FUNCTION is not called if the value associated is NULL when
1108 the key is destroyed. */
1109 extern int pthread_key_create (pthread_key_t *__key,
1110 void (*__destr_function) (void *))
1111 __THROW __nonnull ((1));
1113 /* Destroy KEY. */
1114 extern int pthread_key_delete (pthread_key_t __key) __THROW;
1116 /* Return current value of the thread-specific data slot identified by KEY. */
1117 extern void *pthread_getspecific (pthread_key_t __key) __THROW;
1119 /* Store POINTER in the thread-specific data slot identified by KEY. */
1120 extern int pthread_setspecific (pthread_key_t __key,
1121 const void *__pointer) __THROW ;
1124 #ifdef __USE_XOPEN2K
1125 /* Get ID of CPU-time clock for thread THREAD_ID. */
1126 extern int pthread_getcpuclockid (pthread_t __thread_id,
1127 __clockid_t *__clock_id)
1128 __THROW __nonnull ((2));
1129 #endif
1132 /* Install handlers to be called when a new process is created with FORK.
1133 The PREPARE handler is called in the parent process just before performing
1134 FORK. The PARENT handler is called in the parent process just after FORK.
1135 The CHILD handler is called in the child process. Each of the three
1136 handlers can be NULL, meaning that no handler needs to be called at that
1137 point.
1138 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1139 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1140 first called before FORK), and the PARENT and CHILD handlers are called
1141 in FIFO (first added, first called). */
1143 extern int pthread_atfork (void (*__prepare) (void),
1144 void (*__parent) (void),
1145 void (*__child) (void)) __THROW;
1148 #ifdef __USE_EXTERN_INLINES
1149 /* Optimizations. */
1150 __extern_inline int
1151 __NTH (pthread_equal (pthread_t __thread1, pthread_t __thread2))
1153 return __thread1 == __thread2;
1155 #endif
1157 __END_DECLS
1159 #endif /* pthread.h */