Skip to content

Commit fac43f9

Browse files
author
MarcoFalke
committed
scheduler: Replace stop(true) with StopWhenDrained()
This helps understanding the code at the call site without having to look up the name of the argument or the default value.
1 parent fa9cca0 commit fac43f9

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/scheduler.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,6 @@ void CScheduler::serviceQueue()
6868
newTaskScheduled.notify_one();
6969
}
7070

71-
void CScheduler::stop(bool drain)
72-
{
73-
{
74-
LOCK(newTaskMutex);
75-
if (drain)
76-
stopWhenEmpty = true;
77-
else
78-
stopRequested = true;
79-
}
80-
newTaskScheduled.notify_all();
81-
}
82-
8371
void CScheduler::schedule(CScheduler::Function f, std::chrono::system_clock::time_point t)
8472
{
8573
{

src/scheduler.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,18 @@ class CScheduler
6565
// and interrupted using boost::interrupt_thread
6666
void serviceQueue();
6767

68-
// Tell any threads running serviceQueue to stop as soon as they're
69-
// done servicing whatever task they're currently servicing (drain=false)
70-
// or when there is no work left to be done (drain=true)
71-
void stop(bool drain=false);
68+
/** Tell any threads running serviceQueue to stop as soon as the current task is done */
69+
void stop()
70+
{
71+
WITH_LOCK(newTaskMutex, stopRequested = true);
72+
newTaskScheduled.notify_all();
73+
}
74+
/** Tell any threads running serviceQueue to stop when there is no work left to be done */
75+
void StopWhenDrained()
76+
{
77+
WITH_LOCK(newTaskMutex, stopWhenEmpty = true);
78+
newTaskScheduled.notify_all();
79+
}
7280

7381
// Returns number of tasks waiting to be serviced,
7482
// and first and last task times

src/test/scheduler_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(manythreads)
8989
}
9090

9191
// Drain the task queue then exit threads
92-
microTasks.stop(true);
92+
microTasks.StopWhenDrained();
9393
microThreads.join_all(); // ... wait until all the threads are done
9494

9595
int counterSum = 0;
@@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
155155
}
156156

157157
// finish up
158-
scheduler.stop(true);
158+
scheduler.StopWhenDrained();
159159
threads.join_all();
160160

161161
BOOST_CHECK_EQUAL(counter1, 100);
@@ -186,7 +186,7 @@ BOOST_AUTO_TEST_CASE(mockforward)
186186
scheduler.MockForward(std::chrono::minutes{5});
187187

188188
// ensure scheduler has chance to process all tasks queued for before 1 ms from now.
189-
scheduler.scheduleFromNow([&scheduler] { scheduler.stop(false); }, std::chrono::milliseconds{1});
189+
scheduler.scheduleFromNow([&scheduler] { scheduler.stop(); }, std::chrono::milliseconds{1});
190190
scheduler_thread.join();
191191

192192
// check that the queue only has one job remaining

0 commit comments

Comments
 (0)