shutdown();
}
-void FunctionScheduler::addFunction(const std::function<void()>& cb,
+void FunctionScheduler::addFunction(Function<void()>&& cb,
milliseconds interval,
StringPiece nameID,
milliseconds startDelay) {
addFunctionGenericDistribution(
- cb,
- IntervalDistributionFunc(ConstIntervalFunctor(interval)),
+ std::move(cb),
+ ConstIntervalFunctor(interval),
nameID.str(),
to<std::string>(interval.count(), "ms"),
startDelay);
}
-void FunctionScheduler::addFunction(const std::function<void()>& cb,
+void FunctionScheduler::addFunction(Function<void()>&& cb,
milliseconds interval,
const LatencyDistribution& latencyDistr,
StringPiece nameID,
milliseconds startDelay) {
if (latencyDistr.isPoisson) {
addFunctionGenericDistribution(
- cb,
- IntervalDistributionFunc(
- PoissonDistributionFunctor(latencyDistr.poissonMean)),
+ std::move(cb),
+ PoissonDistributionFunctor(latencyDistr.poissonMean),
nameID.str(),
to<std::string>(latencyDistr.poissonMean, "ms (Poisson mean)"),
startDelay);
} else {
- addFunction(cb, interval, nameID, startDelay);
+ addFunction(std::move(cb), interval, nameID, startDelay);
}
}
void FunctionScheduler::addFunctionUniformDistribution(
- const std::function<void()>& cb,
+ Function<void()>&& cb,
milliseconds minInterval,
milliseconds maxInterval,
StringPiece nameID,
milliseconds startDelay) {
addFunctionGenericDistribution(
- cb,
- IntervalDistributionFunc(
- UniformDistributionFunctor(minInterval, maxInterval)),
+ std::move(cb),
+ UniformDistributionFunctor(minInterval, maxInterval),
nameID.str(),
to<std::string>(
"[", minInterval.count(), " , ", maxInterval.count(), "] ms"),
}
void FunctionScheduler::addFunctionGenericDistribution(
- const std::function<void()>& cb,
- const IntervalDistributionFunc& intervalFunc,
+ Function<void()>&& cb,
+ IntervalDistributionFunc&& intervalFunc,
const std::string& nameID,
const std::string& intervalDescr,
milliseconds startDelay) {
}
addFunctionToHeap(
- l, RepeatFunc(cb, intervalFunc, nameID, intervalDescr, startDelay));
+ l,
+ RepeatFunc(
+ std::move(cb),
+ std::move(intervalFunc),
+ nameID,
+ intervalDescr,
+ startDelay));
}
bool FunctionScheduler::cancelFunction(StringPiece nameID) {
#pragma once
+#include <folly/Function.h>
#include <folly/Range.h>
#include <chrono>
#include <condition_variable>
* Throws an exception on error. In particular, each function must have a
* unique name--two functions cannot be added with the same name.
*/
- void addFunction(const std::function<void()>& cb,
+ void addFunction(Function<void()>&& cb,
std::chrono::milliseconds interval,
StringPiece nameID = StringPiece(),
std::chrono::milliseconds startDelay =
* LatencyDistribution
*/
void addFunction(
- const std::function<void()>& cb,
+ Function<void()>&& cb,
std::chrono::milliseconds interval,
const LatencyDistribution& latencyDistr,
StringPiece nameID = StringPiece(),
* interval being distributed uniformly within the given interval
* [minInterval, maxInterval].
*/
- void addFunctionUniformDistribution(const std::function<void()>& cb,
+ void addFunctionUniformDistribution(Function<void()>&& cb,
std::chrono::milliseconds minInterval,
std::chrono::milliseconds maxInterval,
StringPiece nameID,
* A type alias for function that is called to determine the time
* interval for the next scheduled run.
*/
- using IntervalDistributionFunc = std::function<std::chrono::milliseconds()>;
+ using IntervalDistributionFunc = Function<std::chrono::milliseconds()>;
/**
* Add a new function to the FunctionScheduler. The scheduling interval
* @see FunctionScheduler::addFunctionJitterInterval).
*/
void addFunctionGenericDistribution(
- const std::function<void()>& cb,
- const IntervalDistributionFunc& intervalFunc,
+ Function<void()>&& cb,
+ IntervalDistributionFunc&& intervalFunc,
const std::string& nameID,
const std::string& intervalDescr,
std::chrono::milliseconds startDelay);
private:
struct RepeatFunc {
- std::function<void()> cb;
+ Function<void()> cb;
IntervalDistributionFunc intervalFunc;
std::chrono::steady_clock::time_point nextRunTime;
std::string name;
std::chrono::milliseconds startDelay;
std::string intervalDescr;
- RepeatFunc(const std::function<void()>& cback,
- const IntervalDistributionFunc& intervalFn,
+ RepeatFunc(Function<void()>&& cback,
+ IntervalDistributionFunc&& intervalFn,
const std::string& nameID,
const std::string& intervalDistDescription,
std::chrono::milliseconds delay)
- : cb(cback),
- intervalFunc(intervalFn),
+ : cb(std::move(cback)),
+ intervalFunc(std::move(intervalFn)),
nextRunTime(),
name(nameID),
startDelay(delay),
}
void cancel() {
// Simply reset cb to an empty function.
- cb = std::function<void()>();
+ cb = {};
}
bool isValid() const { return bool(cb); }
};