call static function directly
[folly.git] / folly / futures / ManualExecutor.h
index b17a0bdef2772fcb0a70e80a61dcc99225ca2239..a37b92ff1fd553623d51faca96754d82a5d76e28 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 #pragma once
+#include <folly/LifoSem.h>
+#include <folly/futures/DrivableExecutor.h>
 #include <folly/futures/ScheduledExecutor.h>
-#include <semaphore.h>
 #include <memory>
 #include <mutex>
 #include <queue>
 #include <cstdio>
 
-namespace folly { namespace wangle {
+namespace folly {
   /// A ManualExecutor only does work when you turn the crank, by calling
   /// run() or indirectly with makeProgress() or waitFor().
   ///
@@ -31,10 +32,9 @@ namespace folly { namespace wangle {
   ///
   /// NB No attempt has been made to make anything other than add and schedule
   /// threadsafe.
-  class ManualExecutor : public ScheduledExecutor {
+  class ManualExecutor : public DrivableExecutor,
+                         public ScheduledExecutor {
    public:
-    ManualExecutor();
-
     void add(Func) override;
 
     /// Do work. Returns the number of functions that were executed (maybe 0).
@@ -54,6 +54,11 @@ namespace folly { namespace wangle {
       run();
     }
 
+    /// Implements DrivableExecutor
+    void drive() override {
+      makeProgress();
+    }
+
     /// makeProgress until this Future is ready.
     template <class F> void waitFor(F const& f) {
       // TODO(5427828)
@@ -71,7 +76,7 @@ namespace folly { namespace wangle {
     virtual void scheduleAt(Func&& f, TimePoint const& t) override {
       std::lock_guard<std::mutex> lock(lock_);
       scheduledFuncs_.emplace(t, std::move(f));
-      sem_post(&sem_);
+      sem_.post();
     }
 
     /// Advance the clock. The clock never advances on its own.
@@ -88,17 +93,32 @@ namespace folly { namespace wangle {
 
     TimePoint now() override { return now_; }
 
+    /// Flush the function queue. Destroys all stored functions without
+    /// executing them. Returns number of removed functions.
+    std::size_t clear() {
+      std::queue<Func> funcs;
+      std::priority_queue<ScheduledFunc> scheduled_funcs;
+
+      {
+        std::lock_guard<std::mutex> lock(lock_);
+        funcs_.swap(funcs);
+        scheduledFuncs_.swap(scheduled_funcs);
+      }
+
+      return funcs.size() + scheduled_funcs.size();
+    }
+
    private:
     std::mutex lock_;
     std::queue<Func> funcs_;
-    sem_t sem_;
+    LifoSem sem_;
 
     // helper class to enable ordering of scheduled events in the priority
     // queue
     struct ScheduledFunc {
       TimePoint time;
       size_t ordinal;
-      Func func;
+      Func mutable func;
 
       ScheduledFunc(TimePoint const& t, Func&& f)
         : time(t), func(std::move(f))
@@ -112,9 +132,13 @@ namespace folly { namespace wangle {
           return ordinal < b.ordinal;
         return time < b.time;
       }
+
+      Func&& moveOutFunc() const {
+        return std::move(func);
+      }
     };
     std::priority_queue<ScheduledFunc> scheduledFuncs_;
-    TimePoint now_ = now_.min();
+    TimePoint now_ = TimePoint::min();
   };
 
-}}
+}