xref: /illumos-gate/usr/src/head/threads.h (revision dd4b99162fc201c2eb6176bbfa55c5b8217fab79)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2016 Joyent, Inc.
14  * Copyright 2026 Oxide Computer Company
15  */
16 
17 #ifndef _THREADS_H
18 #define	_THREADS_H
19 
20 /*
21  * ISO/IEC C11 threads.h support
22  */
23 
24 #include <sys/feature_tests.h>
25 
26 #include <sys/types.h>
27 #include <limits.h>
28 #include <time.h>
29 #include <pthread.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #if !defined(_STRICT_SYMBOLS) || defined(_STDC_C11)
36 
37 #if !defined(_NORETURN_KYWD)
38 #if __STDC_VERSION__ - 0 >= 201112L
39 #define	_NORETURN_KYWD	_Noreturn
40 #else
41 #define	_NORETURN_KYWD
42 #endif	/* __STDC_VERSION__ - 0 >= 201112L */
43 #endif	/* !defined(_NORETURN_KYWD) */
44 
45 /*
46  * thread_local is a C++11 keyword and so it must not be defined in an
47  * environment that is C++11 or newer. We define it for non-_STRICT_SYMBOLS
48  * environments prior to C++11 to give them a chance, though not all C++
49  * compilers declare this.
50  */
51 #if !defined(__cplusplus) || __cplusplus < 201103L
52 #define	thread_local	_Thread_local
53 #endif	/* !__cplusplus || __cplusplus < 2021103L */
54 #define	ONCE_FLAG_INIT	PTHREAD_ONCE_INIT
55 #define	TSS_DTOR_ITERATIONS	PTHREAD_DESTRUCTOR_ITERATIONS
56 
57 typedef	pthread_cond_t cnd_t;
58 typedef	pthread_t thrd_t;
59 typedef	pthread_key_t tss_t;
60 typedef	pthread_mutex_t mtx_t;
61 typedef	void (*tss_dtor_t)(void *);
62 typedef int (*thrd_start_t)(void *);
63 typedef	pthread_once_t once_flag;
64 
65 enum {
66 	mtx_plain = 0x1,
67 	mtx_recursive = 0x2,
68 	mtx_timed = 0x4
69 };
70 
71 enum {
72 	thrd_success = 0,
73 	thrd_error = 1,
74 	thrd_busy = 2,
75 	thrd_timedout = 3,
76 	thrd_nomem = 4
77 };
78 
79 extern void call_once(once_flag *, void (*)(void));
80 extern int cnd_broadcast(cnd_t *);
81 extern void cnd_destroy(cnd_t *);
82 extern int cnd_init(cnd_t *);
83 extern int cnd_signal(cnd_t *);
84 extern int cnd_timedwait(cnd_t *_RESTRICT_KYWD, mtx_t *_RESTRICT_KYWD,
85     const struct timespec *_RESTRICT_KYWD);
86 extern int cnd_wait(cnd_t *, mtx_t *);
87 extern void mtx_destroy(mtx_t *);
88 extern int mtx_init(mtx_t *, int);
89 extern int mtx_lock(mtx_t *);
90 extern int mtx_timedlock(mtx_t *_RESTRICT_KYWD,
91     const struct timespec *_RESTRICT_KYWD);
92 extern int mtx_trylock(mtx_t *);
93 extern int mtx_unlock(mtx_t *);
94 extern int thrd_create(thrd_t *, thrd_start_t, void *);
95 extern thrd_t thrd_current(void);
96 extern int thrd_detach(thrd_t);
97 extern int thrd_equal(thrd_t, thrd_t);
98 extern _NORETURN_KYWD void thrd_exit(int) __NORETURN;
99 extern int thrd_join(thrd_t, int *);
100 extern int thrd_sleep(const struct timespec *, struct timespec *);
101 extern void thrd_yield(void);
102 extern int tss_create(tss_t *, tss_dtor_t);
103 extern void tss_delete(tss_t);
104 extern void *tss_get(tss_t);
105 extern int tss_set(tss_t, void *);
106 
107 #endif /* !_STRICT_SYMBOLS | _STDC_C11 */
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif /* _THREADS_H */
114