Perlman Soong
Perlman Soong

Reputation: 131

About FreeRTOS: How to define the xBlockTime value in the "xSemaphoreTake" API?

Firstly, the declaration in FreeRTOS is like this:

#define xSemaphoreTake( xSemaphore, xBlockTime )    xQueueSemaphoreTake( ( xSemaphore ), ( xBlockTime ) )

In the current project that I am working on, the value of the xBlockTime parameter in the "xSemaphoreTake" API is assigned randomly: sometimes it is 5, or it is 10, 200, 1000, and portMAX_DELAY, etc.

So how to determine the correct (or the best) value of this parameter in your eyes?

Upvotes: 0

Views: 1207

Answers (1)

Pinetwig
Pinetwig

Reputation: 695

As mentioned in the documentation for xSemaphoreTake, the second parameter is "The time in ticks to wait for the semaphore to become available". If the semaphore is not available after the specified time interval, then xSemaphoreTake returns pdFALSE.

The range of appropriate values depends entirely on the application requirements. Semaphores are generally used to guard against multiple tasks accessing some resource simultaneously. What happens if the resource isn't available in a timely manner? Should you ring an alarm bell to alert the operator? Log an error message somewhere? Raise a flag? Or insert a hard debugger breakpoint in debug builds to ease troubleshooting? If there's no hurry, then the appropriate thing can be to sleep until the resource is available, so we can set the block time to portMAX_DELAY.

Upvotes: 1

Related Questions