master
1/* ISO C11 Standard: 7.26 - Thread support library <threads.h>.
2 Copyright (C) 2018-2025 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19
20// zig patch: threads header was added in glibc 2.28
21#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 28
22 #error "threads.h did not exist before glibc 2.28"
23#endif /* error for glibc before 2.28 */
24
25#ifndef _THREADS_H
26#define _THREADS_H 1
27
28#include <features.h>
29#include <time.h>
30
31__BEGIN_DECLS
32
33#include <bits/thread-shared-types.h>
34#include <bits/types/struct_timespec.h>
35
36#if (!defined __STDC_VERSION__ \
37 || __STDC_VERSION__ <= 201710L \
38 || !__GNUC_PREREQ (13, 0)) && !defined __cplusplus
39# define thread_local _Thread_local
40#endif
41
42#define TSS_DTOR_ITERATIONS 4
43typedef __tss_t tss_t;
44typedef void (*tss_dtor_t) (void*);
45
46typedef __thrd_t thrd_t;
47typedef int (*thrd_start_t) (void*);
48
49/* Exit and error codes. */
50enum
51{
52 thrd_success = 0,
53 thrd_busy = 1,
54 thrd_error = 2,
55 thrd_nomem = 3,
56 thrd_timedout = 4
57};
58
59/* Mutex types. */
60enum
61{
62 mtx_plain = 0,
63 mtx_recursive = 1,
64 mtx_timed = 2
65};
66
67typedef __once_flag once_flag;
68#define ONCE_FLAG_INIT __ONCE_FLAG_INIT
69
70typedef union
71{
72 char __size[__SIZEOF_PTHREAD_MUTEX_T];
73 long int __align __LOCK_ALIGNMENT;
74} mtx_t;
75
76typedef union
77{
78 char __size[__SIZEOF_PTHREAD_COND_T];
79 __extension__ long long int __align __LOCK_ALIGNMENT;
80} cnd_t;
81
82/* Threads functions. */
83
84/* Create a new thread executing the function __FUNC. Arguments for __FUNC
85 are passed through __ARG. If successful, __THR is set to new thread
86 identifier. */
87extern int thrd_create (thrd_t *__thr, thrd_start_t __func, void *__arg);
88
89/* Check if __LHS and __RHS point to the same thread. */
90extern int thrd_equal (thrd_t __lhs, thrd_t __rhs);
91
92/* Return current thread identifier. */
93extern thrd_t thrd_current (void);
94
95/* Block current thread execution for at least the time pointed by
96 __TIME_POINT. The current thread may resume if receives a signal. In
97 that case, if __REMAINING is not NULL, the remaining time is stored in
98 the object pointed by it. */
99#ifndef __USE_TIME64_REDIRECTS
100extern int thrd_sleep (const struct timespec *__time_point,
101 struct timespec *__remaining);
102#else
103# ifdef __REDIRECT
104extern int __REDIRECT (thrd_sleep, (const struct timespec *__time_point,
105 struct timespec *__remaining),
106 __thrd_sleep64);
107# else
108# define thrd_sleep __thrd_sleep64
109# endif
110#endif
111
112/* Terminate current thread execution, cleaning up any thread local
113 storage and freeing resources. Returns the value specified in __RES. */
114extern void thrd_exit (int __res) __attribute__ ((__noreturn__));
115
116/* Detach the thread identified by __THR from the current environment
117 (it does not allow join or wait for it). */
118extern int thrd_detach (thrd_t __thr);
119
120/* Block current thread until execution of __THR is complete. In case that
121 __RES is not NULL, will store the return value of __THR when exiting. */
122extern int thrd_join (thrd_t __thr, int *__res);
123
124/* Stop current thread execution and call the scheduler to decide which
125 thread should execute next. The current thread may be selected by the
126 scheduler to keep running. */
127extern void thrd_yield (void);
128
129#ifdef __USE_EXTERN_INLINES
130/* Optimizations. */
131__extern_inline int
132thrd_equal (thrd_t __thread1, thrd_t __thread2)
133{
134 return __thread1 == __thread2;
135}
136#endif
137
138
139/* Mutex functions. */
140
141/* Creates a new mutex object with type __TYPE. If successful the new
142 object is pointed by __MUTEX. */
143extern int mtx_init (mtx_t *__mutex, int __type);
144
145/* Block the current thread until the mutex pointed to by __MUTEX is
146 unlocked. In that case current thread will not be blocked. */
147extern int mtx_lock (mtx_t *__mutex);
148
149/* Block the current thread until the mutex pointed by __MUTEX is unlocked
150 or time pointed by __TIME_POINT is reached. In case the mutex is unlock,
151 the current thread will not be blocked. */
152#ifndef __USE_TIME64_REDIRECTS
153extern int mtx_timedlock (mtx_t *__restrict __mutex,
154 const struct timespec *__restrict __time_point);
155#else
156# ifdef __REDIRECT
157extern int __REDIRECT (mtx_timedlock, (mtx_t *__restrict __mutex,
158 const struct timespec *__restrict
159 __time_point),
160 __mtx_timedlock64);
161# else
162# define mtx_timedlock __mtx_timedlock64
163# endif
164#endif
165
166/* Try to lock the mutex pointed by __MUTEX without blocking. If the mutex
167 is free the current threads takes control of it, otherwise it returns
168 immediately. */
169extern int mtx_trylock (mtx_t *__mutex);
170
171/* Unlock the mutex pointed by __MUTEX. It may potentially awake other
172 threads waiting on this mutex. */
173extern int mtx_unlock (mtx_t *__mutex);
174
175/* Destroy the mutex object pointed by __MUTEX. */
176extern void mtx_destroy (mtx_t *__mutex);
177
178
179/* Call function __FUNC exactly once, even if invoked from several threads.
180 All calls must be made with the same __FLAGS object. */
181extern void call_once (once_flag *__flag, void (*__func)(void));
182
183
184/* Condition variable functions. */
185
186/* Initialize new condition variable pointed by __COND. */
187extern int cnd_init (cnd_t *__cond);
188
189/* Unblock one thread that currently waits on condition variable pointed
190 by __COND. */
191extern int cnd_signal (cnd_t *__cond);
192
193/* Unblock all threads currently waiting on condition variable pointed by
194 __COND. */
195extern int cnd_broadcast (cnd_t *__cond);
196
197/* Block current thread on the condition variable pointed by __COND. */
198extern int cnd_wait (cnd_t *__cond, mtx_t *__mutex);
199
200/* Block current thread on the condition variable until condition variable
201 pointed by __COND is signaled or time pointed by __TIME_POINT is
202 reached. */
203#ifndef __USE_TIME64_REDIRECTS
204extern int cnd_timedwait (cnd_t *__restrict __cond,
205 mtx_t *__restrict __mutex,
206 const struct timespec *__restrict __time_point);
207#else
208# ifdef __REDIRECT
209extern int __REDIRECT (cnd_timedwait, (cnd_t *__restrict __cond,
210 mtx_t *__restrict __mutex,
211 const struct timespec *__restrict
212 __time_point),
213 __cnd_timedwait64);
214# else
215# define cnd_timedwait __cnd_timedwait64
216# endif
217#endif
218
219/* Destroy condition variable pointed by __cond and free all of its
220 resources. */
221extern void cnd_destroy (cnd_t *__COND);
222
223
224/* Thread specific storage functions. */
225
226/* Create new thread-specific storage key and stores it in the object pointed
227 by __TSS_ID. If __DESTRUCTOR is not NULL, the function will be called when
228 the thread terminates. */
229extern int tss_create (tss_t *__tss_id, tss_dtor_t __destructor);
230
231/* Return the value held in thread-specific storage for the current thread
232 identified by __TSS_ID. */
233extern void *tss_get (tss_t __tss_id);
234
235/* Sets the value of the thread-specific storage identified by __TSS_ID for
236 the current thread to __VAL. */
237extern int tss_set (tss_t __tss_id, void *__val);
238
239/* Destroys the thread-specific storage identified by __TSS_ID. The
240 destructor is not called until thrd_exit is called. */
241extern void tss_delete (tss_t __tss_id);
242
243__END_DECLS
244
245#endif /* _THREADS_H */