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




Loading

Mutex Priority Inversion Anamoly

Posted by Preet Kang on March 2, 2013
Richard,

I'm making videos of FreeRTOS tutorial (http://www.socialledge.com/sjsu/index.php?title=FreeRTOS_Tutorial) and I've noticed the priority inversion anomaly in FreeRTOS. In my demo program below, it makes no difference if the semaphore is a mutex or a binary semaphore. In either case, I see the same output, but I think it lower priority tasks should get equal access.

Output (regardless if semaphore is a mutex or semaphore):
task4 got sem. Will use it ...
task3 got sem. Will use it ...
task4 got sem. Will use it ...
task3 got sem. Will use it ...
task4 got sem. Will use it ...

xSemaphoreHandle sd_card_lock = 0;
void my_task(void *p)
{
while(1) {
if(xSemaphoreTake(sd_card_lock, 999999)) {
printf("%s got sem. Will use it ... \n", (char*)p);
xSemaphoreGive(sd_card_lock);
}
vTaskDelay(1);
}
}
void main( void )
{
sd_card_lock = xSemaphoreCreateMutex();
//vSemaphoreCreateBinary(sd_card_lock);

xTaskCreate(my_task, (signed char*) "t1", 1024, (void*)"task1", 2, NULL );
xTaskCreate(my_task, (signed char*) "t2", 1024, (void*)"task2", 2, NULL );
xTaskCreate(my_task, (signed char*) "t3", 1024, (void*)"task3", 3, NULL );
xTaskCreate(my_task, (signed char*) "t4", 1024, (void*)"task4", 3, NULL );
vTaskStartScheduler();
}

If all tasks have the same priority, then we see every task taking turn, again regardless if the semaphore is a mutex or binary. But with a mutex, it should be the same behavior but it's not :( Can you please educate me?

Preet

RE: Mutex Priority Inversion Anamoly

Posted by Richard Damon on March 2, 2013
Why would you expect the priority 1 or 2 task to every get the semaphore?

When Task 4 gets the semaphore, everyone else lines up on it and waits for it.
When Task 4 gives the semaphore back, Task3, since it is the highest priority task will get it.
While Task 3 is outputing its message, my guess is that Task 4 finishes its 1 tick wait, and then puts itself in line for the semaphore.
When Task 3 finishes, task 4 get the semaphore because it is the highest priority task waiting on the semaphore.

Making the semaphore into a mutex just says that when Task 4 starts its wait on the semaphore, Task 3 gets a temporary boost in priority to that of Task 4 (the priority inversion logic) .

If all tasks are the same priority, then the priority doesn't select which task gets chosen, and among equal priority tasks, the one waiting the longest it chosen first.


[ 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