Summary:
[Folly] `QueuedImmediateExecutor` tweaks.
* Add a leaky meyers singleton instance.
* Make the thread-local queue an instance variable. Callers which want the singleton thread-local queue can use the singleton executor instance instance, for the same effect.
* Simplify the body of `add`, and perform the thread-local lookup only once per invocation.
Reviewed By: djwatson
Differential Revision:
D6399067
fbshipit-source-id:
03904885a70c4b943141bd83868414d27232fd6a
*/
#include <folly/executors/QueuedImmediateExecutor.h>
-#include <folly/ThreadLocal.h>
-#include <queue>
+
+#include <folly/Indestructible.h>
namespace folly {
-void QueuedImmediateExecutor::addStatic(Func callback) {
- static folly::ThreadLocal<std::queue<Func>> q_;
+QueuedImmediateExecutor& QueuedImmediateExecutor::instance() {
+ static auto instance = Indestructible<QueuedImmediateExecutor>{};
+ return *instance;
+}
- if (q_->empty()) {
- q_->push(std::move(callback));
- while (!q_->empty()) {
- q_->front()();
- q_->pop();
+void QueuedImmediateExecutor::add(Func callback) {
+ auto& q = *q_;
+ q.push(std::move(callback));
+ if (q.size() == 1) {
+ while (!q.empty()) {
+ q.front()();
+ q.pop();
}
- } else {
- q_->push(std::move(callback));
}
}
#pragma once
+#include <queue>
+
#include <folly/Executor.h>
+#include <folly/ThreadLocal.h>
namespace folly {
*/
class QueuedImmediateExecutor : public Executor {
public:
- /// There's really only one queue per thread, no matter how many
- /// QueuedImmediateExecutor objects you may have.
- static void addStatic(Func);
+ static QueuedImmediateExecutor& instance();
+
+ void add(Func func) override;
- void add(Func func) override {
- addStatic(std::move(func));
- }
+ private:
+ folly::ThreadLocal<std::queue<Func>> q_;
};
} // namespace folly