Readers-writer lock in FreeRTOS?

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

Readers-writer lock in FreeRTOS?

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.

Readers-writer lock in FreeRTOS?

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…

Readers-writer lock in FreeRTOS?

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.

Readers-writer lock in FreeRTOS?

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…

Readers-writer lock in FreeRTOS?

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.

Readers-writer lock in FreeRTOS?

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.