Commit 10f39e11 authored by Patryk Zaryjewski's avatar Patryk Zaryjewski Committed by Facebook Github Bot

Fix FunctionScheduler::resetFunctionTimer

Summary: Keeping moved out functions on the heap leads to segfaults. Let's fix that by calling make_heap each time we need to update. We will regress resetFunctionTimer complexity to O(n), which is exactly what it was before D5668557.

Reviewed By: simpkins

Differential Revision: D6822488

fbshipit-source-id: 94f75789b01ff85e2df271822b767160285462c3
parent e3fd8c27
......@@ -303,13 +303,12 @@ bool FunctionScheduler::resetFunctionTimer(StringPiece nameID) {
// fix the heap ordering if we adjust the key (nextRunTime) for the existing
// RepeatFunc. Instead, we just cancel it and add an identical object.
auto it = functionsMap_.find(nameID);
if (it != functionsMap_.end() && it->second->isValid()) {
auto funcCopy = std::make_unique<RepeatFunc>(std::move(*(it->second)));
it->second->cancel();
// This will take care of making sure that functionsMap_[it->first] =
// funcCopy.
addFunctionToHeap(l, std::move(funcCopy));
if (running_) {
it->second->resetNextRunTime(steady_clock::now());
std::make_heap(functions_.begin(), functions_.end(), fnCmp_);
runningCondvar_.notify_one();
}
return true;
}
return false;
......
......@@ -252,6 +252,23 @@ TEST(FunctionScheduler, ResetFunc) {
EXPECT_EQ(12, total);
}
TEST(FunctionScheduler, ResetFunc2) {
int total = 0;
FunctionScheduler fs;
fs.addFunctionOnce([&] { total += 2; }, "add2", testInterval(1));
fs.addFunctionOnce([&] { total += 3; }, "add3", testInterval(1));
fs.start();
delay(2);
fs.addFunctionOnce([&] { total += 3; }, "add4", testInterval(2));
EXPECT_TRUE(fs.resetFunctionTimer("add4"));
fs.addFunctionOnce([&] { total += 3; }, "add6", testInterval(2));
delay(1);
EXPECT_TRUE(fs.resetFunctionTimer("add4"));
delay(2);
EXPECT_FALSE(fs.resetFunctionTimer("add3"));
fs.addFunctionOnce([&] { total += 3; }, "add4", testInterval(1));
}
TEST(FunctionScheduler, ResetFuncWhileRunning) {
struct State {
boost::barrier barrier_a{2};
......
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