Semaphore in a function

Hi Dave,

I have a common resource which I use to add and delete entries from two different functions. But, I cannot use semaphore.get() inside the function as this will be a task, which cannot be called from inside a function. I do not want to use semaphore.try_get(), as my logic is to access the common resource and try_get may or may not enable be access to it as it is not blocking.

My next option is to change the function into a task and then I will not have any problem in calling semaphore.get() from inside the task.

However, the above task / function (which uses semaphore to before accessing the common resource), gets called from a uvm write implementation which is a function. So, again function enabling a task, which cannot happen.

For now, I have used try_get() in a function, but, that is not what I am trying to achieve. I would want to use semaphore.get().

How can I fix this problem ? Also, what are the application scenarios where semaphore.try_get() is used ?

Thanks,
Madhu

In reply to mseyunni:

You need to explain better why you need to use a semaphore for two functions to access a common resource. A function has no duration. The UVM is going to serialize the calls to the write methods anyways.

In reply to dave_59:

The common resource is an associative array used to track the id’s in use. Id’s gets pushed into this from a function which gets queried from sequences when generating the stimulus to push the id and from the uvm write function which is for output transaction to determine, when the id has come out of DUT (or no more active).

In reply to mseyunni:
You are still not very clear.

You say the “common resource is an associative array” and “Id’s gets pushed into this”. You don’t push things onto an associative array.

You say “from a function which gets queried from sequences”. Did you mean called from a sequence?

You say “and from the uvm write function”. Do you mean the write function also is going to call the same function to “push the id” onto the associative array?

And what component is calling the uvm write function? I assume this is an anlysis_port of some component. Should this go into an analysis_fifo instead?

I still can’t see how using a semaphore is going to help you? Where would you call the put() and get() if you were allowed to call a task from a function?

In reply to dave_59:

int id_in_use[bit[ID_BITS-1):0]]

Sorry for not being clear. I shall try to explain how & where I am using

You say the “common resource is an associative array” and “Id’s gets pushed into this”. You don’t push things onto an associative array.
→ I should have said “id” is marked in use, by increasing its use count as in id_in_use[id]++;

You say “from a function which gets queried from sequences”. Did you mean called from a sequence?
→ Yes. First the sequence calls a function to get an id. The function randomly selects an id from another (1) A queue of (list) of free id’s (2) an array of active id’s, and sets into this AA, as said above. I use semaphore in this function to set the id as in use in the aa, and below the id could be removed as below


when the id exits the design,
if(id_in_use.exits(id))
  if(id_in_use[id] == 0)
     id_in_use.delete(id)


You say “and from the uvm write function”. Do you mean the write function also is going to call the same function to “push the id” onto the associative array?
→ This is a uvm write implementation where it gets output transaction. In this *_write() function, I am calling another function which updates the my_array, based on the code snippet given above.

And what component is calling the uvm write function? I assume this is an anlysis_port of some component. Should this go into an analysis_fifo instead?
→ This is an analysis imp connection. Instead of storing in analysis_fifo, I am calling the function to update the id status on-the-fly.

I still can’t see how using a semaphore is going to help you? Where would you call the put() and get() if you were allowed to call a task from a function?
→ I am using the semaphore try_get() and semaphore.put() in the function that gets an “id” to the sequence and in another function which sees, whether the id can be removed from the in-use array and removes and puts that id into the free-id queue.