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


Mutex and tasks with different priorities

Posted by mrazoun on September 13, 2016

Hello, I have two tasks which use the same serial port. I prepared two functions one for "locking", second for "unlocking" (see example bellow). I used the mutex "serLockMutex" for synchronization of usage of serial port. These functions are called from both tasks. When both tasks have the same priority, everything work's well. When I use higher priority for TASK1, there is a problem. TASK1 (priority 2) don't stop on xSemaphoreTake() to wait when the TASK2 (priority 1) released the mutex "serLockMutex". Many thank's in advance for your support.


Example of functions, usefull for only one serial channel SemaphoreHandle_t serLockMutex = NULL;

~~~ void serLockFreeRTOS (unsigned short channel, TaskHandlet notifyToTask) { if (serLockMutex == NULL) { serLockMutex = xSemaphoreCreateMutex(); vQueueAddToRegistry( serLockMutex, "LockMutex" ); } if (serLockMutex == NULL) { MACERRORLOOP; } **xSemaphoreTake( serLockMutex, portMAXDELAY );** ser[channel].notify = notifyToTask; }

void serUnLockFreeRTOS (unsigned short channel) { xSemaphoreGive (serLockMutex); ser[channel].notify = NULL; } ~~~

Mutex and tasks with different priorities

Posted by rtel on September 13, 2016

To make sure I understand what you are reporting - are you saying that when the tasks have different priorities the call to xSemaphoreTake( serLockMutex, portMAX_DELAY ); returns even when it doesn't have the mutex? Does the function return pdTRUE, so it thinks it has the mutex? If this is the case then there could be a logic error somewhere in the code where you are calling these functions, and the task that has the mutex is not the one you think it is - or it could be a simple data corruption somewhere. Do you have configASSERT() defined (and are you using a recent version of FreeRTOS, which has a lot more assert points to trap potential configuration errors)?

Not sure that it is related to your issue, but from the very brief snippet of code you have posted it seems you might have a logic error that could result in a race condition.

In serUnLockFreeRTOS() you give the semaphore back before setting ser[channel].notify to NULL. Is that correct?

Mutex and tasks with different priorities

Posted by mrazoun on September 13, 2016

Thank's for your quick answer. I found my mistake. The problem was with giving back mutex before setting ser[channel].notify to NULL. We are moving our project from old one cooperative OS to FreeRTOS and I need more time to understand what really means PREEMPTIVE. Many thank's one more. Zdenek

[ 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