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

source code of include/pthread.h