SST  15.1.0
StructuralSimulationToolkit
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 
24 namespace SST {
25 
26 class TimeConverter;
27 
28 /**
29  * A Clock class.
30  *
31  * Calls callback functions (handlers) on a specified period
32  */
33 class Clock : public Action
34 {
35 public:
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 */
95  bool registerHandler(Clock::HandlerBase* handler);
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 
106 private:
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 
129 {
130 public:
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 
142 };
143 
144 
145 } // namespace SST
146 
147 #endif // SST_CORE_CLOCK_H
An Action is a schedulable Activity which is not an Event.
Definition: action.h:26
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
Base template for handlers which take a class defined argument.
Definition: ssthandler.h:109
Base template for the class.
Definition: ssthandler.h:1273
Handler class with user-data argument.
Definition: ssthandler.h:1136
A class to convert between a component&#39;s view of time and the core&#39;s view of time.
Definition: timeConverter.h:27
bool isHandlerRegistered(Clock::HandlerBase *handler)
Checks to see if a handler is registered with this clock.
Definition: clock.cc:68
Definition: action.cc:18
Cycle_t getNextCycle()
Return the time of the next clock tick.
Definition: clock.cc:79
void updateCurrentCycle()
Update current cycle count - needed at simulation end if clock has run ahead of simulation end and to...
Definition: clock.cc:147
bool registerHandler(Clock::HandlerBase *handler)
Add a handler to be called on this clock&#39;s tick.
Definition: clock.cc:39
Definition: clock.h:128
A Clock class.
Definition: clock.h:33
std::string toString() const override
Get a string represenation of the event.
Definition: clock.cc:155
Struct used as a base class for all AttachPoint metadata passed to registration functions.
Definition: sst_types.h:80
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
void schedule()
Activates this clock object, by inserting into the simulation&#39;s timeVortex for future execution...
Definition: clock.cc:121