SST  14.1.0
StructuralSimulationToolkit
clock.h
1 // Copyright 2009-2024 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-2024, 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 <vector>
20 
21 #define _CLE_DBG(fmt, args...) __DBG(DBG_CLOCK, Clock, fmt, ##args)
22 
23 namespace SST {
24 
25 class TimeConverter;
26 
27 /**
28  * A Clock class.
29  *
30  * Calls callback functions (handlers) on a specified period
31  */
32 class Clock : public Action
33 {
34 public:
35  /** Create a new clock with a specified period */
36  Clock(TimeConverter* period, int priority = CLOCKPRIORITY);
37  ~Clock();
38 
39  /**
40  Base handler for clock functions.
41  */
43 
44  /**
45  Used to create handlers for clock. The callback function is
46  expected to be in the form of:
47 
48  bool func(Cycle_t cycle)
49 
50  In which case, the class is created with:
51 
52  new Clock::Handler<classname>(this, &classname::function_name)
53 
54  Or, to add static data, the callback function is:
55 
56  bool func(Cycle_t cycle, dataT data)
57 
58  and the class is created with:
59 
60  new Clock::Handler<classname, dataT>(this, &classname::function_name, data)
61 
62  In both cases, the boolean that's returned indicates whether
63  the handler should be removed from the list or not. On return
64  of true, the handler will be removed. On return of false, the
65  handler will be left in the clock list.
66  */
67  template <typename classT, typename dataT = void>
69 
70  /**
71  New style (checkpointable) SSTHandler
72  */
73  template <typename classT, auto funcT, typename dataT = void>
75 
76  /**
77  * Activates this clock object, by inserting into the simulation's
78  * timeVortex for future execution.
79  */
80  void schedule();
81 
82  /** Return the time of the next clock tick */
83  Cycle_t getNextCycle();
84 
85  /**
86  * Update current cycle count - needed at simulation end if clock has run
87  * ahead of simulation end and to return correct cycle count in getNextCycle()
88  * for clocks that are currently not scheduled
89  */
90  void updateCurrentCycle();
91 
92  /** Add a handler to be called on this clock's tick */
93  bool registerHandler(Clock::HandlerBase* handler);
94  /** Remove a handler from the list of handlers to be called on the clock tick */
95  bool unregisterHandler(Clock::HandlerBase* handler, bool& empty);
96 
97  /**
98  Checks to see if a handler is registered with this clock
99  */
101 
102  std::string toString() const override;
103 
104 private:
105  /* typedef std::list<Clock::HandlerBase*> HandlerMap_t; */
106  typedef std::vector<Clock::HandlerBase*> StaticHandlerMap_t;
107 
108  Clock() {}
109 
110  void execute(void) override;
111 
112  Cycle_t currentCycle;
113  TimeConverter* period;
114  StaticHandlerMap_t staticHandlerMap;
115  SimTime_t next;
116  bool scheduled;
117 
118  void serialize_order(SST::Core::Serialization::serializer& ser) override;
119  ImplementSerializable(SST::Clock)
120 };
121 
122 
124 {
125 public:
126  const ComponentId_t comp_id;
127  const std::string comp_name;
128  const std::string comp_type;
129 
130  ClockHandlerMetaData(ComponentId_t id, const std::string& cname, const std::string& ctype) :
131  comp_id(id),
132  comp_name(cname),
133  comp_type(ctype)
134  {}
135 
137 };
138 
139 
140 } // namespace SST
141 
142 #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:43
Handlers with 1 handler defined argument to callback from caller.
Definition: ssthandler.h:210
Base template for the class.
Definition: ssthandler.h:466
Handler class with user-data argument.
Definition: ssthandler.h:284
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:66
Definition: action.cc:18
Cycle_t getNextCycle()
Return the time of the next clock tick.
Definition: clock.cc:77
void updateCurrentCycle()
Update current cycle count - needed at simulation end if clock has run ahead of simulation end and to...
Definition: clock.cc:141
bool registerHandler(Clock::HandlerBase *handler)
Add a handler to be called on this clock&#39;s tick.
Definition: clock.cc:39
Definition: clock.h:123
A Clock class.
Definition: clock.h:32
Just a tag class for the various metadata that will need to be stored in the simulation object to all...
Definition: ssthandler.h:28
std::string toString() const override
Get a string represenation of the event.
Definition: clock.cc:149
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:47
void schedule()
Activates this clock object, by inserting into the simulation&#39;s timeVortex for future execution...
Definition: clock.cc:119