hi, I am confused by the words of RM10:
BaseType_t xSemaphoreGive( SemaphoreHandle_t xSemaphore );
pdPASS: The semaphore ‘give’ operation was successful.
pdFAIL: The semaphore ‘give’ operation was not successful because the task calling
xSemaphoreGive() is not the semaphore holder. A task must successfully ‘take’ a
semaphore before it can successfully ‘give’ it back.
I was thinking like this: if it return fail, it mean the semaphore was full already. because every task can give to a semaphore, even it hasn’t take any of the same semaphore before ,when the semaphore is not full.am I right?
I don’t know what RM10 is. This is the (rather old) documentation for xSemaphoreGive() https://www.freertos.org/a00123.html
A task cannot “give” a semaphore that didn’t “take” it. The task TA takes the semaphore SA, but if TA tries to give a semaphore other than SA, then the function will return pdFAIL.
thank you for reply fast.
RM10 refer to “FreeRTOS_Reference_Manual_V10.0.0.pdf”
you give the link, use a example of mutex. but BaseType_t xSemaphoreGive( SemaphoreHandle_t xSemaphore ); can use with binary and counting semaphores.
the “fail” reason just say using mutex,not give clearly what’s the reason if the situation of using binary and counting semaphore.
yes ,if using a mutex, but if using a binary and counting semaphore. if it return pdFAIL, can not say that it is must because the giving task is not a holder, it can because the semaphore already be full.
It doesn’t matter who created the semaphore. From the documentation:
The semaphore must have previously been created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or xSemaphoreCreateCounting().
A counting semaphore can get full only if it’s “taken” so many times. A counting semaphore can get empty only if it’s “given” so many times.
From the documentation (again). Look for the last part (in bold):
xSemaphoreGive( SemaphoreHandle_t xSemaphore );
Macro to release a semaphore. The semaphore must have previously been created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or xSemaphoreCreateCounting().
This must not be used from an ISR . See xSemaphoreGiveFromISR() for an alternative which can be used from an ISR.
This macro must also not be used on semaphores created using xSemaphoreCreateRecursiveMutex().
Parameters:
xSemaphore A handle to the semaphore being released. This is the handle returned when the semaphore was created.Returns:
pdTRUE if the semaphore was released. pdFALSE if an error occurred. Semaphores are implemented using queues. An error can occur if there is no space on the queue to post a message – indicating that the semaphore was not first obtained correctly.
I just downloaded the reference manual and you are right - the description is only valid for mutex type semaphores, so without that qualification is basically wrong.
Thanks for confirm this.