6bb4698fee6480f95152f51e53bd8617c2176cdd
[cdsspec-compiler.git] / benchmark / chase-lev-deque-bugfix / deque.h
1 #ifndef DEQUE_H
2 #define DEQUE_H
3
4 #include <spec_lib.h>
5 #include <stdlib.h>
6 #include <cdsannotate.h>
7 #include <specannotation.h>
8 #include <model_memory.h>
9 #include "common.h"
10
11 typedef struct {
12         atomic_size_t size;
13         atomic_int buffer[];
14 } Array;
15
16 typedef struct {
17         atomic_size_t top, bottom;
18         atomic_uintptr_t array; /* Atomic(Array *) */
19 } Deque;
20
21 #define EMPTY 0xffffffff
22 #define ABORT 0xfffffffe
23
24 /**
25     @Begin
26     @Options:
27         LANG = C;
28     @Global_define:
29         @DeclareStruct:
30         typedef struct tag_elem {
31             call_id_t id;
32             int data;
33         } tag_elem_t;
34         
35         @DeclareVar:
36         spec_list *__deque;
37         id_tag_t *tag;
38         @InitVar:
39             __deque= new_spec_list();
40             tag = new_id_tag(); // Beginning of available id
41         @DefineFunc:
42             tag_elem_t* new_tag_elem(call_id_t id, int data) {
43                 tag_elem_t *e = (tag_elem_t*) CMODEL_MALLOC(sizeof(tag_elem_t));
44                 e->id = id;
45                 e->data = data;
46                 return e;
47             }
48         @DefineFunc:
49             call_id_t get_id(void *wrapper) {
50                 return ((tag_elem_t*) wrapper)->id;
51             }
52         @DefineFunc:
53             int get_data(void *wrapper) {
54                 return ((tag_elem_t*) wrapper)->data;
55             }
56     @Happens_before:
57         Push -> Steal
58                 Push -> Take
59     @End
60 */
61
62
63 Deque * create();
64 void resize(Deque *q);
65
66 /**
67     @Begin
68     @Interface: Take 
69     @Commit_point_set: Take_Point1 | Take_Point2 | Take_Point3
70     @ID: __RET__ == EMPTY ? DEFAULT_CALL_ID : get_id(back(__deque))
71     @Action:
72         int _Old_Val = EMPTY;
73                 if (__RET__ != EMPTY) {
74                         if (size(__deque) > 0) {
75                                 _Old_Val = get_data(back(__deque));
76                         pop_back(__deque);
77                         }       
78                 }
79     @Post_check:
80         _Old_Val == __RET__
81     @End
82 */
83 int take(Deque *q);
84
85 /**
86     @Begin
87     @Interface: Push 
88     @Commit_point_set: Push_Point
89     @ID: get_and_inc(tag);
90     @Action:
91         tag_elem_t *elem = new_tag_elem(__ID__, x);
92         push_back(__deque, elem);
93     @End
94 */
95 void push(Deque *q, int x);
96
97 /**
98     @Begin
99     @Interface: Steal 
100     @Commit_point_set: Steal_Point1 | Steal_Point2 | Steal_Point3
101     @ID: (__RET__ == EMPTY || __RET__ == ABORT) ? DEFAULT_CALL_ID : get_id(front(__deque))
102     @Action:
103         int _Old_Val = EMPTY;
104                 if (__RET__ != EMPTY && __RET__ != ABORT) {
105                         if (size(__deque) > 0) {
106                                 _Old_Val = get_data(front(__deque));
107                                 pop_front(__deque);
108                         }
109                 }
110     @Post_check:
111         _Old_Val == __RET__ || __RET__ == EMPTY || __RET__ == ABORT
112     @End
113 */
114 int steal(Deque *q);
115
116 #endif