Re:Landing a new syscall, part 1: What is futex?
by chrchang523 on Wednesday February 09, 2022 @03:15PM
(#30277842)
It is worth noting here that this "one assembly instruction" is not that cheap. The hardware on a multicore system does have to perform some locking under the hood to execute that instruction. But yes, it still has enough of an advantage over calling into the kernel to justify the additional usage complexity.
[ Parent
| on HN ]
Re:Landing a new syscall, part 1: What is futex?
by rrauenza on Wednesday February 09, 2022 @09:59PM
(#30281938)
And on ccnuma systems, you end up getting memory contention and huuuuuge memory latencies, as well for the data also residing in the same cacheline. Often we would align and isolate these locks within a cacheline blocke... which also wastes a lot of space if you have a lot of these (I was consulting on an enterprise app that had millions I think. It made a big difference in the software's memory footprint.)
Re:Landing a new syscall, part 1: What is futex?
by chacham15 on Wednesday February 09, 2022 @03:43PM
(#30278215)
Im pretty sure the same construct can be implemented without the compare:
int lock = 0;
void AcquireLock(int *lock){
while (ATOMIC_SWAP(lock, 1)){
sleep(10); //or futex or w/e
}
}
void ReleaseLock(int *lock){
ATOMIC_SWAP(lock, 0);
}
Re:Landing a new syscall, part 1: What is futex?
by ww520 on Wednesday February 09, 2022 @03:51PM
(#30278317)
The 'lock' variable is shared among threads. Compare is needed to avoid stomping on the lock acquired by another thread.
Re:Landing a new syscall, part 1: What is futex?
by dragontamer on Wednesday February 09, 2022 @07:21PM
(#30280794)
No it isn't.
If lock = 1, you set the lock to 1 (aka do nothing).
If the lock is 0, you know it is unlocked and know you succeeded in acquiring the lock.
If many threads try to atomic swap, only one of them gets the zero.
------
The real issue here is the subtle memory barrier bug in that code.