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

enable/disable atomic<> with BOOST_FIBERS_THREADSAFE

This commit is contained in:
Oliver Kowalke
2015-06-16 18:49:28 +02:00
parent 61dc492f31
commit 2002dba9d3
19 changed files with 284 additions and 23 deletions

View File

@@ -38,7 +38,9 @@ recursive_mutex::lock_if_unlocked_() {
}
recursive_mutex::recursive_mutex() :
#if defined(BOOST_FIBERS_THREADSAFE)
splk_(),
#endif
state_( mutex_status::unlocked),
owner_(),
count_( 0),
@@ -56,7 +58,9 @@ recursive_mutex::lock() {
fiber_context * f( detail::scheduler::instance()->active() );
BOOST_ASSERT( nullptr != f);
for (;;) {
#if defined(BOOST_FIBERS_THREADSAFE)
std::unique_lock< detail::spinlock > lk( splk_);
#endif
if ( lock_if_unlocked_() ) {
return;
@@ -66,20 +70,29 @@ recursive_mutex::lock() {
BOOST_ASSERT( waiting_.end() == std::find( waiting_.begin(), waiting_.end(), f) );
waiting_.push_back( f);
#if defined(BOOST_FIBERS_THREADSAFE)
// suspend this fiber
detail::scheduler::instance()->wait( lk);
#else
// suspend this fiber
detail::scheduler::instance()->wait();
#endif
}
}
bool
recursive_mutex::try_lock() {
#if defined(BOOST_FIBERS_THREADSAFE)
std::unique_lock< detail::spinlock > lk( splk_);
#endif
if ( lock_if_unlocked_() ) {
return true;
}
#if defined(BOOST_FIBERS_THREADSAFE)
lk.unlock();
#endif
// let other fiber release the lock
this_fiber::yield();
return false;
@@ -90,7 +103,9 @@ recursive_mutex::unlock() {
BOOST_ASSERT( mutex_status::locked == state_);
BOOST_ASSERT( this_fiber::get_id() == owner_);
#if defined(BOOST_FIBERS_THREADSAFE)
std::unique_lock< detail::spinlock > lk( splk_);
#endif
fiber_context * f( nullptr);
if ( 0 == --count_) {
if ( ! waiting_.empty() ) {
@@ -100,7 +115,9 @@ recursive_mutex::unlock() {
}
owner_ = fiber_context::id();
state_ = mutex_status::unlocked;
#if defined(BOOST_FIBERS_THREADSAFE)
lk.unlock();
#endif
if ( nullptr != f) {
BOOST_ASSERT( ! f->is_terminated() );
f->set_ready();