Let keep-alive tokens be destroyed from any thread
authorYedidya Feldblum <yfeldblum@fb.com>
Tue, 3 Oct 2017 02:45:55 +0000 (19:45 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 3 Oct 2017 02:52:30 +0000 (19:52 -0700)
Summary: [Folly] Let keep-alive tokens be destroyed from any thread.

Reviewed By: andriigrynenko

Differential Revision: D5951397

fbshipit-source-id: 91e72588de4ef33a730ebef5770e77635d4e93ba

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

index 24ca6c2b65db96d1f5034a878c36274d13a61803..a66777f4a85840133d4b318ab38ea5f11a08c5c0 100644 (file)
@@ -81,8 +81,7 @@ class Executor {
   };
 
   /// Returns a keep-alive token which guarantees that Executor will keep
-  /// processing tasks until the token is released. keep-alive token can only
-  /// be destroyed from within the task, scheduled to be run on an executor.
+  /// processing tasks until the token is released.
   ///
   /// If executor does not support keep-alive functionality - dummy token will
   /// be returned.
index 303fd42f809944311bd22240250c15355457bb76..6746aa49ae43f05223685e984b6b8e053337b71f 100644 (file)
@@ -604,8 +604,6 @@ class EventBase : private boost::noncopyable,
 
   /// Implements the DrivableExecutor interface
   void drive() override {
-    // We can't use loopKeepAlive() here since LoopKeepAlive token can only be
-    // released inside a loop.
     ++loopKeepAliveCount_;
     SCOPE_EXIT {
       --loopKeepAliveCount_;
@@ -615,8 +613,7 @@ class EventBase : private boost::noncopyable,
 
   /// Returns you a handle which make loop() behave like loopForever() until
   /// destroyed. loop() will return to its original behavior only when all
-  /// loop keep-alives are released. Loop holder is safe to release only from
-  /// EventBase thread.
+  /// loop keep-alives are released.
   KeepAlive getKeepAliveToken() override {
     if (inRunningEventBaseThread()) {
       loopKeepAliveCount_++;
@@ -653,8 +650,11 @@ class EventBase : private boost::noncopyable,
 
  protected:
   void keepAliveRelease() override {
-    dcheckIsInEventBaseThread();
-    loopKeepAliveCount_--;
+    if (inRunningEventBaseThread()) {
+      loopKeepAliveCount_--;
+    } else {
+      add([=] { loopKeepAliveCount_--; });
+    }
   }
 
  private: