xref: /third_party/musl/include/pthread.h (revision 570af302)
1#ifndef _PTHREAD_H
2#define _PTHREAD_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7#include <features.h>
8
9#define __NEED_time_t
10#define __NEED_clockid_t
11#define __NEED_struct_timespec
12#define __NEED_sigset_t
13#define __NEED_pthread_t
14#define __NEED_pthread_attr_t
15#define __NEED_pthread_mutexattr_t
16#define __NEED_pthread_condattr_t
17#define __NEED_pthread_rwlockattr_t
18#define __NEED_pthread_barrierattr_t
19#define __NEED_pthread_mutex_t
20#define __NEED_pthread_cond_t
21#define __NEED_pthread_rwlock_t
22#define __NEED_pthread_barrier_t
23#define __NEED_pthread_spinlock_t
24#define __NEED_pthread_key_t
25#define __NEED_pthread_once_t
26#define __NEED_size_t
27
28#include <bits/alltypes.h>
29
30#include <sched.h>
31#include <time.h>
32
33#define PTHREAD_CREATE_JOINABLE 0
34#define PTHREAD_CREATE_DETACHED 1
35
36#define PTHREAD_MUTEX_NORMAL 0
37#define PTHREAD_MUTEX_DEFAULT 0
38#define PTHREAD_MUTEX_RECURSIVE 1
39#define PTHREAD_MUTEX_ERRORCHECK 2
40#define PTHREAD_MUTEX_DESTROYED (-1)
41
42#define PTHREAD_MUTEX_STALLED 0
43#define PTHREAD_MUTEX_ROBUST 1
44
45#define PTHREAD_PRIO_NONE 0
46#define PTHREAD_PRIO_INHERIT 1
47#define PTHREAD_PRIO_PROTECT 2
48
49#define PTHREAD_INHERIT_SCHED 0
50#define PTHREAD_EXPLICIT_SCHED 1
51
52#define PTHREAD_SCOPE_SYSTEM 0
53#define PTHREAD_SCOPE_PROCESS 1
54
55#define PTHREAD_PROCESS_PRIVATE 0
56#define PTHREAD_PROCESS_SHARED 1
57
58
59#define PTHREAD_MUTEX_INITIALIZER {{{0}}}
60#define PTHREAD_RWLOCK_INITIALIZER {{{0}}}
61#define PTHREAD_COND_INITIALIZER {{{0}}}
62#define PTHREAD_ONCE_INIT 0
63
64
65#define PTHREAD_CANCEL_ENABLE 0
66#define PTHREAD_CANCEL_DISABLE 1
67#define PTHREAD_CANCEL_MASKED 2
68
69#define PTHREAD_CANCEL_DEFERRED 0
70#define PTHREAD_CANCEL_ASYNCHRONOUS 1
71
72#define PTHREAD_CANCELED ((void *)-1)
73
74
75#define PTHREAD_BARRIER_SERIAL_THREAD (-1)
76
77
78#define PTHREAD_NULL ((pthread_t)0)
79
80
81int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict);
82int pthread_detach(pthread_t);
83_Noreturn void pthread_exit(void *);
84int pthread_join(pthread_t, void **);
85pid_t pthread_gettid_np(pthread_t);
86
87#ifdef __GNUC__
88__attribute__((const))
89#endif
90pthread_t pthread_self(void);
91
92int pthread_equal(pthread_t, pthread_t);
93#ifndef __cplusplus
94#define pthread_equal(x,y) ((x)==(y))
95#endif
96
97int pthread_setcancelstate(int, int *);
98int pthread_setcanceltype(int, int *);
99void pthread_testcancel(void);
100int pthread_cancel(pthread_t);
101
102int pthread_getschedparam(pthread_t, int *__restrict, struct sched_param *__restrict);
103int pthread_setschedparam(pthread_t, int, const struct sched_param *);
104int pthread_setschedprio(pthread_t, int);
105
106int pthread_once(pthread_once_t *, void (*)(void));
107
108int pthread_mutex_init(pthread_mutex_t *__restrict, const pthread_mutexattr_t *__restrict);
109int pthread_mutex_lock(pthread_mutex_t *);
110int pthread_mutex_unlock(pthread_mutex_t *);
111int pthread_mutex_trylock(pthread_mutex_t *);
112int pthread_mutex_timedlock(pthread_mutex_t *__restrict, const struct timespec *__restrict);
113int pthread_mutex_destroy(pthread_mutex_t *);
114int pthread_mutex_consistent(pthread_mutex_t *);
115/**
116  * @brief lock the mutex object referenced by mutex. If the mutex is already locked,
117  *        the calling thread shall block until the mutex becomes available as in the
118  *        pthread_mutex_lock() function. If the mutex cannot be locked without waiting for
119  *        another thread to unlock the mutex, this wait shall be terminated when the specified
120  *        timeout expires. The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock.
121  *        The resolution of the timeout shall be the resolution of the clock on which it is based.
122  * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock.
123  * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock.
124  * @param timespec the timeout shall expire specified by abstime passes.
125  * @return clocklock result.
126  * @retval 0 is returned on success.
127  * @retval -1 is returned on failure, and errno is set to indicate the error.
128  */
129int pthread_mutex_clocklock(pthread_mutex_t *__restrict, clockid_t, const struct timespec *__restrict);
130/**
131  * @brief lock the mutex object referenced by mutex. If the mutex is already locked,
132  *        the calling thread shall block until the mutex becomes available as in the
133  *        pthread_mutex_lock() function. If the mutex cannot be locked without waiting for
134  *        another thread to unlock the mutex, this wait shall be terminated when the specified
135  *        timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock.
136  *        The resolution of the timeout shall be the resolution of the clock on which it is based.
137  * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock.
138  * @param timespec the timeout shall expire specified by abstime passes.
139  * @return clocklock result.
140  * @retval 0 is returned on success.
141  * @retval -1 is returned on failure, and errno is set to indicate the error.
142  */
143int pthread_mutex_timedlock_monotonic_np(pthread_mutex_t *__restrict, const struct timespec *__restrict);
144/**
145  * @brief lock the mutex object referenced by mutex. If the mutex is already locked,
146  *        the calling thread shall block until the mutex becomes available as in the
147  *        pthread_mutex_lock() function. If the mutex cannot be locked without waiting for
148  *        another thread to unlock the mutex, this wait shall be terminated when the specified
149  *        timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock.
150  *        The resolution of the timeout shall be the resolution of the clock on which it is based.
151  * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock.
152  * @param ms the timeout shall expire specified by relative time(ms) passes.
153  * @return clocklock result.
154  * @retval 0 is returned on success.
155  * @retval -1 is returned on failure, and errno is set to indicate the error.
156  */
157int pthread_mutex_lock_timeout_np(pthread_mutex_t *__restrict, unsigned int);
158/**
159  * @brief The thread waits for a signal to trigger, and if timeout or signal is triggered,
160  *        the thread wakes up.
161  * @param pthread_cond_t Condition variables for multithreading.
162  * @param pthread_mutex_t Thread mutex variable.
163  * @param clockid_t Clock ID used in clock and timer functions.
164  * @param timespec The timeout shall expire specified by abstime passes.
165  * @return pthread_cond_clockwait result.
166  * @retval 0 pthread_cond_clockwait successful.
167  * @retval ETIMEDOUT pthread_cond_clockwait Connection timed out.
168  * @retval EINVAL pthread_cond_clockwait error.
169  */
170int pthread_cond_clockwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict,
171                           clockid_t, const struct timespec *__restrict);
172
173/**
174  * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC.
175  *        The thread waits for a signal to trigger, and if timeout or signal is triggered,
176  *        the thread wakes up.
177  * @param pthread_cond_t Condition variables for multithreading.
178  * @param pthread_mutex_t Thread mutex variable.
179  * @param timespec The timeout shall expire specified by abstime passes.
180  * @return pthread_cond_timedwait_monotonic_np result.
181  * @retval 0 pthread_cond_timedwait_monotonic_np successful.
182  * @retval ETIMEDOUT pthread_cond_timedwait_monotonic_np Connection timed out.
183  * @retval EINVAL pthread_cond_timedwait_monotonic_np error.
184  */
185int pthread_cond_timedwait_monotonic_np(pthread_cond_t *__restrict, pthread_mutex_t *__restrict,
186                                        const struct timespec *__restrict);
187
188/**
189  * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC and The time
190  *        parameter is in milliseconds. The thread waits for a signal to trigger, and if timeout or
191  *        signal is triggered, the thread wakes up.
192  * @param pthread_cond_t Condition variables for multithreading.
193  * @param pthread_mutex_t Thread mutex variable.
194  * @param unsigned Timeout, in milliseconds.
195  * @return pthread_cond_timeout_np result.
196  * @retval 0 pthread_cond_timeout_np successful.
197  * @retval ETIMEDOUT pthread_cond_timeout_np Connection timed out.
198  * @retval EINVAL pthread_cond_timeout_np error.
199  */
200int pthread_cond_timeout_np(pthread_cond_t* __restrict, pthread_mutex_t* __restrict, unsigned int);
201/**
202  * @brief Apply a read lock to the read-write lock referenced by rwlock as in the
203  *        pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without
204  *        waiting for other threads to unlock the lock, this wait shall be terminated when
205  *        the specified timeout expires. The timeout shall expire when the absolute time specified by
206  *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
207  *        specified by abstime has already been passed at the time of the call.
208  *        The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock.
209  * @param rw a read lock to the read-write lock referenced.
210  * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock.
211  * @param timespec the timeout shall expire specified by abstime passes.
212  * @return clockrdlock result.
213  * @retval 0 is returned on success.
214  * @retval -1 is returned on failure, and errno is set to indicate the error.
215  */
216int pthread_rwlock_clockrdlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict);
217/**
218  * @brief Apply a read lock to the read-write lock referenced by rwlock as in the
219  *        pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without
220  *        waiting for other threads to unlock the lock, this wait shall be terminated when
221  *        the specified timeout expires. The timeout shall expire when the absolute time specified by
222  *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
223  *        specified by abstime has already been passed at the time of the call.
224  *        The timeout shall be based on the CLOCK_MONOTONIC clock.
225  * @param rw a read lock to the read-write lock referenced.
226  * @param timespec the timeout shall expire specified by abstime passes.
227  * @return clockrdlock result.
228  * @retval 0 is returned on success.
229  * @retval -1 is returned on failure, and errno is set to indicate the error.
230  */
231int pthread_rwlock_timedrdlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
232/**
233  * @brief Read-write lock variables have an initialization option to use CLOCK_MONOTONIC.
234  *        apply a read lock to the read-write lock referenced by rwlock as in the
235  *        pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without
236  *        waiting for other threads to unlock the lock, this wait shall be terminated when
237  *        the specified timeout expires. The timeout shall expire when the absolute time specified by
238  *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
239  *        specified by abstime has already been passed at the time of the call.
240  *        The timeout shall be based on the CLOCK_MONOTONIC clock.
241  * @param rw a read lock to the read-write lock referenced.
242  * @param timespec the timeout shall expire specified by abstime passes.
243  * @return clockrdlock result.
244  * @retval 0 is returned on success.
245  * @retval -1 is returned on failure, and errno is set to indicate the error.
246  */
247int pthread_rwlock_timedwrlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
248
249/**
250  * @brief Apply a read lock to the read-write lock referenced by rwlock as in the
251  *        pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without
252  *        waiting for other threads to unlock the lock, this wait shall be terminated when
253  *        the specified timeout expires. The timeout shall expire when the absolute time specified by
254  *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
255  *        specified by abstime has already been passed at the time of the call.
256  *        The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock.
257  * @param rw a read lock to the read-write lock referenced.
258  * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock.
259  * @param timespec the timeout shall expire specified by abstime passes.
260  * @return clockrdlock result.
261  * @retval 0 is returned on success.
262  * @retval -1 is returned on failure, and errno is set to indicate the error.
263  */
264int pthread_rwlock_clockwrlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict);
265int pthread_mutex_getprioceiling(const pthread_mutex_t *__restrict, int *__restrict);
266int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict, int, int *__restrict);
267
268int pthread_cond_init(pthread_cond_t *__restrict, const pthread_condattr_t *__restrict);
269int pthread_cond_destroy(pthread_cond_t *);
270int pthread_cond_wait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict);
271int pthread_cond_timedwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, const struct timespec *__restrict);
272int pthread_cond_broadcast(pthread_cond_t *);
273int pthread_cond_signal(pthread_cond_t *);
274
275int pthread_rwlock_init(pthread_rwlock_t *__restrict, const pthread_rwlockattr_t *__restrict);
276int pthread_rwlock_destroy(pthread_rwlock_t *);
277int pthread_rwlock_rdlock(pthread_rwlock_t *);
278int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
279int pthread_rwlock_timedrdlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
280int pthread_rwlock_wrlock(pthread_rwlock_t *);
281int pthread_rwlock_trywrlock(pthread_rwlock_t *);
282int pthread_rwlock_timedwrlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
283int pthread_rwlock_unlock(pthread_rwlock_t *);
284
285int pthread_spin_init(pthread_spinlock_t *, int);
286int pthread_spin_destroy(pthread_spinlock_t *);
287int pthread_spin_lock(pthread_spinlock_t *);
288int pthread_spin_trylock(pthread_spinlock_t *);
289int pthread_spin_unlock(pthread_spinlock_t *);
290
291int pthread_barrier_init(pthread_barrier_t *__restrict, const pthread_barrierattr_t *__restrict, unsigned);
292int pthread_barrier_destroy(pthread_barrier_t *);
293int pthread_barrier_wait(pthread_barrier_t *);
294
295int pthread_key_create(pthread_key_t *, void (*)(void *));
296int pthread_key_delete(pthread_key_t);
297void *pthread_getspecific(pthread_key_t);
298int pthread_setspecific(pthread_key_t, const void *);
299
300int pthread_attr_init(pthread_attr_t *);
301int pthread_attr_destroy(pthread_attr_t *);
302
303int pthread_attr_getguardsize(const pthread_attr_t *__restrict, size_t *__restrict);
304int pthread_attr_setguardsize(pthread_attr_t *, size_t);
305int pthread_attr_getstacksize(const pthread_attr_t *__restrict, size_t *__restrict);
306int pthread_attr_setstacksize(pthread_attr_t *, size_t);
307int pthread_attr_getdetachstate(const pthread_attr_t *, int *);
308int pthread_attr_setdetachstate(pthread_attr_t *, int);
309int pthread_attr_getstack(const pthread_attr_t *__restrict, void **__restrict, size_t *__restrict);
310int pthread_attr_setstack(pthread_attr_t *, void *, size_t);
311int pthread_attr_getscope(const pthread_attr_t *__restrict, int *__restrict);
312int pthread_attr_setscope(pthread_attr_t *, int);
313int pthread_attr_getschedpolicy(const pthread_attr_t *__restrict, int *__restrict);
314int pthread_attr_setschedpolicy(pthread_attr_t *, int);
315int pthread_attr_getschedparam(const pthread_attr_t *__restrict, struct sched_param *__restrict);
316int pthread_attr_setschedparam(pthread_attr_t *__restrict, const struct sched_param *__restrict);
317int pthread_attr_getinheritsched(const pthread_attr_t *__restrict, int *__restrict);
318int pthread_attr_setinheritsched(pthread_attr_t *, int);
319
320int pthread_mutexattr_destroy(pthread_mutexattr_t *);
321int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *__restrict, int *__restrict);
322int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *__restrict, int *__restrict);
323int pthread_mutexattr_getpshared(const pthread_mutexattr_t *__restrict, int *__restrict);
324int pthread_mutexattr_getrobust(const pthread_mutexattr_t *__restrict, int *__restrict);
325int pthread_mutexattr_gettype(const pthread_mutexattr_t *__restrict, int *__restrict);
326int pthread_mutexattr_init(pthread_mutexattr_t *);
327int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
328int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int);
329int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
330int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
331int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
332
333int pthread_condattr_init(pthread_condattr_t *);
334int pthread_condattr_destroy(pthread_condattr_t *);
335int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
336int pthread_condattr_setpshared(pthread_condattr_t *, int);
337int pthread_condattr_getclock(const pthread_condattr_t *__restrict, clockid_t *__restrict);
338int pthread_condattr_getpshared(const pthread_condattr_t *__restrict, int *__restrict);
339
340int pthread_rwlockattr_init(pthread_rwlockattr_t *);
341int pthread_rwlockattr_destroy(pthread_rwlockattr_t *);
342int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int);
343int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *__restrict, int *__restrict);
344
345int pthread_barrierattr_destroy(pthread_barrierattr_t *);
346int pthread_barrierattr_getpshared(const pthread_barrierattr_t *__restrict, int *__restrict);
347int pthread_barrierattr_init(pthread_barrierattr_t *);
348int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
349
350int pthread_atfork(void (*)(void), void (*)(void), void (*)(void));
351int pthread_atfork_for_gwpasan(void (*)(void), void (*)(void), void (*)(void));
352
353int pthread_getconcurrency(void);
354int pthread_setconcurrency(int);
355
356int pthread_getcpuclockid(pthread_t, clockid_t *);
357
358struct __ptcb {
359	void (*__f)(void *);
360	void *__x;
361	struct __ptcb *__next;
362};
363
364void _pthread_cleanup_push(struct __ptcb *, void (*)(void *), void *);
365void _pthread_cleanup_pop(struct __ptcb *, int);
366
367#define pthread_cleanup_push(f, x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x);
368#define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0)
369
370#ifdef _GNU_SOURCE
371struct cpu_set_t;
372int pthread_getaffinity_np(pthread_t, size_t, struct cpu_set_t *);
373int pthread_setaffinity_np(pthread_t, size_t, const struct cpu_set_t *);
374int pthread_getattr_np(pthread_t, pthread_attr_t *);
375int pthread_setname_np(pthread_t, const char *);
376int pthread_getname_np(pthread_t, char *, size_t);
377int pthread_getattr_default_np(pthread_attr_t *);
378int pthread_setattr_default_np(const pthread_attr_t *);
379int pthread_tryjoin_np(pthread_t, void **);
380int pthread_timedjoin_np(pthread_t, void **, const struct timespec *);
381#endif
382
383#if _REDIR_TIME64
384__REDIR(pthread_mutex_timedlock, __pthread_mutex_timedlock_time64);
385__REDIR(pthread_cond_timedwait, __pthread_cond_timedwait_time64);
386__REDIR(pthread_rwlock_timedrdlock, __pthread_rwlock_timedrdlock_time64);
387__REDIR(pthread_rwlock_timedwrlock, __pthread_rwlock_timedwrlock_time64);
388#ifdef _GNU_SOURCE
389__REDIR(pthread_timedjoin_np, __pthread_timedjoin_np_time64);
390#endif
391#endif
392
393#ifdef __cplusplus
394}
395#endif
396#endif
397