Commit c45f2aa5 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

QueuedImmediateExecutor tweaks

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
parent 6612bf09
...@@ -15,22 +15,24 @@ ...@@ -15,22 +15,24 @@
*/ */
#include <folly/executors/QueuedImmediateExecutor.h> #include <folly/executors/QueuedImmediateExecutor.h>
#include <folly/ThreadLocal.h>
#include <queue> #include <folly/Indestructible.h>
namespace folly { namespace folly {
void QueuedImmediateExecutor::addStatic(Func callback) { QueuedImmediateExecutor& QueuedImmediateExecutor::instance() {
static folly::ThreadLocal<std::queue<Func>> q_; static auto instance = Indestructible<QueuedImmediateExecutor>{};
return *instance;
}
if (q_->empty()) { void QueuedImmediateExecutor::add(Func callback) {
q_->push(std::move(callback)); auto& q = *q_;
while (!q_->empty()) { q.push(std::move(callback));
q_->front()(); if (q.size() == 1) {
q_->pop(); while (!q.empty()) {
q.front()();
q.pop();
} }
} else {
q_->push(std::move(callback));
} }
} }
......
...@@ -16,7 +16,10 @@ ...@@ -16,7 +16,10 @@
#pragma once #pragma once
#include <queue>
#include <folly/Executor.h> #include <folly/Executor.h>
#include <folly/ThreadLocal.h>
namespace folly { namespace folly {
...@@ -27,13 +30,12 @@ namespace folly { ...@@ -27,13 +30,12 @@ namespace folly {
*/ */
class QueuedImmediateExecutor : public Executor { class QueuedImmediateExecutor : public Executor {
public: public:
/// There's really only one queue per thread, no matter how many static QueuedImmediateExecutor& instance();
/// QueuedImmediateExecutor objects you may have.
static void addStatic(Func); void add(Func func) override;
void add(Func func) override { private:
addStatic(std::move(func)); folly::ThreadLocal<std::queue<Func>> q_;
}
}; };
} // namespace folly } // namespace folly
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