4 ms·
How is CAS guaranteed to be atomic? Is it actually implemented by a machine instruction, rather than the pseudocode in the article?
by dmoney 17y ago
How is CAS guaranteed to be atomic? Is it actually implemented by a machine instruction, rather than the pseudocode in the article?
- rdtsc 17y agoYes, it is implemented by the hardware. In x86 I believe it is the CMPXCHG assembler instruction. GCC, for example has __sync_val_compare_and_swap(...) function that give you access to the hardware compare and swap.
- Freaky 17y agoYes, look up cmpxchg and friends. FreeBSD has atomic.h providing a bunch of different atomic ops, e.g: http://www.freebsd.org/cgi/cvsweb.cgi/src/sys/amd64/include/atomic.h?rev=1.46 http://www.freebsd.org/cgi/cvsweb.cgi/src/sys/amd64/include/... I believe FreeBSD also uses this style of buffer for dmesg, which sadly also results in a lot of interlacing if two kernel threads are trying to write to the buffer at once.