aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/options/ansi/generic/threads.cpp
blob: 70fa055fd2cd7fcf2ff5e2da9bc5a5bdf6ba5a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <abi-bits/errno.h>
#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/thread.hpp>
#include <mlibc/threads.hpp>
#include <threads.h>

int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) {
	int res = mlibc::thread_create(thr, 0, reinterpret_cast<void *>(func), arg, true);

	if(!res) {
		return thrd_success;
	}

	return (res == ENOMEM) ? thrd_nomem : thrd_error;
}

int thrd_equal(thrd_t t1, thrd_t t2) {
	if(t1 == t2) {
		return 1;
	}
	return 0;
}

thrd_t thrd_current(void) {
	return reinterpret_cast<thrd_t>(mlibc::get_current_tcb());
}

int thrd_sleep(const struct timespec *, struct timespec *) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

void thrd_yield(void) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

int thrd_detach(thrd_t) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

int thrd_join(thrd_t thr, int *res) {
	if(mlibc::thread_join(thr, res) != 0) {
		return thrd_error;
	}

	return thrd_success;
}

__attribute__((__noreturn__)) void thrd_exit(int) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

int mtx_init(mtx_t *mtx, int type) {
	struct __mlibc_mutexattr attr;
	mlibc::thread_mutexattr_init(&attr);

	if(type & mtx_recursive) {
		mlibc::thread_mutexattr_settype(&attr, __MLIBC_THREAD_MUTEX_RECURSIVE);
	}

	int res = mlibc::thread_mutex_init(mtx, &attr) == 0 ? thrd_success : thrd_error;
	mlibc::thread_mutexattr_destroy(&attr);

	return res;
}

void mtx_destroy(mtx_t *mtx) {
	mlibc::thread_mutex_destroy(mtx);
}

int mtx_lock(mtx_t *mtx) {
	return mlibc::thread_mutex_lock(mtx) == 0 ? thrd_success : thrd_error;
}

int mtx_unlock(mtx_t *mtx) {
	return mlibc::thread_mutex_unlock(mtx) == 0 ? thrd_success : thrd_error;
}

int cnd_init(cnd_t *cond) {
	return mlibc::thread_cond_init(cond, 0) == 0 ? thrd_success : thrd_error;
}

void cnd_destroy(cnd_t *cond) {
	mlibc::thread_cond_destroy(cond);
}

int cnd_broadcast(cnd_t *cond) {
	return mlibc::thread_cond_broadcast(cond) == 0 ? thrd_success : thrd_error;
}

int cnd_wait(cnd_t *cond, mtx_t *mtx) {
	return mlibc::thread_cond_timedwait(cond, mtx, nullptr) == 0 ? thrd_success : thrd_error;
}