Commit 4bbd587c authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook GitHub Bot

Outline most of TimedDrivableExecutor implementation

Summary: This header is widely included (via `Future.h`) and instantiations of `UnboundedQueue`'s methods are expensive, so outline them (note that the template methods will only be instantiated if used, so that's fine).

Reviewed By: philippv

Differential Revision: D22014083

fbshipit-source-id: b6a71c38c142917e9e0d86fbb64a88462382952a
parent 8ae59d46
...@@ -23,6 +23,14 @@ ...@@ -23,6 +23,14 @@
namespace folly { namespace folly {
TimedDrivableExecutor::TimedDrivableExecutor() = default;
TimedDrivableExecutor::~TimedDrivableExecutor() noexcept {
// Drain on destruction so that if work is added here during the collapse
// of a future train, it will propagate.
drain();
}
void TimedDrivableExecutor::add(Func callback) { void TimedDrivableExecutor::add(Func callback) {
queue_.enqueue(std::move(callback)); queue_.enqueue(std::move(callback));
} }
...@@ -32,6 +40,10 @@ void TimedDrivableExecutor::drive() noexcept { ...@@ -32,6 +40,10 @@ void TimedDrivableExecutor::drive() noexcept {
run(); run();
} }
bool TimedDrivableExecutor::try_drive() noexcept {
return try_wait() && run() > 0;
}
size_t TimedDrivableExecutor::run() noexcept { size_t TimedDrivableExecutor::run() noexcept {
size_t count = 0; size_t count = 0;
size_t n = queue_.size(); size_t n = queue_.size();
...@@ -67,4 +79,8 @@ void TimedDrivableExecutor::wait() noexcept { ...@@ -67,4 +79,8 @@ void TimedDrivableExecutor::wait() noexcept {
} }
} }
bool TimedDrivableExecutor::try_wait() noexcept {
return func_ || queue_.try_dequeue(func_);
}
} // namespace folly } // namespace folly
...@@ -29,20 +29,15 @@ namespace folly { ...@@ -29,20 +29,15 @@ namespace folly {
*/ */
class TimedDrivableExecutor : public DrivableExecutor { class TimedDrivableExecutor : public DrivableExecutor {
public: public:
~TimedDrivableExecutor() noexcept { TimedDrivableExecutor();
// Drain on destruction so that if work is added here during the collapse ~TimedDrivableExecutor() noexcept override;
// of a future train, it will propagate.
drain();
}
/// Implements DrivableExecutor /// Implements DrivableExecutor
void drive() noexcept override; void drive() noexcept override;
// Make progress if there is work to do and return true. Otherwise return // Make progress if there is work to do and return true. Otherwise return
// false. // false.
bool try_drive() noexcept { bool try_drive() noexcept;
return try_wait() && run() > 0;
}
// Make progress on this Executor's work. Acts as drive, except it will only // Make progress on this Executor's work. Acts as drive, except it will only
// wait for a period of timeout for work to be enqueued. If no work is // wait for a period of timeout for work to be enqueued. If no work is
...@@ -84,9 +79,7 @@ class TimedDrivableExecutor : public DrivableExecutor { ...@@ -84,9 +79,7 @@ class TimedDrivableExecutor : public DrivableExecutor {
void wait() noexcept; void wait() noexcept;
// Return true if there is work to do, false otherwise // Return true if there is work to do, false otherwise
bool try_wait() noexcept { bool try_wait() noexcept;
return func_ || queue_.try_dequeue(func_);
}
/// Wait for work to do or for a period of timeout, whichever is sooner. /// Wait for work to do or for a period of timeout, whichever is sooner.
template <typename Rep, typename Period> template <typename Rep, typename Period>
......
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