]> git.ozlabs.org Git - ccan/blob - ccan/antithread/test/run-lock.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / antithread / test / run-lock.c
1 #include <ccan/antithread/antithread.c>
2 #include <assert.h>
3 #include <unistd.h>
4 #include <ccan/tap/tap.h>
5
6 #define NUM_RUNS 100
7
8 static void *test(struct at_pool *atp, int *val)
9 {
10         unsigned int i;
11
12         if (at_read_parent(atp) != test) {
13                 diag("Woah, at_read said bad");
14                 return NULL;
15         }
16
17         /* We increment val, then sleep a little. */
18         for (i = 0; i < NUM_RUNS; i++) {
19                 at_lock(val);
20                 (*(volatile int *)val)++;
21                 usleep(i * 100);
22                 at_unlock(val);
23                 usleep(i * 100);
24         }
25
26         return val;
27 };
28
29 int main(int argc, char *argv[])
30 {
31         struct at_pool *atp;
32         struct athread *at;
33         int *val, i;
34
35         plan_tests(3);
36
37         atp = at_pool(1*1024*1024);
38         assert(atp);
39         val = talloc_zero(at_pool_ctx(atp), int);
40         at = at_run(atp, test, val);
41         assert(at);
42
43         ok1(*val == 0);
44
45         at_tell(at, test);
46
47         /* We increment val, then sleep a little. */
48         for (i = 0; i < NUM_RUNS; i++) {
49                 at_lock(val);
50                 (*(volatile int *)val)++;
51                 usleep(i * 100);
52                 at_unlock(val);
53                 usleep(i * 100);
54         }
55         ok1(at_read(at) == val);
56         talloc_free(at);
57
58         ok1(*val == NUM_RUNS*2);
59
60         return exit_status();
61 }