From: Yedidya Feldblum Date: Thu, 23 Nov 2017 19:59:32 +0000 (-0800) Subject: QueuedImmediateExecutor tweaks X-Git-Tag: v2017.11.27.00~2 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=c45f2aa595428c80bbd34b70925c236f83dc6231;p=folly.git 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 --- diff --git a/folly/executors/QueuedImmediateExecutor.cpp b/folly/executors/QueuedImmediateExecutor.cpp index a8a9d0a8..676d26eb 100644 --- a/folly/executors/QueuedImmediateExecutor.cpp +++ b/folly/executors/QueuedImmediateExecutor.cpp @@ -15,22 +15,24 @@ */ #include -#include -#include + +#include namespace folly { -void QueuedImmediateExecutor::addStatic(Func callback) { - static folly::ThreadLocal> q_; +QueuedImmediateExecutor& QueuedImmediateExecutor::instance() { + static auto instance = Indestructible{}; + 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)); } } diff --git a/folly/executors/QueuedImmediateExecutor.h b/folly/executors/QueuedImmediateExecutor.h index b98047a0..176c2532 100644 --- a/folly/executors/QueuedImmediateExecutor.h +++ b/folly/executors/QueuedImmediateExecutor.h @@ -16,7 +16,10 @@ #pragma once +#include + #include +#include 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> q_; }; } // namespace folly