staticinline __always_inline int FutexWithTimeout(volatilevoid* ftx, int op, int value, bool use_realtime_clock, const timespec* abs_timeout, int bitset) { // pthread's and semaphore's default behavior is to use CLOCK_REALTIME, however this behavior is // essentially never intended, as that clock is prone to change discontinuously. // // What users really intend is to use CLOCK_MONOTONIC, however only pthread_cond_timedwait() // provides this as an option and even there, a large amount of existing code does not opt into // CLOCK_MONOTONIC. // // We have seen numerous bugs directly attributable to this difference. Therefore, we provide // this general workaround to always use CLOCK_MONOTONIC for waiting, regardless of what the input // timespec is.
timespec converted_timeout; if (abs_timeout) { if ((op & FUTEX_CMD_MASK) == FUTEX_LOCK_PI) { if (!use_realtime_clock) {
realtime_time_from_monotonic_time(converted_timeout, *abs_timeout);
abs_timeout = &converted_timeout;
}
} else {
op &= ~FUTEX_CLOCK_REALTIME; if (use_realtime_clock) {
monotonic_time_from_realtime_time(converted_timeout, *abs_timeout);
abs_timeout = &converted_timeout;
}
} if (abs_timeout->tv_sec < 0) { return -ETIMEDOUT;
}
}
int __futex_pi_lock_ex(volatilevoid* ftx, bool shared, bool use_realtime_clock, const timespec* abs_timeout) { // We really want FUTEX_LOCK_PI2 which is default CLOCK_MONOTONIC, but that isn't supported // on linux before 5.14. FUTEX_LOCK_PI uses CLOCK_REALTIME. Here we verify support.
static atomic_int lock_op = 0; int op = atomic_load_explicit(&lock_op, memory_order_relaxed); if (op == 0) {
uint32_t tmp = 0; if (__futex(&tmp, FUTEX_LOCK_PI2, 0, nullptr, 0) == 0) {
__futex(&tmp, FUTEX_UNLOCK_PI, 0, nullptr, 0);
op = FUTEX_LOCK_PI2;
} else {
op = FUTEX_LOCK_PI;
}
atomic_store_explicit(&lock_op, op, memory_order_relaxed);
}
if (!shared) op |= FUTEX_PRIVATE_FLAG; return FutexWithTimeout(ftx, op, 0/* value */, use_realtime_clock, abs_timeout, 0 /* bitset */);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-28)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.