SST  11.1.0
StructuralSimulationToolkit
oneshot.h
1 // Copyright 2009-2021 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2021, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_ONESHOT_H
13 #define SST_CORE_ONESHOT_H
14 
15 #include "sst/core/action.h"
16 #include "sst/core/sst_types.h"
17 #include "sst/core/ssthandler.h"
18 
19 #include <cinttypes>
20 
21 #define _ONESHOT_DBG(fmt, args...) __DBG(DBG_ONESHOT, OneShot, fmt, ##args)
22 
23 namespace SST {
24 
25 class TimeConverter;
26 
27 /**
28  * A OneShot Event class.
29  *
30  * Calls callback functions (handlers) on a specified period
31  */
32 class OneShot : public Action
33 {
34 public:
35  /**
36  Base handler for OneShot callbacks.
37  */
39 
40  /**
41  Used to create handlers for clock. The callback function is
42  expected to be in the form of:
43 
44  void func()
45 
46  In which case, the class is created with:
47 
48  new OneShot::Handler<classname>(this, &classname::function_name)
49 
50  Or, to add static data, the callback function is:
51 
52  void func(dataT data)
53 
54  and the class is created with:
55 
56  new OneShot::Handler<classname, dataT>(this, &classname::function_name, data)
57  */
58  template <typename classT, typename dataT = void>
60 
61 
62  /////////////////////////////////////////////////
63 
64  /** Create a new One Shot for a specified time that will callback the
65  handler function.
66  Note: OneShot cannot be canceled, and will always callback after
67  the timedelay.
68  */
69  OneShot(TimeConverter* timeDelay, int priority = ONESHOTPRIORITY);
70  ~OneShot();
71 
72  /** Is OneShot scheduled */
73  bool isScheduled() { return m_scheduled; }
74 
75  /** Add a handler to be called on this OneShot Event */
77 
78  /** Print details about the OneShot */
79  void print(const std::string& header, Output& out) const override;
80 
81 private:
82  typedef std::vector<OneShot::HandlerBase*> HandlerList_t;
83 
84  // Since this only gets fixed latency events, the times will fire
85  // in order of arrival. No need to use a full map, a double ended
86  // queue will work just as well
87  // typedef std::map<SimTime_t, HandlerList_t*> HandlerVectorMap_t;
88  typedef std::deque<std::pair<SimTime_t, HandlerList_t*>> HandlerVectorMap_t;
89 
90  // Generic constructor for serialization
91  OneShot() {}
92 
93  // Called by the Simulation (Activity Queue) when delay time as elapsed
94  void execute(void) override;
95 
96  // Activates this OneShot object, by inserting into the simulation's
97  // timeVortex for future execution.
98  void scheduleOneShot();
99  SimTime_t computeDeliveryTime();
100 
101  TimeConverter* m_timeDelay;
102  HandlerVectorMap_t m_HandlerVectorMap;
103  bool m_scheduled;
104 };
105 
106 } // namespace SST
107 
108 #endif // SST_CORE_ONESHOT_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:51
An Action is a schedulable Activity which is not an Event.
Definition: action.h:26
A class to convert between a component&#39;s view of time and the core&#39;s view of time.
Definition: timeConverter.h:26
void print(const std::string &header, Output &out) const override
Print details about the OneShot.
Definition: oneshot.cc:140
bool isScheduled()
Is OneShot scheduled.
Definition: oneshot.h:73
Handler with no arguments to callback from caller.
Definition: ssthandler.h:167
void registerHandler(OneShot::HandlerBase *handler)
Add a handler to be called on this OneShot Event.
Definition: oneshot.cc:49
Event Handler class with user-data argument.
Definition: ssthandler.h:181
A OneShot Event class.
Definition: oneshot.h:32