summaryrefslogtreecommitdiff
path: root/libcr_ipc/tests/test_mutex.c
blob: ee088f4df6c31670245c46030a2b0d50b6cf1fe8 (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 cr_worker(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 = {0};
	coroutine_add("a", cr_worker, &mu);
	coroutine_add("b", cr_worker, &mu);
	coroutine_main();
	test_assert(counter == 200);
	return 0;
}