Commit 48866d26 authored by Eric Niebler's avatar Eric Niebler Committed by Facebook GitHub Bot

make Executor::keepAlive(Acquire|Release) and KeepAlive move assign noexcept

Summary:
Moveable types should have no-throw move assignment operators. `Executor::KeepAlive<>`'s move assignment operator is not marked `noexcept`. It calls the virtual `Executor::keepAliveRelease`, which is also not marked `noexcept`, despite the fact that virtually (haha) all overrides of that function do nothing more than decrement an atomic and free some resources.

This diff makes the following `noexcept`:
* `Executor::keepAliveAcquire`
* `Executor::keepAliveRelease`
* `KeepAlive::reset()`
* `KeepAlive::operator=(KeepAlive&&)`

 ---

Reviewed By: yfeldblum

Differential Revision: D22505764

fbshipit-source-id: 8e0a04e057c971673cf75da974c1abca2bdf87e8
parent 2f0542e4
......@@ -88,7 +88,7 @@ class DefaultKeepAliveExecutor : public virtual Executor {
executor_(executor),
numPriorities_(executor->getNumPriorities()) {}
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
auto keepAliveCount =
keepAliveCount_.fetch_add(1, std::memory_order_relaxed);
// We should never increment from 0
......@@ -96,7 +96,7 @@ class DefaultKeepAliveExecutor : public virtual Executor {
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
auto keepAliveCount =
keepAliveCount_.fetch_sub(1, std::memory_order_acq_rel);
DCHECK(keepAliveCount >= 1);
......@@ -130,7 +130,7 @@ class DefaultKeepAliveExecutor : public virtual Executor {
uint8_t numPriorities_;
};
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
auto keepAliveCount =
controlBlock_->keepAliveCount_.fetch_add(1, std::memory_order_relaxed);
// We should never increment from 0
......@@ -138,7 +138,7 @@ class DefaultKeepAliveExecutor : public virtual Executor {
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
auto keepAliveCount =
controlBlock_->keepAliveCount_.fetch_sub(1, std::memory_order_acquire);
DCHECK(keepAliveCount >= 1);
......
......@@ -30,11 +30,11 @@ void Executor::addWithPriority(Func, int8_t /* priority */) {
"addWithPriority() is not implemented for this Executor");
}
bool Executor::keepAliveAcquire() {
bool Executor::keepAliveAcquire() noexcept {
return false;
}
void Executor::keepAliveRelease() {
void Executor::keepAliveRelease() noexcept {
LOG(FATAL) << __func__ << "() should not be called for folly::Executor types "
<< "which do not override keepAliveAcquire()";
}
......
......@@ -126,7 +126,7 @@ class Executor {
*this = getKeepAliveToken(executor);
}
KeepAlive& operator=(KeepAlive&& other) {
KeepAlive& operator=(KeepAlive&& other) noexcept {
reset();
storage_ = std::exchange(other.storage_, 0);
return *this;
......@@ -140,7 +140,7 @@ class Executor {
typename OtherExecutor,
typename = typename std::enable_if<
std::is_convertible<OtherExecutor*, ExecutorT*>::value>::type>
KeepAlive& operator=(KeepAlive<OtherExecutor>&& other) {
KeepAlive& operator=(KeepAlive<OtherExecutor>&& other) noexcept {
return *this = KeepAlive(std::move(other));
}
......@@ -152,7 +152,7 @@ class Executor {
return *this = KeepAlive(other);
}
void reset() {
void reset() noexcept {
if (Executor* executor = get()) {
auto const flags = std::exchange(storage_, 0) & kFlagMask;
if (!(flags & (kDummyFlag | kAliasFlag))) {
......@@ -251,10 +251,10 @@ class Executor {
// Acquire a keep alive token. Should return false if keep-alive mechanism
// is not supported.
virtual bool keepAliveAcquire();
virtual bool keepAliveAcquire() noexcept;
// Release a keep alive token previously acquired by keepAliveAcquire().
// Will never be called if keepAliveAcquire() returns false.
virtual void keepAliveRelease();
virtual void keepAliveRelease() noexcept;
template <typename ExecutorT>
static KeepAlive<ExecutorT> makeKeepAlive(ExecutorT* executor) {
......
......@@ -311,14 +311,14 @@ folly::Executor::KeepAlive<> EDFThreadPoolExecutor::deadlineExecutor(
executor_->add(std::move(f), deadline_);
}
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
const auto count =
keepAliveCount_.fetch_add(1, std::memory_order_relaxed);
DCHECK_GT(count, 0);
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
const auto count =
keepAliveCount_.fetch_sub(1, std::memory_order_acq_rel);
DCHECK_GT(count, 0);
......
......@@ -37,14 +37,14 @@ class ExecutorWithPriorityImpl : public virtual Executor {
}
protected:
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
auto keepAliveCounter =
keepAliveCounter_.fetch_add(1, std::memory_order_relaxed);
DCHECK(keepAliveCounter > 0);
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
auto keepAliveCounter =
keepAliveCounter_.fetch_sub(1, std::memory_order_acq_rel);
DCHECK(keepAliveCounter > 0);
......
......@@ -124,12 +124,12 @@ class ManualExecutor : public DrivableExecutor,
return funcs.size() + scheduled_funcs.size();
}
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
keepAliveCount_.fetch_add(1, std::memory_order_relaxed);
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
if (keepAliveCount_.fetch_sub(1, std::memory_order_acq_rel) == 1) {
add([] {});
}
......
......@@ -40,14 +40,14 @@ SerialExecutor::UniquePtr SerialExecutor::createUnique(
return {executor, Deleter{std::move(parent)}};
}
bool SerialExecutor::keepAliveAcquire() {
bool SerialExecutor::keepAliveAcquire() noexcept {
auto keepAliveCounter =
keepAliveCounter_.fetch_add(1, std::memory_order_relaxed);
DCHECK(keepAliveCounter > 0);
return true;
}
void SerialExecutor::keepAliveRelease() {
void SerialExecutor::keepAliveRelease() noexcept {
auto keepAliveCounter =
keepAliveCounter_.fetch_sub(1, std::memory_order_acq_rel);
DCHECK(keepAliveCounter > 0);
......
......@@ -101,9 +101,9 @@ class SerialExecutor : public SequencedExecutor {
}
protected:
bool keepAliveAcquire() override;
bool keepAliveAcquire() noexcept override;
void keepAliveRelease() override;
void keepAliveRelease() noexcept override;
private:
struct Task {
......
......@@ -196,14 +196,14 @@ uint8_t StrandExecutor::getNumPriorities() const {
return parent_->getNumPriorities();
}
bool StrandExecutor::keepAliveAcquire() {
bool StrandExecutor::keepAliveAcquire() noexcept {
[[maybe_unused]] auto oldCount =
refCount_.fetch_add(1, std::memory_order_relaxed);
DCHECK(oldCount > 0);
return true;
}
void StrandExecutor::keepAliveRelease() {
void StrandExecutor::keepAliveRelease() noexcept {
const auto oldCount = refCount_.fetch_sub(1, std::memory_order_acq_rel);
DCHECK(oldCount > 0);
if (oldCount == 1) {
......
......@@ -153,8 +153,8 @@ class StrandExecutor final : public SequencedExecutor {
uint8_t getNumPriorities() const override;
protected:
bool keepAliveAcquire() override;
void keepAliveRelease() override;
bool keepAliveAcquire() noexcept override;
void keepAliveRelease() noexcept override;
private:
explicit StrandExecutor(
......
......@@ -64,14 +64,14 @@ void TimekeeperScheduledExecutor::scheduleAt(
}
}
bool TimekeeperScheduledExecutor::keepAliveAcquire() {
bool TimekeeperScheduledExecutor::keepAliveAcquire() noexcept {
auto keepAliveCounter =
keepAliveCounter_.fetch_add(1, std::memory_order_relaxed);
DCHECK(keepAliveCounter > 0);
return true;
}
void TimekeeperScheduledExecutor::keepAliveRelease() {
void TimekeeperScheduledExecutor::keepAliveRelease() noexcept {
auto keepAliveCounter =
keepAliveCounter_.fetch_sub(1, std::memory_order_acq_rel);
DCHECK(keepAliveCounter > 0);
......
......@@ -51,8 +51,8 @@ class TimekeeperScheduledExecutor : public ScheduledExecutor {
override;
protected:
bool keepAliveAcquire() override;
void keepAliveRelease() override;
bool keepAliveAcquire() noexcept override;
void keepAliveRelease() noexcept override;
private:
TimekeeperScheduledExecutor(
......
......@@ -324,14 +324,14 @@ class BlockingWaitExecutor final : public folly::DrivableExecutor {
}
private:
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
auto keepAliveCount =
keepAliveCount_.fetch_add(1, std::memory_order_relaxed);
DCHECK(keepAliveCount >= 0);
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
auto keepAliveCount = keepAliveCount_.load(std::memory_order_relaxed);
do {
DCHECK(keepAliveCount > 0);
......
......@@ -148,12 +148,12 @@ TEST_F(CoroTest, ExecutorKeepAlive) {
}
struct CountingExecutor : public ManualExecutor {
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
++keepAliveCounter;
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
--keepAliveCounter;
}
......
......@@ -553,14 +553,14 @@ class WaitExecutor final : public folly::Executor {
private:
WaitExecutor() {}
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
auto keepAliveCount =
keepAliveCount_.fetch_add(1, std::memory_order_relaxed);
DCHECK(keepAliveCount > 0);
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
auto keepAliveCount =
keepAliveCount_.fetch_sub(1, std::memory_order_acq_rel);
DCHECK(keepAliveCount > 0);
......
......@@ -815,7 +815,7 @@ class EventBase : public TimeoutManager,
static std::unique_ptr<EventBaseBackendBase> getDefaultBackend();
protected:
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
if (inRunningEventBaseThread()) {
loopKeepAliveCount_++;
} else {
......@@ -824,7 +824,7 @@ class EventBase : public TimeoutManager,
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
if (!inRunningEventBaseThread()) {
return add([this] { loopKeepAliveCount_--; });
}
......
......@@ -65,11 +65,11 @@ class ScopedEventBaseThread : public IOExecutor, public SequencedExecutor {
}
protected:
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
return getEventBase()->keepAliveAcquire();
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
getEventBase()->keepAliveRelease();
}
......
......@@ -27,7 +27,7 @@ std::future<void> VirtualEventBase::destroy() {
return std::move(destroyFuture_);
}
void VirtualEventBase::destroyImpl() {
void VirtualEventBase::destroyImpl() noexcept {
try {
{
// After destroyPromise_ is posted this object may be destroyed, so make
......
......@@ -124,7 +124,7 @@ class VirtualEventBase : public folly::TimeoutManager,
}
protected:
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
if (evb_->inRunningEventBaseThread()) {
DCHECK(loopKeepAliveCount_ + loopKeepAliveCountAtomic_.load() > 0);
......@@ -135,7 +135,7 @@ class VirtualEventBase : public folly::TimeoutManager,
return true;
}
void keepAliveReleaseEvb() {
void keepAliveReleaseEvb() noexcept {
if (loopKeepAliveCountAtomic_.load()) {
loopKeepAliveCount_ += loopKeepAliveCountAtomic_.exchange(0);
}
......@@ -145,9 +145,9 @@ class VirtualEventBase : public folly::TimeoutManager,
}
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
if (!evb_->inRunningEventBaseThread()) {
evb_->add([=] { keepAliveReleaseEvb(); });
evb_->add([this] { keepAliveReleaseEvb(); });
return;
}
......@@ -165,7 +165,7 @@ class VirtualEventBase : public folly::TimeoutManager,
}
std::future<void> destroy();
void destroyImpl();
void destroyImpl() noexcept;
using LoopCallbackList = EventBase::LoopCallback::List;
......
......@@ -69,7 +69,7 @@ class AsyncioExecutor : public DrivableExecutor, public SequencedExecutor {
}
protected:
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
auto keepAliveCounter =
keepAliveCounter_.fetch_add(1, std::memory_order_relaxed);
// We should never increment from 0
......@@ -77,7 +77,7 @@ class AsyncioExecutor : public DrivableExecutor, public SequencedExecutor {
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
auto keepAliveCounter = --keepAliveCounter_;
DCHECK(keepAliveCounter >= 0);
}
......
......@@ -27,12 +27,12 @@ class KeepAliveTestExecutor : public Executor {
// this executor does nothing
}
bool keepAliveAcquire() override {
bool keepAliveAcquire() noexcept override {
++refCount;
return true;
}
void keepAliveRelease() override {
void keepAliveRelease() noexcept override {
--refCount;
}
......
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