]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/hppa/pthread.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / hppa / pthread.h
CommitLineData
04277e02 1/* Copyright (C) 2002-2019 Free Software Foundation, Inc.
be0f2008
CD
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
a64b8a46
CD
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
be0f2008 17
3c0c543b
CD
18#ifndef _PTHREAD_H
19#define _PTHREAD_H 1
20
21#include <features.h>
22#include <endian.h>
23#include <sched.h>
24#include <time.h>
25
26#include <bits/pthreadtypes.h>
27#include <bits/setjmp.h>
28#include <bits/wordsize.h>
c5f70682 29#include <bits/types/struct_timespec.h>
3c0c543b
CD
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
c5f70682
JDA
86#define PTHREAD_MUTEX_INITIALIZER \
87 { { 0, 0, 0, 0, { 0, 0, 0, 0 }, 0, { __PTHREAD_SPINS }, { 0, 0 } } }
88#ifdef __USE_GNU
89# define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
90 { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, { 0, 0, 0, 0 }, 0, \
91 { __PTHREAD_SPINS }, { 0, 0 } } }
92# define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
93 { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, { 0, 0, 0, 0 }, 0, \
94 { __PTHREAD_SPINS }, { 0, 0 } } }
95# define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
96 { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, { 0, 0, 0, 0 }, 0, \
97 { __PTHREAD_SPINS }, { 0, 0 } } }
3c0c543b
CD
98#endif
99
100
101/* Read-write lock types. */
102#if defined __USE_UNIX98 || defined __USE_XOPEN2K
103enum
104{
105 PTHREAD_RWLOCK_PREFER_READER_NP,
106 PTHREAD_RWLOCK_PREFER_WRITER_NP,
107 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
108 PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_READER_NP
109};
110
111/* Define __PTHREAD_RWLOCK_INT_FLAGS_SHARED to 1 if pthread_rwlock_t
112 has the shared field. All 64-bit architectures have the shared field
113 in pthread_rwlock_t. */
114#ifndef __PTHREAD_RWLOCK_INT_FLAGS_SHARED
115# if __WORDSIZE == 64
116# define __PTHREAD_RWLOCK_INT_FLAGS_SHARED 1
117# endif
118#endif
119
c5f70682 120
3c0c543b
CD
121/* Read-write lock initializers. */
122# define PTHREAD_RWLOCK_INITIALIZER \
c5f70682 123 { { { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
3c0c543b 124# ifdef __USE_GNU
c5f70682
JDA
125# define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
126 { { { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
127 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, 0, 0, 0 } }
3c0c543b
CD
128# endif
129#endif /* Unix98 or XOpen2K */
130
131
132/* Scheduler inheritance. */
133enum
134{
135 PTHREAD_INHERIT_SCHED,
136#define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
137 PTHREAD_EXPLICIT_SCHED
138#define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
139};
140
141
142/* Scope handling. */
143enum
144{
145 PTHREAD_SCOPE_SYSTEM,
146#define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
147 PTHREAD_SCOPE_PROCESS
148#define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
149};
150
151
152/* Process shared or private flag. */
153enum
154{
155 PTHREAD_PROCESS_PRIVATE,
156#define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
157 PTHREAD_PROCESS_SHARED
158#define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
159};
160
161
3c0c543b 162/* Conditional variable handling. */
c5f70682 163#define PTHREAD_COND_INITIALIZER { { {0}, {0}, {0, 0}, {0, 0}, 0, 0, {0, 0} } }
3c0c543b
CD
164
165
166/* Cleanup buffers */
167struct _pthread_cleanup_buffer
168{
169 void (*__routine) (void *); /* Function to call. */
170 void *__arg; /* Its argument. */
171 int __canceltype; /* Saved cancellation type. */
172 struct _pthread_cleanup_buffer *__prev; /* Chaining of cleanup functions. */
173};
174
175/* Cancellation */
176enum
177{
178 PTHREAD_CANCEL_ENABLE,
179#define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
180 PTHREAD_CANCEL_DISABLE
181#define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
182};
183enum
184{
185 PTHREAD_CANCEL_DEFERRED,
186#define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
187 PTHREAD_CANCEL_ASYNCHRONOUS
188#define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
189};
190#define PTHREAD_CANCELED ((void *) -1)
191
192
193/* Single execution handling. */
194#define PTHREAD_ONCE_INIT 0
195
196
197#ifdef __USE_XOPEN2K
198/* Value returned by 'pthread_barrier_wait' for one of the threads after
199 the required number of threads have called this function.
200 -1 is distinct from 0 and all errno constants */
201# define PTHREAD_BARRIER_SERIAL_THREAD -1
202#endif
203
204
205__BEGIN_DECLS
206
207/* Create a new thread, starting with execution of START-ROUTINE
208 getting passed ARG. Creation attributed come from ATTR. The new
209 handle is stored in *NEWTHREAD. */
210extern int pthread_create (pthread_t *__restrict __newthread,
211 const pthread_attr_t *__restrict __attr,
212 void *(*__start_routine) (void *),
213 void *__restrict __arg) __THROWNL __nonnull ((1, 3));
214
215/* Terminate calling thread.
216
217 The registered cleanup handlers are called via exception handling
218 so we cannot mark this function with __THROW.*/
219extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__));
220
221/* Make calling thread wait for termination of the thread TH. The
222 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
223 is not NULL.
224
225 This function is a cancellation point and therefore not marked with
226 __THROW. */
227extern int pthread_join (pthread_t __th, void **__thread_return);
228
229#ifdef __USE_GNU
230/* Check whether thread TH has terminated. If yes return the status of
231 the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
232extern int pthread_tryjoin_np (pthread_t __th, void **__thread_return) __THROW;
233
234/* Make calling thread wait for termination of the thread TH, but only
235 until TIMEOUT. The exit status of the thread is stored in
236 *THREAD_RETURN, if THREAD_RETURN is not NULL.
237
238 This function is a cancellation point and therefore not marked with
239 __THROW. */
240extern int pthread_timedjoin_np (pthread_t __th, void **__thread_return,
241 const struct timespec *__abstime);
242#endif
243
244/* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
245 The resources of TH will therefore be freed immediately when it
246 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
247 on it. */
248extern int pthread_detach (pthread_t __th) __THROW;
249
250
251/* Obtain the identifier of the current thread. */
252extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__));
253
254/* Compare two thread identifiers. */
255extern int pthread_equal (pthread_t __thread1, pthread_t __thread2)
256 __THROW __attribute__ ((__const__));
257
258
259/* Thread attribute handling. */
260
261/* Initialize thread attribute *ATTR with default attributes
262 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
263 no user-provided stack). */
264extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1));
265
266/* Destroy thread attribute *ATTR. */
267extern int pthread_attr_destroy (pthread_attr_t *__attr)
268 __THROW __nonnull ((1));
269
270/* Get detach state attribute. */
271extern int pthread_attr_getdetachstate (const pthread_attr_t *__attr,
272 int *__detachstate)
273 __THROW __nonnull ((1, 2));
274
275/* Set detach state attribute. */
276extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
277 int __detachstate)
278 __THROW __nonnull ((1));
279
280
281/* Get the size of the guard area created for stack overflow protection. */
282extern int pthread_attr_getguardsize (const pthread_attr_t *__attr,
283 size_t *__guardsize)
284 __THROW __nonnull ((1, 2));
285
286/* Set the size of the guard area created for stack overflow protection. */
287extern int pthread_attr_setguardsize (pthread_attr_t *__attr,
288 size_t __guardsize)
289 __THROW __nonnull ((1));
290
291
292/* Return in *PARAM the scheduling parameters of *ATTR. */
293extern int pthread_attr_getschedparam (const pthread_attr_t *__restrict __attr,
294 struct sched_param *__restrict __param)
295 __THROW __nonnull ((1, 2));
296
297/* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
298extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,
299 const struct sched_param *__restrict
300 __param) __THROW __nonnull ((1, 2));
301
302/* Return in *POLICY the scheduling policy of *ATTR. */
303extern int pthread_attr_getschedpolicy (const pthread_attr_t *__restrict
304 __attr, int *__restrict __policy)
305 __THROW __nonnull ((1, 2));
306
307/* Set scheduling policy in *ATTR according to POLICY. */
308extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy)
309 __THROW __nonnull ((1));
310
311/* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
312extern int pthread_attr_getinheritsched (const pthread_attr_t *__restrict
313 __attr, int *__restrict __inherit)
314 __THROW __nonnull ((1, 2));
315
316/* Set scheduling inheritance mode in *ATTR according to INHERIT. */
317extern int pthread_attr_setinheritsched (pthread_attr_t *__attr,
318 int __inherit)
319 __THROW __nonnull ((1));
320
321
322/* Return in *SCOPE the scheduling contention scope of *ATTR. */
323extern int pthread_attr_getscope (const pthread_attr_t *__restrict __attr,
324 int *__restrict __scope)
325 __THROW __nonnull ((1, 2));
326
327/* Set scheduling contention scope in *ATTR according to SCOPE. */
328extern int pthread_attr_setscope (pthread_attr_t *__attr, int __scope)
329 __THROW __nonnull ((1));
330
331/* Return the previously set address for the stack. */
332extern int pthread_attr_getstackaddr (const pthread_attr_t *__restrict
333 __attr, void **__restrict __stackaddr)
334 __THROW __nonnull ((1, 2)) __attribute_deprecated__;
335
336/* Set the starting address of the stack of the thread to be created.
337 Depending on whether the stack grows up or down the value must either
338 be higher or lower than all the address in the memory block. The
339 minimal size of the block must be PTHREAD_STACK_MIN. */
340extern int pthread_attr_setstackaddr (pthread_attr_t *__attr,
341 void *__stackaddr)
342 __THROW __nonnull ((1)) __attribute_deprecated__;
343
344/* Return the currently used minimal stack size. */
345extern int pthread_attr_getstacksize (const pthread_attr_t *__restrict
346 __attr, size_t *__restrict __stacksize)
347 __THROW __nonnull ((1, 2));
348
349/* Add information about the minimum stack size needed for the thread
350 to be started. This size must never be less than PTHREAD_STACK_MIN
351 and must also not exceed the system limits. */
352extern int pthread_attr_setstacksize (pthread_attr_t *__attr,
353 size_t __stacksize)
354 __THROW __nonnull ((1));
355
356#ifdef __USE_XOPEN2K
357/* Return the previously set address for the stack. */
358extern int pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
359 void **__restrict __stackaddr,
360 size_t *__restrict __stacksize)
361 __THROW __nonnull ((1, 2, 3));
362
363/* The following two interfaces are intended to replace the last two. They
364 require setting the address as well as the size since only setting the
365 address will make the implementation on some architectures impossible. */
366extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
367 size_t __stacksize) __THROW __nonnull ((1));
368#endif
369
370#ifdef __USE_GNU
371/* Thread created with attribute ATTR will be limited to run only on
372 the processors represented in CPUSET. */
373extern int pthread_attr_setaffinity_np (pthread_attr_t *__attr,
374 size_t __cpusetsize,
375 const cpu_set_t *__cpuset)
376 __THROW __nonnull ((1, 3));
377
378/* Get bit set in CPUSET representing the processors threads created with
379 ATTR can run on. */
380extern int pthread_attr_getaffinity_np (const pthread_attr_t *__attr,
381 size_t __cpusetsize,
382 cpu_set_t *__cpuset)
383 __THROW __nonnull ((1, 3));
384
385/* Get the default attributes used by pthread_create in this process. */
386extern int pthread_getattr_default_np (pthread_attr_t *__attr)
387 __THROW __nonnull ((1));
388
389/* Set the default attributes to be used by pthread_create in this
390 process. */
391extern int pthread_setattr_default_np (const pthread_attr_t *__attr)
392 __THROW __nonnull ((1));
393
394/* Initialize thread attribute *ATTR with attributes corresponding to the
395 already running thread TH. It shall be called on uninitialized ATTR
396 and destroyed with pthread_attr_destroy when no longer needed. */
397extern int pthread_getattr_np (pthread_t __th, pthread_attr_t *__attr)
398 __THROW __nonnull ((2));
399#endif
400
401
402/* Functions for scheduling control. */
403
404/* Set the scheduling parameters for TARGET_THREAD according to POLICY
405 and *PARAM. */
406extern int pthread_setschedparam (pthread_t __target_thread, int __policy,
407 const struct sched_param *__param)
408 __THROW __nonnull ((3));
409
410/* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
411extern int pthread_getschedparam (pthread_t __target_thread,
412 int *__restrict __policy,
413 struct sched_param *__restrict __param)
414 __THROW __nonnull ((2, 3));
415
416/* Set the scheduling priority for TARGET_THREAD. */
417extern int pthread_setschedprio (pthread_t __target_thread, int __prio)
418 __THROW;
419
420
421#ifdef __USE_GNU
422/* Get thread name visible in the kernel and its interfaces. */
423extern int pthread_getname_np (pthread_t __target_thread, char *__buf,
424 size_t __buflen)
425 __THROW __nonnull ((2));
426
427/* Set thread name visible in the kernel and its interfaces. */
428extern int pthread_setname_np (pthread_t __target_thread, const char *__name)
429 __THROW __nonnull ((2));
430#endif
431
432
433#ifdef __USE_UNIX98
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{
2ec0e7ea
CD
502 struct
503 {
504 __jmp_buf __cancel_jmp_buf;
505 int __mask_was_saved;
506 } __cancel_jmp_buf[1];
3c0c543b
CD
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 (__glibc_unlikely (__not_first_call)) \
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 (__glibc_unlikely (__not_first_call)) \
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) __THROWNL;
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 __THROWNL __nonnull ((1));
737
738/* Lock a mutex. */
739extern int pthread_mutex_lock (pthread_mutex_t *__mutex)
740 __THROWNL __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) __THROWNL __nonnull ((1, 2));
747#endif
748
749/* Unlock a mutex. */
750extern int pthread_mutex_unlock (pthread_mutex_t *__mutex)
751 __THROWNL __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 __THROWNL __nonnull ((1));
876
877/* Try to acquire read lock for RWLOCK. */
878extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock)
879 __THROWNL __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) __THROWNL __nonnull ((1, 2));
886# endif
887
888/* Acquire write lock for RWLOCK. */
889extern int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock)
890 __THROWNL __nonnull ((1));
891
892/* Try to acquire write lock for RWLOCK. */
893extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock)
894 __THROWNL __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) __THROWNL __nonnull ((1, 2));
901# endif
902
903/* Unlock RWLOCK. */
904extern int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock)
905 __THROWNL __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 __cond_attr)
947 __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 __THROWNL __nonnull ((1));
956
957/* Wake up all threads waiting for condition variables COND. */
958extern int pthread_cond_broadcast (pthread_cond_t *__cond)
959 __THROWNL __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 __abstime)
980 __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 condition 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 condition 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 __THROWNL __nonnull ((1));
1031
1032/* Try to lock spinlock LOCK. */
1033extern int pthread_spin_trylock (pthread_spinlock_t *__lock)
1034 __THROWNL __nonnull ((1));
1035
1036/* Release spinlock LOCK. */
1037extern int pthread_spin_unlock (pthread_spinlock_t *__lock)
1038 __THROWNL __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 __THROWNL __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 */