SystemVerilog: Multithreading - when do I need to protect cl

A

Andrew FPGA

Guest
Hi,
Its not clear to me when I need to protect class members, to avoid
(often subtle) multithreading bugs.

Say I have thread A writing a class member from time to time. And then
thread B reading that same class member from time to time. Thread A
and B are completly unsynchronised with respect to each other. Do I
really need to use a semaphore to protect access to the variable? Can
the class member be corrupted if the write and read occur "at the same
time"?

Here is another example.
Thread A ...
....
wait (this.bucket >= num_tokens);
....

Then Thread B...
....
this.bucket++; //eventually this will trigger the thread A wait
....

Is it safe to wait on an expression involving a class member that is
written by another thread?

I use semaphores, mailboxes, etc somewhat often. But I wonder
sometimes if its really necessary, but I do it because I'm paranoid
about multithreading bugs. But then it precludes doing things like the
wait example above, which would otherwise be quite neat, if I was
confident it wasn't going to burn me with a subtle bug later.

Cheers
Andrew












Questa 6.6.
 
Hi Andrew,

I'll take a stab at this. I share your concerns because I too have
been burnt by multi-threading bugs.

In SytemVerilog, the threading is non-preemptive. That is, the thread
runs until you get to a switching point. Now there's the rub. As far
as I know, the LRM and the simulator companies do not publish a
definitive list of what constitutes a switching point. (Dave, Steve ,
care to comment?)

You example is perfectly safe, as I am quite sure "regular" code is
not a switching point. Obviously, you have to use a semaphore to
protect code that uses multiple clock cycles in an atomic way (think
driver.send(packet)).

My style is to bury the semaphore down in the class and then have a
wait_for_<completed,send,etc>() as a public method. Also, I tend to
use the fairly simple get() and put() and leave the counting mo my
code.

Take Care,
mike <> trusster <> com

On Mar 20, 10:17 pm, Andrew FPGA <andrew.newsgr...@gmail.com> wrote:
Hi,
Its not clear to me when I need to protect class members, to avoid
(often subtle) multithreading bugs.

Say I have thread A writing a class member from time to time. And then
thread B reading that same class member from time to time. Thread A
and B are completly unsynchronised with respect to each other. Do I
really need to use a semaphore to protect access to the variable? Can
the class member be corrupted if the write and read occur "at the same
time"?

Here is another example.
Thread A ...
...
wait (this.bucket >= num_tokens);
...

Then Thread B...
...
this.bucket++;  //eventually this will trigger the thread A wait
...

Is it safe to wait on an expression involving a class member that is
written by another thread?

I use semaphores, mailboxes, etc somewhat often. But I wonder
sometimes if its really necessary, but I do it because I'm paranoid
about multithreading bugs. But then it precludes doing things like the
wait example above, which would otherwise be quite neat, if I was
confident it wasn't going to burn me with a subtle bug later.

Cheers
Andrew

Questa 6.6.
 
On Mar 22, 6:46 am, "mmi...@gmail.com" <mmi...@gmail.com> wrote:


Mike,

You're right, the LRM does not prescribe switching points. Some
optimizations, like with continuous assignments are sometimes in-lined
and will make it appear that another process has switched in the
middle of another.

The LRM does not prescribe non-preemptive threading. In fact with
multi-cure simulation on the horizon, you should not count on it.

This is why methodologies like the AVM/OVM/VMM have adopted TLM as the
standard interface for multi-threaded environments.

Dave_59
 

Welcome to EDABoard.com

Sponsor

Back
Top