Commit 46930a2a authored by Mirek Klimos's avatar Mirek Klimos Committed by Facebook Github Bot

FunctionScheduler - return whether shutdown was successful

Summary: We want to fail fast if we attempt to stop a scheduler that was not running

Reviewed By: yfeldblum

Differential Revision: D3976919

fbshipit-source-id: d8aa8b35aa9e22758e6a7ef64f48a7dd6d990b1c
parent b59ee680
...@@ -277,17 +277,18 @@ bool FunctionScheduler::start() { ...@@ -277,17 +277,18 @@ bool FunctionScheduler::start() {
return true; return true;
} }
void FunctionScheduler::shutdown() { bool FunctionScheduler::shutdown() {
{ {
std::lock_guard<std::mutex> g(mutex_); std::lock_guard<std::mutex> g(mutex_);
if (!running_) { if (!running_) {
return; return false;
} }
running_ = false; running_ = false;
runningCondvar_.notify_one(); runningCondvar_.notify_one();
} }
thread_.join(); thread_.join();
return true;
} }
void FunctionScheduler::run() { void FunctionScheduler::run() {
......
...@@ -177,8 +177,9 @@ class FunctionScheduler { ...@@ -177,8 +177,9 @@ class FunctionScheduler {
* Stops the FunctionScheduler. * Stops the FunctionScheduler.
* *
* It may be restarted later by calling start() again. * It may be restarted later by calling start() again.
* Returns false if the scheduler was not running.
*/ */
void shutdown(); bool shutdown();
/** /**
* Set the name of the worker thread. * Set the name of the worker thread.
......
...@@ -50,6 +50,19 @@ void delay(int n) { ...@@ -50,6 +50,19 @@ void delay(int n) {
} // unnamed namespace } // unnamed namespace
TEST(FunctionScheduler, StartAndShutdown) {
FunctionScheduler fs;
EXPECT_TRUE(fs.start());
EXPECT_FALSE(fs.start());
EXPECT_TRUE(fs.shutdown());
EXPECT_FALSE(fs.shutdown());
// start again
EXPECT_TRUE(fs.start());
EXPECT_FALSE(fs.start());
EXPECT_TRUE(fs.shutdown());
EXPECT_FALSE(fs.shutdown());
}
TEST(FunctionScheduler, SimpleAdd) { TEST(FunctionScheduler, SimpleAdd) {
int total = 0; int total = 0;
FunctionScheduler fs; FunctionScheduler fs;
...@@ -76,7 +89,6 @@ TEST(FunctionScheduler, AddCancel) { ...@@ -76,7 +89,6 @@ TEST(FunctionScheduler, AddCancel) {
delay(2); delay(2);
EXPECT_EQ(4, total); EXPECT_EQ(4, total);
fs.addFunction([&] { total += 1; }, testInterval(2), "add2"); fs.addFunction([&] { total += 1; }, testInterval(2), "add2");
EXPECT_FALSE(fs.start()); // already running
delay(1); delay(1);
EXPECT_EQ(5, total); EXPECT_EQ(5, total);
delay(2); delay(2);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment