The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
futex.c File Reference

futex wait and wake (futex.h), and SYS_futex. More...

Macros

#define FUTEX_WAIT   0
#define FUTEX_WAKE   1
#define FUTEX_PRIVATE_FLAG   128
#define FUTEX_QUEUES   16

Functions

void futex_init (void)
 set up the queues.
int futex_wake (struct user_space *space, uintptr_t uaddr, int count)
 wake threads that sleep on a futex.
syscall_ret_t sys_futex (struct registers *regs)
 SYS_futex: args: uaddr (an aligned 32-bit word), op, val, timeout.

Detailed Description

futex wait and wake (futex.h), and SYS_futex.

the waiters sleep on one of a few timed queues, chosen by the futex address. a wake takes the waiters of its futex off the queue and leaves the others. a waiter with a timeout also wakes when the queue's deadline comes (with the others on that queue, which sleep again).

each queue has a lock (a hash bucket, as in Linux). a waiter holds it from the check of the word until it is asleep; a waker changes the word first, then takes the lock, so a wake between the check and the sleep cannot be lost.

Macro Definition Documentation

◆ FUTEX_WAIT

#define FUTEX_WAIT   0

◆ FUTEX_WAKE

#define FUTEX_WAKE   1

◆ FUTEX_PRIVATE_FLAG

#define FUTEX_PRIVATE_FLAG   128

◆ FUTEX_QUEUES

#define FUTEX_QUEUES   16

Function Documentation

◆ futex_init()

void futex_init ( void )

set up the queues.

call once at boot, before the first futex call.

◆ futex_wake()

int futex_wake ( struct user_space * space,
uintptr_t uaddr,
int count )

wake threads that sleep on a futex.

Parameters
spacethe address space of the futex.
uaddrits user address.
countthe most threads to wake.
Returns
the number of threads woken.

◆ sys_futex()

syscall_ret_t sys_futex ( struct registers * regs)

SYS_futex: args: uaddr (an aligned 32-bit word), op, val, timeout.

FUTEX_WAIT sleeps while *uaddr == val, for at most *timeout (a relative struct timespec; NULL: no limit). FUTEX_WAKE wakes at most val threads and returns how many it woke. FUTEX_PRIVATE_FLAG may be set: every futex is private to its process here. returns as above, -EINVAL (a bad address or time), -ENOSYS (another op).