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

Is this safe to do?

Posted by Willem on December 3, 2008
I created a task that reads some hardware (ADC) value's and save the data into for example variable X.

Then i have some other tasks like the display, the SD logbook and the USB that needs this variable X only to be read.

I think it is not safe to do this without a semaphore because at the time a task is reading, it could be possible that the variable gets set with new data.

So in my opinion a binary semaphore would be suffice. The semaphore would only be set when the variable gets a write.

But, does it matter that there are several readers tasks that could be interrupting each others read? So if for example the logbook and display both requests at the same time variable X by using the same variable getDataX();

RE: Is this safe to do?

Posted by Dave on December 3, 2008
You describe a system where the variable X gets written to from one place, but read from many different places. When this is the case you only need to worry about mutual exclusion if writes to X are not atomic. For example, if you are running FreeRTOS on a 16 bit CPU and X is 32 bits then it will take two writes to update the value of X, and mutual exclusion is required to ensure nothing reads X between the first and second write. If however you are running FreeRTOS on a 32bit CPU and X is 8, 16 or 32 bits then it will only take one write to update the value of X and you do not need to worry about mutual exclusion.

If you do need mutual exclusion then you can use a simple critical section but only do this if reading X is done relatively infrequently. Otherwise use a mutex.

RE: Is this safe to do?

Posted by Christopher Coley on December 3, 2008
If the mutual exclusion which would be done by doing taskENTER_CRITICAL and taskEXIT_CRITICAL around the piece of code that is updating the X variable would not work.

I would do it with a single deep queue.

I would have the variable X in the context of the tasks that are reading the value with a function that trys to read from the queue with a timeout of 0. As such if the queue is empty no update to the variable X would occur, however if there is an update on the queue the X variable would be updated. Either way the function would return the X variable. All the reading tasks would call this function.

The task that is reading the ADC would put the value into the Queue instead of into X.

RE: Is this safe to do?

Posted by Willem on December 4, 2008
I used the mutex function from this OS. So if the variable needs an update, it first starts taking the mutex but this is only possible if all of the reading tasks are done.

During this update, the reading tasks will be put automatically in a suspend state until update is complete.

Thanks for the reply's!


[ 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