From bfde9ecae6a0e995def46f6aa2452b3c58f2d855 Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Sat, 10 Jan 2015 21:21:36 +0100 Subject: [PATCH] improve spinlock code --- src/detail/spinlock.cpp | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/detail/spinlock.cpp b/src/detail/spinlock.cpp index d999f25f..ec95f392 100644 --- a/src/detail/spinlock.cpp +++ b/src/detail/spinlock.cpp @@ -22,25 +22,19 @@ spinlock::spinlock() : void spinlock::lock() { - for (;;) - { + do { // access to CPU's cache // first access to state_ -> cache miss - // sucessive acccess to state_ > cache hit - while ( LOCKED == state_) - { + // sucessive acccess to state_ -> cache hit + while ( LOCKED == state_.load( boost::memory_order_relaxed) ) { // busy-wait - if ( 0 != fm_active() ) - fm_yield(); - else - this_thread::yield(); + fm_yield(); } - // state_ was released by other + // state_ was released by other fiber // cached copies are invalidated -> cache miss - // test-and-set over the bus - if ( UNLOCKED == state_.exchange( LOCKED) ) - return; + // test-and-set signaled over the bus } + while ( UNLOCKED != state_.exchange( LOCKED, boost::memory_order_acquire) ); } void