2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-02-18 14:02:18 +00:00

some improved spinlock

This commit is contained in:
Oliver Kowalke
2013-11-03 13:58:40 +01:00
parent ecc0a253f6
commit 05af5d856c

View File

@@ -22,13 +22,24 @@ spinlock::spinlock() :
void
spinlock::lock()
{
while ( LOCKED == state_.exchange( LOCKED) )
for (;;)
{
// busy-wait
if ( scheduler::instance()->active() )
scheduler::instance()->yield();
else
this_thread::yield();
// access to CPU's cache
// first access to state_ -> cache miss
// sucessive acccess to state_ > cache hit
while ( LOCKED == state_)
{
// busy-wait
if ( scheduler::instance()->active() )
scheduler::instance()->yield();
else
this_thread::yield();
}
// state_ was released by other
// cached copies are invalidated -> cache miss
// test-and-set over the bus
if ( UNLOCKED == state_.exchange( LOCKED) )
return;
}
}