SST 15.0
Structural Simulation Toolkit
oneshot.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_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#include <deque>
21#include <string>
22#include <vector>
23
24#define _ONESHOT_DBG(fmt, args...) __DBG(DBG_ONESHOT, OneShot, fmt, ##args)
25
26namespace SST {
27
28class TimeConverter;
29
30/**
31 * A OneShot Event class.
32 *
33 * Calls callback functions (handlers) on a specified period
34 */
35class OneShot : public Action
36{
37public:
38 /**
39 Base handler for OneShot callbacks.
40 */
41 using HandlerBase = SSTHandlerBaseNoArgs<void>;
42
43 /**
44 Used to create handlers for OneShot. The callback function is
45 expected to be in the form of:
46
47 void func()
48
49 In which case, the class is created with:
50
51 new OneShot::Handler<classname>(this, &classname::function_name)
52
53 Or, to add static data, the callback function is:
54
55 void func(dataT data)
56
57 and the class is created with:
58
59 new OneShot::Handler<classname, dataT>(this, &classname::function_name, data)
60 */
61 template <typename classT, typename dataT = void>
62 using Handler
63 [[deprecated("Handler has been deprecated. Please use Handler2 instead as it supports checkpointing.")]] =
65
66 /**
67 Used to create checkpointable handlers for OneShot. The callback function is
68 expected to be in the form of:
69
70 void func()
71
72 In which case, the class is created with:
73
74 new OneShot::Handler2<classname, &classname::function_name>(this)
75
76 Or, to add static data, the callback function is:
77
78 void func(dataT data)
79
80 and the class is created with:
81
82 new OneShot::Handler2<classname, &classname::function_name, dataT>(this, data)
83 */
84 template <typename classT, auto funcT, typename dataT = void>
86
87
88 /////////////////////////////////////////////////
89
90 /** Create a new One Shot for a specified time that will callback the
91 handler function.
92 Note: OneShot cannot be canceled, and will always callback after
93 the timedelay.
94 */
95 OneShot(TimeConverter* timeDelay, int priority = ONESHOTPRIORITY);
96 ~OneShot();
97
98 /** Is OneShot scheduled */
99 bool isScheduled() { return m_scheduled; }
100
101 /** Add a handler to be called on this OneShot Event */
103
104 /** Print details about the OneShot */
105 void print(const std::string& header, Output& out) const override;
106
107 NotSerializable(SST::OneShot)
108
109private:
110 using HandlerList_t = std::vector<OneShot::HandlerBase*>;
111
112 // Since this only gets fixed latency events, the times will fire
113 // in order of arrival. No need to use a full map, a double ended
114 // queue will work just as well
115 // using HandlerVectorMap_t = std::map<SimTime_t, HandlerList_t*>;
116 using HandlerVectorMap_t = std::deque<std::pair<SimTime_t, HandlerList_t*>>;
117
118 // Generic constructor for serialization
119 OneShot() {}
120
121 // Called by the Simulation (Activity Queue) when delay time as elapsed
122 void execute() override;
123
124 // Activates this OneShot object, by inserting into the simulation's
125 // timeVortex for future execution.
126 void scheduleOneShot();
127 SimTime_t computeDeliveryTime();
128
129 TimeConverter* m_timeDelay;
130 HandlerVectorMap_t m_HandlerVectorMap;
131 bool m_scheduled;
132};
133
134} // namespace SST
135
136#endif // SST_CORE_ONESHOT_H
A OneShot Event class.
Definition oneshot.h:36
SSTHandler2< void, void, classT, dataT, funcT > Handler2
Used to create checkpointable handlers for OneShot.
Definition oneshot.h:85
OneShot(TimeConverter *timeDelay, int priority=ONESHOTPRIORITY)
Create a new One Shot for a specified time that will callback the handler function.
Definition oneshot.cc:21
void registerHandler(OneShot::HandlerBase *handler)
Add a handler to be called on this OneShot Event.
Definition oneshot.cc:52
void print(const std::string &header, Output &out) const override
Print details about the OneShot.
Definition oneshot.cc:143
bool isScheduled()
Is OneShot scheduled.
Definition oneshot.h:99
void execute() override
Function which will be called when the time for this Activity comes to pass.
Definition oneshot.cc:105
SSTHandlerBaseNoArgs< void > HandlerBase
Base handler for OneShot callbacks.
Definition oneshot.h:41
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:54
Base template for the class.
Definition ssthandler.h:1274
Event Handler class with user-data argument.
Definition ssthandler.h:1202
A class to convert between a component's view of time and the core's view of time.
Definition timeConverter.h:28