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


Readers-writer lock in FreeRTOS?

Posted by Lars Ole Pontoppidan on January 12, 2010
Hi support forum!

Any recommendations on how to create a readers-writer lock in FreeRTOS?

Basically, I have two threads reading from a datastructure and another thread writing to it occasionally. I want the two reader threads to be able to read simultaneously, while ensuring the writer thread can only write when the readers are not reading.

The solution I'm about to implement is to use a counting semaphore with the max-value equalling the number of readers:

rw_sem = 3





However, this will not give me the benefits of a recursive mutex (priority inheritance and that it can be taken multiple times by same thread) and start/stop writing will be pretty slow.

Any other suggestions?

Best regards


RE: Readers-writer lock in FreeRTOS?

Posted by tgarner on January 12, 2010
Why don't you just use a separate mutex for each read task ?
The write task would have to obtain both before it could write.

RE: Readers-writer lock in FreeRTOS?

Posted by Lars Ole Pontoppidan on January 13, 2010
Yes, that would solve the problem, and it may be the best way to go.

But I was hoping for a more generic and lightweight solution to the problem. I have several chunks of data that would benefit from R/W locking and potentially making a mutex for each task for each resource is tedious and might take up quite a bit of ram...

RE: Readers-writer lock in FreeRTOS?

Posted by Richard on January 13, 2010
If one task is writing, and all other tasks are reading, do you need any protection at all?

If the writer is updating multiple members of a structure, and it is essential that the update is effectively atomic - in other words a reader must only read when all the structure members have been updated and are in a consistent state - then protection *is* required.

Likewise, if the variables being updated are larger than the natural word size of the CPU - for example updating 32bit variables on a 16bit CPU - then protection *is* required.

If the updates are only on single variables, AND the variables are the same size as the natural word size on the CPU, AND there is only one writer, then protection probably isn't required.


RE: Readers-writer lock in FreeRTOS?

Posted by Lars Ole Pontoppidan on January 13, 2010
The data chunks are structs with configuration and structs with data, and yes the updates must be atomic. So far in the development I got away with a single mutex for each struct to give one actor exclusive access.

But now I have a situation where I really need more than one task to have read access. Having learned about the readers/writer lock: http://en.wikipedia.org/wiki/Readers-writer_lock I realise that it would solve the present problem, and it would also make my code more elegant and better performing if I could use start/stop-read and start/stop-write for the other structs.

But niceness is not always free and it seems that an ideal R/W lock does cost some resources...

RE: Readers-writer lock in FreeRTOS?

Posted by tgarner on January 13, 2010
If the structs are small and they are read and written by essentially a quick copy operation,
you could just put a critical section ( or suspend all/resume all ) around all reads and writes.
That's about zero resource usage.

RE: Readers-writer lock in FreeRTOS?

Posted by Richard Damon on January 13, 2010
While FreeRTOS doesn't currently have a read/write lock primitive, one of the advantages of open-source code is that someone can write one and submit it to the code base.

[ 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