In an embedded (ARM) environment with no OS, if I use interrupts, then is there potential for deadlock using std::atomic<T>
? If so, how?
In general, any moment, control can be interrupted to handle an interrupt. In particular, if one were to naively have a mutex and wanted to use it to do a "safe" to a variable, one might lock it, write, and unlock and then elsewhere lock, read, and unlock. But if the read is in an interrupt, you could lock, interrupt, lock => deadlock.
In particular, I have a std::atomic<int>
for which is_always_lock_free
is false
. Should I worry about the deadlock case? When I look at the generated assembly, writing 42
looks like:
bl __sync_synchronize
mov r3, #42
str r3, [sp, #4]
bl __sync_synchronize
which doesn't appear to be locking. The asm for reading the value is similar. Is the (possible) lock for the fancier operations like exchange
?
__sync_synchronize
is only a memory barrier, then this code only protects against multiple cores re-ordering or pre-fetching instructions. As far as I know, it does not protect against interrupts and the generated assembler is then not interrupt safe.__sync_synchronize
would have to disable all maskable interrupts for this code to be safe. If it does not, I would call the implementation non-conforming. – Lewellen_Atomic int
and see if it generates the same assembler. – Lewellen