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