Commit b6861c02 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Move keepalive-acquire code into Executor::keepAliveAcquire overrides

Summary:
[Folly] Move keepalive-acquire code into `Executor::keepAliveAcquire` overrides.

This makes the API more symmetric and allows for more flexibility. Also serves as preparation for generalizing the keepalive-token interface.

Reviewed By: andriigrynenko

Differential Revision: D5974080

fbshipit-source-id: 26209e49a0f5834ba229d4bbfc9272c2e4ffb3fd
parent f20581a4
...@@ -27,8 +27,13 @@ void Executor::addWithPriority(Func, int8_t /* priority */) { ...@@ -27,8 +27,13 @@ void Executor::addWithPriority(Func, int8_t /* priority */) {
"addWithPriority() is not implemented for this Executor"); "addWithPriority() is not implemented for this Executor");
} }
void Executor::keepAliveAcquire() {
LOG(FATAL) << __func__ << "() should not be called for folly::Executor types "
<< "which do not override getKeepAliveToken()";
}
void Executor::keepAliveRelease() { void Executor::keepAliveRelease() {
LOG(FATAL) << "keepAliveRelease() should not be called for folly::Executors " LOG(FATAL) << __func__ << "() should not be called for folly::Executor types "
<< "which do not implement getKeepAliveToken()"; << "which do not override getKeepAliveToken()";
} }
} }
...@@ -90,6 +90,7 @@ class Executor { ...@@ -90,6 +90,7 @@ class Executor {
} }
protected: protected:
virtual void keepAliveAcquire();
virtual void keepAliveRelease(); virtual void keepAliveRelease();
KeepAlive makeKeepAlive() { KeepAlive makeKeepAlive() {
......
...@@ -615,11 +615,7 @@ class EventBase : private boost::noncopyable, ...@@ -615,11 +615,7 @@ class EventBase : private boost::noncopyable,
/// destroyed. loop() will return to its original behavior only when all /// destroyed. loop() will return to its original behavior only when all
/// loop keep-alives are released. /// loop keep-alives are released.
KeepAlive getKeepAliveToken() override { KeepAlive getKeepAliveToken() override {
if (inRunningEventBaseThread()) { keepAliveAcquire();
loopKeepAliveCount_++;
} else {
loopKeepAliveCountAtomic_.fetch_add(1, std::memory_order_relaxed);
}
return makeKeepAlive(); return makeKeepAlive();
} }
...@@ -649,6 +645,14 @@ class EventBase : private boost::noncopyable, ...@@ -649,6 +645,14 @@ class EventBase : private boost::noncopyable,
folly::VirtualEventBase& getVirtualEventBase(); folly::VirtualEventBase& getVirtualEventBase();
protected: protected:
void keepAliveAcquire() override {
if (inRunningEventBaseThread()) {
loopKeepAliveCount_++;
} else {
loopKeepAliveCountAtomic_.fetch_add(1, std::memory_order_relaxed);
}
}
void keepAliveRelease() override { void keepAliveRelease() override {
if (inRunningEventBaseThread()) { if (inRunningEventBaseThread()) {
loopKeepAliveCount_--; loopKeepAliveCount_--;
......
...@@ -120,6 +120,16 @@ class VirtualEventBase : public folly::Executor, public folly::TimeoutManager { ...@@ -120,6 +120,16 @@ class VirtualEventBase : public folly::Executor, public folly::TimeoutManager {
* Returns you a handle which prevents VirtualEventBase from being destroyed. * Returns you a handle which prevents VirtualEventBase from being destroyed.
*/ */
KeepAlive getKeepAliveToken() override { KeepAlive getKeepAliveToken() override {
keepAliveAcquire();
return makeKeepAlive();
}
bool inRunningEventBaseThread() const {
return evb_.inRunningEventBaseThread();
}
protected:
void keepAliveAcquire() override {
DCHECK(loopKeepAliveCount_ + loopKeepAliveCountAtomic_.load() > 0); DCHECK(loopKeepAliveCount_ + loopKeepAliveCountAtomic_.load() > 0);
if (evb_.inRunningEventBaseThread()) { if (evb_.inRunningEventBaseThread()) {
...@@ -127,14 +137,8 @@ class VirtualEventBase : public folly::Executor, public folly::TimeoutManager { ...@@ -127,14 +137,8 @@ class VirtualEventBase : public folly::Executor, public folly::TimeoutManager {
} else { } else {
++loopKeepAliveCountAtomic_; ++loopKeepAliveCountAtomic_;
} }
return makeKeepAlive();
} }
bool inRunningEventBaseThread() const {
return evb_.inRunningEventBaseThread();
}
protected:
void keepAliveRelease() override { void keepAliveRelease() override {
if (!getEventBase().inRunningEventBaseThread()) { if (!getEventBase().inRunningEventBaseThread()) {
return getEventBase().add([=] { keepAliveRelease(); }); return getEventBase().add([=] { keepAliveRelease(); });
......
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