Question

Here is an extract of the FreeRTOS api-reference http://www.freertos.org/a00122.html regarding the xSemaphoreTake() function:

// See if we can obtain the semaphore. If the semaphore is not available
// wait 10 ticks to see if it becomes free.
if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
{
// We were able to obtain the semaphore and can now access the
// shared resource.

My question is: Do i already have the semaphore here or do i have to call xSemaphoreTake( xSemaphore, (portTickType) 10 ) explicit like:

// We have finished accessing the shared resource. Release the
// semaphore.
xSemaphoreGive( xSemaphore );
}
Was it helpful?

Solution

As of the example you link to, within the if (...) body the semaphore is taken. If you're copy-pasting from that example, it's up to you to ensure that you have both xSemaphoreTake and xSemaphoreGive in your program.

OTHER TIPS

You do not know if you owned the xSemaphore semaphore when you call the xSemaphoreTake(). If it was free, your code will continue execution, if it would become free within 10 ticks your code will continue executing without OS scheduler putting you down (that is a timeout specified in your call), if the xSemaphore was not available after specified timeout, your task will go to the blocked state, and next ready task with higher priority will execute.

Your explicit call xSemaphoreGive with reference to the same semaphore would be a serious error here, if you did not own it, it would make no sense to release it.

My question is: Do i already have the semaphore here or do i have to call xSemaphoreTake( xSemaphore, (portTickType) 10 ) explicit like:

Yes you have the semaphore if you enter the body of the if statement. If somehow the semaphore was not available (or given in the duration) after the blocking time (10 ticks in your case), then xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) returns pdFALSE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top