We are creating Binary Semaphore using xSemaphoreCreateBinaryStatic() API - I am not sure if semaphore created its value will be set as 1 or 0.
During our application runtime in a loop we need to create binary Semaphores - so our need is to make use of already created bjnary semaphore - something like below:
On successfull call of xSemaphoreCreateBinaryStatic we will update an array of Data and update semhdl , inuse and bin member variables.
Now in the loop as mentioned below:
Loop:
Step 1: __________________________ or xSemaphoreCreateBinaryStatic()
xSemaphoreTake()
xSemaphoreGive()
Here in Step 1 we either have to validate that an existing semaphore created (using xSemaphoreCreateBinaryStatic) is avaliable for re-use but I am not sure of its startvalue - does not xSemaphoreCreateBinaryStatic capable to create Semaphore with user desired value 0 or 1? If not how do we know what is the value of value of already created Semaphore as in Step 1 ____ - we need ensure that the startvalue is as required 1 or 0?
My question is if I have a startvalue and need to ensure that an already created un-used Samaphore to be reused then how can we reset its initial value?
I’d rethink the approach dynamically creating/destroying resources like semaphores (in a loop). Seems a bit too complicated to me. If I got you right.
Or which bigger problem do you want to solve ?
The semaphore is created in the ‘empty’ state, meaning the semaphore must first be given using the xSemaphoreGive() API function before it can subsequently be taken (obtained) using the xSemaphoreTake() function.
Thanks for the info - At the first step when we call xSemaphoreCreateBinaryStatic API - we would save this handle and then call xSemaphoreTake() followed by xSemaphoreGive() as mentioned in loop. Later in the second loop itreration we would not like to call xSemaphoreCreateBinaryStatic (we have a scenario to find and get if an existing semaphore is there to be reused) to create the semaphore - can we make use of uxSemaphoreGetCount that will return if the binary samphore can be reused.
first of all, are you sure that you do not need a mutex instead of a binary semaphore? How are your semaphores used? Second, at superficial glance your bookkeeping code may be subject to race conditions in which your knowledge of the status is not in sync about the real status.
why do you think you can not do a timed wait on a mutex?
Edit: I was probably mistaken about the race conditions as you only use the bookkeeping for recycling objects, but I would agree with Hartmut that your code seems overambitious. What is the exact reasoning for preallocating your sync objects instead of simply de- and reallocating them as you go?
One thing to point out is that if in your loop you create the semaphore, later in the you should probably delete it, or just create it before the loop.
There is no reason it can’t be available for reuse. If for some reason the code can validly leave semaphore in the taken state at the end (which seems like a problematic definition) then just give the semaphore at the beginning of the loop.
The previous comments are all helpful and I agree with their line of questioning. I’m not sure why you want to create the binary semaphore in a loop. Why not simply create the binary semaphores at the beginning of you program and use their states (available or taken) in the loop?
As for resetting to the original value - you can always take the semaphore with a timeout of 0. You could then rename this if you so please - for example:
void resetBinarySemaphore( SemaphoreHandle_t sem )
{
/* Disregard the return value as we've guaranteed the semaphore must be 'given' */
( void ) xSemaphoreTake(sem, ( TickType_t ) 0U );
}
How do I change the value of semaphore after re-setting - will xSemaphoreTake() make the value to 0 or empty state. Binary semaphore can have value either 1 or 0? How do I create a semaphore whose initial value is 1? After resetting using xSemaphoreTake() how can make it to value 1?
Suppose I created a semaphore whose original value is 0 and want to re-use this semaphore with value 1 - how can I achieve the same?value either 1 or 0?
Just do a give to the semaphore to make it available.
Normal usage of semaphores is to signal that something is available to be processed, and this isn’t normally the default state.
When looking for access to a resource, it is much more common to use a Mutex, which do start in the available state. Mutexes do limit usage to a given task must take the mutex, and when done give it back, but by doing that allows the solving of the priority inversion problem.
If you need something that is more like a mutex, but needs something not quite meeting its specification, you can use a semaphore, and just give it when creating, but check your logic carefully as many cases where people think this is what they want to do, are actually hiding errors.