Add pthread_equal inline function.
[glibc.git] / nptl / sysdeps / pthread / pthread.h
blobf60ecdee18c48559603baec1e5fceb0534f38a8b
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #ifndef _PTHREAD_H
20 #define _PTHREAD_H 1
22 #include <features.h>
23 #include <sched.h>
24 #include <time.h>
26 #define __need_sigset_t
27 #include <signal.h>
28 #include <bits/pthreadtypes.h>
29 #include <bits/setjmp.h>
30 #include <bits/wordsize.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 #ifdef __USE_UNIX98
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_GNU
65 /* Robust mutex or not flags. */
66 enum
68 PTHREAD_MUTEX_STALLED_NP,
69 PTHREAD_MUTEX_ROBUST_NP
71 #endif
74 #ifdef __USE_UNIX98
75 /* Mutex protocols. */
76 enum
78 PTHREAD_PRIO_NONE,
79 PTHREAD_PRIO_INHERIT,
80 PTHREAD_PRIO_PROTECT
82 #endif
85 /* Mutex initializers. */
86 #if __WORDSIZE == 64
87 # define PTHREAD_MUTEX_INITIALIZER \
88 { { 0, 0, 0, 0, 0, 0, { 0, 0 } } }
89 # ifdef __USE_GNU
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 } } }
96 # endif
97 #else
98 # define PTHREAD_MUTEX_INITIALIZER \
99 { { 0, 0, 0, 0, 0, { 0 } } }
100 # ifdef __USE_GNU
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 } } }
107 # endif
108 #endif
111 /* Read-write lock types. */
112 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
113 enum
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 # if __WORDSIZE == 64
123 # define PTHREAD_RWLOCK_INITIALIZER \
124 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
125 # else
126 # define PTHREAD_RWLOCK_INITIALIZER \
127 { { 0, 0, 0, 0, 0, 0, 0, 0 } }
128 # endif
129 # ifdef __USE_GNU
130 # if __WORDSIZE == 64
131 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
132 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
133 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
134 # else
135 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
136 { { 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, 0 } }
137 # endif
138 # endif
139 #endif /* Unix98 or XOpen2K */
142 /* Scheduler inheritance. */
143 enum
145 PTHREAD_INHERIT_SCHED,
146 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
147 PTHREAD_EXPLICIT_SCHED
148 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
152 /* Scope handling. */
153 enum
155 PTHREAD_SCOPE_SYSTEM,
156 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
157 PTHREAD_SCOPE_PROCESS
158 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
162 /* Process shared or private flag. */
163 enum
165 PTHREAD_PROCESS_PRIVATE,
166 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
167 PTHREAD_PROCESS_SHARED
168 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
173 /* Conditional variable handling. */
174 #define PTHREAD_COND_INITIALIZER { { 0, 0, 0, 0, 0, (void *) 0, 0, 0 } }
177 /* Cleanup buffers */
178 struct _pthread_cleanup_buffer
180 void (*__routine) (void *); /* Function to call. */
181 void *__arg; /* Its argument. */
182 int __canceltype; /* Saved cancellation type. */
183 struct _pthread_cleanup_buffer *__prev; /* Chaining of cleanup functions. */
186 /* Cancellation */
187 enum
189 PTHREAD_CANCEL_ENABLE,
190 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
191 PTHREAD_CANCEL_DISABLE
192 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
194 enum
196 PTHREAD_CANCEL_DEFERRED,
197 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
198 PTHREAD_CANCEL_ASYNCHRONOUS
199 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
201 #define PTHREAD_CANCELED ((void *) -1)
204 /* Single execution handling. */
205 #define PTHREAD_ONCE_INIT 0
208 #ifdef __USE_XOPEN2K
209 /* Value returned by 'pthread_barrier_wait' for one of the threads after
210 the required number of threads have called this function.
211 -1 is distinct from 0 and all errno constants */
212 # define PTHREAD_BARRIER_SERIAL_THREAD -1
213 #endif
216 __BEGIN_DECLS
218 /* Create a new thread, starting with execution of START-ROUTINE
219 getting passed ARG. Creation attributed come from ATTR. The new
220 handle is stored in *NEWTHREAD. */
221 extern int pthread_create (pthread_t *__restrict __newthread,
222 __const pthread_attr_t *__restrict __attr,
223 void *(*__start_routine) (void *),
224 void *__restrict __arg) __THROW __nonnull ((1, 3));
226 /* Terminate calling thread.
228 The registered cleanup handlers are called via exception handling
229 so we cannot mark this function with __THROW.*/
230 extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__));
232 /* Make calling thread wait for termination of the thread TH. The
233 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
234 is not NULL.
236 This function is a cancellation point and therefore not marked with
237 __THROW. */
238 extern int pthread_join (pthread_t __th, void **__thread_return);
240 #ifdef __USE_GNU
241 /* Check whether thread TH has terminated. If yes return the status of
242 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
243 extern int pthread_tryjoin_np (pthread_t __th, void **__thread_return) __THROW;
245 /* Make calling thread wait for termination of the thread TH, but only
246 until TIMEOUT. The exit status of the thread is stored in
247 *THREAD_RETURN, if THREAD_RETURN is not NULL.
249 This function is a cancellation point and therefore not marked with
250 __THROW. */
251 extern int pthread_timedjoin_np (pthread_t __th, void **__thread_return,
252 __const struct timespec *__abstime);
253 #endif
255 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
256 The resources of TH will therefore be freed immediately when it
257 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
258 on it. */
259 extern int pthread_detach (pthread_t __th) __THROW;
262 /* Obtain the identifier of the current thread. */
263 extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__));
265 /* Compare two thread identifiers. */
266 extern int pthread_equal (pthread_t __thread1, pthread_t __thread2) __THROW;
269 /* Thread attribute handling. */
271 /* Initialize thread attribute *ATTR with default attributes
272 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
273 no user-provided stack). */
274 extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1));
276 /* Destroy thread attribute *ATTR. */
277 extern int pthread_attr_destroy (pthread_attr_t *__attr)
278 __THROW __nonnull ((1));
280 /* Get detach state attribute. */
281 extern int pthread_attr_getdetachstate (__const pthread_attr_t *__attr,
282 int *__detachstate)
283 __THROW __nonnull ((1, 2));
285 /* Set detach state attribute. */
286 extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
287 int __detachstate)
288 __THROW __nonnull ((1));
291 /* Get the size of the guard area created for stack overflow protection. */
292 extern int pthread_attr_getguardsize (__const pthread_attr_t *__attr,
293 size_t *__guardsize)
294 __THROW __nonnull ((1, 2));
296 /* Set the size of the guard area created for stack overflow protection. */
297 extern int pthread_attr_setguardsize (pthread_attr_t *__attr,
298 size_t __guardsize)
299 __THROW __nonnull ((1));
302 /* Return in *PARAM the scheduling parameters of *ATTR. */
303 extern int pthread_attr_getschedparam (__const pthread_attr_t *__restrict
304 __attr,
305 struct sched_param *__restrict __param)
306 __THROW __nonnull ((1, 2));
308 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
309 extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,
310 __const struct sched_param *__restrict
311 __param) __THROW __nonnull ((1, 2));
313 /* Return in *POLICY the scheduling policy of *ATTR. */
314 extern int pthread_attr_getschedpolicy (__const pthread_attr_t *__restrict
315 __attr, int *__restrict __policy)
316 __THROW __nonnull ((1, 2));
318 /* Set scheduling policy in *ATTR according to POLICY. */
319 extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy)
320 __THROW __nonnull ((1));
322 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
323 extern int pthread_attr_getinheritsched (__const pthread_attr_t *__restrict
324 __attr, int *__restrict __inherit)
325 __THROW __nonnull ((1, 2));
327 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
328 extern int pthread_attr_setinheritsched (pthread_attr_t *__attr,
329 int __inherit)
330 __THROW __nonnull ((1));
333 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
334 extern int pthread_attr_getscope (__const pthread_attr_t *__restrict __attr,
335 int *__restrict __scope)
336 __THROW __nonnull ((1, 2));
338 /* Set scheduling contention scope in *ATTR according to SCOPE. */
339 extern int pthread_attr_setscope (pthread_attr_t *__attr, int __scope)
340 __THROW __nonnull ((1));
342 /* Return the previously set address for the stack. */
343 extern int pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
344 __attr, void **__restrict __stackaddr)
345 __THROW __nonnull ((1, 2)) __attribute_deprecated__;
347 /* Set the starting address of the stack of the thread to be created.
348 Depending on whether the stack grows up or down the value must either
349 be higher or lower than all the address in the memory block. The
350 minimal size of the block must be PTHREAD_STACK_MIN. */
351 extern int pthread_attr_setstackaddr (pthread_attr_t *__attr,
352 void *__stackaddr)
353 __THROW __nonnull ((1)) __attribute_deprecated__;
355 /* Return the currently used minimal stack size. */
356 extern int pthread_attr_getstacksize (__const pthread_attr_t *__restrict
357 __attr, size_t *__restrict __stacksize)
358 __THROW __nonnull ((1, 2));
360 /* Add information about the minimum stack size needed for the thread
361 to be started. This size must never be less than PTHREAD_STACK_MIN
362 and must also not exceed the system limits. */
363 extern int pthread_attr_setstacksize (pthread_attr_t *__attr,
364 size_t __stacksize)
365 __THROW __nonnull ((1));
367 #ifdef __USE_XOPEN2K
368 /* Return the previously set address for the stack. */
369 extern int pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
370 void **__restrict __stackaddr,
371 size_t *__restrict __stacksize)
372 __THROW __nonnull ((1, 2, 3));
374 /* The following two interfaces are intended to replace the last two. They
375 require setting the address as well as the size since only setting the
376 address will make the implementation on some architectures impossible. */
377 extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
378 size_t __stacksize) __THROW __nonnull ((1));
379 #endif
381 #ifdef __USE_GNU
382 /* Thread created with attribute ATTR will be limited to run only on
383 the processors represented in CPUSET. */
384 extern int pthread_attr_setaffinity_np (pthread_attr_t *__attr,
385 size_t __cpusetsize,
386 __const cpu_set_t *__cpuset)
387 __THROW __nonnull ((1, 3));
389 /* Get bit set in CPUSET representing the processors threads created with
390 ATTR can run on. */
391 extern int pthread_attr_getaffinity_np (__const pthread_attr_t *__attr,
392 size_t __cpusetsize,
393 cpu_set_t *__cpuset)
394 __THROW __nonnull ((1, 3));
397 /* Initialize thread attribute *ATTR with attributes corresponding to the
398 already running thread TH. It shall be called on unitialized ATTR
399 and destroyed with pthread_attr_destroy when no longer needed. */
400 extern int pthread_getattr_np (pthread_t __th, pthread_attr_t *__attr)
401 __THROW __nonnull ((2));
402 #endif
405 /* Functions for scheduling control. */
407 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
408 and *PARAM. */
409 extern int pthread_setschedparam (pthread_t __target_thread, int __policy,
410 __const struct sched_param *__param)
411 __THROW __nonnull ((3));
413 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
414 extern int pthread_getschedparam (pthread_t __target_thread,
415 int *__restrict __policy,
416 struct sched_param *__restrict __param)
417 __THROW __nonnull ((2, 3));
419 /* Set the scheduling priority for TARGET_THREAD. */
420 extern int pthread_setschedprio (pthread_t __target_thread, int __prio)
421 __THROW;
424 #ifdef __USE_UNIX98
425 /* Determine level of concurrency. */
426 extern int pthread_getconcurrency (void) __THROW;
428 /* Set new concurrency level to LEVEL. */
429 extern int pthread_setconcurrency (int __level) __THROW;
430 #endif
432 #ifdef __USE_GNU
433 /* Yield the processor to another thread or process.
434 This function is similar to the POSIX `sched_yield' function but
435 might be differently implemented in the case of a m-on-n thread
436 implementation. */
437 extern int pthread_yield (void) __THROW;
440 /* Limit specified thread TH to run only on the processors represented
441 in CPUSET. */
442 extern int pthread_setaffinity_np (pthread_t __th, size_t __cpusetsize,
443 __const cpu_set_t *__cpuset)
444 __THROW __nonnull ((3));
446 /* Get bit set in CPUSET representing the processors TH can run on. */
447 extern int pthread_getaffinity_np (pthread_t __th, size_t __cpusetsize,
448 cpu_set_t *__cpuset)
449 __THROW __nonnull ((3));
450 #endif
453 /* Functions for handling initialization. */
455 /* Guarantee that the initialization function INIT_ROUTINE will be called
456 only once, even if pthread_once is executed several times with the
457 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
458 extern variable initialized to PTHREAD_ONCE_INIT.
460 The initialization functions might throw exception which is why
461 this function is not marked with __THROW. */
462 extern int pthread_once (pthread_once_t *__once_control,
463 void (*__init_routine) (void)) __nonnull ((1, 2));
466 /* Functions for handling cancellation.
468 Note that these functions are explicitly not marked to not throw an
469 exception in C++ code. If cancellation is implemented by unwinding
470 this is necessary to have the compiler generate the unwind information. */
472 /* Set cancelability state of current thread to STATE, returning old
473 state in *OLDSTATE if OLDSTATE is not NULL. */
474 extern int pthread_setcancelstate (int __state, int *__oldstate);
476 /* Set cancellation state of current thread to TYPE, returning the old
477 type in *OLDTYPE if OLDTYPE is not NULL. */
478 extern int pthread_setcanceltype (int __type, int *__oldtype);
480 /* Cancel THREAD immediately or at the next possibility. */
481 extern int pthread_cancel (pthread_t __th);
483 /* Test for pending cancellation for the current thread and terminate
484 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
485 cancelled. */
486 extern void pthread_testcancel (void);
489 /* Cancellation handling with integration into exception handling. */
491 typedef struct
493 struct
495 __jmp_buf __cancel_jmp_buf;
496 int __mask_was_saved;
497 } __cancel_jmp_buf[1];
498 void *__pad[4];
499 } __pthread_unwind_buf_t __attribute__ ((__aligned__));
501 /* No special attributes by default. */
502 #ifndef __cleanup_fct_attribute
503 # define __cleanup_fct_attribute
504 #endif
507 /* Structure to hold the cleanup handler information. */
508 struct __pthread_cleanup_frame
510 void (*__cancel_routine) (void *);
511 void *__cancel_arg;
512 int __do_it;
513 int __cancel_type;
516 #if defined __GNUC__ && defined __EXCEPTIONS
517 # ifdef __cplusplus
518 /* Class to handle cancellation handler invocation. */
519 class __pthread_cleanup_class
521 void (*__cancel_routine) (void *);
522 void *__cancel_arg;
523 int __do_it;
524 int __cancel_type;
526 public:
527 __pthread_cleanup_class (void (*__fct) (void *), void *__arg)
528 : __cancel_routine (__fct), __cancel_arg (__arg), __do_it (1) { }
529 ~__pthread_cleanup_class () { if (__do_it) __cancel_routine (__cancel_arg); }
530 void __setdoit (int __newval) { __do_it = __newval; }
531 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED,
532 &__cancel_type); }
533 void __restore () const { pthread_setcanceltype (__cancel_type, 0); }
536 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
537 when the thread is canceled or calls pthread_exit. ROUTINE will also
538 be called with arguments ARG when the matching pthread_cleanup_pop
539 is executed with non-zero EXECUTE argument.
541 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
542 be used in matching pairs at the same nesting level of braces. */
543 # define pthread_cleanup_push(routine, arg) \
544 do { \
545 __pthread_cleanup_class __clframe (routine, arg)
547 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
548 If EXECUTE is non-zero, the handler function is called. */
549 # define pthread_cleanup_pop(execute) \
550 __clframe.__setdoit (execute); \
551 } while (0)
553 # ifdef __USE_GNU
554 /* Install a cleanup handler as pthread_cleanup_push does, but also
555 saves the current cancellation type and sets it to deferred
556 cancellation. */
557 # define pthread_cleanup_push_defer_np(routine, arg) \
558 do { \
559 __pthread_cleanup_class __clframe (routine, arg); \
560 __clframe.__defer ()
562 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
563 restores the cancellation type that was in effect when the matching
564 pthread_cleanup_push_defer was called. */
565 # define pthread_cleanup_pop_restore_np(execute) \
566 __clframe.__restore (); \
567 __clframe.__setdoit (execute); \
568 } while (0)
569 # endif
570 # else
571 /* Function called to call the cleanup handler. As an extern inline
572 function the compiler is free to decide inlining the change when
573 needed or fall back on the copy which must exist somewhere
574 else. */
575 extern __inline void
576 __pthread_cleanup_routine (struct __pthread_cleanup_frame *__frame)
578 if (__frame->__do_it)
579 __frame->__cancel_routine (__frame->__cancel_arg);
582 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
583 when the thread is canceled or calls pthread_exit. ROUTINE will also
584 be called with arguments ARG when the matching pthread_cleanup_pop
585 is executed with non-zero EXECUTE argument.
587 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
588 be used in matching pairs at the same nesting level of braces. */
589 # define pthread_cleanup_push(routine, arg) \
590 do { \
591 struct __pthread_cleanup_frame __clframe \
592 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
593 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
594 .__do_it = 1 };
596 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
597 If EXECUTE is non-zero, the handler function is called. */
598 # define pthread_cleanup_pop(execute) \
599 __clframe.__do_it = (execute); \
600 } while (0)
602 # ifdef __USE_GNU
603 /* Install a cleanup handler as pthread_cleanup_push does, but also
604 saves the current cancellation type and sets it to deferred
605 cancellation. */
606 # define pthread_cleanup_push_defer_np(routine, arg) \
607 do { \
608 struct __pthread_cleanup_frame __clframe \
609 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
610 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
611 .__do_it = 1 }; \
612 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
613 &__clframe.__cancel_type)
615 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
616 restores the cancellation type that was in effect when the matching
617 pthread_cleanup_push_defer was called. */
618 # define pthread_cleanup_pop_restore_np(execute) \
619 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
620 __clframe.__do_it = (execute); \
621 } while (0)
622 # endif
623 # endif
624 #else
625 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
626 when the thread is canceled or calls pthread_exit. ROUTINE will also
627 be called with arguments ARG when the matching pthread_cleanup_pop
628 is executed with non-zero EXECUTE argument.
630 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
631 be used in matching pairs at the same nesting level of braces. */
632 # define pthread_cleanup_push(routine, arg) \
633 do { \
634 __pthread_unwind_buf_t __cancel_buf; \
635 void (*__cancel_routine) (void *) = (routine); \
636 void *__cancel_arg = (arg); \
637 int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) \
638 __cancel_buf.__cancel_jmp_buf, 0); \
639 if (__builtin_expect (not_first_call, 0)) \
641 __cancel_routine (__cancel_arg); \
642 __pthread_unwind_next (&__cancel_buf); \
643 /* NOTREACHED */ \
646 __pthread_register_cancel (&__cancel_buf); \
647 do {
648 extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
649 __cleanup_fct_attribute;
651 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
652 If EXECUTE is non-zero, the handler function is called. */
653 # define pthread_cleanup_pop(execute) \
654 } while (0); \
655 __pthread_unregister_cancel (&__cancel_buf); \
656 if (execute) \
657 __cancel_routine (__cancel_arg); \
658 } while (0)
659 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
660 __cleanup_fct_attribute;
662 # ifdef __USE_GNU
663 /* Install a cleanup handler as pthread_cleanup_push does, but also
664 saves the current cancellation type and sets it to deferred
665 cancellation. */
666 # define pthread_cleanup_push_defer_np(routine, arg) \
667 do { \
668 __pthread_unwind_buf_t __cancel_buf; \
669 void (*__cancel_routine) (void *) = (routine); \
670 void *__cancel_arg = (arg); \
671 int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) \
672 __cancel_buf.__cancel_jmp_buf, 0); \
673 if (__builtin_expect (not_first_call, 0)) \
675 __cancel_routine (__cancel_arg); \
676 __pthread_unwind_next (&__cancel_buf); \
677 /* NOTREACHED */ \
680 __pthread_register_cancel_defer (&__cancel_buf); \
681 do {
682 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t *__buf)
683 __cleanup_fct_attribute;
685 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
686 restores the cancellation type that was in effect when the matching
687 pthread_cleanup_push_defer was called. */
688 # define pthread_cleanup_pop_restore_np(execute) \
689 } while (0); \
690 __pthread_unregister_cancel_restore (&__cancel_buf); \
691 if (execute) \
692 __cancel_routine (__cancel_arg); \
693 } while (0)
694 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t *__buf)
695 __cleanup_fct_attribute;
696 # endif
698 /* Internal interface to initiate cleanup. */
699 extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
700 __cleanup_fct_attribute __attribute__ ((__noreturn__))
701 # ifndef SHARED
702 __attribute__ ((__weak__))
703 # endif
705 #endif
707 /* Function used in the macros. */
708 struct __jmp_buf_tag;
709 extern int __sigsetjmp (struct __jmp_buf_tag *__env, int __savemask) __THROW;
712 /* Mutex handling. */
714 /* Initialize a mutex. */
715 extern int pthread_mutex_init (pthread_mutex_t *__mutex,
716 __const pthread_mutexattr_t *__mutexattr)
717 __THROW __nonnull ((1));
719 /* Destroy a mutex. */
720 extern int pthread_mutex_destroy (pthread_mutex_t *__mutex)
721 __THROW __nonnull ((1));
723 /* Try locking a mutex. */
724 extern int pthread_mutex_trylock (pthread_mutex_t *__mutex)
725 __THROW __nonnull ((1));
727 /* Lock a mutex. */
728 extern int pthread_mutex_lock (pthread_mutex_t *__mutex)
729 __THROW __nonnull ((1));
731 #ifdef __USE_XOPEN2K
732 /* Wait until lock becomes available, or specified time passes. */
733 extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
734 __const struct timespec *__restrict
735 __abstime) __THROW __nonnull ((1, 2));
736 #endif
738 /* Unlock a mutex. */
739 extern int pthread_mutex_unlock (pthread_mutex_t *__mutex)
740 __THROW __nonnull ((1));
743 #ifdef __USE_UNIX98
744 /* Get the priority ceiling of MUTEX. */
745 extern int pthread_mutex_getprioceiling (__const pthread_mutex_t *
746 __restrict __mutex,
747 int *__restrict __prioceiling)
748 __THROW __nonnull ((1, 2));
750 /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
751 priority ceiling value in *OLD_CEILING. */
752 extern int pthread_mutex_setprioceiling (pthread_mutex_t *__restrict __mutex,
753 int __prioceiling,
754 int *__restrict __old_ceiling)
755 __THROW __nonnull ((1, 3));
756 #endif
759 #ifdef __USE_GNU
760 /* Declare the state protected by MUTEX as consistent. */
761 extern int pthread_mutex_consistent_np (pthread_mutex_t *__mutex)
762 __THROW __nonnull ((1));
763 #endif
766 /* Functions for handling mutex attributes. */
768 /* Initialize mutex attribute object ATTR with default attributes
769 (kind is PTHREAD_MUTEX_TIMED_NP). */
770 extern int pthread_mutexattr_init (pthread_mutexattr_t *__attr)
771 __THROW __nonnull ((1));
773 /* Destroy mutex attribute object ATTR. */
774 extern int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr)
775 __THROW __nonnull ((1));
777 /* Get the process-shared flag of the mutex attribute ATTR. */
778 extern int pthread_mutexattr_getpshared (__const pthread_mutexattr_t *
779 __restrict __attr,
780 int *__restrict __pshared)
781 __THROW __nonnull ((1, 2));
783 /* Set the process-shared flag of the mutex attribute ATTR. */
784 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
785 int __pshared)
786 __THROW __nonnull ((1));
788 #ifdef __USE_UNIX98
789 /* Return in *KIND the mutex kind attribute in *ATTR. */
790 extern int pthread_mutexattr_gettype (__const pthread_mutexattr_t *__restrict
791 __attr, int *__restrict __kind)
792 __THROW __nonnull ((1, 2));
794 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
795 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
796 PTHREAD_MUTEX_DEFAULT). */
797 extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
798 __THROW __nonnull ((1));
800 /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
801 extern int pthread_mutexattr_getprotocol (__const pthread_mutexattr_t *
802 __restrict __attr,
803 int *__restrict __protocol)
804 __THROW __nonnull ((1, 2));
806 /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
807 PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
808 extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr,
809 int __protocol)
810 __THROW __nonnull ((1));
812 /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
813 extern int pthread_mutexattr_getprioceiling (__const pthread_mutexattr_t *
814 __restrict __attr,
815 int *__restrict __prioceiling)
816 __THROW __nonnull ((1, 2));
818 /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
819 extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr,
820 int __prioceiling)
821 __THROW __nonnull ((1));
822 #endif
824 #ifdef __USE_GNU
825 /* Get the robustness flag of the mutex attribute ATTR. */
826 extern int pthread_mutexattr_getrobust_np (__const pthread_mutexattr_t *__attr,
827 int *__robustness)
828 __THROW __nonnull ((1, 2));
830 /* Set the robustness flag of the mutex attribute ATTR. */
831 extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t *__attr,
832 int __robustness)
833 __THROW __nonnull ((1));
834 #endif
837 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
838 /* Functions for handling read-write locks. */
840 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
841 the default values if later is NULL. */
842 extern int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
843 __const pthread_rwlockattr_t *__restrict
844 __attr) __THROW __nonnull ((1));
846 /* Destroy read-write lock RWLOCK. */
847 extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock)
848 __THROW __nonnull ((1));
850 /* Acquire read lock for RWLOCK. */
851 extern int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock)
852 __THROW __nonnull ((1));
854 /* Try to acquire read lock for RWLOCK. */
855 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock)
856 __THROW __nonnull ((1));
858 # ifdef __USE_XOPEN2K
859 /* Try to acquire read lock for RWLOCK or return after specfied time. */
860 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
861 __const struct timespec *__restrict
862 __abstime) __THROW __nonnull ((1, 2));
863 # endif
865 /* Acquire write lock for RWLOCK. */
866 extern int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock)
867 __THROW __nonnull ((1));
869 /* Try to acquire write lock for RWLOCK. */
870 extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock)
871 __THROW __nonnull ((1));
873 # ifdef __USE_XOPEN2K
874 /* Try to acquire write lock for RWLOCK or return after specfied time. */
875 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
876 __const struct timespec *__restrict
877 __abstime) __THROW __nonnull ((1, 2));
878 # endif
880 /* Unlock RWLOCK. */
881 extern int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock)
882 __THROW __nonnull ((1));
885 /* Functions for handling read-write lock attributes. */
887 /* Initialize attribute object ATTR with default values. */
888 extern int pthread_rwlockattr_init (pthread_rwlockattr_t *__attr)
889 __THROW __nonnull ((1));
891 /* Destroy attribute object ATTR. */
892 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr)
893 __THROW __nonnull ((1));
895 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
896 extern int pthread_rwlockattr_getpshared (__const pthread_rwlockattr_t *
897 __restrict __attr,
898 int *__restrict __pshared)
899 __THROW __nonnull ((1, 2));
901 /* Set process-shared attribute of ATTR to PSHARED. */
902 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr,
903 int __pshared)
904 __THROW __nonnull ((1));
906 /* Return current setting of reader/writer preference. */
907 extern int pthread_rwlockattr_getkind_np (__const pthread_rwlockattr_t *
908 __restrict __attr,
909 int *__restrict __pref)
910 __THROW __nonnull ((1, 2));
912 /* Set reader/write preference. */
913 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *__attr,
914 int __pref) __THROW __nonnull ((1));
915 #endif
918 /* Functions for handling conditional variables. */
920 /* Initialize condition variable COND using attributes ATTR, or use
921 the default values if later is NULL. */
922 extern int pthread_cond_init (pthread_cond_t *__restrict __cond,
923 __const pthread_condattr_t *__restrict
924 __cond_attr) __THROW __nonnull ((1));
926 /* Destroy condition variable COND. */
927 extern int pthread_cond_destroy (pthread_cond_t *__cond)
928 __THROW __nonnull ((1));
930 /* Wake up one thread waiting for condition variable COND. */
931 extern int pthread_cond_signal (pthread_cond_t *__cond)
932 __THROW __nonnull ((1));
934 /* Wake up all threads waiting for condition variables COND. */
935 extern int pthread_cond_broadcast (pthread_cond_t *__cond)
936 __THROW __nonnull ((1));
938 /* Wait for condition variable COND to be signaled or broadcast.
939 MUTEX is assumed to be locked before.
941 This function is a cancellation point and therefore not marked with
942 __THROW. */
943 extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,
944 pthread_mutex_t *__restrict __mutex)
945 __nonnull ((1, 2));
947 /* Wait for condition variable COND to be signaled or broadcast until
948 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
949 absolute time specification; zero is the beginning of the epoch
950 (00:00:00 GMT, January 1, 1970).
952 This function is a cancellation point and therefore not marked with
953 __THROW. */
954 extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
955 pthread_mutex_t *__restrict __mutex,
956 __const struct timespec *__restrict
957 __abstime) __nonnull ((1, 2, 3));
959 /* Functions for handling condition variable attributes. */
961 /* Initialize condition variable attribute ATTR. */
962 extern int pthread_condattr_init (pthread_condattr_t *__attr)
963 __THROW __nonnull ((1));
965 /* Destroy condition variable attribute ATTR. */
966 extern int pthread_condattr_destroy (pthread_condattr_t *__attr)
967 __THROW __nonnull ((1));
969 /* Get the process-shared flag of the condition variable attribute ATTR. */
970 extern int pthread_condattr_getpshared (__const pthread_condattr_t *
971 __restrict __attr,
972 int *__restrict __pshared)
973 __THROW __nonnull ((1, 2));
975 /* Set the process-shared flag of the condition variable attribute ATTR. */
976 extern int pthread_condattr_setpshared (pthread_condattr_t *__attr,
977 int __pshared) __THROW __nonnull ((1));
979 #ifdef __USE_XOPEN2K
980 /* Get the clock selected for the conditon variable attribute ATTR. */
981 extern int pthread_condattr_getclock (__const pthread_condattr_t *
982 __restrict __attr,
983 __clockid_t *__restrict __clock_id)
984 __THROW __nonnull ((1, 2));
986 /* Set the clock selected for the conditon variable attribute ATTR. */
987 extern int pthread_condattr_setclock (pthread_condattr_t *__attr,
988 __clockid_t __clock_id)
989 __THROW __nonnull ((1));
990 #endif
993 #ifdef __USE_XOPEN2K
994 /* Functions to handle spinlocks. */
996 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
997 be shared between different processes. */
998 extern int pthread_spin_init (pthread_spinlock_t *__lock, int __pshared)
999 __THROW __nonnull ((1));
1001 /* Destroy the spinlock LOCK. */
1002 extern int pthread_spin_destroy (pthread_spinlock_t *__lock)
1003 __THROW __nonnull ((1));
1005 /* Wait until spinlock LOCK is retrieved. */
1006 extern int pthread_spin_lock (pthread_spinlock_t *__lock)
1007 __THROW __nonnull ((1));
1009 /* Try to lock spinlock LOCK. */
1010 extern int pthread_spin_trylock (pthread_spinlock_t *__lock)
1011 __THROW __nonnull ((1));
1013 /* Release spinlock LOCK. */
1014 extern int pthread_spin_unlock (pthread_spinlock_t *__lock)
1015 __THROW __nonnull ((1));
1018 /* Functions to handle barriers. */
1020 /* Initialize BARRIER with the attributes in ATTR. The barrier is
1021 opened when COUNT waiters arrived. */
1022 extern int pthread_barrier_init (pthread_barrier_t *__restrict __barrier,
1023 __const pthread_barrierattr_t *__restrict
1024 __attr, unsigned int __count)
1025 __THROW __nonnull ((1));
1027 /* Destroy a previously dynamically initialized barrier BARRIER. */
1028 extern int pthread_barrier_destroy (pthread_barrier_t *__barrier)
1029 __THROW __nonnull ((1));
1031 /* Wait on barrier BARRIER. */
1032 extern int pthread_barrier_wait (pthread_barrier_t *__barrier)
1033 __THROW __nonnull ((1));
1036 /* Initialize barrier attribute ATTR. */
1037 extern int pthread_barrierattr_init (pthread_barrierattr_t *__attr)
1038 __THROW __nonnull ((1));
1040 /* Destroy previously dynamically initialized barrier attribute ATTR. */
1041 extern int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr)
1042 __THROW __nonnull ((1));
1044 /* Get the process-shared flag of the barrier attribute ATTR. */
1045 extern int pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
1046 __restrict __attr,
1047 int *__restrict __pshared)
1048 __THROW __nonnull ((1, 2));
1050 /* Set the process-shared flag of the barrier attribute ATTR. */
1051 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr,
1052 int __pshared)
1053 __THROW __nonnull ((1));
1054 #endif
1057 /* Functions for handling thread-specific data. */
1059 /* Create a key value identifying a location in the thread-specific
1060 data area. Each thread maintains a distinct thread-specific data
1061 area. DESTR_FUNCTION, if non-NULL, is called with the value
1062 associated to that key when the key is destroyed.
1063 DESTR_FUNCTION is not called if the value associated is NULL when
1064 the key is destroyed. */
1065 extern int pthread_key_create (pthread_key_t *__key,
1066 void (*__destr_function) (void *))
1067 __THROW __nonnull ((1));
1069 /* Destroy KEY. */
1070 extern int pthread_key_delete (pthread_key_t __key) __THROW;
1072 /* Return current value of the thread-specific data slot identified by KEY. */
1073 extern void *pthread_getspecific (pthread_key_t __key) __THROW;
1075 /* Store POINTER in the thread-specific data slot identified by KEY. */
1076 extern int pthread_setspecific (pthread_key_t __key,
1077 __const void *__pointer) __THROW ;
1080 #ifdef __USE_XOPEN2K
1081 /* Get ID of CPU-time clock for thread THREAD_ID. */
1082 extern int pthread_getcpuclockid (pthread_t __thread_id,
1083 __clockid_t *__clock_id)
1084 __THROW __nonnull ((2));
1085 #endif
1088 /* Install handlers to be called when a new process is created with FORK.
1089 The PREPARE handler is called in the parent process just before performing
1090 FORK. The PARENT handler is called in the parent process just after FORK.
1091 The CHILD handler is called in the child process. Each of the three
1092 handlers can be NULL, meaning that no handler needs to be called at that
1093 point.
1094 PTHREAD_ATFORK can be called several times, in which case the PREPARE
1095 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
1096 first called before FORK), and the PARENT and CHILD handlers are called
1097 in FIFO (first added, first called). */
1099 extern int pthread_atfork (void (*__prepare) (void),
1100 void (*__parent) (void),
1101 void (*__child) (void)) __THROW;
1104 #ifdef __USE_EXTERN_INLINES
1105 /* Optimizations. */
1106 extern __inline int
1107 __NTH (pthread_equal (pthread_t __thread1, pthread_t __thread2))
1109 return __thread1 == __thread2;
1111 #endif
1113 __END_DECLS
1115 #endif /* pthread.h */