PatchworkOS
Loading...
Searching...
No Matches
threads.h
Go to the documentation of this file.
1#ifndef _THREADS_H
2#define _THREADS_H 1
3
4#include <stdatomic.h>
5
6#if defined(__cplusplus)
7extern "C"
8{
9#endif
10
11#include "_internal/config.h"
12#include "_internal/pid_t.h"
13#include "_internal/tid_t.h"
14#include "_internal/timespec.h"
15
16#include <stdatomic.h>
17
18#if __STDC_NO_THREADS__ == 1
19#error __STDC_NO_THREADS__ defined but <threads.h> included. Something is wrong about your setup.
20#endif
21
22#if __STDC_VERSION__ >= 201112L
23#define thread_local _Thread_local
24#endif
25
26#define ONCE_FLAG_INIT 0
27
28#define TSS_DTOR_ITERATIONS 4
29
30// TODO: Implement this
31typedef struct
32{
33 char todo;
34} cnd_t;
35
36typedef struct
37{
39} thrd_t;
40
41// TODO: Implement this
42typedef struct
43{
44 char todo;
45} tss_t;
46
47#define _MTX_UNLOCKED 0
48#define _MTX_LOCKED 1
49#define _MTX_CONTESTED 2
50
51typedef struct
52{
53 atomic_uint64_t state;
56} mtx_t;
57
58typedef void (*tss_dtor_t)(void*);
59
60typedef int (*thrd_start_t)(void*);
61
62typedef int once_flag;
63
64enum
65{
69};
70
71enum
72{
78};
79
80_PUBLIC void call_once(once_flag* flag, void (*func)(void));
81
83
85
87
89
91
92int cnd_wait(cnd_t* cond, mtx_t* mtx);
93
94_PUBLIC void mtx_destroy(mtx_t* mtx);
95
96_PUBLIC int mtx_init(mtx_t* mtx, int type);
97
98_PUBLIC int mtx_lock(mtx_t* mtx);
99
101
103
104_PUBLIC int mtx_unlock(mtx_t* mtx);
105
106_PUBLIC int thrd_create(thrd_t* thr, thrd_start_t func, void* arg);
107
109
110_PUBLIC int thrd_detach(thrd_t thr);
111
112_PUBLIC int thrd_equal(thrd_t thr0, thrd_t thr1);
113
114_PUBLIC _NORETURN void thrd_exit(int res);
115
116_PUBLIC int thrd_join(thrd_t thr, int* res);
117
118_PUBLIC int thrd_sleep(const struct timespec* duration, struct timespec* remaining);
119
120_PUBLIC void thrd_yield(void);
121
123
125
127
128_PUBLIC int tss_set(tss_t key, void* val);
129
130#if defined(__cplusplus)
131}
132#endif
133
134#endif
__UINT64_TYPE__ tid_t
Thread Identifier.
Definition tid_t.h:12
#define _PUBLIC
Definition config.h:41
#define _NORETURN
Definition config.h:28
#define _RESTRICT
Definition config.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
char todo
Definition threads.h:33
tid_t owner
Definition threads.h:54
uint64_t depth
Definition threads.h:55
atomic_uint64_t state
Definition threads.h:53
tid_t id
Definition threads.h:38
Task State Segment structure.
Definition tss.h:88
char todo
Definition threads.h:44
_PUBLIC int cnd_init(cnd_t *cond)
_PUBLIC void call_once(once_flag *flag, void(*func)(void))
_PUBLIC int mtx_timedlock(mtx_t *_RESTRICT mtx, const struct timespec *_RESTRICT ts)
int(* thrd_start_t)(void *)
Definition threads.h:60
_PUBLIC int mtx_lock(mtx_t *mtx)
Definition mtx_lock.c:11
_PUBLIC thrd_t thrd_current(void)
int once_flag
Definition threads.h:62
_PUBLIC int mtx_init(mtx_t *mtx, int type)
Definition mtx_init.c:10
_PUBLIC int thrd_sleep(const struct timespec *duration, struct timespec *remaining)
Definition thrd_sleep.c:10
_PUBLIC int thrd_equal(thrd_t thr0, thrd_t thr1)
Definition thrd_equal.c:10
_PUBLIC int cnd_timedwait(cnd_t *_RESTRICT cond, mtx_t *_RESTRICT mtx, const struct timespec *_RESTRICT ts)
_PUBLIC int tss_create(tss_t *key, tss_dtor_t dtor)
_PUBLIC void thrd_yield(void)
Definition thrd_yield.c:10
_PUBLIC int mtx_unlock(mtx_t *mtx)
Definition mtx_unlock.c:10
_PUBLIC void cnd_destroy(cnd_t *cond)
_PUBLIC void tss_delete(tss_t key)
@ thrd_timedout
Definition threads.h:73
@ thrd_success
Definition threads.h:74
@ thrd_busy
Definition threads.h:75
@ thrd_nomem
Definition threads.h:77
@ thrd_error
Definition threads.h:76
_PUBLIC int cnd_broadcast(cnd_t *cond)
_PUBLIC int cnd_signal(cnd_t *cond)
_PUBLIC int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Definition thrd_create.c:30
_PUBLIC int thrd_detach(thrd_t thr)
Definition thrd_detach.c:10
_PUBLIC int thrd_join(thrd_t thr, int *res)
Definition thrd_join.c:10
@ mtx_timed
Definition threads.h:68
@ mtx_plain
Definition threads.h:66
@ mtx_recursive
Definition threads.h:67
_PUBLIC void * tss_get(tss_t key)
int cnd_wait(cnd_t *cond, mtx_t *mtx)
_PUBLIC _NORETURN void thrd_exit(int res)
Definition thrd_exit.c:11
_PUBLIC void mtx_destroy(mtx_t *mtx)
Definition mtx_destroy.c:10
_PUBLIC int tss_set(tss_t key, void *val)
_PUBLIC int mtx_trylock(mtx_t *mtx)
void(* tss_dtor_t)(void *)
Definition threads.h:58