2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-02-10 23:32:28 +00:00

protect mutex' with spunlock only

This commit is contained in:
Oliver Kowalke
2013-11-05 16:13:29 +01:00
parent f3d33345a0
commit 1c8cd1d880
19 changed files with 323 additions and 252 deletions

View File

@@ -10,7 +10,6 @@
#include <boost/assert.hpp>
#include "boost/fiber/detail/main_notifier.hpp"
#include "boost/fiber/detail/scheduler.hpp"
#include "boost/fiber/interruption.hpp"
#include "boost/fiber/operations.hpp"
@@ -23,10 +22,10 @@ namespace boost {
namespace fibers {
recursive_mutex::recursive_mutex() :
splk_(),
state_( UNLOCKED),
owner_(),
count_( 0),
state_( UNLOCKED),
splk_(),
waiting_()
{}
@@ -40,12 +39,6 @@ recursive_mutex::~recursive_mutex()
void
recursive_mutex::lock()
{
if ( LOCKED == state_ && this_fiber::get_id() == owner_)
{
++count_;
return;
}
detail::notify::ptr_t n( detail::scheduler::instance()->active() );
if ( n)
{
@@ -53,9 +46,19 @@ recursive_mutex::lock()
{
unique_lock< detail::spinlock > lk( splk_);
state_t expected = UNLOCKED;
if ( state_.compare_exchange_strong( expected, LOCKED) )
break;
if ( UNLOCKED == state_)
{
state_ = LOCKED;
BOOST_ASSERT( ! owner_);
owner_ = this_fiber::get_id();
++count_;
return;
}
else if ( this_fiber::get_id() == owner_)
{
++count_;
return;
}
// store this fiber in order to be notified later
waiting_.push_back( n);
@@ -66,17 +69,27 @@ recursive_mutex::lock()
}
else
{
// notifier for main-fiber
detail::main_notifier mn;
n = detail::main_notifier::make_pointer( mn);
// local notification for main-fiber
detail::scheduler::local_context ctx;
n = detail::scheduler::make_notification( ctx);
for (;;)
{
unique_lock< detail::spinlock > lk( splk_);
state_t expected = UNLOCKED;
if ( state_.compare_exchange_strong( expected, LOCKED) )
break;
if ( UNLOCKED == state_)
{
state_ = LOCKED;
BOOST_ASSERT( ! owner_);
owner_ = this_fiber::get_id();
++count_;
return;
}
else if ( this_fiber::get_id() == owner_)
{
++count_;
return;
}
// store this fiber in order to be notified later
waiting_.push_back( n);
@@ -90,35 +103,33 @@ recursive_mutex::lock()
}
}
}
BOOST_ASSERT( ! owner_);
BOOST_ASSERT( 0 == count_);
owner_ = this_fiber::get_id();
++count_;
}
bool
recursive_mutex::try_lock()
{
if ( LOCKED == state_ && this_fiber::get_id() == owner_)
unique_lock< detail::spinlock > lk( splk_);
if ( UNLOCKED == state_)
{
state_ = LOCKED;
BOOST_ASSERT( ! owner_);
owner_ = this_fiber::get_id();
++count_;
return true;
}
else if ( this_fiber::get_id() == owner_)
{
++count_;
return true;
}
state_t expected = UNLOCKED;
if ( ! state_.compare_exchange_strong( expected, LOCKED) )
else
{
lk.unlock();
// let other fiber release the lock
detail::scheduler::instance()->yield();
return false;
}
owner_ = this_fiber::get_id();
++count_;
return true;
}
void
@@ -126,20 +137,20 @@ recursive_mutex::unlock()
{
BOOST_ASSERT( LOCKED == state_);
BOOST_ASSERT( this_fiber::get_id() == owner_);
detail::notify::ptr_t n;
unique_lock< detail::spinlock > lk( splk_);
if ( 0 == --count_)
{
detail::notify::ptr_t n;
unique_lock< detail::spinlock > lk( splk_);
if ( ! waiting_.empty() ) {
if ( ! waiting_.empty() )
{
n.swap( waiting_.front() );
waiting_.pop_front();
}
owner_ = detail::fiber_base::id();
state_ = UNLOCKED;
lk.unlock();
if ( n) n->set_ready();
}
}