From 6f3b7616f3402355441c62ca60a36159435aa818 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Mon, 2 Oct 2017 19:45:55 -0700 Subject: [PATCH] Let keep-alive tokens be destroyed from any thread Summary: [Folly] Let keep-alive tokens be destroyed from any thread. Reviewed By: andriigrynenko Differential Revision: D5951397 fbshipit-source-id: 91e72588de4ef33a730ebef5770e77635d4e93ba --- folly/Executor.h | 3 +-- folly/io/async/EventBase.h | 12 ++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/folly/Executor.h b/folly/Executor.h index 24ca6c2b..a66777f4 100644 --- a/folly/Executor.h +++ b/folly/Executor.h @@ -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. diff --git a/folly/io/async/EventBase.h b/folly/io/async/EventBase.h index 303fd42f..6746aa49 100644 --- a/folly/io/async/EventBase.h +++ b/folly/io/async/EventBase.h @@ -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: -- 2.34.1