call static function directly
[folly.git] / folly / futures / ManualExecutor.h
index afdf059743e2e9aa9d0c4c04fcac90a7e48c606b..a37b92ff1fd553623d51faca96754d82a5d76e28 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 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.
@@ -93,6 +93,21 @@ namespace folly {
 
     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_;
@@ -103,7 +118,7 @@ namespace folly {
     struct ScheduledFunc {
       TimePoint time;
       size_t ordinal;
-      Func func;
+      Func mutable func;
 
       ScheduledFunc(TimePoint const& t, Func&& f)
         : time(t), func(std::move(f))
@@ -117,9 +132,13 @@ namespace folly {
           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();
   };
 
 }