This was one of the last required functions missing from Clang's libatomic.
Changes in behaviours:
- On x86-64, it'll now be able to detect and use cmpxchg16b
- The rest should be unchanged, given it'd already been able to figure it out statically.
oontvoo on Jul 31 2020, 1:59 PM.Authored by
There is going to be a bunch more complexity required here, I'm afraid.
Not only do we need to detect the CPU capabilities in order to implement atomic_is_lock_free, but we also need to ensure that the implementations of atomic_load_c / etc can actually use those atomic instructions as appropriate.
This means we need to compile the function with different flags, e.g. -mcx16 for x86-64, in order for the compiler to be able to emit cmpxchg16b, or -march=i586 on 32bit x86. But -- we can't compile the entire file that way. Only the functions called after the runtime detection has assured that we're running on the appropriate hardware can be built with those flags.
GCC handles this by compiling the source files multiple times, with different flags, adding some additional #defines which change the function names for each of those compiles. Then, it arranges to dispatch to the correct function depending on the runtime CPU detection. Ideally, we'd also be able to use the "target" attribute to do this on an individual function within the file, but it looks like that may not actually work properly.