1 /*
2  * Copyright (c) 2023, Meta
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_LIB_LIBC_COMMON_INCLUDE_THREADS_H_
8 #define ZEPHYR_LIB_LIBC_COMMON_INCLUDE_THREADS_H_
9 
10 #include <time.h>
11 
12 #include <machine/_threads.h>
13 #include <zephyr/toolchain.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 typedef int (*thrd_start_t)(void *arg);
20 
21 enum {
22 	thrd_success,
23 #define thrd_success thrd_success
24 	thrd_nomem,
25 #define thrd_nomem thrd_nomem
26 	thrd_timedout,
27 #define thrd_timedout thrd_timedout
28 	thrd_busy,
29 #define thrd_busy thrd_busy
30 	thrd_error,
31 #define thrd_error thrd_error
32 };
33 
34 int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);
35 int thrd_equal(thrd_t lhs, thrd_t rhs);
36 thrd_t thrd_current(void);
37 int thrd_sleep(const struct timespec *duration, struct timespec *remaining);
38 void thrd_yield(void);
39 _Noreturn void thrd_exit(int res);
40 int thrd_detach(thrd_t thr);
41 int thrd_join(thrd_t thr, int *res);
42 
43 enum {
44 	mtx_plain,
45 #define mtx_plain mtx_plain
46 	mtx_timed,
47 #define mtx_timed mtx_timed
48 	mtx_recursive,
49 #define mtx_recursive mtx_recursive
50 };
51 
52 int mtx_init(mtx_t *mutex, int type);
53 void mtx_destroy(mtx_t *mutex);
54 int mtx_lock(mtx_t *mutex);
55 int mtx_timedlock(mtx_t *ZRESTRICT mutex, const struct timespec *ZRESTRICT time_point);
56 int mtx_trylock(mtx_t *mutex);
57 int mtx_unlock(mtx_t *mutex);
58 
59 int cnd_init(cnd_t *cond);
60 int cnd_wait(cnd_t *cond, mtx_t *mtx);
61 int cnd_timedwait(cnd_t *ZRESTRICT cond, mtx_t *ZRESTRICT mtx, const struct timespec *ZRESTRICT ts);
62 int cnd_signal(cnd_t *cond);
63 int cnd_broadcast(cnd_t *cond);
64 void cnd_destroy(cnd_t *cond);
65 
66 #ifndef thread_local
67 #define	thread_local _Thread_local
68 #endif
69 
70 typedef void (*tss_dtor_t)(void *val);
71 
72 int tss_create(tss_t *key, tss_dtor_t destructor);
73 void *tss_get(tss_t key);
74 int tss_set(tss_t key, void *val);
75 void tss_delete(tss_t key);
76 
77 void call_once(once_flag *flag, void (*func)(void));
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 
83 #endif /* ZEPHYR_LIB_LIBC_COMMON_INCLUDE_THREADS_H_ */
84