SST 16.0.0
Structural Simulation Toolkit
clock.h
1// Copyright 2009-2026 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-2026, 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_CLOCK_H
13#define SST_CORE_CLOCK_H
14
15#include "sst/core/action.h"
16#include "sst/core/ssthandler.h"
17#include "sst/core/timeConverter.h"
18
19#include <cinttypes>
20#include <string>
21#include <vector>
22
23#define _CLE_DBG(fmt, args...) __DBG(DBG_CLOCK, Clock, fmt, ##args)
24
25namespace SST {
26
27class TimeConverter;
28
29/**
30 * A Clock class.
31 *
32 * Calls callback functions (handlers) on a specified period
33 */
34class Clock : public Action
35{
36public:
37 /** Create a new clock with a specified period */
38 Clock(TimeConverter period, int priority = CLOCKPRIORITY);
39 ~Clock() = default; // Handlers are owned by BaseComponent and are deleted there
40
41 /**
42 Base handler for clock functions.
43 */
45
46 /**
47 Used to create handlers for clock. The callback function is
48 expected to be in the form of:
49
50 bool func(Cycle_t cycle)
51
52 In which case, the class is created with:
53
54 new Clock::Handler<classname, &classname::function_name>(this)
55
56 Or, to add static data, the callback function is:
57
58 bool func(Cycle_t cycle, dataT data)
59
60 and the class is created with:
61
62 new Clock::Handler<classname, &classname::function_name, dataT>(this, data)
63
64 In both cases, the boolean that's returned indicates whether
65 the handler should be removed from the list or not. On return
66 of true, the handler will be removed. On return of false, the
67 handler will be left in the clock list.
68 */
69 template <typename classT, auto funcT, typename dataT = void>
71
72 /**
73 Handler2 version which is now the same as Handler and is provided for backward compatibility until SST 17
74 */
75 template <typename classT, auto funcT, typename dataT = void>
76 using Handler2 [[deprecated(
77 "The name Handler2 has been deprecated and will be removed in SST 17. Please rename Handler2 to Handler.")]]
79
80 /**
81 * Activates this clock object, by inserting into the simulation's
82 * timeVortex for future execution.
83 */
84 void schedule();
85
86 /** Return the time of the next clock tick */
87 Cycle_t getNextCycle();
88
89 /**
90 * Update current cycle count - needed at simulation end if clock has run
91 * ahead of simulation end and to return correct cycle count in getNextCycle()
92 * for clocks that are currently not scheduled
93 */
94 void updateCurrentCycle();
95
96 /** Add a handler to be called on this clock's tick */
98 /** Remove a handler from the list of handlers to be called on the clock tick */
99 bool unregisterHandler(Clock::HandlerBase* handler, bool& empty);
100
101 /**
102 Checks to see if a handler is registered with this clock
103 */
105
106 std::string toString() const override;
107
108private:
109 /* using HandlerMap_t = std::list<Clock::HandlerBase*>; */
110 using StaticHandlerMap_t = std::vector<Clock::HandlerBase*>;
111
112 Clock() {}
113
114 Clock(const Clock&) = delete;
115 Clock& operator=(const Clock&) = delete;
116
117 void execute() override;
118
119 Cycle_t currentCycle;
120 TimeConverter period;
121 StaticHandlerMap_t staticHandlerMap;
122 SimTime_t next;
123 bool scheduled;
124
125 void serialize_order(SST::Core::Serialization::serializer& ser) override;
126 ImplementSerializable(SST::Clock)
127};
128
129
130class ClockHandlerMetaData : public AttachPointMetaData
131{
132public:
133 const ComponentId_t comp_id;
134 const std::string comp_name;
135 const std::string comp_type;
136
137 ClockHandlerMetaData(ComponentId_t id, const std::string& cname, const std::string& ctype) :
138 comp_id(id),
139 comp_name(cname),
140 comp_type(ctype)
141 {}
142
143 ~ClockHandlerMetaData() {}
144};
145
146
147} // namespace SST
148
149#endif // SST_CORE_CLOCK_H
virtual void execute()=0
Function which will be called when the time for this Activity comes to pass.
A Clock class.
Definition clock.h:35
Cycle_t getNextCycle()
Return the time of the next clock tick.
Definition clock.cc:74
void schedule()
Activates this clock object, by inserting into the simulation's timeVortex for future execution.
Definition clock.cc:116
SSTHandlerBase< bool, Cycle_t > HandlerBase
Base handler for clock functions.
Definition clock.h:44
std::string toString() const override
Get a string represenation of the event.
Definition clock.cc:150
bool isHandlerRegistered(Clock::HandlerBase *handler)
Checks to see if a handler is registered with this clock.
Definition clock.cc:63
bool unregisterHandler(Clock::HandlerBase *handler, bool &empty)
Remove a handler from the list of handlers to be called on the clock tick.
Definition clock.cc:44
SSTHandler< bool, Cycle_t, classT, dataT, funcT > Handler
Used to create handlers for clock.
Definition clock.h:70
Clock(TimeConverter period, int priority=CLOCKPRIORITY)
Create a new clock with a specified period.
Definition clock.cc:24
bool registerHandler(Clock::HandlerBase *handler)
Add a handler to be called on this clock's tick.
Definition clock.cc:34
void updateCurrentCycle()
Update current cycle count - needed at simulation end if clock has run ahead of simulation end and to...
Definition clock.cc:142
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43
Base template for handlers which take a class defined argument.
Definition ssthandler.h:79
Base template for the class.
Definition ssthandler.h:1102
A class to convert between a component's view of time and the core's view of time.
Definition timeConverter.h:31