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

run dispatcher context at scheduler destruction

This commit is contained in:
Oliver Kowalke
2015-09-16 19:20:47 +02:00
parent eafeaaae01
commit ddc5af0a36
6 changed files with 251 additions and 171 deletions

View File

@@ -6,7 +6,6 @@
#include "boost/fiber/context.hpp"
#include "boost/fiber/fixedsize_stack.hpp"
#include "boost/fiber/scheduler.hpp"
#ifdef BOOST_HAS_ABI_HEADERS
@@ -16,25 +15,23 @@
namespace boost {
namespace fibers {
static context * main_context() {
static context * make_main_context() {
// main fiber context for this thread
static thread_local context main_ctx;
static thread_local context main_ctx( main_context);
// scheduler for this thread
static thread_local scheduler sched;
// attach scheduler to main fiber context
main_ctx.set_scheduler( & sched);
// attach main fiber context to scheduler
sched.main_context( & main_ctx);
// attach dispatching fiber context to scheduler
sched.dispatching_context(
make_context(
fixedsize_stack(),
& scheduler::dispatch, & sched) );
sched.set_main_context( & main_ctx);
// create and attach dispatching fiber context to scheduler
sched.set_dispatching_context(
make_dispatcher_context( & sched) );
return & main_ctx;
}
thread_local context *
context::active_ = main_context();
context::active_ = make_main_context();
context *
context::active() noexcept {
@@ -47,6 +44,47 @@ context::active( context * active) noexcept {
active_ = active;
}
void
context::set_terminated_() noexcept {
scheduler_->set_terminated( this);
}
void
context::suspend_() noexcept {
scheduler_->re_schedule( this);
}
// main fiber context
context::context( main_context_t) :
ready_hook_(),
terminated_hook_(),
wait_hook_(),
use_count_( 1), // allocated on main- or thread-stack
flags_( flag_main_context),
scheduler_( nullptr),
ctx_( boost::context::execution_context::current() ),
wait_queue_() {
}
// dispatcher fiber context
context::context( dispatcher_context_t, boost::context::preallocated const& palloc,
fixedsize_stack const& salloc, scheduler * sched) :
ready_hook_(),
terminated_hook_(),
wait_hook_(),
use_count_( 0), // scheduler will own dispatcher context
flags_( flag_dispatcher_context),
scheduler_( nullptr),
ctx_( palloc, salloc,
[=] () -> void {
// execute scheduler::dispatch()
sched->dispatch();
// dispatcher context should never return from scheduler::dispatch()
BOOST_ASSERT_MSG( false, "disatcher fiber already terminated");
}),
wait_queue_() {
}
context::~context() {
BOOST_ASSERT( ! wait_is_linked() );
BOOST_ASSERT( wait_queue_.empty() );
@@ -71,13 +109,12 @@ context::get_id() const noexcept {
void
context::resume() {
BOOST_ASSERT( is_running() ); // set by the scheduler-algorithm
ctx_();
}
void
context::release() noexcept {
BOOST_ASSERT( is_terminated() );
BOOST_ASSERT( terminated_is_linked() );
// notify all waiting fibers
wait_queue_t::iterator e = wait_queue_.end();
@@ -93,16 +130,21 @@ context::wait_is_linked() {
return wait_hook_.is_linked();
}
void
context::wait_unlink() {
wait_hook_.unlink();
}
bool
context::ready_is_linked() {
return ready_hook_.is_linked();
}
bool
context::terminated_is_linked() {
return terminated_hook_.is_linked();
}
void
context::wait_unlink() {
wait_hook_.unlink();
}
}}
#ifdef BOOST_HAS_ABI_HEADERS