quic/qbox
Loading...
Searching...
No Matches
observer_event.h
1/*
2 * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _GS_OBSERVER_EVENT_
8#define _GS_OBSERVER_EVENT_
9
10#include <systemc>
11#include <tlm>
12#include <scp/report.h>
13#include <string>
14
15namespace gs {
16class observer_event : public sc_core::sc_module, public sc_core::sc_event, public sc_core::sc_stage_callback_if
17{
18 SCP_LOGGER();
19
20private:
21 sc_core::sc_event m_update;
22 sc_core::sc_time m_scheduled;
23 sc_core::sc_process_handle m_th;
24
25 void update_m()
26 {
27 auto now = sc_core::sc_time_stamp();
28 if (now >= m_scheduled) {
29 sc_core::sc_event::notify();
30 } else {
31 sc_core::sc_time pending = now;
32 if (sc_core::sc_pending_activity_at_future_time()) {
33 pending = now + sc_core::sc_time_to_pending_activity();
34 }
35 SCP_TRACE(()) << "time of future pending activity: " << pending.to_seconds() << " sec";
36 if (pending >= m_scheduled) {
37 sc_core::sc_event::notify(m_scheduled - now);
38 } else {
39 if (pending > now) {
40 m_update.notify(pending - now);
41 } else {
42 sc_core::sc_register_stage_callback(*this, sc_core::SC_POST_UPDATE);
43 }
44 }
45 }
46 }
47
48 void future_events_notify_th()
49 {
50 while (true) {
51 m_th.suspend();
52 update_m();
53 }
54 }
55
56public:
57 observer_event(const sc_core::sc_module_name& n = sc_core::sc_gen_unique_name("observer_event"))
58 : sc_module(n)
59 , sc_core::sc_event((std::string(n) + "_notifier_event").c_str())
60 , m_update((std::string(n) + "_update_event").c_str())
61 , m_scheduled(sc_core::SC_ZERO_TIME)
62 {
63 SC_METHOD(update_m);
64 sensitive << m_update;
66 SC_THREAD(future_events_notify_th);
67 m_th = sc_core::sc_get_current_process_handle();
68 }
69 void notify()
70 {
71 m_scheduled = sc_core::sc_time_stamp();
72 m_update.notify(sc_core::SC_ZERO_TIME);
73 }
74 void notify(double delay, sc_core::sc_time_unit unit)
75 {
76 m_scheduled = sc_core::sc_time_stamp() + sc_core::sc_time(delay, unit);
77 m_update.notify(sc_core::SC_ZERO_TIME);
78 }
79 void notify(sc_core::sc_time delay)
80 {
81 m_scheduled = sc_core::sc_time_stamp() + delay;
82 m_update.notify(sc_core::SC_ZERO_TIME);
83 }
84
85 void stage_callback(const sc_core::sc_stage& stage)
86 {
87 if (sc_core::sc_pending_activity_at_future_time()) {
88 m_th.resume();
89 sc_core::sc_unregister_stage_callback(*this, sc_core::SC_POST_UPDATE);
90 }
91 }
92
94};
95} // namespace gs
96
97#endif // OBSERVER_EVENT
Definition target.h:160
Definition observer_event.h:17
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10