SST  12.0.1
StructuralSimulationToolkit
clock.h
1 // Copyright 2009-2022 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-2022, 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  /** Add a handler to be called on this clock's tick */
80  bool registerHandler(Clock::HandlerBase* handler);
81  /** Remove a handler from the list of handlers to be called on the clock tick */
82  bool unregisterHandler(Clock::HandlerBase* handler, bool& empty);
83 
84  std::string toString() const override;
85 
86 private:
87  /* typedef std::list<Clock::HandlerBase*> HandlerMap_t; */
88  typedef std::vector<Clock::HandlerBase*> StaticHandlerMap_t;
89 
90  Clock() {}
91 
92  void execute(void) override;
93 
94  Cycle_t currentCycle;
95  TimeConverter* period;
96  StaticHandlerMap_t staticHandlerMap;
97  SimTime_t next;
98  bool scheduled;
99 
100  NotSerializable(SST::Clock)
101 };
102 
103 } // namespace SST
104 
105 #endif // SST_CORE_CLOCK_H
An Action is a schedulable Activity which is not an Event.
Definition: action.h:26
Definition: ssthandler.h:100
Event Handler class with user-data argument.
Definition: ssthandler.h:115
A class to convert between a component&#39;s view of time and the core&#39;s view of time.
Definition: timeConverter.h:26
Cycle_t getNextCycle()
Return the time of the next clock tick.
Definition: clock.cc:84
bool registerHandler(Clock::HandlerBase *handler)
Add a handler to be called on this clock&#39;s tick.
Definition: clock.cc:58
A Clock class.
Definition: clock.h:32
std::string toString() const override
Get a string represenation of the event.
Definition: clock.cc:158
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:66
void schedule()
Activates this clock object, by inserting into the simulation&#39;s timeVortex for future execution...
Definition: clock.cc:136