From f28b6fc4976255d72cdf1c40ec07745081f992a0 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Mon, 7 Sep 2015 14:55:30 -0400 Subject: [PATCH 1/2] Update shared_ready_queue::main_fiber comments. --- examples/work_sharing.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/work_sharing.cpp b/examples/work_sharing.cpp index 4bfdf569..ca0cc14a 100644 --- a/examples/work_sharing.cpp +++ b/examples/work_sharing.cpp @@ -31,10 +31,11 @@ private: static std::mutex mutex_; typedef std::unique_lock lock_t; - // Reserve a separate, thread-specific slot for this thread's main fiber. - // It would be Bad News for thread B to retrieve and attempt to execute - // thread A's main fiber. This slot might be empty (nullptr) or full (== - // context::main_fiber()): pick_next() must only return the main fiber's + // Reserve a separate, scheduler-specific slot for this thread's main + // fiber. When we're passed the main fiber, stash it there instead of in + // the shared queue: it would be Bad News for thread B to retrieve and + // attempt to execute thread A's main fiber. This slot might be empty + // (nullptr) or full: pick_next() must only return the main fiber's // context* after it has been passed to awakened(). boost::fibers::context* main_fiber; From 2b6135364e14c028fa20e6e1df9e79734ed5b050 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 9 Sep 2015 21:46:35 -0400 Subject: [PATCH 2/2] Make awakened() unconditionally insert fiber. There was a bug when the ready queue wasn't empty, but there was no lower-priority fiber already in the queue. In that case the fiber wouldn't be inserted. We want the loop just to advance the iterator, but to perform the insert regardless of where the iterator ends up. (With this logic, empty() is no longer a special case.) Restore the ~Verbose() message. --- examples/priority.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/priority.cpp b/examples/priority.cpp index e18d3d5b..2b861af1 100644 --- a/examples/priority.cpp +++ b/examples/priority.cpp @@ -18,6 +18,7 @@ public: } ~Verbose() { + std::cout << desc << ' ' << stop << std::endl; } private: @@ -93,16 +94,15 @@ public: // we're handed a new context*, put it at the end of the fibers // with that same priority. In other words: search for the first fiber // in the queue with LOWER priority, and insert before that one. - if ( rqueue_.empty() ) { - rqueue_.push_back( * f); - } else { - rqueue_t::iterator e( rqueue_.end() ); - for ( rqueue_t::iterator i( rqueue_.begin() ); i != e; ++i) { - if ( properties( & ( * i) ).get_priority() < f_priority) { - rqueue_.insert( i, * f); - } + rqueue_t::iterator i( rqueue_.begin() ), e( rqueue_.end() ); + for ( ; i != e; ++i) { + if ( properties( & ( * i) ).get_priority() < f_priority) { + break; } } + // Now, whether or not we found a fiber with lower priority, + // insert this new fiber here. + rqueue_.insert( i, * f); //<- std::cout << "awakened(" << props.name << "): ";