summaryrefslogtreecommitdiff
path: root/src/user/lib/esemaphore.c
blob: 2fdd65968a6338afa3e1b7e2ec8693c60b13be66 (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
#include <user/lib/esemaphore.h>
#include <user/lib/stdlib.h>
#include <shared/flags.h>
#include <shared/syscalls.h>

void esem_signal(struct evil_sem *sem) {
	_syscall_write(sem->signal, NULL, 0, 0);
}

void esem_wait(struct evil_sem *sem) {
	_syscall_read(sem->wait, NULL, 0, 0);
}

struct evil_sem *esem_new(int value) {
	handle_t ends_wait[2], ends_signal[2];
	struct evil_sem *sem;

	if (value < 0) return NULL;
	if (_syscall_pipe(ends_wait, 0) < 0) return NULL;
	if (_syscall_pipe(ends_signal, 0) < 0) goto fail_signal;
	if (!(sem = malloc(sizeof *sem))) goto fail_malloc;

	if (!_syscall_fork(FORK_NOREAP, NULL)) {
		close(ends_signal[1]);
		while (_syscall_read(ends_signal[0], NULL, 0, 0) >= 0) {
			if (!_syscall_fork(FORK_NOREAP, NULL)) {
				_syscall_write(ends_wait[1], NULL, 0, 0);
				_syscall_exit(0);
			}
		}
		_syscall_exit(0);
	}
	close(ends_signal[0]);
	close(ends_wait[1]);

	sem->wait = ends_wait[0];
	sem->signal = ends_signal[1];

	while (value--) esem_signal(sem);
	return sem;

fail_malloc:
	close(ends_signal[0]);
	close(ends_signal[1]);
fail_signal:
	close(ends_wait[0]);
	close(ends_wait[1]);
	return NULL;
}

void esem_free(struct evil_sem *sem) {
	close(sem->wait);
	close(sem->signal);
	free(sem);
}