model: add build_reads_from_past() function
[cdsspec-compiler.git] / userprog.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "libthreads.h"
5 #include "libatomic.h"
6 #include "librace.h"
7
8 static void a(atomic_int *obj)
9 {
10         int i;
11         int ret;
12
13         store_32(&i, 10);
14         printf("load 32 yields: %d\n", load_32(&i));
15
16         for (i = 0; i < 2; i++) {
17                 printf("Thread %d, loop %d\n", thrd_current(), i);
18                 switch (i % 2) {
19                 case 1:
20                         ret = atomic_load(obj);
21                         printf("Read value: %d\n", ret);
22                         break;
23                 case 0:
24                         atomic_store(obj, i + 1);
25                         printf("Write value: %d\n", i + 1);
26                         break;
27                 }
28         }
29 }
30
31 void user_main()
32 {
33         thrd_t t1, t2;
34         atomic_int *obj;
35
36         obj = malloc(sizeof(*obj));
37
38         atomic_init(obj, 0);
39
40         printf("Thread %d: creating 2 threads\n", thrd_current());
41         thrd_create(&t1, (thrd_start_t)&a, obj);
42         thrd_create(&t2, (thrd_start_t)&a, obj);
43
44         thrd_join(t1);
45         thrd_join(t2);
46         free(obj);
47         printf("Thread %d is finished\n", thrd_current());
48 }