Is core.internal.atomic.atomicFetchAdd implementation really lock free?
mw
mingwu at gmail.com
Wed Nov 30 00:04:22 UTC 2022
According to the doc:
https://dlang.org/library/core/atomic/atomic_fetch_add.html
Atomically adds mod to the value referenced by val and returns
the value val held previously. This operation is both lock-free
and atomic.
However, when I check its final implementation, I saw
https://github.com/dlang/dmd/blob/master/druntime/src/core/internal/atomic.d#L275
```
lock; xadd[%0], %1;
```
Is this really lock free?
If not, can we use `cas` to implement it, e.g pseudo code:
```
int atomicFetchAdd(int * pAddr, int nIncr ) {
while (true) {
int ncur = atomicLoad(pAddr);
if (cas( pAddr, ncur, ncur + nIncr )
return ncur ;
}
}
```
More information about the Digitalmars-d
mailing list