-1

I need to model some portion of my hardware in systemverilog and it kind of looks like following:

I can have two treads -(SV task) running in parallel.

Thread:

1. get_resource_from_manager()  [sema.get(1) ??]
2. repeat(until_finish)
3.   do_work()
4.   give_contorl_to_thread1() [sema.put(1) ??]
5.   wait_for_thread1_to_return_control() [sema.get(1) ??]
6.   continue_work()
7. endrepeat
8. do_some_cleanup()
9. exit()

Both Threads do the same work. I am thinking about using semaphore. I've never used it before. Is it a valid assumption/fact that the thread will block at line 5? What I wanted to achieve is, thread1 will give up resource to thread2 by using semaphore put() in line 4 -> thread 2 will pick up this resource and do it's work and eventually release it using put. In between this time, thread1 will be waiting on blocking get() call at line 5.

newbie
  • 4,639
  • 10
  • 32
  • 45
  • I do not think you understand the concept of threads in SystemVerilog and the use of fork/join to spawn threads. There may be no need for a semaphore here. – dave_59 Jun 18 '14 at 19:27
  • I think I need semaphore or mutex here because in my case, both thread will try to access a single shared interface. – newbie Jun 19 '14 at 18:31

1 Answers1

2

Yes if one thread gets the semaphore, the other one will be blocked. You need to define the semaphore as:

 semaphore  sema = new(1);

Here is a working example similar to what you are looking for. In order for the two tasks to run concurrently, they should be called from two parallel bodies, such as inside a fork-join block. Also, notice that semaphores are not synthesizable by common synthesis tools.

Ari
  • 7,251
  • 11
  • 40
  • 70