Menu

Atomics

Philipp Klaus Krause

volatile sig_atomic_t

The type for which the C standard guarantees that it can be accessed as atomic entitiy, even in the presence of signals (interrupts).
SDCC makes sig_atomic_t an unsiged char, i.e. a single byte. As long as volatile is implemented correctly, this should fulfill the C standard's requirements of reads and writes being atomic. No lock is required.

atomic_flag

C requires this to be a "lock-free" atomic type. Currently the representations of the set and reset states are not fixed in the C standard. But many programmers expect reset to be 0 and set to be 1.
For many targets, SDCC currently uses different representations, typically because it would be impossible or inefficient to use 0 for reset and 1 for set.
Most targets do not have a dedicated test-and-set instruction. For those, we usually use a right-shift with indirect addressing instead, forcing different representations.
For tlcs90, there is tset, but the shift is more efficient. For the Rabbits, bit b, (hl) is a privileged instruction, which makes bit b, (hl) set b(hl) an atomic test-and-set, but again, the shift is more efficient.
For f8 xch is used.
For mcs51 / ds390, we use an atomic_exchange implemented via restartable sequences.

Other atomics

Not yet supported. For a single hardware thread, they could be implemented based on restartable sequences (already supported in the backend for mcs51 / ds390 and used for atomic_flag).


Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.