xSemaphoreCreateRecursiveMutex
[Semaphores]

semphr. h
SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( void )
Creates a recursive mutex, and returns a handle by which the mutex can be referenced. Recursive mutexes cannot be used in interrupt service routines. configSUPPORT_DYNAMIC_ALLOCATION and configUSE_RECURSIVE_MUTEXES must both be set to 1 in FreeRTOSConfig.h for xSemaphoreCreateRecursiveMutex() to be available (configSUPPORT_DYNAMIC_ALLOCATION can also be left undefined, in which case it will defailt to 1).

Each recursive mutex require a small amount of RAM that is used to hold the recursive mutex’s state. If a mutex is created using xSemaphoreCreateRecursiveMutex() then the required RAM is automatically allocated from the FreeRTOS heap. If a recursive mutex is created using xSemaphoreCreateRecursiveMutexStatic() then the RAM is provided by the application writer, which requires an additional parameter, but allows the RAM to be statically allocated at compile time. See the Static Vs Dynamic allocation page for more information.

Recursive mutexes are ‘taken’ (obtained) using the xSemaphoreTakeRecursive() and given (released) using the xSemaphoreGiveRecursive() API functions respectively. xSemaphoreTake() and xSemaphoreGive() must not be used.

Non-recursive mutexes are created using xSemaphoreCreateMutex() and xSemaphoreCreateMutexStatic(). A non-recursive mutex can only be ‘taken’ by a task once. Any attempt by a task to take a non-recursive mutex that it already holds will fail – and the mutex will always be given back the first time the task ‘gives’ the mutex.

Contrary to non-recursive mutexes, a task can ‘take’ a recursive mutex multiple times, and the recursive mutex will only be returned after the holding task has ‘given’ the mutex the same number of times it ‘took’ the mutex.

Like non-recursive mutexes, recursive mutexes implement a priority inheritance algorithm. The priority of a task that ‘takes’ a mutex will be temporarily raised if another task of higher priority attempts to obtain the same mutex. The task that owns the mutex ‘inherits’ the priority of the task attempting to ‘take’ the same mutex. This means the mutex must always be ‘given’ back – otherwise the higher priority task will never be able to obtain the mutex, and the lower priority task will never ‘disinherit’ the priority.

Returns:
If the recursive mutex was created successfully then a handle to the created mutex is returned. If the recursive mutex was not created because the memory required to hold the mutex could not be allocated then NULL is returned.

Example usage:

 SemaphoreHandle_t xMutex;

 void vATask( void * pvParameters )
 {
    Create a recursive mutex.
    xMutex = xSemaphoreCreateRecursiveMutex();

    if( xMutex != NULL )
    {
        /* The recursive mutex was created successfully and
        can now be used. */
    }
 }
 




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS v10.2.1 is available for immediate download. MIT licensed, includes 64-bit RISC-V, NXP Cortex-M33 demo & Nuvoton Cortex-M23 demo & STM32H745 dual core (AMP) demo.

New MIT licensed IoT MQTT and IoT Task Pool libraries now available. Includes demo projects.

View a recording of the “OTA Update Security and Reliability” webinar, presented by TI and AWS.

Careers

FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners
ARM Connected RTOS partner for all ARM microcontroller cores
Espressif ESP32
IAR Partner
Microchip Premier RTOS Partner
Renesas
STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0
Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers
OpenRTOS and SafeRTOS
Xilinx Microblaze and Zynq partner