ogiroux/atomic_wait

What is the reason for using 2 seconds timeout in linux futex

Closed this issue · 1 comments

I can't understand the purpose of using 2 seconds timeout in linux futex syscall, What is the purpose of it?

constexpr timespec timeout = { 2, 0 }; // Hedge on rare 'int version' aliasing.

There is a not-quite theoretical corner case where if the proxy integer overflowed at exactly the wrong time, in exactly the wrong conditions, it could cause this use of Futex to hang if there was no timeout.

In an alternate world where Futex works with 64-bit integers, there would be no need to ever worry about it, the computer would turn back into sand before the condition could occur. But since this world's Futex uses 32-bit integers and it takes ~seconds to wrap around this quantity, we do need to guard against it here.

This 2s time is chosen arbitrarily -- it bounds the apparent hiccup the application would experience (to 2s) in the extremely rare case, and it still suppresses polling to a very high degree (once every 2s is one-in-many-billions suppression). Users who need real-time control should not be using this path anyway, they should be using atomic_signed_lock_free instead, which does not go to a proxy integer like this.