On x86, atomic RMW instructions like lock add dword [rdi], 1 are implemented using cache locking on modern CPUs. So a cache line is locked for duration of the instruction. This is done by getting the line EXCLUSIVE/MODIFIED state when value is read and the CPU will not respond to MESI requests from other CPU's until the instruction is finished.
There are 2 flavors of concurrent progress conditions, blocking and non-blocking. Atomic RMW instructions are non-blocking. CPU hardware will never sleep or do something else while holding a cache lock (an interrupt happens before or after an atomic RMW, not during), there is a finite (and small) upper bound on the number of steps before a cache line is released.
Non blocking algorithms can be split in 3 flavors in theoretical computer science:
wait free: all threads will make progress in a finite number of steps.
lock free: at least one thread will make progress in a finite number of steps
obstruction free: if there is no contention, a thread will make progress in a finite number of steps
What kind of guarantee does x86 provide?
I guess it is at least lock free; if there is contention, at least one CPU will make progress.
But is x86 wait free for atomic instructions? Is every CPU guaranteed to make progress in a finite number of steps or could it be that one or more CPU's are starved and could potentially be delayed indefinitely?
So what happens when there are multiple cores doing atomic operations on the same cache line?
When multiple threads happen to lock the same cache line, their execution is serialized. This is called write contention due to false sharing.
The single-writer principle stems from this. Writes cannot be performed concurrently, as opposed to reads.
The execution time of atomic read-modify-write instructions themselves is fixed and does not depend on the number of threads contending the cache line. Therefore on x86 they are wait-free population-oblivious.
The upper bound of the time it takes to lock a contended cache line is proportional to how much contention the cache line experiences.
From Intel Community:
Since cache line locking will be continuously retried, eventually all atomic read-modify-write operations will succeed (the operation is the instruction plus the retrying done by the hardware to lock the cache line).
So yes, every CPU is guaranteed to make progress in a finite number of steps, and atomic read-modify-write operations as a whole on x86 are wait-free bounded.
By the same logic, the x86 store operation is wait-free bounded, x86 store instruction is wait-free population-oblivious, and x86 load is always wait-free population-oblivious.
While, as someone suggests, a ucode bug could cause the lock to stay on forever, we do not consider external factors when describing the flavor of an algorithm, but only the logic itself.
Cache line lock acquisition is not fair.
The probability that a thread is selected to acquire the lock is proportional to how close it is to the thread that released the lock. So, threads on the same core are more likely to acquire the lock than threads that share the L2 cache, which are more likely than threads that share the L3 cache. Then, threads on shorter QPI/UPI/NUMA Node paths have an edge over others, and so on.
This holds true for software locks (spin locks) too, since when a release store is issued, it propagates the same way.
I ran a benchmark on Intel Q4'17 desktop CPU that confirms all of the above.
When continuously
lock xadding over the same memory location...lock xadded 2.5 times more than the slowest one, and out of 10 threads running on different two-way hyper-threads it did 3 times morelock xadds take increasingly greater amounts of time, up to 1.1ms for 5 threads running on different cores and up to 193ms for 10 threads running on different two-way hyper-threadsand variance across runs of different processes is high.