From: Sven Over Date: Thu, 14 Jul 2016 15:13:31 +0000 (-0700) Subject: ManualExecutor: add clear method X-Git-Tag: 2016.07.26~55 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=a5f4fbe06a9962f5f230960fb418cb2d5ecb88b2;p=folly.git ManualExecutor: add clear method Summary: ManualExecutor::clear removes all waiting functions from the executor. Reviewed By: yfeldblum Differential Revision: D3555434 fbshipit-source-id: 604c352f2299b0dada062e5f8287be98e2a5f72c --- diff --git a/folly/futures/ManualExecutor.h b/folly/futures/ManualExecutor.h index e84a0e01..1dfc5bb4 100644 --- a/folly/futures/ManualExecutor.h +++ b/folly/futures/ManualExecutor.h @@ -93,6 +93,21 @@ namespace folly { TimePoint now() override { return now_; } + /// Flush the function queue. Destroys all stored functions without + /// executing them. Returns number of removed functions. + std::size_t clear() { + std::queue funcs; + std::priority_queue scheduled_funcs; + + { + std::lock_guard lock(lock_); + funcs_.swap(funcs); + scheduledFuncs_.swap(scheduled_funcs); + } + + return funcs.size() + scheduled_funcs.size(); + } + private: std::mutex lock_; std::queue funcs_; diff --git a/folly/futures/test/ExecutorTest.cpp b/folly/futures/test/ExecutorTest.cpp index b29846a7..3e7e72e5 100644 --- a/folly/futures/test/ExecutorTest.cpp +++ b/folly/futures/test/ExecutorTest.cpp @@ -121,6 +121,19 @@ TEST(ManualExecutor, getViaDoesNotDeadlock) { t.join(); } +TEST(ManualExecutor, clear) { + ManualExecutor x; + size_t count = 0; + x.add([&] { ++count; }); + x.scheduleAt([&] { ++count; }, x.now() + std::chrono::milliseconds(10)); + EXPECT_EQ(0, count); + + x.clear(); + x.advance(std::chrono::milliseconds(10)); + x.run(); + EXPECT_EQ(0, count); +} + TEST(Executor, InlineExecutor) { InlineExecutor x; size_t counter = 0;