d9165d124a5a517ce3d38570d7c3bac387e65df8
[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:
58                                 bool _lock_acquired;
59                         @InitVar:
60                                 _lock_acquired = false;
61                 @Happens_before:
62                         Unlock -> Lock
63                 @End
64         */
65
66         /**
67                 @Begin
68                 @Interface: Lock
69                 @Commit_point_set: Lock_Enqueue_Point1 | Lock_Enqueue_Point2
70                 @Check:
71                         _lock_acquired == false;
72                 @Action:
73                         _lock_acquired = true;
74                 @End
75         */
76         void lock(guard * I) {
77                 mcs_node * me = &(I->m_node);
78
79                 // set up my node :
80                 // not published yet so relaxed :
81                 me->next.store(NULL, std::mo_relaxed );
82                 me->gate.store(1, std::mo_relaxed );
83
84                 // publish my node as the new tail :
85                 mcs_node * pred = m_tail.exchange(me, std::mo_acq_rel);
86                 /**
87                         @Begin
88                         @Commit_point_define_check: pred == NULL
89                         @Label: Lock_Enqueue_Point1
90                         @End
91                 */
92                 if ( pred != NULL ) {
93                         // (*1) race here
94                         // unlock of pred can see me in the tail before I fill next
95
96                         // publish me to previous lock-holder :
97                         // FIXME: detection miss, execution never ends
98                         // If this is relaxed, the store 0 to gate will be read before and
99                         // that lock will never ends.
100                         pred->next.store(me, std::mo_release );
101                         //printf("lock_miss1\n");
102
103                         // (*2) pred not touched any more       
104
105                         // now this is the spin -
106                         // wait on predecessor setting my flag -
107                         rl::linear_backoff bo;
108                         int my_gate = 1;
109                         while (my_gate ) {
110                                 my_gate = me->gate.load(std::mo_acquire);
111                                 //if (my_gate == 0)
112                                         //printf("lock at gate!\n");
113                                 /**
114                                         @Begin
115                                         @Commit_point_define_check: my_gate == 0
116                                         @Label: Lock_Enqueue_Point2
117                                         @End
118                                 */
119                                 thrd_yield();
120                         }
121                 }
122         }
123
124         /**
125                 @Begin
126                 @Interface: Unlock
127                 @Commit_point_set:
128                         Unlock_Point_Success_1 | Unlock_Point_Success_2
129                 @Check:
130                         _lock_acquired == true
131                 @Action:
132                         _lock_acquired = false;
133                 @End
134         */
135         void unlock(guard * I) {
136                 mcs_node * me = &(I->m_node);
137
138                 // FIXME: detection miss, execution never ends
139                 mcs_node * next = me->next.load(std::mo_acquire);
140                 //printf("unlock_miss2\n");
141                 if ( next == NULL )
142                 {
143                         mcs_node * tail_was_me = me;
144                         bool success;
145                         success = m_tail.compare_exchange_strong(
146                                 tail_was_me,NULL,std::mo_acq_rel);
147                         /**
148                                 @Begin
149                                 @Commit_point_define_check: success == true
150                                 @Label: Unlock_Point_Success_1
151                                 @End
152                         */
153                         if (success) {
154                                 
155                                 // got null in tail, mutex is unlocked
156                                 return;
157                         }
158
159                         // (*1) catch the race :
160                         rl::linear_backoff bo;
161                         for(;;) {
162                                 // FIXME: detection miss, execution never ends
163                                 next = me->next.load(std::mo_acquire);
164                                 //printf("unlock_miss3\n");
165                                 if ( next != NULL )
166                                         break;
167                                 thrd_yield();
168                         }
169                 }
170
171                 // (*2) - store to next must be done,
172                 //  so no locker can be viewing my node any more        
173
174                 // let next guy in :
175                 next->gate.store( 0, std::mo_release );
176                 /**
177                         @Begin
178                         @Commit_point_define_check: true
179                         @Label: Unlock_Point_Success_2
180                         @End
181                 */
182         }
183 };
184 /**
185         @Begin
186         @Class_end
187         @End
188 */