8 #include <cdsannotate.h>
9 #include <specannotation.h>
10 #include <model_memory.h>
14 std::atomic<mcs_node *> next;
15 std::atomic<int> gate;
26 // tail is null when lock is not held
27 std::atomic<mcs_node *> m_tail;
38 //ASSERT( m_tail.load() == NULL );
41 // Each thread will have their own guard.
45 mcs_node m_node; // node held on the stack
47 guard(mcs_mutex * t) : m_t(t) { t->lock(this); }
48 ~guard() { m_t->unlock(this); }
60 _lock_acquired = false;
69 @Commit_point_set: Lock_Enqueue_Point1 | Lock_Enqueue_Point2
71 _lock_acquired == false;
73 _lock_acquired = true;
76 void lock(guard * I) {
77 mcs_node * me = &(I->m_node);
80 // not published yet so relaxed :
81 me->next.store(NULL, std::mo_relaxed );
82 me->gate.store(1, std::mo_relaxed );
84 // publish my node as the new tail :
85 mcs_node * pred = m_tail.exchange(me, std::mo_acq_rel);
88 @Commit_point_define_check: pred == NULL
89 @Label: Lock_Enqueue_Point1
94 // unlock of pred can see me in the tail before I fill next
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");
103 // (*2) pred not touched any more
105 // now this is the spin -
106 // wait on predecessor setting my flag -
107 rl::linear_backoff bo;
110 my_gate = me->gate.load(std::mo_acquire);
112 //printf("lock at gate!\n");
115 @Commit_point_define_check: my_gate == 0
116 @Label: Lock_Enqueue_Point2
128 Unlock_Point_Success_1 | Unlock_Point_Success_2
130 _lock_acquired == true
132 _lock_acquired = false;
135 void unlock(guard * I) {
136 mcs_node * me = &(I->m_node);
138 // FIXME: detection miss, execution never ends
139 mcs_node * next = me->next.load(std::mo_acquire);
140 //printf("unlock_miss2\n");
143 mcs_node * tail_was_me = me;
145 success = m_tail.compare_exchange_strong(
146 tail_was_me,NULL,std::mo_acq_rel);
149 @Commit_point_define_check: success == true
150 @Label: Unlock_Point_Success_1
155 // got null in tail, mutex is unlocked
159 // (*1) catch the race :
160 rl::linear_backoff bo;
162 // FIXME: detection miss, execution never ends
163 next = me->next.load(std::mo_acquire);
164 //printf("unlock_miss3\n");
171 // (*2) - store to next must be done,
172 // so no locker can be viewing my node any more
175 next->gate.store( 0, std::mo_release );
178 @Commit_point_define_check: true
179 @Label: Unlock_Point_Success_2