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 @@
*/
#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));
}
}
......
......@@ -16,7 +16,10 @@
#pragma once
#include <queue>
#include <folly/Executor.h>
#include <folly/ThreadLocal.h>
namespace folly {
......@@ -27,13 +30,12 @@ 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
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