Updated Jun 2025
xSemaphoreTakeRecursive
semphr. h
1xSemaphoreTakeRecursive( SemaphoreHandle_t xMutex,2 TickType_t xTicksToWait );
Macro to recursively obtain, or 'take', a mutex type semaphore. The mutex must have previously been created using a call to xSemaphoreCreateRecursiveMutex();
configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this macro to be available.
This macro must not be used on mutexes created using xSemaphoreCreateMutex().
A mutex used recursively can be 'taken' repeatedly by the owner. The mutex doesn't become available again until the owner has called xSemaphoreGiveRecursive() for each successful 'take' request. For example, if a task successfully 'takes' the same mutex 5 times then the mutex will not be available to any other task until it has also 'given' the mutex back exactly five times.
Parameters:
-
xMutex
A handle to the mutex being obtained. This is the handle returned by xSemaphoreCreateRecursiveMutex().
-
xTicksToWait
The time in ticks to wait for the semaphore to become available. The macro portTICK_PERIOD_MS can be used to convert this to a real time. A block time of zero can be used to poll the semaphore. If the task already owns the semaphore then xSemaphoreTakeRecursive() will return immediately no matter what the value of xTicksToWait.
Returns:
pdTRUE if the semaphore was obtained. pdFALSE if xTicksToWait expired without the semaphore becoming available.
Example usage:
1 SemaphoreHandle_t xMutex = NULL;23 // A task that creates a mutex.4 void vATask( void * pvParameters )5 {6 // Create the mutex to guard a shared resource.7 xMutex = xSemaphoreCreateRecursiveMutex();8 }910 // A task that uses the mutex.11 void vAnotherTask( void * pvParameters )12 {13 // ... Do other things.1415 if( xMutex != NULL )16 {17 // See if we can obtain the mutex. If the mutex is not available18 // wait 10 ticks to see if it becomes free.19 if( xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 ) == pdTRUE )20 {21 // We were able to obtain the mutex and can now access the22 // shared resource.2324 // ...25 // For some reason due to the nature of the code further calls to26 // xSemaphoreTakeRecursive() are made on the same mutex. In real27 // code these would not be just sequential calls as this would make28 // no sense. Instead the calls are likely to be buried inside29 // a more complex call structure.30 xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );31 xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );3233 // The mutex has now been 'taken' three times, so will not be34 // available to another task until it has also been given back35 // three times. Again it is unlikely that real code would have36 // these calls sequentially, but instead buried in a more complex37 // call structure. This is just for illustrative purposes.38 xSemaphoreGiveRecursive( xMutex );39 xSemaphoreGiveRecursive( xMutex );40 xSemaphoreGiveRecursive( xMutex );4142 // Now the mutex can be taken by other tasks.43 }44 else45 {46 // We could not obtain the mutex and can therefore not access47 // the shared resource safely.48 }49 }50 }