* sysdeps/i386/add_n.S: Add call frame information.
[glibc.git] / nptl / sysdeps / pthread / pthread.h
blob05f3b31fb5986035632fc4ed985b5cbec34fd572
1 /* Copyright (C) 2002, 2003, 2004, 2005 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
63 /* Mutex initializers. */
64 #define PTHREAD_MUTEX_INITIALIZER \
65 { { 0, } }
66 #ifdef __USE_GNU
67 # if __WORDSIZE == 64
68 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
69 { { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP } }
70 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
71 { { 0, 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP } }
72 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
73 { { 0, 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP } }
74 # else
75 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
76 { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP } }
77 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
78 { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP } }
79 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
80 { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP } }
81 # endif
82 #endif
85 /* Read-write lock types. */
86 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
87 enum
89 PTHREAD_RWLOCK_PREFER_READER_NP,
90 PTHREAD_RWLOCK_PREFER_WRITER_NP,
91 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
92 PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_READER_NP
95 /* Read-write lock initializers. */
96 # define PTHREAD_RWLOCK_INITIALIZER \
97 { { 0, } }
98 # ifdef __USE_GNU
99 # if __WORDSIZE == 64
100 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
101 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
102 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
103 # else
104 # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
105 { { 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
106 # endif
107 # endif
108 #endif /* Unix98 or XOpen2K */
111 /* Scheduler inheritance. */
112 enum
114 PTHREAD_INHERIT_SCHED,
115 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
116 PTHREAD_EXPLICIT_SCHED
117 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
121 /* Scope handling. */
122 enum
124 PTHREAD_SCOPE_SYSTEM,
125 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
126 PTHREAD_SCOPE_PROCESS
127 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
131 /* Process shared or private flag. */
132 enum
134 PTHREAD_PROCESS_PRIVATE,
135 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
136 PTHREAD_PROCESS_SHARED
137 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
142 /* Conditional variable handling. */
143 #define PTHREAD_COND_INITIALIZER { { 0, } }
146 /* Cleanup buffers */
147 struct _pthread_cleanup_buffer
149 void (*__routine) (void *); /* Function to call. */
150 void *__arg; /* Its argument. */
151 int __canceltype; /* Saved cancellation type. */
152 struct _pthread_cleanup_buffer *__prev; /* Chaining of cleanup functions. */
155 /* Cancellation */
156 enum
158 PTHREAD_CANCEL_ENABLE,
159 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
160 PTHREAD_CANCEL_DISABLE
161 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
163 enum
165 PTHREAD_CANCEL_DEFERRED,
166 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
167 PTHREAD_CANCEL_ASYNCHRONOUS
168 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
170 #define PTHREAD_CANCELED ((void *) -1)
173 /* Single execution handling. */
174 #define PTHREAD_ONCE_INIT 0
177 #ifdef __USE_XOPEN2K
178 /* Value returned by 'pthread_barrier_wait' for one of the threads after
179 the required number of threads have called this function.
180 -1 is distinct from 0 and all errno constants */
181 # define PTHREAD_BARRIER_SERIAL_THREAD -1
182 #endif
185 __BEGIN_DECLS
187 /* Create a new thread, starting with execution of START-ROUTINE
188 getting passed ARG. Creation attributed come from ATTR. The new
189 handle is stored in *NEWTHREAD. */
190 extern int pthread_create (pthread_t *__restrict __newthread,
191 __const pthread_attr_t *__restrict __attr,
192 void *(*__start_routine) (void *),
193 void *__restrict __arg) __THROW;
195 /* Terminate calling thread.
197 The registered cleanup handlers are called via exception handling
198 so we cannot mark this function with __THROW.*/
199 extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__));
201 /* Make calling thread wait for termination of the thread TH. The
202 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
203 is not NULL.
205 This function is a cancellation point and therefore not marked with
206 __THROW. */
207 extern int pthread_join (pthread_t __th, void **__thread_return);
209 #ifdef __USE_GNU
210 /* Check whether thread TH has terminated. If yes return the status of
211 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
212 extern int pthread_tryjoin_np (pthread_t __th, void **__thread_return) __THROW;
214 /* Make calling thread wait for termination of the thread TH, but only
215 until TIMEOUT. The exit status of the thread is stored in
216 *THREAD_RETURN, if THREAD_RETURN is not NULL.
218 This function is a cancellation point and therefore not marked with
219 __THROW. */
220 extern int pthread_timedjoin_np (pthread_t __th, void **__thread_return,
221 __const struct timespec *__abstime);
222 #endif
224 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
225 The resources of TH will therefore be freed immediately when it
226 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
227 on it. */
228 extern int pthread_detach (pthread_t __th) __THROW;
231 /* Obtain the identifier of the current thread. */
232 extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__));
234 /* Compare two thread identifiers. */
235 extern int pthread_equal (pthread_t __thread1, pthread_t __thread2) __THROW;
238 /* Thread attribute handling. */
240 /* Initialize thread attribute *ATTR with default attributes
241 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
242 no user-provided stack). */
243 extern int pthread_attr_init (pthread_attr_t *__attr) __THROW;
245 /* Destroy thread attribute *ATTR. */
246 extern int pthread_attr_destroy (pthread_attr_t *__attr) __THROW;
248 /* Get detach state attribute. */
249 extern int pthread_attr_getdetachstate (__const pthread_attr_t *__attr,
250 int *__detachstate) __THROW;
252 /* Set detach state attribute. */
253 extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
254 int __detachstate) __THROW;
257 /* Get the size of the guard area created for stack overflow protection. */
258 extern int pthread_attr_getguardsize (__const pthread_attr_t *__attr,
259 size_t *__guardsize) __THROW;
261 /* Set the size of the guard area created for stack overflow protection. */
262 extern int pthread_attr_setguardsize (pthread_attr_t *__attr,
263 size_t __guardsize) __THROW;
266 /* Return in *PARAM the scheduling parameters of *ATTR. */
267 extern int pthread_attr_getschedparam (__const pthread_attr_t *__restrict
268 __attr,
269 struct sched_param *__restrict __param)
270 __THROW;
272 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
273 extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,
274 __const struct sched_param *__restrict
275 __param) __THROW;
277 /* Return in *POLICY the scheduling policy of *ATTR. */
278 extern int pthread_attr_getschedpolicy (__const pthread_attr_t *__restrict
279 __attr, int *__restrict __policy)
280 __THROW;
282 /* Set scheduling policy in *ATTR according to POLICY. */
283 extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy)
284 __THROW;
286 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
287 extern int pthread_attr_getinheritsched (__const pthread_attr_t *__restrict
288 __attr, int *__restrict __inherit)
289 __THROW;
291 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
292 extern int pthread_attr_setinheritsched (pthread_attr_t *__attr,
293 int __inherit) __THROW;
296 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
297 extern int pthread_attr_getscope (__const pthread_attr_t *__restrict __attr,
298 int *__restrict __scope) __THROW;
300 /* Set scheduling contention scope in *ATTR according to SCOPE. */
301 extern int pthread_attr_setscope (pthread_attr_t *__attr, int __scope)
302 __THROW;
304 /* Return the previously set address for the stack. */
305 extern int pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
306 __attr, void **__restrict __stackaddr)
307 __THROW __attribute_deprecated__;
309 /* Set the starting address of the stack of the thread to be created.
310 Depending on whether the stack grows up or down the value must either
311 be higher or lower than all the address in the memory block. The
312 minimal size of the block must be PTHREAD_STACK_MIN. */
313 extern int pthread_attr_setstackaddr (pthread_attr_t *__attr,
314 void *__stackaddr)
315 __THROW __attribute_deprecated__;
317 /* Return the currently used minimal stack size. */
318 extern int pthread_attr_getstacksize (__const pthread_attr_t *__restrict
319 __attr, size_t *__restrict __stacksize)
320 __THROW;
322 /* Add information about the minimum stack size needed for the thread
323 to be started. This size must never be less than PTHREAD_STACK_MIN
324 and must also not exceed the system limits. */
325 extern int pthread_attr_setstacksize (pthread_attr_t *__attr,
326 size_t __stacksize) __THROW;
328 #ifdef __USE_XOPEN2K
329 /* Return the previously set address for the stack. */
330 extern int pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
331 void **__restrict __stackaddr,
332 size_t *__restrict __stacksize) __THROW;
334 /* The following two interfaces are intended to replace the last two. They
335 require setting the address as well as the size since only setting the
336 address will make the implementation on some architectures impossible. */
337 extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
338 size_t __stacksize) __THROW;
339 #endif
341 #ifdef __USE_GNU
342 /* Thread created with attribute ATTR will be limited to run only on
343 the processors represented in CPUSET. */
344 extern int pthread_attr_setaffinity_np (pthread_attr_t *__attr,
345 size_t __cpusetsize,
346 __const cpu_set_t *__cpuset) __THROW;
348 /* Get bit set in CPUSET representing the processors threads created with
349 ATTR can run on. */
350 extern int pthread_attr_getaffinity_np (__const pthread_attr_t *__attr,
351 size_t __cpusetsize,
352 cpu_set_t *__cpuset) __THROW;
355 /* Initialize thread attribute *ATTR with attributes corresponding to the
356 already running thread TH. It shall be called on unitialized ATTR
357 and destroyed with pthread_attr_destroy when no longer needed. */
358 extern int pthread_getattr_np (pthread_t __th, pthread_attr_t *__attr) __THROW;
359 #endif
362 /* Functions for scheduling control. */
364 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
365 and *PARAM. */
366 extern int pthread_setschedparam (pthread_t __target_thread, int __policy,
367 __const struct sched_param *__param)
368 __THROW;
370 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
371 extern int pthread_getschedparam (pthread_t __target_thread,
372 int *__restrict __policy,
373 struct sched_param *__restrict __param)
374 __THROW;
376 /* Set the scheduling priority for TARGET_THREAD. */
377 extern int pthread_setschedprio (pthread_t __target_thread, int __prio)
378 __THROW;
381 #ifdef __USE_UNIX98
382 /* Determine level of concurrency. */
383 extern int pthread_getconcurrency (void) __THROW;
385 /* Set new concurrency level to LEVEL. */
386 extern int pthread_setconcurrency (int __level) __THROW;
387 #endif
389 #ifdef __USE_GNU
390 /* Yield the processor to another thread or process.
391 This function is similar to the POSIX `sched_yield' function but
392 might be differently implemented in the case of a m-on-n thread
393 implementation. */
394 extern int pthread_yield (void) __THROW;
397 /* Limit specified thread TH to run only on the processors represented
398 in CPUSET. */
399 extern int pthread_setaffinity_np (pthread_t __th, size_t __cpusetsize,
400 __const cpu_set_t *__cpuset) __THROW;
402 /* Get bit set in CPUSET representing the processors TH can run on. */
403 extern int pthread_getaffinity_np (pthread_t __th, size_t __cpusetsize,
404 cpu_set_t *__cpuset) __THROW;
405 #endif
408 /* Functions for handling initialization. */
410 /* Guarantee that the initialization function INIT_ROUTINE will be called
411 only once, even if pthread_once is executed several times with the
412 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
413 extern variable initialized to PTHREAD_ONCE_INIT.
415 The initialization functions might throw exception which is why
416 this function is not marked with __THROW. */
417 extern int pthread_once (pthread_once_t *__once_control,
418 void (*__init_routine) (void));
421 /* Functions for handling cancellation.
423 Note that these functions are explicitly not marked to not throw an
424 exception in C++ code. If cancellation is implemented by unwinding
425 this is necessary to have the compiler generate the unwind information. */
427 /* Set cancelability state of current thread to STATE, returning old
428 state in *OLDSTATE if OLDSTATE is not NULL. */
429 extern int pthread_setcancelstate (int __state, int *__oldstate);
431 /* Set cancellation state of current thread to TYPE, returning the old
432 type in *OLDTYPE if OLDTYPE is not NULL. */
433 extern int pthread_setcanceltype (int __type, int *__oldtype);
435 /* Cancel THREAD immediately or at the next possibility. */
436 extern int pthread_cancel (pthread_t __th);
438 /* Test for pending cancellation for the current thread and terminate
439 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
440 cancelled. */
441 extern void pthread_testcancel (void);
444 /* Cancellation handling with integration into exception handling. */
446 typedef struct
448 struct
450 __jmp_buf __cancel_jmp_buf;
451 int __mask_was_saved;
452 } __cancel_jmp_buf[1];
453 void *__pad[4];
454 } __pthread_unwind_buf_t __attribute__ ((__aligned__));
456 /* No special attributes by default. */
457 #ifndef __cleanup_fct_attribute
458 # define __cleanup_fct_attribute
459 #endif
462 /* Structure to hold the cleanup handler information. */
463 struct __pthread_cleanup_frame
465 void (*__cancel_routine) (void *);
466 void *__cancel_arg;
467 int __do_it;
468 int __cancel_type;
471 #if defined __GNUC__ && defined __EXCEPTIONS
472 # ifdef __cplusplus
473 /* Class to handle cancellation handler invocation. */
474 class __pthread_cleanup_class
476 void (*__cancel_routine) (void *);
477 void *__cancel_arg;
478 int __do_it;
479 int __cancel_type;
481 public:
482 __pthread_cleanup_class (void (*__fct) (void *), void *__arg)
483 : __cancel_routine (__fct), __cancel_arg (__arg), __do_it (1) { }
484 ~__pthread_cleanup_class () { if (__do_it) __cancel_routine (__cancel_arg); }
485 void __setdoit (int __newval) { __do_it = __newval; }
486 void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED,
487 &__cancel_type); }
488 void __restore () const { pthread_setcanceltype (__cancel_type, 0); }
491 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
492 when the thread is canceled or calls pthread_exit. ROUTINE will also
493 be called with arguments ARG when the matching pthread_cleanup_pop
494 is executed with non-zero EXECUTE argument.
496 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
497 be used in matching pairs at the same nesting level of braces. */
498 # define pthread_cleanup_push(routine, arg) \
499 do { \
500 __pthread_cleanup_class __clframe (routine, arg)
502 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
503 If EXECUTE is non-zero, the handler function is called. */
504 # define pthread_cleanup_pop(execute) \
505 __clframe.__setdoit (execute); \
506 } while (0)
508 # ifdef __USE_GNU
509 /* Install a cleanup handler as pthread_cleanup_push does, but also
510 saves the current cancellation type and sets it to deferred
511 cancellation. */
512 # define pthread_cleanup_push_defer_np(routine, arg) \
513 do { \
514 __pthread_cleanup_class __clframe (routine, arg); \
515 __clframe.__defer ()
517 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
518 restores the cancellation type that was in effect when the matching
519 pthread_cleanup_push_defer was called. */
520 # define pthread_cleanup_pop_restore_np(execute) \
521 __clframe.__restore (); \
522 __clframe.__setdoit (execute); \
523 } while (0)
524 # endif
525 # else
526 /* Function called to call the cleanup handler. As an extern inline
527 function the compiler is free to decide inlining the change when
528 needed or fall back on the copy which must exist somewhere
529 else. */
530 extern __inline void
531 __pthread_cleanup_routine (struct __pthread_cleanup_frame *__frame)
533 if (__frame->__do_it)
534 __frame->__cancel_routine (__frame->__cancel_arg);
537 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
538 when the thread is canceled or calls pthread_exit. ROUTINE will also
539 be called with arguments ARG when the matching pthread_cleanup_pop
540 is executed with non-zero EXECUTE argument.
542 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
543 be used in matching pairs at the same nesting level of braces. */
544 # define pthread_cleanup_push(routine, arg) \
545 do { \
546 struct __pthread_cleanup_frame __clframe \
547 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
548 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
549 .__do_it = 1 };
551 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
552 If EXECUTE is non-zero, the handler function is called. */
553 # define pthread_cleanup_pop(execute) \
554 __clframe.__do_it = (execute); \
555 } while (0)
557 # ifdef __USE_GNU
558 /* Install a cleanup handler as pthread_cleanup_push does, but also
559 saves the current cancellation type and sets it to deferred
560 cancellation. */
561 # define pthread_cleanup_push_defer_np(routine, arg) \
562 do { \
563 struct __pthread_cleanup_frame __clframe \
564 __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
565 = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
566 .__do_it = 1 }; \
567 (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
568 &__clframe.__cancel_type)
570 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
571 restores the cancellation type that was in effect when the matching
572 pthread_cleanup_push_defer was called. */
573 # define pthread_cleanup_pop_restore_np(execute) \
574 (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
575 __clframe.__do_it = (execute); \
576 } while (0)
577 # endif
578 # endif
579 #else
580 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
581 when the thread is canceled or calls pthread_exit. ROUTINE will also
582 be called with arguments ARG when the matching pthread_cleanup_pop
583 is executed with non-zero EXECUTE argument.
585 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
586 be used in matching pairs at the same nesting level of braces. */
587 # define pthread_cleanup_push(routine, arg) \
588 do { \
589 __pthread_unwind_buf_t __cancel_buf; \
590 void (*__cancel_routine) (void *) = (routine); \
591 void *__cancel_arg = (arg); \
592 int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) \
593 __cancel_buf.__cancel_jmp_buf, 0); \
594 if (__builtin_expect (not_first_call, 0)) \
596 __cancel_routine (__cancel_arg); \
597 __pthread_unwind_next (&__cancel_buf); \
598 /* NOTREACHED */ \
601 __pthread_register_cancel (&__cancel_buf); \
602 do {
603 extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
604 __cleanup_fct_attribute;
606 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
607 If EXECUTE is non-zero, the handler function is called. */
608 # define pthread_cleanup_pop(execute) \
609 } while (0); \
610 __pthread_unregister_cancel (&__cancel_buf); \
611 if (execute) \
612 __cancel_routine (__cancel_arg); \
613 } while (0)
614 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
615 __cleanup_fct_attribute;
617 # ifdef __USE_GNU
618 /* Install a cleanup handler as pthread_cleanup_push does, but also
619 saves the current cancellation type and sets it to deferred
620 cancellation. */
621 # define pthread_cleanup_push_defer_np(routine, arg) \
622 do { \
623 __pthread_unwind_buf_t __cancel_buf; \
624 void (*__cancel_routine) (void *) = (routine); \
625 void *__cancel_arg = (arg); \
626 int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) \
627 __cancel_buf.__cancel_jmp_buf, 0); \
628 if (__builtin_expect (not_first_call, 0)) \
630 __cancel_routine (__cancel_arg); \
631 __pthread_unwind_next (&__cancel_buf); \
632 /* NOTREACHED */ \
635 __pthread_register_cancel_defer (&__cancel_buf); \
636 do {
637 extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t *__buf)
638 __cleanup_fct_attribute;
640 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
641 restores the cancellation type that was in effect when the matching
642 pthread_cleanup_push_defer was called. */
643 # define pthread_cleanup_pop_restore_np(execute) \
644 } while (0); \
645 __pthread_unregister_cancel_restore (&__cancel_buf); \
646 if (execute) \
647 __cancel_routine (__cancel_arg); \
648 } while (0)
649 extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t *__buf)
650 __cleanup_fct_attribute;
651 # endif
653 /* Internal interface to initiate cleanup. */
654 extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
655 __cleanup_fct_attribute __attribute ((__noreturn__))
656 # ifndef SHARED
657 __attribute ((__weak__))
658 # endif
660 #endif
662 /* Function used in the macros. */
663 struct __jmp_buf_tag;
664 extern int __sigsetjmp (struct __jmp_buf_tag *__env, int __savemask) __THROW;
667 /* Mutex handling. */
669 /* Initialize a mutex. */
670 extern int pthread_mutex_init (pthread_mutex_t *__mutex,
671 __const pthread_mutexattr_t *__mutexattr)
672 __THROW;
674 /* Destroy a mutex. */
675 extern int pthread_mutex_destroy (pthread_mutex_t *__mutex) __THROW;
677 /* Try locking a mutex. */
678 extern int pthread_mutex_trylock (pthread_mutex_t *_mutex) __THROW;
680 /* Lock a mutex. */
681 extern int pthread_mutex_lock (pthread_mutex_t *__mutex) __THROW;
683 #ifdef __USE_XOPEN2K
684 /* Wait until lock becomes available, or specified time passes. */
685 extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
686 __const struct timespec *__restrict
687 __abstime) __THROW;
688 #endif
690 /* Unlock a mutex. */
691 extern int pthread_mutex_unlock (pthread_mutex_t *__mutex) __THROW;
694 /* Functions for handling mutex attributes. */
696 /* Initialize mutex attribute object ATTR with default attributes
697 (kind is PTHREAD_MUTEX_TIMED_NP). */
698 extern int pthread_mutexattr_init (pthread_mutexattr_t *__attr) __THROW;
700 /* Destroy mutex attribute object ATTR. */
701 extern int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr) __THROW;
703 /* Get the process-shared flag of the mutex attribute ATTR. */
704 extern int pthread_mutexattr_getpshared (__const pthread_mutexattr_t *
705 __restrict __attr,
706 int *__restrict __pshared) __THROW;
708 /* Set the process-shared flag of the mutex attribute ATTR. */
709 extern int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
710 int __pshared) __THROW;
712 #ifdef __USE_UNIX98
713 /* Return in *KIND the mutex kind attribute in *ATTR. */
714 extern int pthread_mutexattr_gettype (__const pthread_mutexattr_t *__restrict
715 __attr, int *__restrict __kind) __THROW;
717 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
718 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
719 PTHREAD_MUTEX_DEFAULT). */
720 extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
721 __THROW;
722 #endif
725 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
726 /* Functions for handling read-write locks. */
728 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
729 the default values if later is NULL. */
730 extern int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
731 __const pthread_rwlockattr_t *__restrict
732 __attr) __THROW;
734 /* Destroy read-write lock RWLOCK. */
735 extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock) __THROW;
737 /* Acquire read lock for RWLOCK. */
738 extern int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock) __THROW;
740 /* Try to acquire read lock for RWLOCK. */
741 extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock) __THROW;
743 # ifdef __USE_XOPEN2K
744 /* Try to acquire read lock for RWLOCK or return after specfied time. */
745 extern int pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
746 __const struct timespec *__restrict
747 __abstime) __THROW;
748 # endif
750 /* Acquire write lock for RWLOCK. */
751 extern int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock) __THROW;
753 /* Try to acquire write lock for RWLOCK. */
754 extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock) __THROW;
756 # ifdef __USE_XOPEN2K
757 /* Try to acquire write lock for RWLOCK or return after specfied time. */
758 extern int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
759 __const struct timespec *__restrict
760 __abstime) __THROW;
761 # endif
763 /* Unlock RWLOCK. */
764 extern int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock) __THROW;
767 /* Functions for handling read-write lock attributes. */
769 /* Initialize attribute object ATTR with default values. */
770 extern int pthread_rwlockattr_init (pthread_rwlockattr_t *__attr) __THROW;
772 /* Destroy attribute object ATTR. */
773 extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr) __THROW;
775 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
776 extern int pthread_rwlockattr_getpshared (__const pthread_rwlockattr_t *
777 __restrict __attr,
778 int *__restrict __pshared) __THROW;
780 /* Set process-shared attribute of ATTR to PSHARED. */
781 extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr,
782 int __pshared) __THROW;
784 /* Return current setting of reader/writer preference. */
785 extern int pthread_rwlockattr_getkind_np (__const pthread_rwlockattr_t *
786 __restrict __attr,
787 int *__restrict __pref) __THROW;
789 /* Set reader/write preference. */
790 extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *__attr,
791 int __pref) __THROW;
792 #endif
795 /* Functions for handling conditional variables. */
797 /* Initialize condition variable COND using attributes ATTR, or use
798 the default values if later is NULL. */
799 extern int pthread_cond_init (pthread_cond_t *__restrict __cond,
800 __const pthread_condattr_t *__restrict
801 __cond_attr) __THROW;
803 /* Destroy condition variable COND. */
804 extern int pthread_cond_destroy (pthread_cond_t *__cond) __THROW;
806 /* Wake up one thread waiting for condition variable COND. */
807 extern int pthread_cond_signal (pthread_cond_t *__cond) __THROW;
809 /* Wake up all threads waiting for condition variables COND. */
810 extern int pthread_cond_broadcast (pthread_cond_t *__cond) __THROW;
812 /* Wait for condition variable COND to be signaled or broadcast.
813 MUTEX is assumed to be locked before.
815 This function is a cancellation point and therefore not marked with
816 __THROW. */
817 extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,
818 pthread_mutex_t *__restrict __mutex);
820 /* Wait for condition variable COND to be signaled or broadcast until
821 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
822 absolute time specification; zero is the beginning of the epoch
823 (00:00:00 GMT, January 1, 1970).
825 This function is a cancellation point and therefore not marked with
826 __THROW. */
827 extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
828 pthread_mutex_t *__restrict __mutex,
829 __const struct timespec *__restrict
830 __abstime);
832 /* Functions for handling condition variable attributes. */
834 /* Initialize condition variable attribute ATTR. */
835 extern int pthread_condattr_init (pthread_condattr_t *__attr) __THROW;
837 /* Destroy condition variable attribute ATTR. */
838 extern int pthread_condattr_destroy (pthread_condattr_t *__attr) __THROW;
840 /* Get the process-shared flag of the condition variable attribute ATTR. */
841 extern int pthread_condattr_getpshared (__const pthread_condattr_t *
842 __restrict __attr,
843 int *__restrict __pshared) __THROW;
845 /* Set the process-shared flag of the condition variable attribute ATTR. */
846 extern int pthread_condattr_setpshared (pthread_condattr_t *__attr,
847 int __pshared) __THROW;
849 #ifdef __USE_XOPEN2K
850 /* Get the clock selected for the conditon variable attribute ATTR. */
851 extern int pthread_condattr_getclock (__const pthread_condattr_t *
852 __restrict __attr,
853 __clockid_t *__restrict __clock_id)
854 __THROW;
856 /* Set the clock selected for the conditon variable attribute ATTR. */
857 extern int pthread_condattr_setclock (pthread_condattr_t *__attr,
858 __clockid_t __clock_id) __THROW;
860 #endif
863 #ifdef __USE_XOPEN2K
864 /* Functions to handle spinlocks. */
866 /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
867 be shared between different processes. */
868 extern int pthread_spin_init (pthread_spinlock_t *__lock, int __pshared)
869 __THROW;
871 /* Destroy the spinlock LOCK. */
872 extern int pthread_spin_destroy (pthread_spinlock_t *__lock) __THROW;
874 /* Wait until spinlock LOCK is retrieved. */
875 extern int pthread_spin_lock (pthread_spinlock_t *__lock) __THROW;
877 /* Try to lock spinlock LOCK. */
878 extern int pthread_spin_trylock (pthread_spinlock_t *__lock) __THROW;
880 /* Release spinlock LOCK. */
881 extern int pthread_spin_unlock (pthread_spinlock_t *__lock) __THROW;
884 /* Functions to handle barriers. */
886 /* Initialize BARRIER with the attributes in ATTR. The barrier is
887 opened when COUNT waiters arrived. */
888 extern int pthread_barrier_init (pthread_barrier_t *__restrict __barrier,
889 __const pthread_barrierattr_t *__restrict
890 __attr, unsigned int __count) __THROW;
892 /* Destroy a previously dynamically initialized barrier BARRIER. */
893 extern int pthread_barrier_destroy (pthread_barrier_t *__barrier) __THROW;
895 /* Wait on barrier BARRIER. */
896 extern int pthread_barrier_wait (pthread_barrier_t *__barrier) __THROW;
899 /* Initialize barrier attribute ATTR. */
900 extern int pthread_barrierattr_init (pthread_barrierattr_t *__attr) __THROW;
902 /* Destroy previously dynamically initialized barrier attribute ATTR. */
903 extern int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr) __THROW;
905 /* Get the process-shared flag of the barrier attribute ATTR. */
906 extern int pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
907 __restrict __attr,
908 int *__restrict __pshared) __THROW;
910 /* Set the process-shared flag of the barrier attribute ATTR. */
911 extern int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr,
912 int __pshared) __THROW;
913 #endif
916 /* Functions for handling thread-specific data. */
918 /* Create a key value identifying a location in the thread-specific
919 data area. Each thread maintains a distinct thread-specific data
920 area. DESTR_FUNCTION, if non-NULL, is called with the value
921 associated to that key when the key is destroyed.
922 DESTR_FUNCTION is not called if the value associated is NULL when
923 the key is destroyed. */
924 extern int pthread_key_create (pthread_key_t *__key,
925 void (*__destr_function) (void *)) __THROW;
927 /* Destroy KEY. */
928 extern int pthread_key_delete (pthread_key_t __key) __THROW;
930 /* Return current value of the thread-specific data slot identified by KEY. */
931 extern void *pthread_getspecific (pthread_key_t __key) __THROW;
933 /* Store POINTER in the thread-specific data slot identified by KEY. */
934 extern int pthread_setspecific (pthread_key_t __key,
935 __const void *__pointer) __THROW;
938 #ifdef __USE_XOPEN2K
939 /* Get ID of CPU-time clock for thread THREAD_ID. */
940 extern int pthread_getcpuclockid (pthread_t __thread_id,
941 __clockid_t *__clock_id) __THROW;
942 #endif
945 /* Install handlers to be called when a new process is created with FORK.
946 The PREPARE handler is called in the parent process just before performing
947 FORK. The PARENT handler is called in the parent process just after FORK.
948 The CHILD handler is called in the child process. Each of the three
949 handlers can be NULL, meaning that no handler needs to be called at that
950 point.
951 PTHREAD_ATFORK can be called several times, in which case the PREPARE
952 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
953 first called before FORK), and the PARENT and CHILD handlers are called
954 in FIFO (first added, first called). */
956 extern int pthread_atfork (void (*__prepare) (void),
957 void (*__parent) (void),
958 void (*__child) (void)) __THROW;
960 __END_DECLS
962 #endif /* pthread.h */