Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


taking a mutex multiple times

Posted by Jesper Matthiesen on September 18, 2008
the mutex documentation says:
"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. An example of a mutex being used to implement mutual exclusion is provided on the xSemaphoreTake() documentation page. "

which implies that a task can take a mutex as many times as it wants and then give it back once, when done with it..

Is this a correct observation? (note: i'm talking regular mutexes, not recursive ones.)


RE: taking a mutex multiple times

Posted by Dave on September 18, 2008
Mutexes are like binary semaphores in that they are either available or not. You can take it as many times as you like but only the first take call will return pdTRUE assuming the mutex was available. Following calls will return pdFALSE until you have given the semaphore back.

RE: taking a mutex multiple times

Posted by Jesper Matthiesen on September 18, 2008
ok.. what i was after was a "reentrant" mutex that can be taken succesfully an infinite number of times by the same task (assuming it is initially available)..

RE: taking a mutex multiple times

Posted by Dave on September 18, 2008
I think that would be easy enough to implement. The mutuex holder is saved in the pxMutexHolder member so if the same task takes the mutuex but is already the mutuex holder it can return true. It might be better to keep the FreeRTOS code unchanged, and provide your own wrapper to the take function to add in this extra functionality.

RE: taking a mutex multiple times

Posted by Jesper Matthiesen on October 3, 2008
That looks easy enough, yah... only thing is, the xQUEUE type is private to FreeRTOS, so the wrapper would have to be a hack..

richard, if you read this:

maybe it would be an idea to have a function in the API like

xMutexTake(xSemaphoreHandle, portTickType, portBASE_TYPE reentrant);

that would just call xSemaphoreTake if reentrant is false and use xTaskGetCurrentTaskHandle to check pxMutexHolder if true and then skip calling xSemaphoreTake if the current task is already the owner...


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists