fixed bugs
[cdsspec-compiler.git] / benchmark / ms-queue / my_queue.h
1 #include <stdatomic.h>
2
3 #define MAX_NODES                       0xf
4
5 typedef unsigned long long pointer;
6 typedef atomic_ullong pointer_t;
7
8 #define MAKE_POINTER(ptr, count)        ((((pointer)count) << 32) | ptr)
9 #define PTR_MASK 0xffffffffLL
10 #define COUNT_MASK (0xffffffffLL << 32)
11
12 static inline void set_count(pointer *p, unsigned int val) { *p = (*p & ~COUNT_MASK) | ((pointer)val << 32); }
13 static inline void set_ptr(pointer *p, unsigned int val) { *p = (*p & ~PTR_MASK) | val; }
14 static inline unsigned int get_count(pointer p) { return (p & COUNT_MASK) >> 32; }
15 static inline unsigned int get_ptr(pointer p) { return p & PTR_MASK; }
16
17 typedef struct node {
18         unsigned int value;
19         pointer_t next;
20 } node_t;
21
22 typedef struct {
23         pointer_t head;
24         pointer_t tail;
25         node_t nodes[MAX_NODES + 1];
26 } queue_t;
27
28 void init_queue(queue_t *q, int num_threads);
29
30 /**
31         @Begin
32         @Global_define:
33                 @DeclareStruct:
34                 typedef struct tag_elem {
35                         Tag id;
36                         unsigned int data;
37                         
38                         tag_elem(Tag _id, unsigned int _data) {
39                                 id = _id;
40                                 data = _data;
41                         }
42                 } tag_elem_t;
43                 @DeclareVar:
44                 spec_queue<tag_elem_t> queue;
45                 Tag tag;
46                 @InitVar:
47                         queue = spec_queue<tag_elem_t>();
48                         tag = Tag();
49         @Happens_before:
50                 # Only check the happens-before relationship according to the id of the
51                 # commit_point_set. For commit_point_set that has same ID, A -> B means
52                 # B happens after the previous A.
53                 Enqueue -> Dequeue
54         @End
55 */
56
57
58
59 /**
60         @Begin
61         @Interface: Enqueue
62         @Commit_point_set: Enqueue_Success_Point
63         @ID: __sequential.tag.getCurAndInc()
64         @Action:
65                 # __ID__ is an internal macro that refers to the id of the current
66                 # interface call
67                 __sequential.queue.enqueue(tag_elem_t(__ID__, val));
68         @End
69 */
70 void enqueue(queue_t *q, unsigned int val);
71
72 /**
73         @Begin
74         @Interface: Dequeue
75         @Commit_point_set: Dequeue_Success_Point
76         @ID: __sequential.queue.peak().tag
77         @Action:
78                 unsigned int _Old_Val = __sequential.queue.dequeue().data;
79         @Post_check:
80                 _Old_Val == __RET__
81         @End
82 */
83 unsigned int dequeue(queue_t *q);
84 int get_thread_num();