Commit state of repository at time of OOPSLA 2015 submission.
[satcheck.git] / clang / test / spsc-queue_unannotated.cc
1 #include <threads.h>
2 #include "libinterface.h"
3
4 struct node {
5         // atomic<struct node*> next;
6         struct node * next;
7         int data;
8         
9         node(int d = 0) : data(d) {
10         store_64(&next, 0);
11         }
12 };
13
14
15 class spsc_queue {
16 public:
17         spsc_queue()
18         {
19                 node* n = new node ();
20                 head = n;
21                 tail = n;
22         }
23
24         ~spsc_queue()
25         {
26         }
27
28         void enqueue(int data)
29         {
30                 node* n = new node(data);
31                 store_64(&head->next, (uint64_t)n);
32                 head = n;
33         }
34
35         int dequeue() {
36                 int data=0;
37                 while (0 == data) {
38                         data = try_dequeue();
39                 }
40                 return data;
41         }
42         
43 private:
44
45         struct node* head;
46         struct node* tail;
47
48         int try_dequeue() {
49                 node* t = tail;
50                 node* n = (node *)load_64(&t->next);
51                 if (0 == n) {
52                         return 0;
53                 }
54                 int data = load_64(&n->data);
55                 tail = n;
56                 return data;
57         }
58 };
59
60
61 spsc_queue *q;
62
63 void thread(unsigned thread_index)
64 {
65         for (int i = 0; i < 40; i++) {
66                 if (0 == thread_index)
67                         {
68                                 q->enqueue(11);
69                         }
70                 else
71                         {
72                                 int d = q->dequeue();
73                                 // RL_ASSERT(11 == d);
74                         }
75         }
76 }
77
78 int user_main(int argc, char **argv)
79 {
80         thrd_t A; thrd_t B;
81         
82         q = new spsc_queue();
83         
84         thrd_create(&A, (thrd_start_t)&thread, (void *)0);
85         thrd_create(&B, (thrd_start_t)&thread, (void *)1);
86         thrd_join(A);
87         thrd_join(B);
88
89
90         return 0;
91 }