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,11 +45,12 @@ class FutureExecutor : public ExecutorImpl {
using T = typename invoke_result_t<F>::value_type;
folly::Promise<T> promise;
auto future = promise.getFuture();
ExecutorImpl::add(
[ promise = std::move(promise), func = std::move(func) ]() mutable {
func().then([promise = std::move(promise)](
folly::Try<T> && t) mutable { promise.setTry(std::move(t)); });
});
ExecutorImpl::add([promise = std::move(promise),
func = std::move(func)]() mutable {
func().then([promise = std::move(promise)](folly::Try<T>&& t) mutable {
promise.setTry(std::move(t));
});
});
return future;
}
......@@ -70,7 +71,7 @@ class FutureExecutor : public ExecutorImpl {
folly::Promise<T> promise;
auto future = promise.getFuture();
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));
});
return future;
......
......@@ -92,7 +92,7 @@ void IOThreadPoolExecutor::add(
auto ioThread = pickThread();
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));
ioThread->pendingTasks--;
};
......
......@@ -27,133 +27,133 @@
#include <folly/synchronization/LifoSem.h>
namespace folly {
/// A ManualExecutor only does work when you turn the crank, by calling
/// run() or indirectly with makeProgress() or waitFor().
///
/// 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.
///
/// NB No attempt has been made to make anything other than add and schedule
/// threadsafe.
class ManualExecutor : public DrivableExecutor,
public ScheduledExecutor,
public SequencedExecutor {
public:
void add(Func) override;
/// Do work. Returns the number of functions that were executed (maybe 0).
/// Non-blocking, in the sense that we don't wait for work (we can't
/// control whether one of the functions blocks).
/// This is stable, it will not chase an ever-increasing tail of work.
/// This also means, there may be more work available to perform at the
/// moment that this returns.
size_t run();
// Do work until there is no more work to do.
// Returns the number of functions that were executed (maybe 0).
// Unlike run, this method is not stable. It will chase an infinite tail of
// work so should be used with care.
// There will be no work available to perform at the moment that this
// returns.
size_t drain();
/// Wait for work to do.
void wait();
/// Wait for work to do, and do it.
void makeProgress() {
wait();
run();
}
/// Implements DrivableExecutor
void drive() override {
makeProgress();
}
/// makeProgress until this Future is ready.
template <class F> void waitFor(F const& f) {
// TODO(5427828)
/// A ManualExecutor only does work when you turn the crank, by calling
/// run() or indirectly with makeProgress() or waitFor().
///
/// 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.
///
/// NB No attempt has been made to make anything other than add and schedule
/// threadsafe.
class ManualExecutor : public DrivableExecutor,
public ScheduledExecutor,
public SequencedExecutor {
public:
void add(Func) override;
/// Do work. Returns the number of functions that were executed (maybe 0).
/// Non-blocking, in the sense that we don't wait for work (we can't
/// control whether one of the functions blocks).
/// This is stable, it will not chase an ever-increasing tail of work.
/// This also means, there may be more work available to perform at the
/// moment that this returns.
size_t run();
// Do work until there is no more work to do.
// Returns the number of functions that were executed (maybe 0).
// Unlike run, this method is not stable. It will chase an infinite tail of
// work so should be used with care.
// There will be no work available to perform at the moment that this
// returns.
size_t drain();
/// Wait for work to do.
void wait();
/// Wait for work to do, and do it.
void makeProgress() {
wait();
run();
}
/// Implements DrivableExecutor
void drive() override {
makeProgress();
}
/// makeProgress until this Future is ready.
template <class F>
void waitFor(F const& f) {
// TODO(5427828)
#if 0
while (!f.isReady())
makeProgress();
#else
while (!f.isReady()) {
run();
}
#endif
while (!f.isReady()) {
run();
}
void scheduleAt(Func&& f, TimePoint const& t) override {
#endif
}
void scheduleAt(Func&& f, TimePoint const& t) override {
std::lock_guard<std::mutex> lock(lock_);
scheduledFuncs_.emplace(t, std::move(f));
sem_.post();
}
/// Advance the clock. The clock never advances on its own.
/// Advancing the clock causes some work to be done, if work is available
/// to do (perhaps newly available because of the advanced clock).
/// If dur is <= 0 this is a noop.
void advance(Duration const& dur) {
advanceTo(now_ + dur);
}
/// Advance the clock to this absolute time. If t is <= now(),
/// this is a noop.
void advanceTo(TimePoint const& t);
TimePoint now() override {
return now_;
}
/// Flush the function queue. Destroys all stored functions without
/// executing them. Returns number of removed functions.
std::size_t clear() {
std::queue<Func> funcs;
std::priority_queue<ScheduledFunc> scheduled_funcs;
{
std::lock_guard<std::mutex> lock(lock_);
scheduledFuncs_.emplace(t, std::move(f));
sem_.post();
}
/// Advance the clock. The clock never advances on its own.
/// Advancing the clock causes some work to be done, if work is available
/// to do (perhaps newly available because of the advanced clock).
/// If dur is <= 0 this is a noop.
void advance(Duration const& dur) {
advanceTo(now_ + dur);
funcs_.swap(funcs);
scheduledFuncs_.swap(scheduled_funcs);
}
/// Advance the clock to this absolute time. If t is <= now(),
/// this is a noop.
void advanceTo(TimePoint const& t);
return funcs.size() + scheduled_funcs.size();
}
TimePoint now() override { return now_; }
private:
std::mutex lock_;
std::queue<Func> funcs_;
LifoSem sem_;
/// Flush the function queue. Destroys all stored functions without
/// executing them. Returns number of removed functions.
std::size_t clear() {
std::queue<Func> funcs;
std::priority_queue<ScheduledFunc> scheduled_funcs;
{
std::lock_guard<std::mutex> lock(lock_);
funcs_.swap(funcs);
scheduledFuncs_.swap(scheduled_funcs);
}
// helper class to enable ordering of scheduled events in the priority
// queue
struct ScheduledFunc {
TimePoint time;
size_t ordinal;
Func mutable func;
return funcs.size() + scheduled_funcs.size();
ScheduledFunc(TimePoint const& t, Func&& f) : time(t), func(std::move(f)) {
static size_t seq = 0;
ordinal = seq++;
}
private:
std::mutex lock_;
std::queue<Func> funcs_;
LifoSem sem_;
// helper class to enable ordering of scheduled events in the priority
// queue
struct ScheduledFunc {
TimePoint time;
size_t ordinal;
Func mutable func;
ScheduledFunc(TimePoint const& t, Func&& f)
: time(t), func(std::move(f))
{
static size_t seq = 0;
ordinal = seq++;
}
bool operator<(ScheduledFunc const& b) const {
// Earlier-scheduled things must be *higher* priority
// in the max-based std::priority_queue
if (time == b.time) {
return ordinal > b.ordinal;
}
return time > b.time;
bool operator<(ScheduledFunc const& b) const {
// Earlier-scheduled things must be *higher* priority
// in the max-based std::priority_queue
if (time == b.time) {
return ordinal > b.ordinal;
}
return time > b.time;
}
Func&& moveOutFunc() const {
return std::move(func);
}
};
std::priority_queue<ScheduledFunc> scheduledFuncs_;
TimePoint now_ = TimePoint::min();
Func&& moveOutFunc() const {
return std::move(func);
}
};
std::priority_queue<ScheduledFunc> scheduledFuncs_;
TimePoint now_ = TimePoint::min();
};
} // namespace folly
} // namespace folly
......@@ -24,36 +24,38 @@
#include <folly/lang/Exception.h>
namespace folly {
// An executor that supports timed scheduling. Like RxScheduler.
class ScheduledExecutor : public virtual Executor {
public:
// Reality is that better than millisecond resolution is very hard to
// achieve. However, we reserve the right to be incredible.
typedef std::chrono::microseconds Duration;
typedef std::chrono::steady_clock::time_point TimePoint;
~ScheduledExecutor() override = default;
void add(Func) override = 0;
/// Alias for add() (for Rx consistency)
void schedule(Func&& a) { add(std::move(a)); }
/// Schedule a Func to be executed after dur time has elapsed
/// Expect millisecond resolution at best.
void schedule(Func&& a, Duration const& dur) {
scheduleAt(std::move(a), now() + dur);
}
/// Schedule a Func to be executed at time t, or as soon afterward as
/// possible. Expect millisecond resolution at best. Must be threadsafe.
virtual void scheduleAt(Func&& /* a */, TimePoint const& /* t */) {
throw_exception<std::logic_error>("unimplemented");
}
/// Get this executor's notion of time. Must be threadsafe.
virtual TimePoint now() {
return std::chrono::steady_clock::now();
}
};
} // namespace folly
// An executor that supports timed scheduling. Like RxScheduler.
class ScheduledExecutor : public virtual Executor {
public:
// Reality is that better than millisecond resolution is very hard to
// achieve. However, we reserve the right to be incredible.
typedef std::chrono::microseconds Duration;
typedef std::chrono::steady_clock::time_point TimePoint;
~ScheduledExecutor() override = default;
void add(Func) override = 0;
/// Alias for add() (for Rx consistency)
void schedule(Func&& a) {
add(std::move(a));
}
/// Schedule a Func to be executed after dur time has elapsed
/// Expect millisecond resolution at best.
void schedule(Func&& a, Duration const& dur) {
scheduleAt(std::move(a), now() + dur);
}
/// Schedule a Func to be executed at time t, or as soon afterward as
/// possible. Expect millisecond resolution at best. Must be threadsafe.
virtual void scheduleAt(Func&& /* a */, TimePoint const& /* t */) {
throw_exception<std::logic_error>("unimplemented");
}
/// Get this executor's notion of time. Must be threadsafe.
virtual TimePoint now() {
return std::chrono::steady_clock::now();
}
};
} // namespace folly
......@@ -16,7 +16,6 @@
#include <folly/executors/SerialExecutor.h>
#include <glog/logging.h>
#include <folly/ExceptionString.h>
......
......@@ -76,8 +76,7 @@ class SerialExecutor : public SequencedExecutor {
};
using UniquePtr = std::unique_ptr<SerialExecutor, Deleter>;
[[deprecated("Replaced by create")]]
static UniquePtr createUnique(
[[deprecated("Replaced by create")]] static UniquePtr createUnique(
std::shared_ptr<Executor> parent = getCPUExecutor());
/**
......
......@@ -96,7 +96,7 @@ void ThreadedExecutor::controlLaunchEnqueuedTasks() {
with_unique_lock(enqueuedm_, [&] { std::swap(enqueuedt, enqueued_); });
for (auto& f : enqueuedt) {
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();
running_[id] = std::move(th);
}
......
......@@ -30,7 +30,10 @@ TEST(ManualExecutor, runIsStable) {
ManualExecutor x;
size_t count = 0;
auto f1 = [&]() { count++; };
auto f2 = [&]() { x.add(f1); x.add(f1); };
auto f2 = [&]() {
x.add(f1);
x.add(f1);
};
x.add(f2);
x.run();
EXPECT_EQ(count, 0);
......@@ -52,14 +55,14 @@ TEST(ManualExecutor, drainIsNotStable) {
TEST(ManualExecutor, scheduleDur) {
ManualExecutor x;
size_t count = 0;
std::chrono::milliseconds dur {10};
x.schedule([&]{ count++; }, dur);
std::chrono::milliseconds dur{10};
x.schedule([&] { count++; }, dur);
EXPECT_EQ(count, 0);
x.run();
EXPECT_EQ(count, 0);
x.advance(dur/2);
x.advance(dur / 2);
EXPECT_EQ(count, 0);
x.advance(dur/2);
x.advance(dur / 2);
EXPECT_EQ(count, 1);
}
......@@ -114,7 +117,7 @@ TEST(ManualExecutor, clockStartsAt0) {
TEST(ManualExecutor, scheduleAbs) {
ManualExecutor x;
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);
x.advance(std::chrono::milliseconds(10));
EXPECT_EQ(count, 1);
......@@ -123,7 +126,7 @@ TEST(ManualExecutor, scheduleAbs) {
TEST(ManualExecutor, advanceTo) {
ManualExecutor x;
size_t count = 0;
x.scheduleAt([&]{ count++; }, std::chrono::steady_clock::now());
x.scheduleAt([&] { count++; }, std::chrono::steady_clock::now());
EXPECT_EQ(count, 0);
x.advanceTo(std::chrono::steady_clock::now());
EXPECT_EQ(count, 1);
......@@ -133,7 +136,7 @@ TEST(ManualExecutor, advanceBack) {
ManualExecutor x;
size_t count = 0;
x.advance(std::chrono::microseconds(5));
x.schedule([&]{ count++; }, std::chrono::microseconds(6));
x.schedule([&] { count++; }, std::chrono::microseconds(6));
EXPECT_EQ(count, 0);
x.advanceTo(x.now() - std::chrono::microseconds(1));
EXPECT_EQ(count, 0);
......@@ -143,7 +146,7 @@ TEST(ManualExecutor, advanceNeg) {
ManualExecutor x;
size_t count = 0;
x.advance(std::chrono::microseconds(5));
x.schedule([&]{ count++; }, std::chrono::microseconds(6));
x.schedule([&] { count++; }, std::chrono::microseconds(6));
EXPECT_EQ(count, 0);
x.advance(std::chrono::microseconds(-1));
EXPECT_EQ(count, 0);
......@@ -153,10 +156,10 @@ TEST(ManualExecutor, waitForDoesNotDeadlock) {
ManualExecutor east, west;
folly::Baton<> baton;
auto f = makeFuture()
.via(&east)
.then([](Try<Unit>){ return makeFuture(); })
.via(&west);
std::thread t([&]{
.via(&east)
.then([](Try<Unit>) { return makeFuture(); })
.via(&west);
std::thread t([&] {
baton.post();
west.waitFor(f);
});
......@@ -168,9 +171,10 @@ TEST(ManualExecutor, waitForDoesNotDeadlock) {
TEST(ManualExecutor, getViaDoesNotDeadlock) {
ManualExecutor east, west;
folly::Baton<> baton;
auto f = makeFuture().via(&east).then([](Try<Unit>) {
return makeFuture();
}).via(&west);
auto f = makeFuture()
.via(&east)
.then([](Try<Unit>) { return makeFuture(); })
.via(&west);
std::thread t([&] {
baton.post();
f.getVia(&west);
......@@ -196,8 +200,8 @@ TEST(ManualExecutor, clear) {
TEST(Executor, InlineExecutor) {
InlineExecutor x;
size_t counter = 0;
x.add([&]{
x.add([&]{
x.add([&] {
x.add([&] {
EXPECT_EQ(counter, 0);
counter++;
});
......@@ -210,8 +214,8 @@ TEST(Executor, InlineExecutor) {
TEST(Executor, QueuedImmediateExecutor) {
QueuedImmediateExecutor x;
size_t counter = 0;
x.add([&]{
x.add([&]{
x.add([&] {
x.add([&] {
EXPECT_EQ(1, counter);
counter++;
});
......@@ -226,10 +230,12 @@ TEST(Executor, Runnable) {
size_t counter = 0;
struct Runnable {
std::function<void()> fn;
void operator()() { fn(); }
void operator()() {
fn();
}
};
Runnable f;
f.fn = [&]{ counter++; };
f.fn = [&] { counter++; };
x.add(f);
EXPECT_EQ(counter, 1);
}
......@@ -247,7 +253,9 @@ TEST(Executor, ThrowableThen) {
class CrappyExecutor : public Executor {
public:
void add(Func /* f */) override { throw std::runtime_error("bad"); }
void add(Func /* f */) override {
throw std::runtime_error("bad");
}
};
TEST(Executor, CrappyExecutor) {
......
......@@ -35,7 +35,7 @@ class NamedThreadFactory : public ThreadFactory {
std::thread newThread(Func&& func) override {
auto name = folly::to<std::string>(prefix_, suffix_++);
return std::thread(
[ func = std::move(func), name = std::move(name) ]() mutable {
[func = std::move(func), name = std::move(name)]() mutable {
folly::setThreadName(name);
func();
});
......
......@@ -43,7 +43,7 @@ class PriorityThreadFactory : public ThreadFactory {
std::thread newThread(Func&& func) override {
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) {
LOG(ERROR) << "setpriority failed (are you root?) with error " << 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