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