edits
[model-checker-benchmarks.git] / ms-queue / testcase3.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <threads.h>
4
5 #include "queue.h"
6 #include "model-assert.h"
7
8 static queue_t *queue;
9 static thrd_t *threads;
10 static unsigned int *input;
11 static unsigned int *output;
12 static int num_threads;
13
14 int get_thread_num()
15 {
16         thrd_t curr = thrd_current();
17         int i;
18         for (i = 0; i < num_threads; i++)
19                 if (curr.priv == threads[i].priv)
20                         return i;
21         MODEL_ASSERT(0);
22         return -1;
23 }
24
25 bool succ1, succ2;
26 atomic_int x[3];
27 int idx1, idx2;
28 unsigned int reclaimNode1, reclaimNode2;
29
30 static int procs = 2;
31
32
33 /** This testcase can infer w2->release & w4->acquire.
34         The initial node that Head and Tail points to is 1, so when T3 enqueue with
35         node 2, and dequeue(get node 1), and enqueue node 1 again, the second time
36         it enqueues node 1 it actually first initialize node1->next. At the same
37         time in T2, it reads that node1->next (because it reads the old Tail at the
38         very beginning), then loads the Tail agian (w4), it can actully reads an old
39         value. And this is a bug because if node 1 is again dequeued, then for T2 to
40         update node1->next, it can potentially contaminate the memory...
41 */
42
43 static void main_task(void *param)
44 {
45         unsigned int val;
46         int pid = *((int *)param);
47         if (pid % 4 == 0) {
48                 enqueue(queue, 0, 3);
49         } else if (pid % 4 == 1) {
50                 enqueue(queue, 1, 2);
51                 succ1 = dequeue(queue, &idx1, &reclaimNode1);
52                 enqueue(queue, 1, 1);
53                 //succ1 = dequeue(queue, &idx1, &reclaimNode1);
54                 //enqueue(queue, 1, false, 2);
55         } else if (pid % 4 == 2) {
56
57         } else if (pid % 4 == 3) {
58         
59         }
60 }
61
62 int user_main(int argc, char **argv)
63 {
64         int i;
65         int *param;
66         unsigned int in_sum = 0, out_sum = 0;
67
68         queue = calloc(1, sizeof(*queue));
69         MODEL_ASSERT(queue);
70
71         num_threads = procs;
72         threads = malloc(num_threads * sizeof(thrd_t));
73         param = malloc(num_threads * sizeof(*param));
74         input = calloc(num_threads, sizeof(*input));
75         output = calloc(num_threads, sizeof(*output));
76
77         atomic_init(&x[0], 0);
78         atomic_init(&x[1], 0);
79         atomic_init(&x[2], 0);
80         init_queue(queue, num_threads);
81         for (i = 0; i < num_threads; i++) {
82                 param[i] = i;
83                 thrd_create(&threads[i], main_task, &param[i]);
84         }
85         for (i = 0; i < num_threads; i++)
86                 thrd_join(threads[i]);
87 /*
88         for (i = 0; i < num_threads; i++) {
89                 in_sum += input[i];
90                 out_sum += output[i];
91         }
92         for (i = 0; i < num_threads; i++)
93                 printf("input[%d] = %u\n", i, input[i]);
94         for (i = 0; i < num_threads; i++)
95                 printf("output[%d] = %u\n", i, output[i]);
96         if (succ1 && succ2)
97                 MODEL_ASSERT(in_sum == out_sum);
98         else
99                 MODEL_ASSERT (false);
100 */
101         free(param);
102         free(threads);
103         free(queue);
104
105         return 0;
106 }