28 using Ptr = std::shared_ptr<AsyncJob>;
31 std::packaged_task<
void()> m_task;
33 bool m_cancelled =
false;
35 void run_job() { m_task(); }
45 void operator()() { run_job(); }
61 auto future = m_task.get_future();
63 while (!m_cancelled &&
future.wait_for(std::chrono::milliseconds(500)) == std::future_status::timeout) {
71 bool is_cancelled()
const {
return m_cancelled; }
74 std::thread::id m_thread_id;
77 std::queue<AsyncJob::Ptr> m_async_jobs;
78 AsyncJob::Ptr m_running_job;
79 std::mutex m_async_jobs_mutex;
81 async_event m_jobs_handler_event;
82 std::atomic<bool> running =
true;
87 std::unique_lock<std::mutex>
lock(m_async_jobs_mutex);
90 while (running && !m_async_jobs.empty()) {
91 m_running_job = m_async_jobs.front();
95 sc_core::sc_unsuspendable();
97 sc_core::sc_suspendable();
100 m_running_job.reset();
103 wait(m_jobs_handler_event);
110 void cancel_pendings_locked()
112 while (!m_async_jobs.empty()) {
113 m_async_jobs.front()->cancel();
119 runonsysc(
const sc_core::sc_module_name&
n = sc_core::sc_module_name(
"run-on-sysc"))
122 , m_jobs_handler_event(
false)
135 std::lock_guard<std::mutex>
lock(m_async_jobs_mutex);
137 cancel_pendings_locked();
151 std::lock_guard<std::mutex>
lock(m_async_jobs_mutex);
153 cancel_pendings_locked();
156 m_running_job->cancel();
157 m_running_job.reset();
163 m_jobs_handler_event.async_notify();
165 void end_of_simulation()
185 if (!running)
return false;
193 std::lock_guard<std::mutex>
lock(m_async_jobs_mutex);
195 m_async_jobs.push(
job);
200 m_jobs_handler_event.async_notify();
206 }
catch (std::runtime_error
const&
e) {
208 auto old = sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
209 sc_core::SC_LOG | sc_core::SC_DISPLAY);
212 (
"Run on systemc received a runtime error from job: " + std::string(
e.what())).c_str());
213 sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
old);
216 }
catch (
const std::exception&
exc) {
217 if (sc_core::sc_report_handler::get_count(sc_core::SC_ERROR) == 0) {
219 auto old = sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
220 sc_core::SC_LOG | sc_core::SC_DISPLAY);
223 (
"Run on systemc received an exception from job: " + std::string(
exc.what())).c_str());
224 sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
old);
229 auto old = sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
230 sc_core::SC_LOG | sc_core::SC_DISPLAY);
231 SC_REPORT_ERROR(
"RunOnSysc",
"Run on systemc received an unknown exception from job");
232 sc_core::sc_report_handler::set_actions(sc_core::SC_ERROR,
old);
237 return !
job->is_cancelled();
247 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:183