Khushi Posted July 31, 2019 Report Share Posted July 31, 2019 Hi I have a use case where multiple threads are writing on a signal port and I get the sc_signal<T> cannot have more than one driver error. To avoid that I started to use sc_mutex and use lock/unlock., but still I get the same error. Here is what I tried to reproduce my use case with a small example. Can you help me to understand what is going wrong even after using mutex and how to avoid this issue. #include"systemc.h" using namespace std; using namespace sc_core; class module : public sc_module { public: sc_signal<bool> sig; sc_mutex m; explicit module(sc_module_name name): sc_module(name),sig("sig"){ SC_HAS_PROCESS(module); SC_THREAD(p1); SC_THREAD(p2); } void p1(){ while(1){ wait(10,SC_NS); m.lock(); sig.write(1); m.unlock(); } } void p2(){ while(1){ wait(10,SC_NS); m.lock(); sig.write(0); m.unlock(); } } ~module(){ } }; int sc_main(int, char**){ module m("m"); sc_start(); return 0; } Thanks Khushi Quote Link to comment Share on other sites More sharing options...
karthickg Posted July 31, 2019 Report Share Posted July 31, 2019 Check the documentation of SC_ONE_WRITER and SC_MANY_WRITERS in the LRM. The default value of the "WRITER_POLICY" in sc_signal is SC_ONE_WRITER. Quote Link to comment Share on other sites More sharing options...
Khushi Posted July 31, 2019 Author Report Share Posted July 31, 2019 Just to add, in my use case it is guaranteed that the different threads write to same port(signal) at different time stamp. Thanks Khushi Quote Link to comment Share on other sites More sharing options...
Khushi Posted July 31, 2019 Author Report Share Posted July 31, 2019 my point is that if multiple threads write to same port at same time than this error make sense but when multiple threads write to same port at different time than it should be ok as there is no race in that case. It is as good as same thread write to that port at different time stamp. I am trying to understand the logic behind this error when threads are competing for the same resource in different time points. Quote Link to comment Share on other sites More sharing options...
karthickg Posted July 31, 2019 Report Share Posted July 31, 2019 1 minute ago, Khushi said: Just to add, in my use case it is guaranteed that the different threads write to same port(signal) at different time stamp. Thanks Khushi That still is a problem with SC_ONE_WRITER policy. Quoting from LRM (my emphasis): Quote If the template argument WRITER_POLICY has the value SC_ONE_WRITER, it shall be an error to write to a given signal instance from more than one process instance at any time during simulation. See the change if you declare sig as: sc_signal<bool, SC_MANY_WRITERS> sig; Quote Link to comment Share on other sites More sharing options...
Khushi Posted July 31, 2019 Author Report Share Posted July 31, 2019 Yes agreed wrt LRM But why "at any time" instead of "at same time" ? Thanks Khushi Quote Link to comment Share on other sites More sharing options...
David Black Posted July 31, 2019 Report Share Posted July 31, 2019 If you want multiple drivers, use sc_signal_resolved or sc_signal_rv<N>. The reason for SC_ONE_WRITER is to catch errors since the normal use case for sc_signal is single drivers. The SC_MANY_WRITERS was intended to allow one process to initialize and then hand-off to another process. Your use case likely the resolved signal situation. Verilog uses wires for the resolved case. VHDL has you to be more explicit. Quote Link to comment Share on other sites More sharing options...
Moreshwar Salpekar Posted August 22, 2023 Report Share Posted August 22, 2023 I had a similar question only difference is that in my case, I have declared: sc_signal<bool, SC_MANY_WRITERS> on one side and sc_port<sc_signal_inout_if<bool>, 2> on other side. I used this because on the side of DUT, i need an inout port and there is only one wire for bidirectional data. Still i am getting same error: sc_signal<T> cannot have more than one driver. I wanted to know why the above code (using SC_MANY_WRITERS with sc_signal) cannot work. if you need any files or output, please let me know. Please note that using sc_signal_resolved and sc_inout_resolved works fine though I had to change all true to SC_LOGIC_0 and false to SC_LOGIC_1 to make it work. Quote Link to comment Share on other sites More sharing options...
David Black Posted August 29, 2023 Report Share Posted August 29, 2023 Many writers detects race conditions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.