Revert D4164236: [EventBase] Move runAfterDelay/tryRunAfterDelay into TimeoutManager
[folly.git] / folly / io / async / EventBase.h
index a8d4926f26ac0c05e16c8d6f7b78fe0c05741532..16d406eaa898c205bbcb5f05a2487fdd1d21b713 100644 (file)
@@ -397,6 +397,28 @@ class EventBase : private boost::noncopyable,
    */
   bool runImmediatelyOrRunInEventBaseThreadAndWait(Func fn);
 
+  /**
+   * Runs the given Cob at some time after the specified number of
+   * milliseconds.  (No guarantees exactly when.)
+   *
+   * Throws a std::system_error if an error occurs.
+   */
+  void runAfterDelay(
+      Func c,
+      uint32_t milliseconds,
+      TimeoutManager::InternalEnum in = TimeoutManager::InternalEnum::NORMAL);
+
+  /**
+   * @see tryRunAfterDelay for more details
+   *
+   * @return  true iff the cob was successfully registered.
+   *
+   * */
+  bool tryRunAfterDelay(
+      Func cob,
+      uint32_t milliseconds,
+      TimeoutManager::InternalEnum in = TimeoutManager::InternalEnum::NORMAL);
+
   /**
    * Set the maximum desired latency in us and provide a callback which will be
    * called when that latency is exceeded.
@@ -408,6 +430,7 @@ class EventBase : private boost::noncopyable,
     maxLatencyCob_ = std::move(maxLatencyCob);
   }
 
+
   /**
    * Set smoothing coefficient for loop load average; # of milliseconds
    * for exp(-1) (1/2.71828...) decay.
@@ -579,23 +602,22 @@ class EventBase : private boost::noncopyable,
     return LoopKeepAlive(this);
   }
 
+ private:
   // TimeoutManager
-  void attachTimeoutManager(
-      AsyncTimeout* obj,
-      TimeoutManager::InternalEnum internal) override final;
+  void attachTimeoutManager(AsyncTimeout* obj,
+                            TimeoutManager::InternalEnum internal) override;
 
-  void detachTimeoutManager(AsyncTimeout* obj) override final;
+  void detachTimeoutManager(AsyncTimeout* obj) override;
 
   bool scheduleTimeout(AsyncTimeout* obj, TimeoutManager::timeout_type timeout)
-      override final;
+    override;
 
-  void cancelTimeout(AsyncTimeout* obj) override final;
+  void cancelTimeout(AsyncTimeout* obj) override;
 
   bool isInTimeoutManagerThread() override final {
     return isInEventBaseThread();
   }
 
- private:
   void applyLoopKeepAlive();
 
   /*
@@ -604,6 +626,30 @@ class EventBase : private boost::noncopyable,
    */
   bool nothingHandledYet() const noexcept;
 
+  // small object used as a callback arg with enough info to execute the
+  // appropriate client-provided Cob
+  class CobTimeout : public AsyncTimeout {
+   public:
+    CobTimeout(EventBase* b, Func c, TimeoutManager::InternalEnum in)
+        : AsyncTimeout(b, in), cob_(std::move(c)) {}
+
+    virtual void timeoutExpired() noexcept;
+
+   private:
+    Func cob_;
+
+   public:
+    typedef boost::intrusive::list_member_hook<
+      boost::intrusive::link_mode<boost::intrusive::auto_unlink> > ListHook;
+
+    ListHook hook;
+
+    typedef boost::intrusive::list<
+      CobTimeout,
+      boost::intrusive::member_hook<CobTimeout, ListHook, &CobTimeout::hook>,
+      boost::intrusive::constant_time_size<false> > List;
+  };
+
   typedef LoopCallback::List LoopCallbackList;
   class FunctionRunner;
 
@@ -617,6 +663,8 @@ class EventBase : private boost::noncopyable,
   // should only be accessed through public getter
   HHWheelTimer::UniquePtr wheelTimer_;
 
+  CobTimeout::List pendingCobTimeouts_;
+
   LoopCallbackList loopCallbacks_;
   LoopCallbackList runBeforeLoopCallbacks_;
   LoopCallbackList onDestructionCallbacks_;