summaryrefslogtreecommitdiff
path: root/libcr_ipc/tests/test_chan.c
blob: 9d6eecf3f8212707b38b775604881eeaebf1b841 (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
/* libcr_ipc/tests/test_chan.c - Tests for <libcr_ipc/chan.h>
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

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

#include "test.h"

CR_CHAN_DECLARE(intchan, int)

COROUTINE cr_producer(void *_ch) {
	intchan_t *ch = _ch;
	cr_begin();

	intchan_send(ch, 1);

	while (!intchan_can_send(ch))
		cr_yield();

	intchan_send(ch, 2);


	cr_end();
}

COROUTINE cr_consumer(void *_ch) {
	int x;
	intchan_t *ch = _ch;
	cr_begin();

	x = intchan_recv(ch);
	test_assert(x == 1);

	x = intchan_recv(ch);
	test_assert(x == 2);

	cr_end();
}

int main() {
	intchan_t ch = {0};
	coroutine_add("producer", cr_producer, &ch);
	coroutine_add("consumer", cr_consumer, &ch);
	coroutine_main();
	return 0;
}