FUTEX_LOCK_PI(2const)
β¨ FUTEX_LOCK_PI β¨
okay bestie, welcome to FUTEX_LOCK_PI(2const) β the priority-inheritance futex lock, section 2const, and yes it's giving main character energy π₯
π NAME π
FUTEX_LOCK_PI β lock a priority-inheritance futex π
π LIBRARY π
Standard C library (libc, -lc) β shoutout to libc fr fr π«Ά
π§ SYNOPSIS π§
#include <linux/futex.h> /* Definition of FUTEX_* constants */
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <unistd.h>
long syscall(SYS_futex, uint32_t *uaddr, FUTEX_LOCK_PI, 0,
const struct timespec *timeout);
πΈ DESCRIPTION πΈ
So here's the vibe: you already tried to grab the lock with an atomic user-mode instruction, and it failed because the futex word had a nonzero value β specifically because it was holding the (PID-namespace-specific) TID of the lock owner. That's when you call this. No cap.
The operation peeks at the futex word sitting at the address uaddr. Two paths, bestie:
- If the value is 0, the kernel tries to atomically set the futex value to the caller's TID. slay.
- If the futex word's value is nonzero, the kernel atomically sets the
FUTEX_WAITERSbit, which is basically the kernel telling the futex owner "girl you cannot unlock this in user space atomically by setting the futex value to 0." π
After that little move, the kernel:
- Tries to find the thread which is associated with the owner TID. π
- Creates or reuses kernel state on behalf of the owner. (If this is the first waiter, there is no kernel state for this futex, so kernel state is created by locking the RT-mutex and the futex owner is made the owner of the RT-mutex. If there are already existing waiters, then the existing state is reused.) π§
- Attaches the waiter to the futex β i.e., the waiter is enqueued on the RT-mutex waiter list. π
If more than one waiter exists, the enqueueing of the waiter is in descending priority order. (For the tea on priority ordering, go read the discussion of the SCHED_DEADLINE, SCHED_FIFO, and SCHED_RR scheduling policies in sched(7).) π
And the owner inherits either the waiter's CPU bandwidth (if the waiter is scheduled under the SCHED_DEADLINE policy) or the waiter's priority (if the waiter is scheduled under the SCHED_RR or SCHED_FIFO policy). This inheritance follows the lock chain in the case of nested locking and performs deadlock detection. iconic behavior honestly π
The timeout argument gives the lock attempt a timeout. If timeout is not NULL, the structure it points to specifies an absolute timeout. If timeout is NULL, the operation will block indefinitely. π΄
π RETURN VALUE π
On error, -1 is returned, and errno is set to indicate the error. π
On success, FUTEX_LOCK_PI returns 0 if the futex was successfully locked. periodt β
π ERRORS π
See futex(2). (go read it, iykyk)
EAGAINβ The futex owner thread ID ofuaddris about to exit, but has not yet handled the internal state cleanup. Try again. πEDEADLKβ The futex word atuaddris already locked by the caller. girl that's you, you already own it πEFAULTβtimeoutdid not point to a valid user-space address. π«EINVALβ The suppliedtimeoutargument was invalid (tv_secwas less than zero, ortv_nsecwas not less than 1,000,000,000). β°EINVALβ The kernel detected an inconsistency between the user-space state atuaddrand the kernel state. This indicates either state corruption or that the kernel found a waiter onuaddrwhich is waiting viaFUTEX_WAIT(2const)orFUTEX_WAIT_BITSET(2const). π΅ENOMEMβ The kernel could not allocate memory to hold state information. no room bestie π§ENOSYSβ A run-time check determined that the operation is not available. The PI-futex operations are not implemented on all architectures and are not supported on some CPU variants. lowkey tragic πEPERMβ The caller is not allowed to attach itself to the futex atuaddr. (This may be caused by a state corruption in user space.) πESRCHβ The thread ID in the futex word atuaddrdoes not exist. who even is she π»ETIMEDOUTβ The timeout expired before the operation completed. ran out of time fr fr β
π STANDARDS π
Linux. that's it, that's the standard π§
π¦ HISTORY π¦
Linux 2.6.18. vintage but make it iconic π
π CAVEATS π
Unlike otherfutex(2)operations, thetimeoutis measured against theCLOCK_REALTIMEclock. β οΈ like, yes, really β don't @ me, that's just how it is.
π SEE ALSO π
okay and for the cute sign-off: this gorgeous little page comes from Linux man-pages 6.20, dated 2025-12-25 π β happy reading bestie, stay locked and loaded πβ¨π¦
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms