FreeRTOS Support Archive
The FreeRTOS support forum is used to obtain active support directly from Real
Time Engineers Ltd. In return for using our top quality software and services for
free, we request you play fair and do your bit to help others too! Sign up
to receive notifications of new support topics then help where you can.
This is a read only archive of threads posted to the FreeRTOS support forum.
The archive is updated every week, so will not always contain the very latest posts.
Use these archive pages to search previous posts. Use the Live FreeRTOS Forum
link to reply to a post, or start a new support thread.
[FreeRTOS Home] [Live FreeRTOS Forum] [FAQ] [Archive Top] [January 2010 Threads] 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: init() { rw_sem = 3 }
start_read() { rw_sem-- }
stop_read() { rw_sem++ }
start_write() { rw_sem-- rw_sem-- rw_sem-- }
stop_write() { rw_sem++ rw_sem++ rw_sem++ }
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 Lars
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.
Regards.
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.
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.
|