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