summaryrefslogtreecommitdiff
path: root/libcr_ipc/sema.c
blob: 5f489aac1bfbe43280c0647f72befa20203dca0c (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
/* coroutine_sema.h - Simple semaphores for coroutine.{h,c}
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-Licence-Identifier: AGPL-3.0-or-later
 */

#include <assert.h>

#include <libcr/coroutine.h>
#include <libcr_ipc/sema.h>

/** Drain the sema->{head,tail} list.  Returns true if cr_getcid() was drained.  */
static inline bool drain(cr_sema_t *sema) {
	assert(!sema->locked);
	cid_t self = cr_getcid();

	enum drain_result {
		DRAINING,
		DRAINED_SELF, /* stopped because drained `self` */
		DRAINED_ALL,  /* stopped because sema->head == NULL */
		DRAINED_SOME, /* stopped because sema->cnt == 0 */
	} state = DRAINING;
	do { 
		sema->locked = true;
		while (state == DRAINING) {
			if (!sema->head) {
				state = DRAINED_ALL;
			} else if (!sema->cnt) {
				state = DRAINED_SOME;
			} else {
				sema->cnt--;
				cid_t cid = sema->head->val;
				if (cid == self)
					state = DRAINED_SELF;
				else
					cr_unpause(sema->head->val);
				sema->head = sema->head->next;
				if (!sema->head)
					sema->tail = &sema->head;
			}
		}
		sema->locked = false;
		/* If there are still coroutines in sema->head, check
		 * that sema->cnt wasn't incremented between `if
		 * (!sema->cnt)` and `sema->locked = false`.  */
	} while (state == DRAINED_SOME && sema->cnt);
	/* If state == DRAINED_SELF, then we better have been the last
	 * item in the list!  */
	assert(state != DRAINED_SELF || !sema->head);
	return state == DRAINED_SELF;
}

void cr_sema_signal(cr_sema_t *sema) {
	sema->cnt++;
	if (!sema->locked)
		drain(sema);
}

void cr_sema_wait(cr_sema_t *sema) {
	struct _cr_sema_cid_list self = {
		.val = cr_getcid(),
		.next = NULL,
	};

	sema->locked = true;
	if (!sema->tail)
		sema->head = &self;
	else
		*(sema->tail) = &self;
	sema->tail = &(self.next);
	sema->locked = false;

	if (drain(sema))
		/* DRAINED_SELF: (1) No need to pause+yield, (2) we
		 * better have been the last item in the list!  */
		assert(!self.next);
	else
		cr_pause_and_yield();
}