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