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