SST  13.0.0
StructuralSimulationToolkit
clock.h
1 // Copyright 2009-2023 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-2023, 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  * Activates this clock object, by inserting into the simulation's
72  * timeVortex for future execution.
73  */
74  void schedule();
75 
76  /** Return the time of the next clock tick */
77  Cycle_t getNextCycle();
78 
79  /**
80  * Update current cycle count - needed at simulation end if clock has run
81  * ahead of simulation end and to return correct cycle count in getNextCycle()
82  * for clocks that are currently not scheduled
83  */
84  void updateCurrentCycle();
85 
86  /** Add a handler to be called on this clock's tick */
87  bool registerHandler(Clock::HandlerBase* handler);
88  /** Remove a handler from the list of handlers to be called on the clock tick */
89  bool unregisterHandler(Clock::HandlerBase* handler, bool& empty);
90 
91  std::string toString() const override;
92 
93 private:
94  /* typedef std::list<Clock::HandlerBase*> HandlerMap_t; */
95  typedef std::vector<Clock::HandlerBase*> StaticHandlerMap_t;
96 
97  Clock() {}
98 
99  void execute(void) override;
100 
101  Cycle_t currentCycle;
102  TimeConverter* period;
103  StaticHandlerMap_t staticHandlerMap;
104  SimTime_t next;
105  bool scheduled;
106 
107  NotSerializable(SST::Clock)
108 };
109 
110 
112 {
113 public:
114  const ComponentId_t comp_id;
115  const std::string comp_name;
116  const std::string comp_type;
117 
118  ClockHandlerMetaData(ComponentId_t id, const std::string& cname, const std::string& ctype) :
119  comp_id(id),
120  comp_name(cname),
121  comp_type(ctype)
122  {}
123 
125 };
126 
127 
128 } // namespace SST
129 
130 #endif // SST_CORE_CLOCK_H
An Action is a schedulable Activity which is not an Event.
Definition: action.h:26
Handlers with 1 handler defined argument to callback from caller.
Definition: ssthandler.h:170
Handler class with user-data argument.
Definition: ssthandler.h:219
A class to convert between a component&#39;s view of time and the core&#39;s view of time.
Definition: timeConverter.h:26
Definition: action.cc:18
Cycle_t getNextCycle()
Return the time of the next clock tick.
Definition: clock.cc:65
void updateCurrentCycle()
Update current cycle count - needed at simulation end if clock has run ahead of simulation end and to...
Definition: clock.cc:129
bool registerHandler(Clock::HandlerBase *handler)
Add a handler to be called on this clock&#39;s tick.
Definition: clock.cc:39
Definition: clock.h:111
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:27
std::string toString() const override
Get a string represenation of the event.
Definition: clock.cc:137
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:107