summaryrefslogtreecommitdiff
path: root/ccan/antithread/test/run-lock.c
blob: 712d3e1de04cbda2a9a7a99686b34ce188ad0516 (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
#include <ccan/antithread/antithread.c>
#include <assert.h>
#include <unistd.h>
#include <ccan/tap/tap.h>

#define NUM_RUNS 100

static void *test(struct at_pool *atp, int *val)
{
	unsigned int i;

	if (at_read_parent(atp) != test) {
		diag("Woah, at_read said bad");
		return NULL;
	}

	/* We increment val, then sleep a little. */
	for (i = 0; i < NUM_RUNS; i++) {
		at_lock(val);
		(*(volatile int *)val)++;
		usleep(i * 100);
		at_unlock(val);
		usleep(i * 100);
	}

	return val;
};

int main(int argc, char *argv[])
{
	struct at_pool *atp;
	struct athread *at;
	int *val, i;

	plan_tests(3);

	atp = at_pool(1*1024*1024);
	assert(atp);
	val = talloc_zero(at_pool_ctx(atp), int);
	at = at_run(atp, test, val);
	assert(at);

	ok1(*val == 0);

	at_tell(at, test);

	/* We increment val, then sleep a little. */
	for (i = 0; i < NUM_RUNS; i++) {
		at_lock(val);
		(*(volatile int *)val)++;
		usleep(i * 100);
		at_unlock(val);
		usleep(i * 100);
	}
	ok1(at_read(at) == val);
	talloc_free(at);

	ok1(*val == NUM_RUNS*2);

	return exit_status();
}