sim: Add generic EventFunctionWrapper
authorSean Wilson <spwilson2@wisc.edu>
Tue, 6 Jun 2017 21:05:48 +0000 (16:05 -0500)
committerSean Wilson <spwilson2@wisc.edu>
Tue, 20 Jun 2017 18:03:21 +0000 (18:03 +0000)
Add EventFunctionWrapper, an event wrapper which takes any callable
object to use as its callback. (This includes c++ lambdas, function
pointers, bound functions, and std::functions.)

Change-Id: Iab140df47bd0f7e4b3fe3b568f9dd122a43cee1c
Signed-off-by: Sean Wilson <spwilson2@wisc.edu>
Reviewed-on: https://gem5-review.googlesource.com/3743
Reviewed-by: Anthony Gutierrez <anthony.gutierrez@amd.com>
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Anthony Gutierrez <anthony.gutierrez@amd.com>

src/sim/eventq.hh

index 9d3c5c36a3d52d3e5eb4e9af196de60e09ff3159..6d68b4e3aa6f18d3a24e943cb6cf55fc1b18ca80 100644 (file)
@@ -800,4 +800,32 @@ class EventWrapper : public Event
     const char *description() const { return "EventWrapped"; }
 };
 
+class EventFunctionWrapper : public Event
+{
+  private:
+      std::function<void(void)> callback;
+      std::string _name;
+
+  public:
+    EventFunctionWrapper(const std::function<void(void)> &callback,
+                         const std::string &name,
+                         bool del = false,
+                         Priority p = Default_Pri)
+        : Event(p), callback(callback), _name(name)
+    {
+        if (del)
+            setFlags(AutoDelete);
+    }
+
+    void process() { callback(); }
+
+    const std::string
+    name() const
+    {
+        return _name + ".wrapped_function_event";
+    }
+
+    const char *description() const { return "EventFunctionWrapped"; }
+};
+
 #endif // __SIM_EVENTQ_HH__