/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/Function.h>
#include <folly/Range.h>
+#include <folly/hash/Hash.h>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
+#include <unordered_map>
#include <vector>
namespace folly {
*
*
* Note: the class uses only one thread - if you want to use more than one
- * thread use multiple FunctionScheduler objects
+ * thread, either use multiple FunctionScheduler objects, or check out
+ * ThreadedRepeatingFunctionRunner.h for a much simpler contract of
+ * "run each function periodically in its own thread".
*
* start() schedules the functions, while shutdown() terminates further
* scheduling.
};
struct RunTimeOrder {
- bool operator()(const RepeatFunc& f1, const RepeatFunc& f2) const {
- return f1.getNextRunTime() > f2.getNextRunTime();
+ bool operator()(const std::unique_ptr<RepeatFunc>& f1, const std::unique_ptr<RepeatFunc>& f2) const {
+ return f1->getNextRunTime() > f2->getNextRunTime();
}
};
- typedef std::vector<RepeatFunc> FunctionHeap;
+ typedef std::vector<std::unique_ptr<RepeatFunc>> FunctionHeap;
+ typedef std::unordered_map<StringPiece, RepeatFunc*, Hash> FunctionMap;
void run();
void runOneFunction(std::unique_lock<std::mutex>& lock,
std::chrono::steady_clock::time_point now);
void cancelFunction(const std::unique_lock<std::mutex>& lock,
- FunctionHeap::iterator it);
+ RepeatFunc* it);
void addFunctionToHeap(const std::unique_lock<std::mutex>& lock,
- RepeatFunc&& func);
+ std::unique_ptr<RepeatFunc> func);
void addFunctionInternal(
Function<void()>&& cb,
std::chrono::milliseconds startDelay,
bool runOnce);
+ // Return true if the current function is being canceled
+ bool cancelAllFunctionsWithLock(std::unique_lock<std::mutex>& lock);
+ bool cancelFunctionWithLock(
+ std::unique_lock<std::mutex>& lock,
+ StringPiece nameID);
+
std::thread thread_;
// Mutex to protect our member variables.
// The functions to run.
// This is a heap, ordered by next run time.
FunctionHeap functions_;
+ FunctionMap functionsMap_;
RunTimeOrder fnCmp_;
// The function currently being invoked by the running thread.
std::string threadName_;
bool steady_{false};
+ bool cancellingCurrentFunction_{false};
};
-}
+} // namespace folly