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

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

#include "test.h"

int counter = 0;

COROUTINE worker_cr(void *_mu) {
	cr_mutex_t *mu = _mu;
	cr_begin();

	for (int i = 0; i < 100; i++) {
		cr_mutex_lock(mu);
		int a = counter;
		cr_yield();
		counter = a + 1;
		cr_mutex_unlock(mu);
		cr_yield();
	}

	cr_end();
}

int main() {
	cr_mutex_t mu = {};
	coroutine_add("a", worker_cr, &mu);
	coroutine_add("b", worker_cr, &mu);
	coroutine_main();
	test_assert(counter == 200);
	return 0;
}