mirror of
https://github.com/boostorg/fiber.git
synced 2026-02-13 12:22:36 +00:00
Re-add thread_affinity specific to workstealing_round_robin.
thread_affinity is a good example of a property relevant only to a particular sched_algorithm implementation. In examples/cpp03/migration, introduce an 'affinity' subclass of fiber_properties with a thread_affinity data member. Derive workstealing_round_robin from sched_algorithm_with_properties<affinity> and, as required by that base class, forward awakened() calls to base-class awakened() method. Reimplement workstealing_round_robin's queue from a std::deque to a "by hand" intrusive singly-linked list so we can efficiently remove an arbitrary item. Make steal() method, instead of always popping the last item, scan the list to find the last item willing to migrate (! thread_affinity). From examples/cpp03/migration/workstealing_round_robin.hpp, an example of a user-supplied sched_algorithm implementation, remove all boost/fiber/detail #includes. These should no longer be needed. Change sched_algorithm_with_properties::properties(worker_fiber*) method to accept fiber_base* instead. The original signature was introduced when every sched_algorithm implementation necessarily manipulated worker_fiber* pointers. Now we're intentionally avoiding the need. For the same reason, introduce a fiber_properties::back_ptr typedef so subclasses can opaquely pass such pointers through their own constructor to the base-class constructor.
This commit is contained in:
@@ -12,11 +12,22 @@
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
workstealing_round_robin::workstealing_round_robin():
|
||||
rhead_(0),
|
||||
rtail_(&rhead_)
|
||||
{}
|
||||
|
||||
void
|
||||
workstealing_round_robin::awakened( boost::fibers::fiber_base * f)
|
||||
{
|
||||
// forward this call to base-class method
|
||||
boost::fibers::sched_algorithm_with_properties<affinity>::awakened(f);
|
||||
|
||||
boost::mutex::scoped_lock lk( mtx_);
|
||||
rqueue_.push_back( f);
|
||||
// append this fiber_base* to ready queue
|
||||
BOOST_ASSERT(! f->nxt_);
|
||||
*rtail_ = f;
|
||||
rtail_ = &f->nxt_;
|
||||
}
|
||||
|
||||
boost::fibers::fiber_base *
|
||||
@@ -24,10 +35,15 @@ workstealing_round_robin::pick_next()
|
||||
{
|
||||
boost::mutex::scoped_lock lk( mtx_);
|
||||
boost::fibers::fiber_base * f = 0;
|
||||
if ( ! rqueue_.empty() )
|
||||
if ( rhead_ )
|
||||
{
|
||||
f = rqueue_.front();
|
||||
rqueue_.pop_front();
|
||||
f = rhead_;
|
||||
// pop head item from ready queue
|
||||
rhead_ = rhead_->nxt_;
|
||||
f->nxt_ = 0;
|
||||
// if that was the last item, reset tail_
|
||||
if (! rhead_)
|
||||
rtail_ = &rhead_;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
@@ -46,13 +62,31 @@ boost::fibers::fiber
|
||||
workstealing_round_robin::steal() BOOST_NOEXCEPT
|
||||
{
|
||||
boost::mutex::scoped_lock lk( mtx_);
|
||||
boost::fibers::fiber_base * f = 0;
|
||||
if ( ! rqueue_.empty() )
|
||||
|
||||
// Search the queue for the LAST fiber_base that's willing to migrate,
|
||||
// in other words (! thread_affinity).
|
||||
boost::fibers::fiber_base ** fp = &rhead_, ** found = 0;
|
||||
for ( ; *fp; fp = &(*fp)->nxt_)
|
||||
{
|
||||
f = rqueue_.back();
|
||||
rqueue_.pop_back();
|
||||
// do not consider any fiber whose thread_affinity is set
|
||||
if (! properties(*fp).thread_affinity)
|
||||
found = fp;
|
||||
}
|
||||
return boost::fibers::fiber( f);
|
||||
if (! found)
|
||||
{
|
||||
// either the queue is completely empty or all current entries have
|
||||
// thread_affinity set
|
||||
return boost::fibers::fiber(static_cast<boost::fibers::fiber_base*>(0));
|
||||
}
|
||||
// We found at least one fiber_base whose thread_affinity is NOT set;
|
||||
// *found points to the last of these. Unlink and return it.
|
||||
boost::fibers::fiber_base* ret = *found;
|
||||
*found = ret->nxt_;
|
||||
ret->nxt_ = 0;
|
||||
// if that was the last item, reset tail_
|
||||
if (! *found)
|
||||
rtail_ = &rhead_;
|
||||
return boost::fibers::fiber(ret);
|
||||
}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
|
||||
Reference in New Issue
Block a user