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