![]() |
Home | Libraries | People | FAQ | More |
The fibers in a thread are coordinated by a fiber manager. Fibers trade control cooperatively, rather than preemptively: the currently-running fiber retains control until it invokes some operation that passes control to the manager. Each time a fiber suspends (or yields), the fiber manager consults a scheduler to determine which fiber will run next.
Boost.Fiber provides the fiber manager, but the scheduler is a customization point. (See Customization.)
Each thread has its own scheduler. By default, Boost.Fiber
implicitly instantiates round_robin as the scheduler for each
thread.
You are explicitly permitted to code your own sched_algorithm subclass,
and to specify it to use_scheduling_algorithm().
void thread_fn() { boost::fibers::use_scheduling_algorithm< my_fiber_scheduler >(); ... }
A scheduler class must implement interface sched_algorithm.
Boost.Fiber provides one scheduler: round_robin.
sched_algorithm
sched_algorithm is the abstract
base class defining the interface that a fiber scheduler must implement.
#include <boost/fiber/algorithm.hpp> struct sched_algorithm { virtual ~sched_algorithm(); virtual void awakened( context *) noexcept = 0; virtual context * pick_next() noexcept = 0; virtual bool has_ready_fibers() const noexcept = 0; virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept = 0; virtual void notify() noexcept = 0; };
awakened()
virtual void awakened( context * f) noexcept = 0;
Informs the scheduler that fiber f
is ready to run. Fiber f
might be newly launched, or it might have been blocked but has just been
awakened, or it might have called this_fiber::yield().
This method advises the scheduler to add fiber f
to its collection of fibers ready to run. A typical scheduler implementation
places f into a queue.
pick_next()
virtual context * pick_next() noexcept = 0;
the fiber which is to be resumed next, or nullptr
if there is no ready fiber.
This is where the scheduler actually specifies the fiber which is to run next. A typical scheduler implementation chooses the head of the ready queue.
has_ready_fibers()
virtual bool has_ready_fibers() const noexcept = 0;
true if scheduler has fibers
ready to run.
suspend_until()
virtual void suspend_until( std::chrono::steady_clock::time_point const& abs_time) noexcept = 0;
Informs the scheduler that no ready fiber will be available till time-point
abs_time.
notify()
virtual void notify() noexcept = 0;
wake-up the scheduler, some fibers might ready.
round_robin
This class implements sched_algorithm, scheduling fibers
in round-robin fashion.
#include <boost/fiber/round_robin.hpp> class round_robin : public sched_algorithm { virtual void awakened( context *) noexcept; virtual context * pick_next() noexcept; virtual bool has_ready_fibers() const noexcept; virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept; virtual void notify() noexcept; };
awakened()
virtual void awakened( context * f) noexcept;
Enqueues fiber f onto
a ready queue.
Nothing.
pick_next()
virtual context * pick_next() noexcept;
the fiber at the head of the ready queue, or 0 if the queue is empty.
Nothing.
Placing ready fibers onto a queue, and returning them from the head of that queue, shares the thread between ready fibers in round-robin fashion.
has_ready_fibers()
virtual bool has_ready_fibers() const noexcept;
true if scheduler has fibers
ready to run.
Nothing.
suspend_until()
virtual void suspend_until( std::chrono::steady_clock::time_point const& abs_time) noexcept;
Informs the scheduler that no ready fiber will be available till time-point
abs_time. round_robin blocks in std::condition_variable::wait_until().
Nothing.
notify()
virtual void notify() noexcept = 0;
wake-up the scheduler, some fibers might ready. signals round_robin via std::condition_variable::notify_all().
Nothing.
A scheduler class directly derived from sched_algorithm can
use any information available from context to implement the sched_algorithm interface. But a custom scheduler
might need to track additional properties for a fiber. For instance, a priority-based
scheduler would need to track a fiber's priority.
Boost.Fiber provides a mechanism by which your custom scheduler can associate custom properties with each fiber.
fiber_properties
A custom fiber properties class must be derived from fiber_properties.
#include <boost/fiber/properties.hpp> class fiber_properties { public: fiber_properties( context *) noexcept; virtual ~fiber_properties(); protected: void notify() noexcept; };
fiber_properties( context * f) noexcept;
Constructs base-class component of custom subclass.
Nothing.
Your subclass constructor must accept a context* and pass it to the base-class fiber_properties constructor.
notify()
void notify() noexcept;
Pass control to the custom sched_algorithm_with_properties<> subclass's
sched_algorithm_with_properties::property_change() method.
Nothing.
A custom scheduler's sched_algorithm_with_properties::pick_next() method
might dynamically select from the ready fibers, or sched_algorithm_with_properties::awakened() might
instead insert each ready fiber into some form of ready queue for pick_next().
In the latter case, if application code modifies a fiber property (e.g.
priority) that should affect that fiber's relationship to other ready
fibers, the custom scheduler must be given the opportunity to reorder
its ready queue. The custom property subclass should implement an access
method to modify such a property; that access method should call notify()
once the new property value has been stored. This passes control to the
custom scheduler's property_change() method, allowing the custom scheduler
to reorder its ready queue appropriately. Use at your discretion. Of
course, if you define a property which does not affect the behavior of
the pick_next()
method, you need not call notify() when that property is modified.
sched_algorithm_with_properties<>
A custom scheduler that depends on a custom properties class PROPS should be derived from sched_algorithm_with_properties<PROPS>.
PROPS should be derived from
fiber_properties.
#include <boost/fiber/algorithm.hpp> template< typename PROPS > struct sched_algorithm_with_properties { virtual void awakened( context *, PROPS &) noexcept = 0; virtual context * pick_next() noexcept; virtual bool has_ready_fibers() const noexcept; PROPS & properties( context *) noexcept; virtual void property_change( context *, PROPS &) noexcept; virtual fiber_properties * new_properties( context *); };
awakened()
virtual void awakened( context * f, PROPS & properties) noexcept;
Informs the scheduler that fiber f
is ready to run, like sched_algorithm::awakened().
Passes the fiber's associated PROPS
instance.
Nothing.
A sched_algorithm_with_properties<> subclass must override this method
instead of sched_algorithm::awakened().
pick_next()
virtual context * pick_next() noexcept;
the fiber which is to be resumed next, or nullptr
if there is no ready fiber.
Nothing.
same as sched_algorithm::pick_next()
has_ready_fibers()
virtual bool has_ready_fibers() const noexcept;
the number of fibers ready to run.
Nothing.
properties()
PROPS& properties( context * f) noexcept;
the PROPS instance associated
with fiber f.
Nothing.
The fiber's associated PROPS
instance is already passed to sched_algorithm_with_properties::awakened() and
sched_algorithm_with_properties::property_change().
However, every sched_algorithm subclass is expected
to track a collection of ready context instances. This method
allows your custom scheduler to retrieve the fiber_properties subclass
instance for any context
in its collection.
property_change()
virtual void property_change( context * f, PROPS & properties) noexcept;
Notify the custom scheduler of a possibly-relevant change to a property
belonging to fiber f.
properties contains the
new values of all relevant properties.
Nothing.
This method is only called when a custom fiber_properties subclass
explicitly calls fiber_properties::notify().
new_properties()
virtual fiber_properties * new_properties( context * f);
A new instance of fiber_properties subclass PROPS.
By default, sched_algorithm_with_properties<>::new_properties() simply returns new
PROPS(f),
placing the PROPS instance
on the heap. Override this method to allocate PROPS
some other way. The returned fiber_properties
pointer must point to the PROPS
instance to be associated with fiber f.
context
While you are free to treat context* as an opaque token, certain context members may be useful to a custom
scheduler implementation.
(Most context members are implementation
details; most of interest are implementations of fiber methods.)
#include <boost/fiber/context.hpp> class context { public: class id; static context * active() noexcept; context( context const&) = delete; context & operator=( context const&) = delete; id get_id() const noexcept; void migrate( context *) noexcept; bool is_main_context() const noexcept; bool is_dispatcher_context() const noexcept; bool is_worker_context() const noexcept; bool is_terminated() const noexcept; bool ready_is_linked() const noexcept; bool remote_ready_is_linked() const noexcept; bool wait_is_linked() const noexcept; template< typename List > void ready_link( List &) noexcept; template< typename List > void remote_ready_link( List &) noexcept; template< typename List > void wait_link( List &) noexcept; void ready_unlink() noexcept; void remote_ready_unlink() noexcept; void wait_unlink() noexcept; }; bool operator<( context const& l, context const& r) noexcept;
active()
static context * active() noexcept;
Pointer to instance of current fiber.
Nothing
get_id()
context::id get_id() const noexcept;
migrate()
void migrate( context * f) noexcept;
Migrate fiber f to scheduler
running *this.
Nothing
is_main_context()
bool is_main_context() const noexcept;
true if *this is main context.
Nothing
The context is associated
with the "main" fiber of the thread: the one implicitly created
by the thread itself, rather than one explicitly created by Boost.Fiber.
is_dispatcher_context()
bool is_dispatcher_context() const noexcept;
true if *this is dispatcher context.
Nothing
The context is associated
with a "dispatching" fiber, responsible for dispatching awakened
fibers to scheduler's ready-queue.
is_worker_context()
bool is_worker_context() const noexcept;
true if *this is worker context.
Nothing
The context is associated
with the fiber created by Boost.Fiber.
is_terminated()
bool is_terminated() const noexcept;
true if *this is no longer an valid context.
Nothing
The context has returned
from its fiber-function and is no longer considered a an valid context.
ready_is_linked()
bool ready_is_linked() const noexcept;
true if *this is stored in scheduler's ready-queue.
Nothing
remote_ready_is_linked()
bool remote_ready_is_linked() const noexcept;
true if *this is stored in scheduler's remote-ready-queue.
Nothing
A context signaled as
ready by another thread is always stored in scheduler's remote-ready-queue.
wait_is_linked()
bool wait_is_linked() const noexcept;
true if *this is stored in the wait-queue of some
synchronization object.
Nothing
The context of a fiber,
waiting on an synchronization object (mutex,
condition_variable etc.),
is stored in the waiting-queue of that synchronization object.
ready_link()
template< typename List > void ready_link( List & lst) noexcept;
Stores *this
in ready-queue lst.
Nothing
Argument lst must be
double-liked list from Boost.Intrusive.
remote_ready_link()
template< typename List > void remote_ready_link( List & lst) noexcept;
Stores *this
in remote-ready-queue lst.
Nothing
Argument lst must be
double-liked list from Boost.Intrusive.
wait_link()
template< typename List > void wait_link( List & lst) noexcept;
Stores *this
in wait-queue lst.
Nothing
Argument lst must be
double-liked list from Boost.Intrusive.
ready_unlink()
void ready_unlink() noexcept;
Removes *this
from ready-queue.
Nothing
remote_ready_unlink()
void remote_ready_unlink() noexcept;
Removes *this
from remote-ready-queue.
Nothing
wait_unlink()
void wait_unlink() noexcept;
Removes *this
from wait-queue.
Nothing
operator<()
bool operator<( context const& l, context const& r) noexcept;
true if l.get_id() < r.get_id()
is true and the implementation-defined total order of context::id
values places *this
before other, false otherwise.
Nothing.