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

context::terminate() as replacement for set_terminated_()/release()

This commit is contained in:
Oliver Kowalke
2015-10-17 15:10:28 +02:00
parent edd439233a
commit 559e498982
3 changed files with 46 additions and 59 deletions

View File

@@ -152,14 +152,6 @@ context::reset_active() noexcept {
active_ = nullptr;
}
void
context::set_terminated_() noexcept {
// protect for concurrent access
std::unique_lock< detail::spinlock > lk( splk_);
flags_ |= flag_terminated;
scheduler_->set_terminated( this);
}
// main fiber context
context::context( main_context_t) :
use_count_( 1), // allocated on main- or thread-stack
@@ -238,30 +230,6 @@ context::suspend( std::function< void() > * func) noexcept {
scheduler_->re_schedule( this, func);
}
void
context::release() noexcept {
BOOST_ASSERT( is_terminated() );
wait_queue_t tmp;
// protect for concurrent access
std::unique_lock< detail::spinlock > lk( splk_);
tmp.swap( wait_queue_);
lk.unlock();
// notify all waiting fibers
wait_queue_t::iterator e = tmp.end();
for ( wait_queue_t::iterator i = tmp.begin(); i != e;) {
context * ctx = & ( * i);
// remove fiber from wait-queue
i = tmp.erase( i);
// notify scheduler
scheduler_->set_ready( ctx);
}
// release fiber-specific-data
for ( fss_data_t::value_type & data : fss_data_) {
data.second.do_cleanup();
}
fss_data_.clear();
}
void
context::join() {
// get active context
@@ -296,6 +264,30 @@ context::yield() noexcept {
scheduler_->yield( active_ctx);
}
void
context::terminate() noexcept {
// protect for concurrent access
std::unique_lock< detail::spinlock > lk( splk_);
// mark as terminated
flags_ |= flag_terminated;
// notify all waiting fibers
while ( ! wait_queue_.empty() ) {
context * ctx = & wait_queue_.front();
// remove fiber from wait-queue
wait_queue_.pop_front();
// notify scheduler
scheduler_->set_ready( ctx);
}
lk.unlock();
// release fiber-specific-data
for ( fss_data_t::value_type & data : fss_data_) {
data.second.do_cleanup();
}
fss_data_.clear();
// switch to another context
scheduler_->set_terminated( this);
}
bool
context::wait_until( std::chrono::steady_clock::time_point const& tp,
std::function< void() > * func) noexcept {