SST  15.1.0
StructuralSimulationToolkit
oneshotManager.h
1 // Copyright 2009-2025 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-2025, 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_ONESHOTMANAGER_H
13 #define SST_CORE_ONESHOTMANAGER_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 #include <map>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 namespace SST {
26 
27 class Simulation_impl;
28 
29 namespace Core {
30 
31 // Forward declare OneShotManager
32 class OneShotManager;
33 
34 // General using for these classes
35 using TimeStamp_t = std::pair<SimTime_t, int>;
36 
37 
38 class OneShot : public Action
39 {
40 public:
41 
42  /**
43  Base handler for OneShot callbacks.
44  */
46 
47  /**
48  Used to create checkpointable handlers for OneShot. The callback function is
49  expected to be in the form of:
50 
51  void func()
52 
53  In which case, the class is created with:
54 
55  new OneShot::Handler2<classname, &classname::function_name>(this)
56 
57  Or, to add static data, the callback function is:
58 
59  void func(dataT data)
60 
61  and the class is created with:
62 
63  new OneShot::Handler2<classname, &classname::function_name, dataT>(this, data)
64  */
65  template <typename classT, auto funcT, typename dataT = void>
67 
68  using HandlerList_t = std::vector<OneShot::HandlerBase*>;
69 
70  OneShotManager* manager;
71  HandlerList_t& handlers;
72  TimeStamp_t time;
73 
74  OneShot(TimeStamp_t time, OneShotManager* manager, HandlerList_t& handlers) :
75  manager(manager),
76  handlers(handlers),
77  time(time)
78  {
79  setDeliveryTime(time.first);
80  setPriority(time.second);
81  }
82 
83  ~OneShot() {}
84 
85  void execute() override;
86 };
87 
88 
89 /**
90  Manages the OneShot actions for the core. This allows handlers to
91  be registered with the core that will be called at a specific time
92  and priority.
93 */
95 {
96 
97  using HandlerVectorMap_t = std::map<TimeStamp_t, std::pair<OneShot::HandlerList_t, bool>>;
98 
99 public:
100 
101  /////////////////////////////////////////////////
102 
103  /** Create a new One Shot for a specified time that will callback the
104  handler function.
105  Note: OneShot cannot be canceled, and will always callback after
106  the timedelay.
107  */
109  ~OneShotManager();
110 
111  /** Add a handler to be called on this OneShot Event */
112  template <class classT, auto funcT, typename dataT>
113  void registerRelativeHandler(SimTime_t trigger_time, int priority, classT* obj, dataT metadata)
114  {
116  registerHandlerBase(trigger_time, priority, true, handler);
117  }
118 
119  /** Add a handler to be called on this OneShot Event */
120  template <class classT, auto funcT>
121  void registerRelativeHandler(SimTime_t trigger_time, int priority, classT* obj)
122  {
124  registerHandlerBase(trigger_time, priority, true, handler);
125  }
126 
127  /** Add a handler to be called on this OneShot Event */
128  template <class classT, auto funcT, typename dataT>
129  void registerAbsoluteHandler(SimTime_t trigger_time, int priority, classT* obj, dataT metadata)
130  {
132  registerHandlerBase(trigger_time, priority, false, handler);
133  }
134 
135  /** Add a handler to be called on this OneShot Event */
136  template <class classT, auto funcT>
137  void registerAbsoluteHandler(SimTime_t trigger_time, int priority, classT* obj)
138  {
140  registerHandlerBase(trigger_time, priority, false, handler);
141  }
142 
143 
144 private:
145  friend class OneShot;
146 
147  HandlerVectorMap_t handler_vector_map_;
148  Simulation_impl* sim_ = nullptr;
149 
150  /**
151  Registers a handler for delivery at the specified time
152  */
153  void registerHandlerBase(SimTime_t trigger_time, int priority, bool relative, OneShot::HandlerBase* handler);
154 
155  /**
156  Schedules the first entry in handler_vector_map_ if it is not
157  already scheduled.
158  */
159  void scheduleNextOneShot();
160 
161  // Function that will be called when a oneshot is done calling its
162  // handlers. This allows the OneShotManager to clean things up
163  // and schedule the next OneShot
164  void oneshotCallback(TimeStamp_t time, OneShot* oneshot);
165 };
166 
167 } // namespace Core
168 } // namespace SST
169 
170 #endif // SST_CORE_ONESHOTMANAGER_H
An Action is a schedulable Activity which is not an Event.
Definition: action.h:26
void setDeliveryTime(SimTime_t time)
Set the time for which this Activity should be delivered.
Definition: activity.h:147
void registerAbsoluteHandler(SimTime_t trigger_time, int priority, classT *obj)
Add a handler to be called on this OneShot Event.
Definition: oneshotManager.h:137
Base template for the class.
Definition: ssthandler.h:1273
Manages the OneShot actions for the core.
Definition: oneshotManager.h:94
void registerAbsoluteHandler(SimTime_t trigger_time, int priority, classT *obj, dataT metadata)
Add a handler to be called on this OneShot Event.
Definition: oneshotManager.h:129
void setPriority(uint64_t priority)
Set the priority of the Activity.
Definition: activity.h:200
Definition: action.cc:18
void execute() override
Function which will be called when the time for this Activity comes to pass.
Definition: oneshotManager.cc:24
Definition: oneshotManager.h:38
Base template for handlers which don&#39;t take a class defined argument.
Definition: ssthandler.h:702
Main control class for a SST Simulation.
Definition: simulation_impl.h:122
void registerRelativeHandler(SimTime_t trigger_time, int priority, classT *obj)
Add a handler to be called on this OneShot Event.
Definition: oneshotManager.h:121
void registerRelativeHandler(SimTime_t trigger_time, int priority, classT *obj, dataT metadata)
Add a handler to be called on this OneShot Event.
Definition: oneshotManager.h:113
OneShotManager(Simulation_impl *sim)
Create a new One Shot for a specified time that will callback the handler function.
Definition: oneshotManager.cc:37