Hello,
I have a pretty generic question about embedded RTOS systems and data integrity.
I'm using RTX and I have several instances of structures that need to be read/written from several task.
As an example, take:
struct demo { int var_a; int var_b; int var_a_max; int var_a_min; }
If I have several global demo instances, what is the 'danger' in allowing several tasks to R/W the global instances?
I understand that the values could change mid execution but say you implemented a simple "lock and copy" procedure before each tasks loop execution.
Would mailboxes really be required in this case?
Further, what if all tasks only ever read/write one of the fields (var_a) do you even need a lock and copy or would simple copy do?
Finally, say I did want to implement a generic mailbox pattern so that any task could request a copy from a managing task and send updates to a managing task. Does any one have an example of such a pattern?
Thanks. I appreciate any discussion and opinion on this topic, I'm looking for the most efficient (time and complexity) way to proceed while still being 'safe' in my logic.
M
For writes I would use a semaphore, not a mailbox, to make sure only one task can write at a time. For reads I would figure out whether the read is atomic to avoid seeing intermediate results in case of a preemption in the middle of a modification.
For Cortex M3 I think 32 bit reads and writes are generally atomic.
If each task only writes certain fields and only reads certain others you can probably get away without any synchronization but I would definitely look at the assembly code to make sure you know what's going on.
It's also important to understand which tasks can even interrupt others, most embeeded RTOSes use fixed priorities so you can make assumptions who gets to finish their task and who gets interrupted.
Andrew