From 18869c55f75a2801099ef5db7b8e2b95dd2e3e7f Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Tue, 9 Oct 2012 18:08:12 -0700 Subject: [PATCH] mcs-lock: fixup Relacy code Also, switch out 'compare_exchange_strong()' for 'compare_exchange()', since we don't support the _strong syntax right now. --- mcs-lock/mcs-lock.h | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/mcs-lock/mcs-lock.h b/mcs-lock/mcs-lock.h index df64d8b..47c2f8f 100644 --- a/mcs-lock/mcs-lock.h +++ b/mcs-lock/mcs-lock.h @@ -1,12 +1,15 @@ // mcs on stack +#include +#include + struct mcs_node { std::atomic next; std::atomic gate; mcs_node() { - next($).store(0); - gate($).store(0); + next.store(0); + gate.store(0); } }; @@ -16,10 +19,10 @@ public: std::atomic m_tail; mcs_mutex() { - m_tail($).store( NULL ); + m_tail.store( NULL ); } ~mcs_mutex() { - ASSERT( m_tail($).load() == NULL ); + ASSERT( m_tail.load() == NULL ); } class guard { @@ -36,25 +39,25 @@ public: // set up my node : // not published yet so relaxed : - me->next($).store(NULL, std::mo_relaxed ); - me->gate($).store(1, std::mo_relaxed ); + me->next.store(NULL, std::mo_relaxed ); + me->gate.store(1, std::mo_relaxed ); // publish my node as the new tail : - mcs_node * pred = m_tail($).exchange(me, std::mo_acq_rel); + mcs_node * pred = m_tail.exchange(me, std::mo_acq_rel); if ( pred != NULL ) { // (*1) race here // unlock of pred can see me in the tail before I fill next // publish me to previous lock-holder : - pred->next($).store(me, std::mo_release ); + pred->next.store(me, std::mo_release ); // (*2) pred not touched any more // now this is the spin - // wait on predecessor setting my flag - rl::linear_backoff bo; - while ( me->gate($).load(std::mo_acquire) ) { - bo.yield($); + while ( me->gate.load(std::mo_acquire) ) { + bo.yield(); } } } @@ -62,11 +65,11 @@ public: void unlock(guard * I) { mcs_node * me = &(I->m_node); - mcs_node * next = me->next($).load(std::mo_acquire); + mcs_node * next = me->next.load(std::mo_acquire); if ( next == NULL ) { mcs_node * tail_was_me = me; - if ( m_tail($).compare_exchange_strong( tail_was_me,NULL,std::mo_acq_rel) ) { + if ( m_tail.compare_exchange( tail_was_me,NULL,std::mo_acq_rel) ) { // got null in tail, mutex is unlocked return; } @@ -74,10 +77,10 @@ public: // (*1) catch the race : rl::linear_backoff bo; for(;;) { - next = me->next($).load(std::mo_acquire); + next = me->next.load(std::mo_acquire); if ( next != NULL ) break; - bo.yield($); + bo.yield(); } } @@ -85,6 +88,6 @@ public: // so no locker can be viewing my node any more // let next guy in : - next->gate($).store( 0, std::mo_release ); + next->gate.store( 0, std::mo_release ); } }; -- 2.34.1