Commit a5f4fbe0 authored by Sven Over's avatar Sven Over Committed by Facebook Github Bot 8

ManualExecutor: add clear method

Summary:
ManualExecutor::clear removes all waiting functions from the
executor.

Reviewed By: yfeldblum

Differential Revision: D3555434

fbshipit-source-id: 604c352f2299b0dada062e5f8287be98e2a5f72c
parent f51158b6
......@@ -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<Func> funcs;
std::priority_queue<ScheduledFunc> scheduled_funcs;
{
std::lock_guard<std::mutex> lock(lock_);
funcs_.swap(funcs);
scheduledFuncs_.swap(scheduled_funcs);
}
return funcs.size() + scheduled_funcs.size();
}
private:
std::mutex lock_;
std::queue<Func> funcs_;
......
......@@ -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;
......
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