summaryrefslogtreecommitdiff
path: root/libcr_ipc/waitgroup.c
blob: e76db01043e19b0e2518befb5f6ac6b299c87535 (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
/* libcr_ipc/waitgroup.c - Go-like WaitGroups for libcr
 *
 * Copyright (C) 2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <libcr/coroutine.h> /* for cid_t, cr_* */

#define IMPLEMENTATION_FOR_LIBCR_IPC_WAITGROUP_H YES
#include <libcr_ipc/waitgroup.h>

#include "_linkedlist.h"

struct cr_waitgroup_waiter {
	cr_ipc_sll_node;
	cid_t                    cid;
};

void cr_waitgroup_wait(cr_waitgroup_t *wg) {
	assert(wg);
	cr_assert_in_coroutine();

	struct cr_waitgroup_waiter self = {
		.cid = cr_getcid(),
	};
	cr_ipc_sll_push_to_rear(&wg->waiters, &self);
	if (wg->waiters.front != &self.cr_ipc_sll_node || wg->count > 0)
		cr_pause_and_yield();
	assert(wg->waiters.front == &self.cr_ipc_sll_node);

	cr_ipc_sll_pop_from_front(&wg->waiters);
	if (wg->waiters.front) {
		assert(wg->unpausing);
		cr_unpause(cr_ipc_sll_node_cast(struct cr_waitgroup_waiter, wg->waiters.front)->cid);
	} else {
		wg->unpausing = false;
	}
}

void cr_waitgroup_add(cr_waitgroup_t *wg, int delta) {
	assert(wg);
	cr_assert_in_coroutine();

	if (delta == 0)
		return;
	bool saved = cr_save_and_disable_interrupts();
	wg->count += delta;
	assert(wg->count >= 0);
	if (wg->count == 0 && !wg->unpausing && wg->waiters.front) {
		wg->unpausing = true;
		cr_unpause(cr_ipc_sll_node_cast(struct cr_waitgroup_waiter, wg->waiters.front)->cid);
	}
	cr_restore_interrupts(saved);
}

void cr_waitgroup_add_from_intrhandler(cr_waitgroup_t *wg, int delta) {
	assert(wg);
	cr_assert_in_intrhandler();

	if (delta == 0)
		return;
	wg->count += delta;
	assert(wg->count >= 0);
	if (wg->count == 0 && !wg->unpausing && wg->waiters.front) {
		wg->unpausing = true;
		cr_unpause_from_intrhandler(cr_ipc_sll_node_cast(struct cr_waitgroup_waiter, wg->waiters.front)->cid);
	}
}