Move keepalive-acquire code into Executor::keepAliveAcquire overrides
authorYedidya Feldblum <yfeldblum@fb.com>
Wed, 4 Oct 2017 18:56:32 +0000 (11:56 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 4 Oct 2017 19:12:34 +0000 (12:12 -0700)
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

folly/Executor.cpp
folly/Executor.h
folly/io/async/EventBase.h
folly/io/async/VirtualEventBase.h

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