Commit 056e121f authored by Chad Austin's avatar Chad Austin Committed by Facebook Github Bot

clang-format folly/executors/

Summary:
Run clang-format across folly/executors/.

```
find . \( -iname '*.cpp' -o -iname '*.h' \) -exec clang-format -i {} \;
```

Reviewed By: yfeldblum

Differential Revision: D8843064

fbshipit-source-id: 0a3c82083eebf2c684a4ab2e12067f0f742bf1d4
parent 1d2790bc
...@@ -45,10 +45,11 @@ class FutureExecutor : public ExecutorImpl { ...@@ -45,10 +45,11 @@ class FutureExecutor : public ExecutorImpl {
using T = typename invoke_result_t<F>::value_type; using T = typename invoke_result_t<F>::value_type;
folly::Promise<T> promise; folly::Promise<T> promise;
auto future = promise.getFuture(); auto future = promise.getFuture();
ExecutorImpl::add( ExecutorImpl::add([promise = std::move(promise),
[ promise = std::move(promise), func = std::move(func) ]() mutable { func = std::move(func)]() mutable {
func().then([promise = std::move(promise)]( func().then([promise = std::move(promise)](folly::Try<T>&& t) mutable {
folly::Try<T> && t) mutable { promise.setTry(std::move(t)); }); promise.setTry(std::move(t));
});
}); });
return future; return future;
} }
...@@ -70,7 +71,7 @@ class FutureExecutor : public ExecutorImpl { ...@@ -70,7 +71,7 @@ class FutureExecutor : public ExecutorImpl {
folly::Promise<T> promise; folly::Promise<T> promise;
auto future = promise.getFuture(); auto future = promise.getFuture();
ExecutorImpl::add( ExecutorImpl::add(
[ promise = std::move(promise), func = std::move(func) ]() mutable { [promise = std::move(promise), func = std::move(func)]() mutable {
promise.setWith(std::move(func)); promise.setWith(std::move(func));
}); });
return future; return future;
......
...@@ -92,7 +92,7 @@ void IOThreadPoolExecutor::add( ...@@ -92,7 +92,7 @@ void IOThreadPoolExecutor::add(
auto ioThread = pickThread(); auto ioThread = pickThread();
auto task = Task(std::move(func), expiration, std::move(expireCallback)); auto task = Task(std::move(func), expiration, std::move(expireCallback));
auto wrappedFunc = [ ioThread, task = std::move(task) ]() mutable { auto wrappedFunc = [ioThread, task = std::move(task)]() mutable {
runTask(ioThread, std::move(task)); runTask(ioThread, std::move(task));
ioThread->pendingTasks--; ioThread->pendingTasks--;
}; };
......
...@@ -27,15 +27,15 @@ ...@@ -27,15 +27,15 @@
#include <folly/synchronization/LifoSem.h> #include <folly/synchronization/LifoSem.h>
namespace folly { namespace folly {
/// A ManualExecutor only does work when you turn the crank, by calling /// A ManualExecutor only does work when you turn the crank, by calling
/// run() or indirectly with makeProgress() or waitFor(). /// run() or indirectly with makeProgress() or waitFor().
/// ///
/// The clock for a manual executor starts at 0 and advances only when you /// The clock for a manual executor starts at 0 and advances only when you
/// ask it to. i.e. time is also under manual control. /// ask it to. i.e. time is also under manual control.
/// ///
/// NB No attempt has been made to make anything other than add and schedule /// NB No attempt has been made to make anything other than add and schedule
/// threadsafe. /// threadsafe.
class ManualExecutor : public DrivableExecutor, class ManualExecutor : public DrivableExecutor,
public ScheduledExecutor, public ScheduledExecutor,
public SequencedExecutor { public SequencedExecutor {
public: public:
...@@ -72,7 +72,8 @@ namespace folly { ...@@ -72,7 +72,8 @@ namespace folly {
} }
/// makeProgress until this Future is ready. /// makeProgress until this Future is ready.
template <class F> void waitFor(F const& f) { template <class F>
void waitFor(F const& f) {
// TODO(5427828) // TODO(5427828)
#if 0 #if 0
while (!f.isReady()) while (!f.isReady())
...@@ -82,7 +83,6 @@ namespace folly { ...@@ -82,7 +83,6 @@ namespace folly {
run(); run();
} }
#endif #endif
} }
void scheduleAt(Func&& f, TimePoint const& t) override { void scheduleAt(Func&& f, TimePoint const& t) override {
...@@ -103,7 +103,9 @@ namespace folly { ...@@ -103,7 +103,9 @@ namespace folly {
/// this is a noop. /// this is a noop.
void advanceTo(TimePoint const& t); void advanceTo(TimePoint const& t);
TimePoint now() override { return now_; } TimePoint now() override {
return now_;
}
/// Flush the function queue. Destroys all stored functions without /// Flush the function queue. Destroys all stored functions without
/// executing them. Returns number of removed functions. /// executing them. Returns number of removed functions.
...@@ -132,9 +134,7 @@ namespace folly { ...@@ -132,9 +134,7 @@ namespace folly {
size_t ordinal; size_t ordinal;
Func mutable func; Func mutable func;
ScheduledFunc(TimePoint const& t, Func&& f) ScheduledFunc(TimePoint const& t, Func&& f) : time(t), func(std::move(f)) {
: time(t), func(std::move(f))
{
static size_t seq = 0; static size_t seq = 0;
ordinal = seq++; ordinal = seq++;
} }
...@@ -154,6 +154,6 @@ namespace folly { ...@@ -154,6 +154,6 @@ namespace folly {
}; };
std::priority_queue<ScheduledFunc> scheduledFuncs_; std::priority_queue<ScheduledFunc> scheduledFuncs_;
TimePoint now_ = TimePoint::min(); TimePoint now_ = TimePoint::min();
}; };
} // namespace folly } // namespace folly
...@@ -24,8 +24,8 @@ ...@@ -24,8 +24,8 @@
#include <folly/lang/Exception.h> #include <folly/lang/Exception.h>
namespace folly { namespace folly {
// An executor that supports timed scheduling. Like RxScheduler. // An executor that supports timed scheduling. Like RxScheduler.
class ScheduledExecutor : public virtual Executor { class ScheduledExecutor : public virtual Executor {
public: public:
// Reality is that better than millisecond resolution is very hard to // Reality is that better than millisecond resolution is very hard to
// achieve. However, we reserve the right to be incredible. // achieve. However, we reserve the right to be incredible.
...@@ -37,7 +37,9 @@ namespace folly { ...@@ -37,7 +37,9 @@ namespace folly {
void add(Func) override = 0; void add(Func) override = 0;
/// Alias for add() (for Rx consistency) /// Alias for add() (for Rx consistency)
void schedule(Func&& a) { add(std::move(a)); } void schedule(Func&& a) {
add(std::move(a));
}
/// Schedule a Func to be executed after dur time has elapsed /// Schedule a Func to be executed after dur time has elapsed
/// Expect millisecond resolution at best. /// Expect millisecond resolution at best.
...@@ -55,5 +57,5 @@ namespace folly { ...@@ -55,5 +57,5 @@ namespace folly {
virtual TimePoint now() { virtual TimePoint now() {
return std::chrono::steady_clock::now(); return std::chrono::steady_clock::now();
} }
}; };
} // namespace folly } // namespace folly
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <folly/executors/SerialExecutor.h> #include <folly/executors/SerialExecutor.h>
#include <glog/logging.h> #include <glog/logging.h>
#include <folly/ExceptionString.h> #include <folly/ExceptionString.h>
......
...@@ -76,8 +76,7 @@ class SerialExecutor : public SequencedExecutor { ...@@ -76,8 +76,7 @@ class SerialExecutor : public SequencedExecutor {
}; };
using UniquePtr = std::unique_ptr<SerialExecutor, Deleter>; using UniquePtr = std::unique_ptr<SerialExecutor, Deleter>;
[[deprecated("Replaced by create")]] [[deprecated("Replaced by create")]] static UniquePtr createUnique(
static UniquePtr createUnique(
std::shared_ptr<Executor> parent = getCPUExecutor()); std::shared_ptr<Executor> parent = getCPUExecutor());
/** /**
......
...@@ -96,7 +96,7 @@ void ThreadedExecutor::controlLaunchEnqueuedTasks() { ...@@ -96,7 +96,7 @@ void ThreadedExecutor::controlLaunchEnqueuedTasks() {
with_unique_lock(enqueuedm_, [&] { std::swap(enqueuedt, enqueued_); }); with_unique_lock(enqueuedm_, [&] { std::swap(enqueuedt, enqueued_); });
for (auto& f : enqueuedt) { for (auto& f : enqueuedt) {
auto th = threadFactory_->newThread( auto th = threadFactory_->newThread(
[ this, f = std::move(f) ]() mutable { work(f); }); [this, f = std::move(f)]() mutable { work(f); });
auto id = th.get_id(); auto id = th.get_id();
running_[id] = std::move(th); running_[id] = std::move(th);
} }
......
...@@ -30,7 +30,10 @@ TEST(ManualExecutor, runIsStable) { ...@@ -30,7 +30,10 @@ TEST(ManualExecutor, runIsStable) {
ManualExecutor x; ManualExecutor x;
size_t count = 0; size_t count = 0;
auto f1 = [&]() { count++; }; auto f1 = [&]() { count++; };
auto f2 = [&]() { x.add(f1); x.add(f1); }; auto f2 = [&]() {
x.add(f1);
x.add(f1);
};
x.add(f2); x.add(f2);
x.run(); x.run();
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
...@@ -52,14 +55,14 @@ TEST(ManualExecutor, drainIsNotStable) { ...@@ -52,14 +55,14 @@ TEST(ManualExecutor, drainIsNotStable) {
TEST(ManualExecutor, scheduleDur) { TEST(ManualExecutor, scheduleDur) {
ManualExecutor x; ManualExecutor x;
size_t count = 0; size_t count = 0;
std::chrono::milliseconds dur {10}; std::chrono::milliseconds dur{10};
x.schedule([&]{ count++; }, dur); x.schedule([&] { count++; }, dur);
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
x.run(); x.run();
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
x.advance(dur/2); x.advance(dur / 2);
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
x.advance(dur/2); x.advance(dur / 2);
EXPECT_EQ(count, 1); EXPECT_EQ(count, 1);
} }
...@@ -114,7 +117,7 @@ TEST(ManualExecutor, clockStartsAt0) { ...@@ -114,7 +117,7 @@ TEST(ManualExecutor, clockStartsAt0) {
TEST(ManualExecutor, scheduleAbs) { TEST(ManualExecutor, scheduleAbs) {
ManualExecutor x; ManualExecutor x;
size_t count = 0; size_t count = 0;
x.scheduleAt([&]{ count++; }, x.now() + std::chrono::milliseconds(10)); x.scheduleAt([&] { count++; }, x.now() + std::chrono::milliseconds(10));
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
x.advance(std::chrono::milliseconds(10)); x.advance(std::chrono::milliseconds(10));
EXPECT_EQ(count, 1); EXPECT_EQ(count, 1);
...@@ -123,7 +126,7 @@ TEST(ManualExecutor, scheduleAbs) { ...@@ -123,7 +126,7 @@ TEST(ManualExecutor, scheduleAbs) {
TEST(ManualExecutor, advanceTo) { TEST(ManualExecutor, advanceTo) {
ManualExecutor x; ManualExecutor x;
size_t count = 0; size_t count = 0;
x.scheduleAt([&]{ count++; }, std::chrono::steady_clock::now()); x.scheduleAt([&] { count++; }, std::chrono::steady_clock::now());
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
x.advanceTo(std::chrono::steady_clock::now()); x.advanceTo(std::chrono::steady_clock::now());
EXPECT_EQ(count, 1); EXPECT_EQ(count, 1);
...@@ -133,7 +136,7 @@ TEST(ManualExecutor, advanceBack) { ...@@ -133,7 +136,7 @@ TEST(ManualExecutor, advanceBack) {
ManualExecutor x; ManualExecutor x;
size_t count = 0; size_t count = 0;
x.advance(std::chrono::microseconds(5)); x.advance(std::chrono::microseconds(5));
x.schedule([&]{ count++; }, std::chrono::microseconds(6)); x.schedule([&] { count++; }, std::chrono::microseconds(6));
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
x.advanceTo(x.now() - std::chrono::microseconds(1)); x.advanceTo(x.now() - std::chrono::microseconds(1));
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
...@@ -143,7 +146,7 @@ TEST(ManualExecutor, advanceNeg) { ...@@ -143,7 +146,7 @@ TEST(ManualExecutor, advanceNeg) {
ManualExecutor x; ManualExecutor x;
size_t count = 0; size_t count = 0;
x.advance(std::chrono::microseconds(5)); x.advance(std::chrono::microseconds(5));
x.schedule([&]{ count++; }, std::chrono::microseconds(6)); x.schedule([&] { count++; }, std::chrono::microseconds(6));
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
x.advance(std::chrono::microseconds(-1)); x.advance(std::chrono::microseconds(-1));
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
...@@ -154,9 +157,9 @@ TEST(ManualExecutor, waitForDoesNotDeadlock) { ...@@ -154,9 +157,9 @@ TEST(ManualExecutor, waitForDoesNotDeadlock) {
folly::Baton<> baton; folly::Baton<> baton;
auto f = makeFuture() auto f = makeFuture()
.via(&east) .via(&east)
.then([](Try<Unit>){ return makeFuture(); }) .then([](Try<Unit>) { return makeFuture(); })
.via(&west); .via(&west);
std::thread t([&]{ std::thread t([&] {
baton.post(); baton.post();
west.waitFor(f); west.waitFor(f);
}); });
...@@ -168,9 +171,10 @@ TEST(ManualExecutor, waitForDoesNotDeadlock) { ...@@ -168,9 +171,10 @@ TEST(ManualExecutor, waitForDoesNotDeadlock) {
TEST(ManualExecutor, getViaDoesNotDeadlock) { TEST(ManualExecutor, getViaDoesNotDeadlock) {
ManualExecutor east, west; ManualExecutor east, west;
folly::Baton<> baton; folly::Baton<> baton;
auto f = makeFuture().via(&east).then([](Try<Unit>) { auto f = makeFuture()
return makeFuture(); .via(&east)
}).via(&west); .then([](Try<Unit>) { return makeFuture(); })
.via(&west);
std::thread t([&] { std::thread t([&] {
baton.post(); baton.post();
f.getVia(&west); f.getVia(&west);
...@@ -196,8 +200,8 @@ TEST(ManualExecutor, clear) { ...@@ -196,8 +200,8 @@ TEST(ManualExecutor, clear) {
TEST(Executor, InlineExecutor) { TEST(Executor, InlineExecutor) {
InlineExecutor x; InlineExecutor x;
size_t counter = 0; size_t counter = 0;
x.add([&]{ x.add([&] {
x.add([&]{ x.add([&] {
EXPECT_EQ(counter, 0); EXPECT_EQ(counter, 0);
counter++; counter++;
}); });
...@@ -210,8 +214,8 @@ TEST(Executor, InlineExecutor) { ...@@ -210,8 +214,8 @@ TEST(Executor, InlineExecutor) {
TEST(Executor, QueuedImmediateExecutor) { TEST(Executor, QueuedImmediateExecutor) {
QueuedImmediateExecutor x; QueuedImmediateExecutor x;
size_t counter = 0; size_t counter = 0;
x.add([&]{ x.add([&] {
x.add([&]{ x.add([&] {
EXPECT_EQ(1, counter); EXPECT_EQ(1, counter);
counter++; counter++;
}); });
...@@ -226,10 +230,12 @@ TEST(Executor, Runnable) { ...@@ -226,10 +230,12 @@ TEST(Executor, Runnable) {
size_t counter = 0; size_t counter = 0;
struct Runnable { struct Runnable {
std::function<void()> fn; std::function<void()> fn;
void operator()() { fn(); } void operator()() {
fn();
}
}; };
Runnable f; Runnable f;
f.fn = [&]{ counter++; }; f.fn = [&] { counter++; };
x.add(f); x.add(f);
EXPECT_EQ(counter, 1); EXPECT_EQ(counter, 1);
} }
...@@ -247,7 +253,9 @@ TEST(Executor, ThrowableThen) { ...@@ -247,7 +253,9 @@ TEST(Executor, ThrowableThen) {
class CrappyExecutor : public Executor { class CrappyExecutor : public Executor {
public: public:
void add(Func /* f */) override { throw std::runtime_error("bad"); } void add(Func /* f */) override {
throw std::runtime_error("bad");
}
}; };
TEST(Executor, CrappyExecutor) { TEST(Executor, CrappyExecutor) {
......
...@@ -35,7 +35,7 @@ class NamedThreadFactory : public ThreadFactory { ...@@ -35,7 +35,7 @@ class NamedThreadFactory : public ThreadFactory {
std::thread newThread(Func&& func) override { std::thread newThread(Func&& func) override {
auto name = folly::to<std::string>(prefix_, suffix_++); auto name = folly::to<std::string>(prefix_, suffix_++);
return std::thread( return std::thread(
[ func = std::move(func), name = std::move(name) ]() mutable { [func = std::move(func), name = std::move(name)]() mutable {
folly::setThreadName(name); folly::setThreadName(name);
func(); func();
}); });
......
...@@ -43,7 +43,7 @@ class PriorityThreadFactory : public ThreadFactory { ...@@ -43,7 +43,7 @@ class PriorityThreadFactory : public ThreadFactory {
std::thread newThread(Func&& func) override { std::thread newThread(Func&& func) override {
int priority = priority_; int priority = priority_;
return factory_->newThread([ priority, func = std::move(func) ]() mutable { return factory_->newThread([priority, func = std::move(func)]() mutable {
if (setpriority(PRIO_PROCESS, 0, priority) != 0) { if (setpriority(PRIO_PROCESS, 0, priority) != 0) {
LOG(ERROR) << "setpriority failed (are you root?) with error " << errno, LOG(ERROR) << "setpriority failed (are you root?) with error " << errno,
strerror(errno); strerror(errno);
......
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