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

make mutex thread-safe

This commit is contained in:
Oliver Kowalke
2013-10-18 17:41:08 +02:00
parent ef0367bec4
commit ee13cc1700
8 changed files with 138 additions and 62 deletions

View File

@@ -26,6 +26,7 @@ recursive_timed_mutex::recursive_timed_mutex() :
owner_(),
count_( 0),
state_( UNLOCKED),
splk_(),
waiting_()
{}
@@ -45,13 +46,17 @@ recursive_timed_mutex::lock()
return;
}
while ( LOCKED == state_)
state_t expected = UNLOCKED;
while ( ! state_.compare_exchange_strong( expected, LOCKED) )
{
expected = UNLOCKED;
detail::notify::ptr_t n( detail::scheduler::instance()->active() );
if ( n)
{
unique_lock< detail::spinlock > lk( splk_);
// store this fiber in order to be notified later
waiting_.push_back( n);
splk_.unlock();
// suspend this fiber
detail::scheduler::instance()->wait();
@@ -62,8 +67,10 @@ recursive_timed_mutex::lock()
detail::main_notifier mn;
n = detail::main_notifier::make_pointer( mn);
unique_lock< detail::spinlock > lk( splk_);
// store this fiber in order to be notified later
waiting_.push_back( n);
splk_.unlock();
// wait until main-fiber gets notified
while ( ! n->is_ready() )
@@ -73,10 +80,10 @@ recursive_timed_mutex::lock()
}
}
}
BOOST_ASSERT( ! owner_);
BOOST_ASSERT( 0 == count_);
state_ = LOCKED;
owner_ = this_fiber::get_id();
++count_;
}
@@ -84,24 +91,23 @@ recursive_timed_mutex::lock()
bool
recursive_timed_mutex::try_lock()
{
if ( LOCKED == state_)
if ( LOCKED == state_ && this_fiber::get_id() == owner_)
{
if ( this_fiber::get_id() == owner_)
{
++count_;
return true;
}
else
{
// let other fiber release the lock
detail::scheduler::instance()->yield();
return false;
}
++count_;
return true;
}
state_t expected = UNLOCKED;
if ( ! state_.compare_exchange_strong( expected, LOCKED) )
{
// let other fiber release the lock
detail::scheduler::instance()->yield();
return false;
}
state_ = LOCKED;
owner_ = this_fiber::get_id();
++count_;
return true;
}
@@ -114,21 +120,29 @@ recursive_timed_mutex::try_lock_until( clock_type::time_point const& timeout_tim
return true;
}
while ( LOCKED == state_ && clock_type::now() < timeout_time)
state_t expected = UNLOCKED;
while ( clock_type::now() < timeout_time && ! state_.compare_exchange_strong( expected, LOCKED) )
{
expected = UNLOCKED;
detail::notify::ptr_t n( detail::scheduler::instance()->active() );
try
{
if ( n)
{
unique_lock< detail::spinlock > lk( splk_);
// store this fiber in order to be notified later
waiting_.push_back( n);
splk_.unlock();
// suspend this fiber until notified or timed-out
if ( ! detail::scheduler::instance()->wait_until( timeout_time) )
if ( ! detail::scheduler::instance()->wait_until( timeout_time) ) {
unique_lock< detail::spinlock > lk( splk_);
// remove fiber from waiting-list
waiting_.erase(
std::find( waiting_.begin(), waiting_.end(), n) );
splk_.unlock();
return false;
}
}
else
{
@@ -136,18 +150,22 @@ recursive_timed_mutex::try_lock_until( clock_type::time_point const& timeout_tim
detail::main_notifier mn;
n = detail::main_notifier::make_pointer( mn);
unique_lock< detail::spinlock > lk( splk_);
// store this fiber in order to be notified later
waiting_.push_back( n);
splk_.unlock();
// wait until main-fiber gets notified
while ( ! n->is_ready() )
{
if ( ! ( clock_type::now() < timeout_time) )
{
unique_lock< detail::spinlock > lk( splk_);
// remove fiber from waiting-list
waiting_.erase(
std::find( waiting_.begin(), waiting_.end(), n) );
break;
splk_.unlock();
return false;
}
// run scheduler
detail::scheduler::instance()->run();
@@ -156,19 +174,20 @@ recursive_timed_mutex::try_lock_until( clock_type::time_point const& timeout_tim
}
catch (...)
{
unique_lock< detail::spinlock > lk( splk_);
// remove fiber from waiting-list
waiting_.erase(
std::find( waiting_.begin(), waiting_.end(), n) );
std::find( waiting_.begin(), waiting_.end(), n) );
splk_.unlock();
throw;
}
}
if ( LOCKED == state_) return false;
if ( LOCKED != state_) return false;
BOOST_ASSERT( ! owner_);
BOOST_ASSERT( 0 == count_);
state_ = LOCKED;
owner_ = this_fiber::get_id();
++count_;
@@ -185,13 +204,15 @@ recursive_timed_mutex::unlock()
{
detail::notify::ptr_t n;
unique_lock< detail::spinlock > lk( splk_);
if ( ! waiting_.empty() ) {
n.swap( waiting_.front() );
waiting_.pop_front();
}
splk_.unlock();
state_ = UNLOCKED;
owner_ = detail::fiber_base::id();
state_ = UNLOCKED;
if ( n)
n->set_ready();