changes with lines of spec counted
[cdsspec-compiler.git] / benchmark / mcs-lock / mcs-lock.h
1 // mcs on stack
2
3 #include <stdatomic.h>
4 #include <unrelacy.h>
5
6 #include <spec_lib.h>
7 #include <stdlib.h>
8 #include <cdsannotate.h>
9 #include <specannotation.h>
10 #include <model_memory.h>
11 #include "common.h" 
12
13 struct mcs_node {
14         std::atomic<mcs_node *> next;
15         std::atomic<int> gate;
16
17         mcs_node() {
18                 next.store(0);
19                 gate.store(0);
20         }
21 };
22
23
24 struct mcs_mutex {
25 public:
26         // tail is null when lock is not held
27         std::atomic<mcs_node *> m_tail;
28
29         mcs_mutex() {
30                 /**
31                         @Begin
32                 @Entry_point
33                         @End
34                 */
35                 m_tail.store( NULL );
36         }
37         ~mcs_mutex() {
38                 //ASSERT( m_tail.load() == NULL );
39         }
40
41         // Each thread will have their own guard.
42         class guard {
43         public:
44                 mcs_mutex * m_t;
45                 mcs_node    m_node; // node held on the stack
46
47                 guard(mcs_mutex * t) : m_t(t) { t->lock(this); }
48                 ~guard() { m_t->unlock(this); }
49         };
50
51         /**
52                 @Begin
53                 @Options:
54                         LANG = CPP;
55                         CLASS = mcs_mutex;
56                 @Global_define:
57                         @DeclareVar: bool _lock_acquired;
58                         @InitVar: _lock_acquired = false;
59                 @Happens_before: Unlock -> Lock
60                 @End
61         */
62
63         /**
64                 @Begin
65                 @Interface: Lock
66                 @Commit_point_set: Lock_Enqueue_Point1 | Lock_Enqueue_Point2
67                 @Check: _lock_acquired == false;
68                 @Action: _lock_acquired = true;
69                 @End
70         */
71         void lock(guard * I) {
72                 mcs_node * me = &(I->m_node);
73
74                 // set up my node :
75                 // not published yet so relaxed :
76                 me->next.store(NULL, std::mo_relaxed );
77                 me->gate.store(1, std::mo_relaxed );
78
79                 /** Run this in the -Y mode to expose the HB bug */
80                 // publish my node as the new tail :
81                 mcs_node * pred = m_tail.exchange(me, std::mo_acq_rel);
82                 /**
83                         @Begin
84                         @Commit_point_define_check: pred == NULL
85                         @Label: Lock_Enqueue_Point1
86                         @End
87                 */
88                 if ( pred != NULL ) {
89                         // (*1) race here
90                         // unlock of pred can see me in the tail before I fill next
91
92                         // publish me to previous lock-holder :
93                         // FIXME: detection miss, execution never ends
94                         // If this is relaxed, the store 0 to gate will be read before and
95                         // that lock will never ends.
96                         pred->next.store(me, std::mo_release );
97                         //printf("lock_miss1\n");
98
99                         // (*2) pred not touched any more       
100
101                         // now this is the spin -
102                         // wait on predecessor setting my flag -
103                         rl::linear_backoff bo;
104                         int my_gate = 1;
105                         while (my_gate ) {
106                                 my_gate = me->gate.load(std::mo_acquire);
107                                 //if (my_gate == 0)
108                                         //printf("lock at gate!\n");
109                                 /**
110                                         @Begin
111                                         @Commit_point_define_check: my_gate == 0
112                                         @Label: Lock_Enqueue_Point2
113                                         @End
114                                 */
115                                 thrd_yield();
116                         }
117                 }
118         }
119
120         /**
121                 @Begin
122                 @Interface: Unlock
123                 @Commit_point_set: Unlock_Point_Success_1 | Unlock_Point_Success_2
124                 @Check: _lock_acquired == true
125                 @Action: _lock_acquired = false;
126                 @End
127         */
128         void unlock(guard * I) {
129                 mcs_node * me = &(I->m_node);
130
131                 // FIXME: detection miss, execution never ends
132                 mcs_node * next = me->next.load(std::mo_acquire);
133                 //printf("unlock_miss2\n");
134                 if ( next == NULL )
135                 {
136                         mcs_node * tail_was_me = me;
137                         bool success;
138                         success = m_tail.compare_exchange_strong(
139                                 tail_was_me,NULL,std::mo_acq_rel);
140                         /**
141                                 @Begin
142                                 @Commit_point_define_check: success == true
143                                 @Label: Unlock_Point_Success_1
144                                 @End
145                         */
146                         if (success) {
147                                 
148                                 // got null in tail, mutex is unlocked
149                                 return;
150                         }
151
152                         // (*1) catch the race :
153                         rl::linear_backoff bo;
154                         for(;;) {
155                                 // FIXME: detection miss, execution never ends
156                                 next = me->next.load(std::mo_acquire);
157                                 //printf("unlock_miss3\n");
158                                 if ( next != NULL )
159                                         break;
160                                 thrd_yield();
161                         }
162                 }
163
164                 // (*2) - store to next must be done,
165                 //  so no locker can be viewing my node any more        
166
167                 // let next guy in :
168                 next->gate.store( 0, std::mo_release );
169                 /**
170                         @Begin
171                         @Commit_point_define_check: true
172                         @Label: Unlock_Point_Success_2
173                         @End
174                 */
175         }
176 };
177 /**
178         @Begin
179         @Class_end
180         @End
181 */