Commit e65a4541 authored by Lee Howes's avatar Lee Howes Committed by Facebook Github Bot

Add a full drain for folly's ManualExecutor.

Summary: ManualExecutor::run() is stable, which means that in cases where we want to fully drain the executor we have to loop over it. This adds ManualExecutor::drain() which does that internally.

Reviewed By: yfeldblum

Differential Revision: D6126840

fbshipit-source-id: e36cba5c373a57fe01de244977ec852636b58dbd
parent 6cc78c35
......@@ -69,6 +69,15 @@ size_t ManualExecutor::run() {
return count;
}
size_t ManualExecutor::drain() {
size_t tasksRun = 0;
size_t tasksForSingleRun = 0;
while ((tasksForSingleRun = run()) != 0) {
tasksRun += tasksForSingleRun;
}
return tasksRun;
}
void ManualExecutor::wait() {
while (true) {
{
......
......@@ -47,6 +47,14 @@ namespace folly {
/// moment that this returns.
size_t run();
// Do work until there is no more work to do.
// Returns the number of functions that were executed (maybe 0).
// Unlike run, this method is not stable. It will chase an infinite tail of
// work so should be used with care.
// There will be no work available to perform at the moment that this
// returns.
size_t drain();
/// Wait for work to do.
void wait();
......
......@@ -33,6 +33,20 @@ TEST(ManualExecutor, runIsStable) {
auto f2 = [&]() { x.add(f1); x.add(f1); };
x.add(f2);
x.run();
EXPECT_EQ(count, 0);
}
TEST(ManualExecutor, drainIsNotStable) {
ManualExecutor x;
size_t count = 0;
auto f1 = [&]() { count++; };
auto f2 = [&]() {
x.add(f1);
x.add(f1);
};
x.add(f2);
x.drain();
EXPECT_EQ(count, 2);
}
TEST(ManualExecutor, scheduleDur) {
......
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