summaryrefslogtreecommitdiff
path: root/lib/mlibc/options/posix/generic/semaphore-stubs.cpp
blob: d41eccb72bd91f78117385303c034f921f3e6156 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <semaphore.h>
#include <errno.h>

#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/ansi-sysdeps.hpp>
#include <mlibc/posix-sysdeps.hpp>

static constexpr unsigned int semaphoreHasWaiters = static_cast<uint32_t>(1 << 31);
static constexpr unsigned int semaphoreCountMask = static_cast<uint32_t>(1 << 31) - 1;

int sem_init(sem_t *sem, int pshared, unsigned int initial_count) {
	if (pshared) {
		mlibc::infoLogger() << "mlibc: shared semaphores are unsuppored" << frg::endlog;
		errno = ENOSYS;
		return -1;
	}

	if (initial_count > SEM_VALUE_MAX) {
		errno = EINVAL;
		return -1;
	}

	sem->__mlibc_count = initial_count;

	return 0;
}

int sem_destroy(sem_t *) {
	return 0;
}

int sem_wait(sem_t *sem) {
	unsigned int state = 0;

	while (1) {
		if (!(state & semaphoreCountMask)) {
			if (__atomic_compare_exchange_n(&sem->__mlibc_count, &state, semaphoreHasWaiters,
						false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)) {
				int e = mlibc::sys_futex_wait((int *)&sem->__mlibc_count, state, nullptr);
				if (e == 0 || e == EAGAIN) {
					continue;
				} else if (e == EINTR) {
					errno = EINTR;
					return -1;
				} else {
					mlibc::panicLogger() << "sys_futex_wait() failed with error code " << e << frg::endlog;
				}
			}
		} else {
			unsigned int desired = (state - 1);
			if (__atomic_compare_exchange_n(&sem->__mlibc_count, &state, desired, false,
						__ATOMIC_RELAXED, __ATOMIC_RELAXED))
				return 0;
		}
	}
}

int sem_timedwait(sem_t *sem, const struct timespec *) {
	mlibc::infoLogger() << "\e[31mmlibc: sem_timedwait is implemented as sem_wait\e[0m" << frg::endlog;
	return sem_wait(sem);
}

int sem_post(sem_t *sem) {
	auto old_count = __atomic_load_n(&sem->__mlibc_count, __ATOMIC_RELAXED) & semaphoreCountMask;

	if (old_count + 1 > SEM_VALUE_MAX) {
		errno = EOVERFLOW;
		return -1;
	}

	auto state = __atomic_exchange_n(&sem->__mlibc_count, old_count + 1, __ATOMIC_RELEASE);

	if (state & semaphoreHasWaiters)
		if (int e = mlibc::sys_futex_wake((int *)&sem->__mlibc_count); e)
			__ensure(!"sys_futex_wake() failed");

	return 0;
}

sem_t *sem_open(const char *, int, ...) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

int sem_close(sem_t *) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

int sem_getvalue(sem_t *, int *) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

int sem_unlink(const char *) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

int sem_trywait(sem_t *sem) {
	while (true) {
		auto state = __atomic_load_n(&sem->__mlibc_count, __ATOMIC_ACQUIRE);

		if (state & semaphoreHasWaiters) {
			return EAGAIN;
		}

		auto desired = state - 1;
		if (__atomic_compare_exchange_n(&sem->__mlibc_count, &state, desired, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) {
			return 0;
		}
	}
}