Re: [RFC PATCH 0/2] Add a futex interface to glibc (sys/futex.h)
Adhemerval Zanella Netto <[email protected]>
| Newsgroups | gmane.comp.lib.glibc.alpha |
|---|---|
| Organization | Linaro |
| Message-ID | <[email protected]> |
On 25/08/26 20:58, Carlos O'Donell wrote: > On 8/25/26 12:37 PM, Adhemerval Zanella wrote: >> This patchset adds wrappers for the futex system call through a new >> <sys/futex.h> header. They are futex_wait, futex_timedwait, >> futex_wake, futex_requeue, and futex_waitv. > > I worked through a prototype for this last year after my LPC talk about > creating wrappers for all syscalls. > > I think this is the right direction to go and I can review the wrappers. > > I think we should expose as much as is not deprecated. > >> Exporting a futex interface from libc is a long-standing request, as >> the futex syscall is the blocking building block for essentially >> every custom synchronization primitive on Linux. And since glibc >> provides no wrapper, every project ends up hand-rolling it; with >> per-architecture syscall assembly, ad-hoc time64 syscall selection, >> and workarounds for the syscall's historical quirks duplicated in >> each of them (libstdc++, libc++, libgomp, libitm, the Go runtime, >> OpenMP, allocators, and so on). > > Right. > >> It has also become relevant as the kernel expands the futex interface, >> where futex_waitv and the futex2 syscall family only make sense to >> expose from libc if there is a consistent futex API to build on. > > Agreed. > >> This patchset provides that base operations plus futex_waitv, sharing >> one set of conventions (flags following the kernel FUTEX_PRIVATE_FLAG, >> absolute timeouts with an explicit clockid, -1/errno error reporting, >> no cancellation points), so future futex2 additions are incremental >> rather than a new design. >> >> Only the well-defined subset of the kernel interface is exposed (the >> racy unchecked FUTEX_REQUEUE, the removed FUTEX_FD, FUTEX_WAKE_OP, >> bitset matching, and the PI operations are left out). The >> futex_timedwait supports both time_t sizes with the usual TIME_BITS=64 >> redirection; futex_waitv mirrors the kernel ABI directly (struct layout, >> argument order, FUTEX2* per-waiter flags) and is 64-bit time_t only. >> The wrappers compile down to the same code as a hand-rolled syscall >> (the waits and requeue are a flags check plus a tail call; wake >> inlines the syscall), so there is no cost to adopting them. >> >> To verify the interface's usability, I built a proof of concept that >> converts the two futex users in GCC (libitm and libgomp) to the new >> functions. The required work is minimal: libitm's wrappers become >> three-line calls, and libgomp only needs an asm-label binding because >> its internal wrappers reuse the utex_wait/futex_wake names. >> >> In both cases the per-architecture syscall assembly and the obsolete >> private-to-shared runtime fallbacks become unnecessary. A survey of >> the remaining futex users in GCC and LLVM (libstdc++, libc++, libgo, >> OpenMP, scudo) showed that all of their usage should map onto this >> interface as well. > > Great! I'll wait for a full non-RFC to review. There you go [1]. I am still not fully sure about providing futex_timedwait for 32-bit time_t, although it *might* simplify adoption in some cases [2]. [1] https://patchwork.sourceware.org/project/glibc/list/?series=65564 [2] https://sourceware.org/pipermail/libc-alpha/2026-August/180012.html