git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/futex.rs
//! Futexes: a thread waits while a word of its program's memory holds what
//! it expects, until another thread wakes the waiters on that word. What
//! Rust's std builds its `Mutex`, `Condvar`, `RwLock`, `Once` and thread
//! parking on; the word itself is the program's, so taking a free lock
//! costs no system call.
//!
//! A waiter is its task's slot, keyed by its program and the word's address.
//! The value is checked and the waiter recorded under one lock, which a wake
//! takes too, so a wake that follows a change of the word cannot slip in
//! between and be lost. A woken call runs again (the scheduler rewinds it)
//! and finds the word changed; one that finds it unchanged waits again,
//! which a futex user must expect anyway.
use crate::exceptions::Frame;
use crate::sched;
use crate::sync::SpinLock;
use alloc::collections::BTreeMap;
use core::sync::atomic::{AtomicU32, Ordering};
use pinnacle_abi::{ERROR, TIMEOUT};
/// Each waiting task's slot, with its program (by address) and the word's.
static WAITERS: SpinLock<BTreeMap<usize, (usize, u64)>> = SpinLock::new(BTreeMap::new());
/// The calling program, by the address of what holds it.
fn program() -> Option<usize> {
sched::current_process().map(|p| alloc::sync::Arc::as_ptr(&p) as usize)
}
/// FUTEX_WAIT(address, expected, deadline): the frame to resume, with the
/// answer in x0 when the call returns at once.
pub fn wait(frame: *mut Frame, address: u64, expected: u32, deadline: Option<u64>) -> *mut Frame {
// SAFETY: the vector stub wrote this frame; nothing else holds it.
let f = unsafe { &mut *frame };
let me = sched::current();
let answer = |f: &mut Frame, value: u64| {
f.set_first_result(value);
frame
};
let Some(program) = program() else {
return answer(f, ERROR);
};
if !address.is_multiple_of(4)
|| sched::with_process(|p| p.space.allows(address as usize, 4, false)) != Some(true)
{
return answer(f, ERROR);
}
let mut waiters = WAITERS.lock();
waiters.remove(&me);
// SAFETY: an aligned word the caller's space maps readable, and TTBR0
// holds that space during its call.
let value = unsafe { (*(address as *const AtomicU32)).load(Ordering::SeqCst) };
if value != expected {
return answer(f, 0);
}
if deadline.is_some_and(|d| crate::timer::now_ms() >= d) {
return answer(f, TIMEOUT);
}
waiters.insert(me, (program, address));
drop(waiters);
// A wake between here and the block is kept by the scheduler (`woken`),
// and the block then returns at once.
f.repeat_call();
sched::block(frame, deadline)
}
/// FUTEX_WAKE(address, count): wakes up to `count` of the calling program's
/// waiters on the word. Returns how many it woke.
pub fn wake(address: u64, count: u64) -> u64 {
let Some(program) = program() else { return ERROR };
let mut woken = alloc::vec::Vec::new();
{
let mut waiters = WAITERS.lock();
waiters.retain(|&slot, &mut (p, a)| {
if p == program && a == address && (woken.len() as u64) < count {
woken.push(slot);
false
} else {
true
}
});
}
for &slot in &woken {
// A slot whose thread ended (and was perhaps reused) since it waited
// is left alone: only a thread of this program is woken, and a
// thread woken from some other wait makes that call again.
let ours = sched::process_of(slot).is_some_and(|p| p.as_ptr() as usize == program);
if ours {
sched::wake(slot);
}
}
woken.len() as u64
}