qualcomm/qbox
Loading...
Searching...
No Matches
runonsysc.h
1/*
2 * Copyright (c) 2022-2026 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 * Author: GreenSocs 2022
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef RUNONSYSTEMC_H
9#define RUNONSYSTEMC_H
10
11#include <systemc>
12#include <atomic>
13#include <functional>
14#include <future>
15#include <memory>
16#include <mutex>
17#include <queue>
18#include <thread>
19
20namespace gs {
21
22class runonsysc : public sc_core::sc_module
23{
24private:
25 // ============================================================
26 // Core: shared lifetime state
27 // ============================================================
28 struct Core {
29 // --------------------------------------------------------
30 // AsyncJob (private implementation detail)
31 // --------------------------------------------------------
32 struct AsyncJob {
33 using Ptr = std::shared_ptr<AsyncJob>;
34
35 std::function<void()> job;
36
37 std::promise<void> done_promise;
38 std::shared_future<void> done_future;
39
40 std::atomic<bool> cancelled{ false };
41
42 explicit AsyncJob(std::function<void()> j)
43 : job(std::move(j)), done_future(done_promise.get_future().share())
44 {
45 }
46
47 void operator()()
48 {
49 try {
50 if (!cancelled.load(std::memory_order_relaxed)) {
51 job();
52 }
53 done_promise.set_value();
54 } catch (...) {
55 try {
56 done_promise.set_exception(std::current_exception());
57 } catch (...) {
58 // Likely that this is due to jobs being canceled (at cleanup)
59 }
60 }
61 }
62
63 void cancel()
64 {
65 cancelled.store(true, std::memory_order_relaxed);
66 try {
67 done_promise.set_value(); // unblock waiters immediately
68 } catch (...) {
69 // Too late to cancel the job, it already threw itself!
70 }
71 }
72
73 void wait()
74 {
75 done_future.wait();
76 done_future.get(); // rethrows if job threw
77 }
78
79 bool is_cancelled() const { return cancelled.load(std::memory_order_relaxed); }
80 };
81
82 // --------------------------------------------------------
83 // Core state
84 // --------------------------------------------------------
85 std::thread::id thread_id;
86
87 std::queue<typename AsyncJob::Ptr> async_jobs;
88 typename AsyncJob::Ptr running_job;
89 std::mutex async_jobs_mutex;
90
91 async_event jobs_handler_event;
92 std::atomic<bool> running{ true };
93
94 explicit Core(std::thread::id id): thread_id(id), jobs_handler_event(false) {}
95 };
96
97 std::shared_ptr<Core> m_core;
98
99 // ============================================================
100 // SystemC job handler thread
101 // ============================================================
102 void jobs_handler()
103 {
104 auto core = m_core; // hold shared ownership
105 if (!core) return;
106
107 std::unique_lock<std::mutex> lock(core->async_jobs_mutex);
108
109 while (core->running.load(std::memory_order_relaxed)) {
110 while (core->running.load(std::memory_order_relaxed) && !core->async_jobs.empty()) {
111 core->running_job = core->async_jobs.front();
112 core->async_jobs.pop();
113
114 lock.unlock();
115
116 sc_core::sc_unsuspendable();
117 (*core->running_job)();
118 /* Drain any notify(SC_ZERO_TIME) events the job queued
119 * (e.g. deferred IRQ SC_METHODs) before allowing suspend. */
120 wait(sc_core::SC_ZERO_TIME);
121 sc_core::sc_suspendable();
122
123 lock.lock();
124 core->running_job.reset();
125 }
126
127 lock.unlock();
128 wait(core->jobs_handler_event);
129 lock.lock();
130 }
131
132 SC_REPORT_WARNING("RunOnSysc", "Stopped");
133 }
134
135 void stop()
136 {
137 auto core = m_core;
138 if (!core) return;
139
140 core->running.store(false, std::memory_order_relaxed);
141 }
142
143 void cancel_all()
144 {
145 auto core = m_core;
146 if (!core) return;
147
148 std::lock_guard<std::mutex> lock(core->async_jobs_mutex);
149
150 while (!core->async_jobs.empty()) {
151 core->async_jobs.front()->cancel();
152 core->async_jobs.pop();
153 }
154
155 if (core->running_job) {
156 core->running_job->cancel();
157 core->running_job.reset();
158 }
159 }
160
161public:
162 // ============================================================
163 // Constructor
164 // ============================================================
165 explicit runonsysc(const sc_core::sc_module_name& n = "run-on-sysc"): sc_module(n)
166 {
167 m_core = std::make_shared<Core>(std::this_thread::get_id());
168 SC_THREAD(jobs_handler);
169 }
170
171 // ============================================================
172 // Destructor
173 // ============================================================
174 ~runonsysc() override
175 {
176 auto core = m_core;
177 if (!core) return;
178
179 stop();
180
181 m_core.reset(); // safe: state lives until last user exits
182 }
183
184 // ============================================================
185 // Public API
186 // ============================================================
187
188 bool is_on_sysc() const
189 {
190 auto core = m_core;
191 return core && (std::this_thread::get_id() == core->thread_id);
192 }
193
194 void end_of_simulation() override
195 {
196 auto core = m_core;
197 if (!core) return;
198 stop();
199 cancel_all();
200 }
201
202 void fork_on_systemc(std::function<void()> job_entry) { run_on_sysc(job_entry, false); }
203
214 bool run_on_sysc(std::function<void()> job_entry, bool wait = true)
215 {
216 auto core = m_core; // snapshot lifetime
217 if (!core) return false;
218
219 if (!core->running.load(std::memory_order_relaxed)) return false;
220
221 if (is_on_sysc()) {
222 job_entry();
223 return true;
224 }
225
226 auto job = std::make_shared<typename Core::AsyncJob>(std::move(job_entry));
227
228 {
229 std::lock_guard<std::mutex> lock(core->async_jobs_mutex);
230
231 if (!core->running.load(std::memory_order_relaxed)) return false;
232
233 core->async_jobs.push(job);
234 }
235
236 core->jobs_handler_event.async_notify();
237
238 if (wait) {
239 try {
240 job->wait();
241 } catch (...) {
242 auto old = sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
243 sc_core::SC_LOG | sc_core::SC_DISPLAY);
244 SC_REPORT_ERROR("RunOnSysc", "Run on systemc received an unknown exception from job");
245 sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR, old);
246 stop();
247 // Notify the job handler so that it can jump out of it's loop.
248 core->jobs_handler_event.async_notify();
249 return false;
250 }
251
252 return !job->is_cancelled();
253 }
254
255 return true;
256 }
257};
258
259} // namespace gs
260
261#endif // RUNONSYSTEMC_H
Definition async_event.h:22
bool run_on_sysc(std::function< void()> job_entry, bool wait=true)
Run a job on the SystemC kernel thread.
Definition runonsysc.h:214
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10