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