preetkang wrote on Saturday, March 02, 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