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
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) {
{
/// 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();
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) {