diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -956,7 +956,7 @@ BOOST_CHECK(!m_processor->stopEventLoop()); // Wait for the scheduler to stop. - s.stop(true); + s.StopWhenDrained(); schedulerThread.join(); } @@ -980,7 +980,7 @@ BOOST_CHECK_EQUAL(s.getQueueInfo(start, stop), 0); // Wait for the scheduler to stop. - s.stop(true); + s.StopWhenDrained(); schedulerThread.join(); } diff --git a/src/scheduler.h b/src/scheduler.h --- a/src/scheduler.h +++ b/src/scheduler.h @@ -68,10 +68,23 @@ // using boost::interrupt_thread void serviceQueue(); - // Tell any threads running serviceQueue to stop as soon as they're done - // servicing whatever task they're currently servicing (drain=false) or when - // there is no work left to be done (drain=true) - void stop(bool drain = false); + /** + * Tell any threads running serviceQueue to stop as soon as the current + * task is done + */ + void stop() { + WITH_LOCK(newTaskMutex, stopRequested = true); + newTaskScheduled.notify_all(); + } + + /** + * Tell any threads running serviceQueue to stop when there is no work + * left to be done + */ + void StopWhenDrained() { + WITH_LOCK(newTaskMutex, stopWhenEmpty = true); + newTaskScheduled.notify_all(); + } // Returns number of tasks waiting to be serviced, // and first and last task times diff --git a/src/scheduler.cpp b/src/scheduler.cpp --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -69,18 +69,6 @@ newTaskScheduled.notify_one(); } -void CScheduler::stop(bool drain) { - { - LOCK(newTaskMutex); - if (drain) { - stopWhenEmpty = true; - } else { - stopRequested = true; - } - } - newTaskScheduled.notify_all(); -} - void CScheduler::schedule(CScheduler::Function f, std::chrono::system_clock::time_point t) { { diff --git a/src/test/scheduler_tests.cpp b/src/test/scheduler_tests.cpp --- a/src/test/scheduler_tests.cpp +++ b/src/test/scheduler_tests.cpp @@ -118,7 +118,7 @@ } // Drain the task queue then exit threads - microTasks.stop(true); + microTasks.StopWhenDrained(); // ... wait until all the threads are done microThreads.join_all(); @@ -174,7 +174,7 @@ } BOOST_CHECK_EQUAL(counter, 0); - scheduler.stop(true); + scheduler.StopWhenDrained(); schedulerThread.join(); BOOST_CHECK_EQUAL(counter, 42); } @@ -235,7 +235,7 @@ } // finish up - scheduler.stop(true); + scheduler.StopWhenDrained(); threads.join_all(); BOOST_CHECK_EQUAL(counter1, 100); @@ -265,7 +265,7 @@ // ensure scheduler has chance to process all tasks queued for before 1 ms // from now. - scheduler.scheduleFromNow([&scheduler] { scheduler.stop(false); }, + scheduler.scheduleFromNow([&scheduler] { scheduler.stop(); }, std::chrono::milliseconds{1}); scheduler_thread.join();