27 using Ptr = std::shared_ptr<AsyncJob>;
30 std::packaged_task<
void()> m_task;
32 bool m_cancelled =
false;
34 void run_job() { m_task(); }
44 void operator()() { run_job(); }
60 auto future = m_task.get_future();
69 bool is_cancelled()
const {
return m_cancelled; }
72 std::thread::id m_thread_id;
75 std::queue<AsyncJob::Ptr> m_async_jobs;
76 AsyncJob::Ptr m_running_job;
77 std::mutex m_async_jobs_mutex;
79 async_event m_jobs_handler_event;
80 std::atomic<bool> running =
true;
85 std::unique_lock<std::mutex>
lock(m_async_jobs_mutex);
88 while (!m_async_jobs.empty()) {
89 m_running_job = m_async_jobs.front();
93 sc_core::sc_unsuspendable();
95 sc_core::sc_suspendable();
98 m_running_job.reset();
101 wait(m_jobs_handler_event);
108 void cancel_pendings_locked()
110 while (!m_async_jobs.empty()) {
111 m_async_jobs.front()->cancel();
117 runonsysc(
const sc_core::sc_module_name&
n = sc_core::sc_module_name(
"run-on-sysc"))
120 , m_jobs_handler_event(
false)
133 std::lock_guard<std::mutex>
lock(m_async_jobs_mutex);
135 cancel_pendings_locked();
149 std::lock_guard<std::mutex>
lock(m_async_jobs_mutex);
151 cancel_pendings_locked();
154 m_running_job->cancel();
155 m_running_job.reset();
161 m_jobs_handler_event.async_notify();
163 void end_of_simulation()
183 if (!running)
return false;
191 std::lock_guard<std::mutex>
lock(m_async_jobs_mutex);
192 m_async_jobs.push(
job);
195 m_jobs_handler_event.async_notify();
201 }
catch (std::runtime_error
const&
e) {
203 auto old = sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
204 sc_core::SC_LOG | sc_core::SC_DISPLAY);
207 (
"Run on systemc received a runtime error from job: " + std::string(
e.what())).c_str());
208 sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
old);
211 }
catch (
const std::exception&
exc) {
212 if (sc_core::sc_report_handler::get_count(sc_core::SC_ERROR) == 0) {
214 auto old = sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
215 sc_core::SC_LOG | sc_core::SC_DISPLAY);
218 (
"Run on systemc received an exception from job: " + std::string(
exc.what())).c_str());
219 sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
old);
224 auto old = sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
225 sc_core::SC_LOG | sc_core::SC_DISPLAY);
226 SC_REPORT_ERROR(
"RunOnSysc",
"Run on systemc received an unknown exception from job");
227 sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
old);
232 return !
job->is_cancelled();
242 bool is_on_sysc()
const {
return std::this_thread::get_id() == m_thread_id; }
bool run_on_sysc(std::function< void()> job_entry, bool wait=true)
Run a job on the SystemC kernel thread.
Definition runonsysc.h:181