From 4cf30a1441af267ee42ab2fc9976dc3332f36ed8 Mon Sep 17 00:00:00 2001 From: Sven Over Date: Fri, 12 Aug 2016 02:01:54 -0700 Subject: [PATCH] prepare for folly::Executor taking folly::Function Summary: A bunch of small changes that are necessary before we change the definition of folly::Func from std::function to folly::Function. Reviewed By: yfeldblum, mzlee Differential Revision: D3706210 fbshipit-source-id: 198d8a391ef6f545ad4411d316ba9e271ea795e3 --- folly/futures/ManualExecutor.cpp | 4 ++-- folly/futures/ManualExecutor.h | 6 +++++- folly/futures/QueuedImmediateExecutor.cpp | 2 +- folly/futures/test/ViaTest.cpp | 2 +- folly/io/async/EventBase.h | 2 +- folly/test/SingletonTest.cpp | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/folly/futures/ManualExecutor.cpp b/folly/futures/ManualExecutor.cpp index 9b5c87f2..5586afba 100644 --- a/folly/futures/ManualExecutor.cpp +++ b/folly/futures/ManualExecutor.cpp @@ -26,7 +26,7 @@ namespace folly { void ManualExecutor::add(Func callback) { std::lock_guard lock(lock_); - funcs_.push(std::move(callback)); + funcs_.emplace(std::move(callback)); sem_.post(); } @@ -42,7 +42,7 @@ size_t ManualExecutor::run() { auto& sf = scheduledFuncs_.top(); if (sf.time > now_) break; - funcs_.push(sf.func); + funcs_.emplace(sf.moveOutFunc()); scheduledFuncs_.pop(); } diff --git a/folly/futures/ManualExecutor.h b/folly/futures/ManualExecutor.h index 1dfc5bb4..db7f012e 100644 --- a/folly/futures/ManualExecutor.h +++ b/folly/futures/ManualExecutor.h @@ -118,7 +118,7 @@ namespace folly { struct ScheduledFunc { TimePoint time; size_t ordinal; - Func func; + Func mutable func; ScheduledFunc(TimePoint const& t, Func&& f) : time(t), func(std::move(f)) @@ -132,6 +132,10 @@ namespace folly { return ordinal < b.ordinal; return time < b.time; } + + Func&& moveOutFunc() const { + return std::move(func); + } }; std::priority_queue scheduledFuncs_; TimePoint now_ = now_.min(); diff --git a/folly/futures/QueuedImmediateExecutor.cpp b/folly/futures/QueuedImmediateExecutor.cpp index 5a924afd..1d2aeea7 100644 --- a/folly/futures/QueuedImmediateExecutor.cpp +++ b/folly/futures/QueuedImmediateExecutor.cpp @@ -30,7 +30,7 @@ void QueuedImmediateExecutor::addStatic(Func callback) { q_->pop(); } } else { - q_->push(callback); + q_->push(std::move(callback)); } } diff --git a/folly/futures/test/ViaTest.cpp b/folly/futures/test/ViaTest.cpp index 78231873..c864b902 100644 --- a/folly/futures/test/ViaTest.cpp +++ b/folly/futures/test/ViaTest.cpp @@ -31,7 +31,7 @@ struct ManualWaiter : public DrivableExecutor { explicit ManualWaiter(std::shared_ptr ex) : ex(ex) {} void add(Func f) override { - ex->add(f); + ex->add(std::move(f)); } void drive() override { diff --git a/folly/io/async/EventBase.h b/folly/io/async/EventBase.h index b450e97f..6a508129 100644 --- a/folly/io/async/EventBase.h +++ b/folly/io/async/EventBase.h @@ -50,7 +50,7 @@ namespace folly { -typedef std::function Cob; +using Cob = Func; // defined in folly/Executor.h template class NotificationQueue; diff --git a/folly/test/SingletonTest.cpp b/folly/test/SingletonTest.cpp index a5a1cd78..42d5bf7d 100644 --- a/folly/test/SingletonTest.cpp +++ b/folly/test/SingletonTest.cpp @@ -495,7 +495,7 @@ class TestEagerInitParallelExecutor : public folly::Executor { virtual void add(folly::Func func) override { const auto index = (counter_ ++) % eventBases_.size(); - eventBases_[index]->add(func); + eventBases_[index]->add(std::move(func)); } private: -- 2.34.1