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

TaskSuspendAll Vs Mutex

Posted by Jazilla on December 17, 2012
I am new to using an RTOS so please execuse my ignorance.

Why would you use a mutex instead of TaskSuspendAll for task synchronization if I don't need to worry about sync between task and ISR? Initially I though mutex is more efficient but it seems like xSemaphore calls TaskSuspendAll anyway?

/* Interrupts and other tasks can send to and receive from the queue
now the critical section has been exited. */

vTaskSuspendAll();
prvLockQueue( pxQueue );

RE: TaskSuspendAll Vs Mutex

Posted by Richard Damon on December 17, 2012
vTaskSuspendAll() is used to make sure the the following code occurs without interruption by other tasks, but not stopping interrupts from occurring.

A mutex on the other hand only interlocks with other tasks that test the same mutex, and if another task has the mutex, blocks the current task ( vTaskSuspendAll() will never block). Note also that a task that has called vTaskSuspendAll() shouldn't block while a task using a semaphore can safely block (as long as you aren't blocking on something that will require another task to acquire the mutex you hold).

Note that the code you quoted lists the other type of interlock, a critical section, which disables interrupts so that not even interrupts can interfere with the code running.

RE: TaskSuspendAll Vs Mutex

Posted by Jazilla on December 17, 2012
Thanks for the reply richard. What I want to achieve is one thread is writing to the variables and the other thread reads them. The operation is quite short but I need to make sure that a write does not happen while a read is happening (or vice versa). Would a critical section implemented by TaskSuspendAll be appropriote (if I am not worried about ISR)? Could you confirm that the semaphore/mutex all call TaskSuspendAll? I was worried that the TaskResumeAll will take a long time but if it is going to be called anyway I may as well just use it as the critical section is very short.

BTW, the code I copied was in xQueueGenericReceive (queue.c) which is called by xSemaphoreTake.

Many thanks.

RE: TaskSuspendAll Vs Mutex

Posted by Richard Damon on December 17, 2012
If it is only for a few instructions, then a critical section is likely the best option (vTaskEnterCritical). This is basically that disables the interrupts, and then re-enables them when your are done. The limitation is that by disabling interrupts, you increase the interrupt response latency possible, thus best to use only for short actions.

I would need to look closer at the code, but I suspect that a mutex/semaphore call will not always hit the TaskSuspendAll call, and that taking or giving an uncontested mutex will not enter that piece of code, but only due that if it needs to walk the list of tasks to see who needs to be unblocked. User code rarely needs to call TaskSuspendAll directly, but normally is better served with semaphores/mutexes for longer interlocks, and critical sections for the very short ones.


[ 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