I have following code in my project at my work using Vxworks5.5
m_SemServState = semBCreate(SEM_Q_FIFO, SEM_FULL );
//.... In another function I have following code.
SemStatus = semTake(m_SemServState, 500);
if(OK == SemStatus)
{
// ...
}
else
{
//...
}
semGive(m_SemServState);
I have following questions in above code.
Is above code works as we are calling semGive even if semTake failed ?.
When I talked to author I was told that we can call semGive even i semTake fails. Will it have any side effects?
Is programming like above is good practice?
Thanks for inputs.
First, since you're creating a binary semaphore, there is no "count" per se. The semaphore is either full or empty and there is no concept of ownership (like there would be for a mutex).
The thing that confuses me about this particular code is that the task running this function seems to signal itself.
There are two possible types of error returned here:
I would hope that you do check the errno to determine which of those 2 cases you encounter. If the error is not a timeout, then you probably have a serious problem on your hands and almost certainly, the operations of the semaphore are totally compromised, in which case doing a semGive probably also fails.
If the error is a timeout (i.e. after 500 ticks) then your code should be fine.
If there is only a single task running this function (and there are no other tasks waiting on this same semaphore), then loops through the function would always succeed right away. In fact, in fact, I can't see how this would fail.
So I must deduce that there are multiple tasks waiting for the same semaphore.
Without more details, it's difficult to say for sure if the code will do what you want it to, but it's legal. It really depends on what happens in the "else" branch if it's not a timeout.