Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Updated Jun 2025

xSemaphoreTakeRecursive

[Semaphores]

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;
2
3 // 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 }
9
10 // A task that uses the mutex.
11 void vAnotherTask( void * pvParameters )
12 {
13 // ... Do other things.
14
15 if( xMutex != NULL )
16 {
17 // See if we can obtain the mutex. If the mutex is not available
18 // 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 the
22 // shared resource.
23
24 // ...
25 // For some reason due to the nature of the code further calls to
26 // xSemaphoreTakeRecursive() are made on the same mutex. In real
27 // code these would not be just sequential calls as this would make
28 // no sense. Instead the calls are likely to be buried inside
29 // a more complex call structure.
30 xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
31 xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
32
33 // The mutex has now been 'taken' three times, so will not be
34 // available to another task until it has also been given back
35 // three times. Again it is unlikely that real code would have
36 // these calls sequentially, but instead buried in a more complex
37 // call structure. This is just for illustrative purposes.
38 xSemaphoreGiveRecursive( xMutex );
39 xSemaphoreGiveRecursive( xMutex );
40 xSemaphoreGiveRecursive( xMutex );
41
42 // Now the mutex can be taken by other tasks.
43 }
44 else
45 {
46 // We could not obtain the mutex and can therefore not access
47 // the shared resource safely.
48 }
49 }
50 }