quic/qbox
Loading...
Searching...
No Matches
async_event.h
1/*
2 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 * Author: GreenSocs 2022
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef ASYNC_EVENT
9#define ASYNC_EVENT
10
11#include <iostream>
12
13#include <systemc>
14#include <tlm>
15#include <thread>
16#include <mutex>
17
18#include <pre_suspending_sc_support.h>
19
20namespace gs {
21class async_event : public sc_core::sc_prim_channel, public sc_core::sc_event
22{
23private:
24 sc_core::sc_time m_delay;
25 std::thread::id tid;
26 std::mutex mutex; // Belt and braces
27 bool outstanding = false;
28 enum { none, attach, detach } attach_pending_state = none;
29
30public:
31 async_event(bool start_attached = true): outstanding(0)
32 {
33 tid = std::this_thread::get_id();
34 enable_attach_suspending(start_attached);
35 }
36
37 void async_notify() { notify(); }
38
39 void notify()
40 {
41 if (tid == std::this_thread::get_id()) {
42 sc_core::sc_event::notify();
43 } else {
44 notify(sc_core::SC_ZERO_TIME);
45 }
46 }
47 void notify(double d, sc_core::sc_time_unit u) { notify(sc_core::sc_time(d, u)); }
48 void notify(sc_core::sc_time delay)
49 {
50 if (tid == std::this_thread::get_id()) {
51 sc_core::sc_event::notify(delay);
52 } else {
53 mutex.lock();
54 m_delay = delay;
55 outstanding = true;
56 mutex.unlock();
58#ifndef SC_HAS_SUSPENDING
59 sc_core::sc_internal_async_wakeup();
60#endif
61 }
62 }
63
64 void async_attach_suspending()
65 {
66#ifndef SC_HAS_ASYNC_ATTACH_SUSPENDING
67 sc_core::async_attach_suspending(this);
68#else
69 if (tid == std::this_thread::get_id()) {
70 this->sc_core::sc_prim_channel::async_attach_suspending();
71 } else {
72 mutex.lock();
73 attach_pending_state = attach;
74 mutex.unlock();
76 }
77#endif
78 }
79
80 void async_detach_suspending()
81 {
82#ifndef SC_HAS_ASYNC_ATTACH_SUSPENDING
83 sc_core::async_detach_suspending(this);
84#else
85 if (tid == std::this_thread::get_id()) {
86 this->sc_core::sc_prim_channel::async_detach_suspending();
87 } else {
88 mutex.lock();
89 attach_pending_state = detach;
90 mutex.unlock();
92 }
93#endif
94 }
95
96 void enable_attach_suspending(bool e) { e ? this->async_attach_suspending() : this->async_detach_suspending(); }
97
98private:
99 void update(void)
100 {
101 mutex.lock();
102 // we should be in SystemC thread
103 if (outstanding) {
104 sc_event::notify(m_delay);
105 outstanding = false;
106 }
107 switch (attach_pending_state) {
108 case attach:
109 this->async_attach_suspending();
110 break;
111 case detach:
112 this->async_detach_suspending();
113 break;
114 default:
115 break;
116 }
117 attach_pending_state = none;
118 mutex.unlock();
119 }
120 void start_of_simulation()
121 {
122 // we should be in SystemC thread
123 if (outstanding) {
125 }
126 }
127};
128} // namespace gs
129
130#endif // ASYNC_EVENT
Definition target.h:160
Definition async_event.h:22
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10