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


Guarded Resources

Posted by Mathias Zenger on July 10, 2009
In my application I have several tasks which need to share data. So far I have just defined some global variables (e.g. byte, word or long size variables and structures) without considering about MUTEX. In the different C files I have referenced the variables with the key word external.

Since I know that this is no proper way for a multitasking system I would like to clean up my design. The resources should be accessed exclusively. Of course I would like to avoid priority inversion or dead locks. Do you think it could be a possible solution to write "setter" and "getter" functions for each resource? Example:

static xSemaphoreHandle xMyTaskMutex = xSemaphoreCreateMutex();
static unsigned long ulMyTaskSharedLong = 0;

void ulMyTaskSetSharedLong(unsigned long param) {
xSemaphoreTake(xMyTaskMutex, portMAX_DELAY) {
ulMyTaskSharedLong = param;

unsigned long ulMyTaskGetSharedLong() {
return ulMyTaskSharedLong;

Do you think this could work? Any better (simpler, saver) solutions? Thanks!
Regards, M.

RE: Guarded Resources

Posted by Dave on July 10, 2009
First consider if the variables need guarding at all. This will depend on their size and where they are read from and written to. If the size is the natural word size of the MCU, and the variable is only written to from one place, then you might not need any guards in place.

RE: Guarded Resources

Posted by Mathias Zenger on July 13, 2009
Thanks for the comment. Perhaps I am chasing shadows. You are right: if word size matches then the operations become atomic. On my AVR32 system the context switches performed by FreeRTOS save/restore all 32 general purpose registers, the staus register, the program counter and the two stack pointer registers. Obviously the kernel does the main work anyway. However, do you think using mutex for writing shared resources is appropriate? What cases do you consider as critical and how do you handle them in your projects?

[ 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