From: Mirek Klimos Date: Fri, 7 Oct 2016 18:18:57 +0000 (-0700) Subject: FunctionScheduler - return whether shutdown was successful X-Git-Tag: v2016.10.10.00~6 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=46930a2aef23cb94dbedb76477dc04733c724ad5;p=folly.git 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 --- diff --git a/folly/experimental/FunctionScheduler.cpp b/folly/experimental/FunctionScheduler.cpp index 6727ca35..2e0ee6eb 100644 --- a/folly/experimental/FunctionScheduler.cpp +++ b/folly/experimental/FunctionScheduler.cpp @@ -277,17 +277,18 @@ bool FunctionScheduler::start() { return true; } -void FunctionScheduler::shutdown() { +bool FunctionScheduler::shutdown() { { std::lock_guard g(mutex_); if (!running_) { - return; + return false; } running_ = false; runningCondvar_.notify_one(); } thread_.join(); + return true; } void FunctionScheduler::run() { diff --git a/folly/experimental/FunctionScheduler.h b/folly/experimental/FunctionScheduler.h index e5514596..1668c501 100644 --- a/folly/experimental/FunctionScheduler.h +++ b/folly/experimental/FunctionScheduler.h @@ -177,8 +177,9 @@ class FunctionScheduler { * Stops the FunctionScheduler. * * 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. diff --git a/folly/experimental/test/FunctionSchedulerTest.cpp b/folly/experimental/test/FunctionSchedulerTest.cpp index e54d1b4a..eb122d6f 100644 --- a/folly/experimental/test/FunctionSchedulerTest.cpp +++ b/folly/experimental/test/FunctionSchedulerTest.cpp @@ -50,6 +50,19 @@ void delay(int n) { } // 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) { int total = 0; FunctionScheduler fs; @@ -76,7 +89,6 @@ TEST(FunctionScheduler, AddCancel) { delay(2); EXPECT_EQ(4, total); fs.addFunction([&] { total += 1; }, testInterval(2), "add2"); - EXPECT_FALSE(fs.start()); // already running delay(1); EXPECT_EQ(5, total); delay(2);