Skip to content

Commit

Permalink
de/attach API is not threadsafe in the SystemC 3.0 standard
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Burton <mburton@quicinc.com>
  • Loading branch information
markfoodyburton committed Dec 3, 2024
1 parent d1e9398 commit 7604ae3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
49 changes: 40 additions & 9 deletions systemc-components/common/include/async_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ class async_event : public sc_core::sc_prim_channel, public sc_core::sc_event
sc_core::sc_time m_delay;
std::thread::id tid;
std::mutex mutex; // Belt and braces
bool outstanding;
bool outstanding = false;
enum { none, attach, detach } attach_pending_state = none;

public:
async_event(bool start_attached = true): outstanding(0)
{
tid = std::this_thread::get_id();
outstanding = false;
enable_attach_suspending(start_attached);
}

void async_notify() { notify(); }

void notify(sc_core::sc_time delay = sc_core::sc_time(sc_core::SC_ZERO_TIME))
void notify()
{
if (tid == std::this_thread::get_id()) {
sc_core::sc_event::notify();
} else {
notify(sc_core::SC_ZERO_TIME);
}
}
void notify(double d, sc_core::sc_time_unit u) { notify(sc_core::sc_time(d, u)); }
void notify(sc_core::sc_time delay)
{
if (tid == std::this_thread::get_id()) {
sc_core::sc_event::notify(delay);
Expand All @@ -59,7 +68,14 @@ class async_event : public sc_core::sc_prim_channel, public sc_core::sc_event
#ifndef SC_HAS_ASYNC_ATTACH_SUSPENDING
sc_core::async_attach_suspending(this);
#else
this->sc_core::sc_prim_channel::async_attach_suspending();
if (tid == std::this_thread::get_id()) {
this->sc_core::sc_prim_channel::async_attach_suspending();
} else {
mutex.lock();
attach_pending_state = attach;
mutex.unlock();
async_request_update();
}
#endif
}

Expand All @@ -68,14 +84,18 @@ class async_event : public sc_core::sc_prim_channel, public sc_core::sc_event
#ifndef SC_HAS_ASYNC_ATTACH_SUSPENDING
sc_core::async_detach_suspending(this);
#else
this->sc_core::sc_prim_channel::async_detach_suspending();
if (tid == std::this_thread::get_id()) {
this->sc_core::sc_prim_channel::async_detach_suspending();
} else {
mutex.lock();
attach_pending_state = detach;
mutex.unlock();
async_request_update();
}
#endif
}

void enable_attach_suspending(bool e)
{
e ? this->async_attach_suspending() : this->async_detach_suspending();
}
void enable_attach_suspending(bool e) { e ? this->async_attach_suspending() : this->async_detach_suspending(); }

private:
void update(void)
Expand All @@ -86,6 +106,17 @@ class async_event : public sc_core::sc_prim_channel, public sc_core::sc_event
sc_event::notify(m_delay);
outstanding = false;
}
switch (attach_pending_state) {
case attach:
this->async_attach_suspending();
break;
case detach:
this->async_detach_suspending();
break;
default:
break;
}
attach_pending_state = none;
mutex.unlock();
}
void start_of_simulation()
Expand Down
2 changes: 1 addition & 1 deletion systemc-components/common/include/ports/biflow-socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class biflow_socket : public sc_core::sc_module, public biflow_bindable
default:
SCP_FATAL(())("Unkown command");
}
m_send_event.notify();
m_send_event.notify(sc_core::SC_ZERO_TIME);
}
void send_ctrl(ctrl& c)
{
Expand Down
4 changes: 3 additions & 1 deletion systemc-components/python_binder/src/python_binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ PYBIND11_EMBEDDED_MODULE(gs, m)
pybind11::class_<gs::async_event>(m, "async_event")
.def(pybind11::init<bool>())
.def("async_notify", &gs::async_event::async_notify)
.def("notify", &gs::async_event::notify)
.def("notify", static_cast<void (gs::async_event::*)()>(&gs::async_event::notify))
.def("notify", static_cast<void (gs::async_event::*)(double, sc_core::sc_time_unit)>(&gs::async_event::notify))
.def("notify", static_cast<void (gs::async_event::*)(sc_core::sc_time)>(&gs::async_event::notify))
.def("async_attach_suspending", &gs::async_event::async_attach_suspending)
.def("async_detach_suspending", &gs::async_event::async_detach_suspending)
.def("enable_attach_suspending", &gs::async_event::enable_attach_suspending);
Expand Down

0 comments on commit 7604ae3

Please sign in to comment.