hermit/syscalls/
futex.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use core::sync::atomic::AtomicU32;

use crate::errno::EINVAL;
use crate::synch::futex::{self as synch, Flags};
use crate::time::timespec;

/// Like `synch::futex_wait`, but does extra sanity checks and takes a `timespec`.
///
/// Returns -EINVAL if
/// * `address` is null
/// * `timeout` is negative
/// * `flags` contains unknown flags
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_futex_wait(
	address: *mut u32,
	expected: u32,
	timeout: *const timespec,
	flags: u32,
) -> i32 {
	if address.is_null() {
		return -EINVAL;
	}

	let address = unsafe { &*(address as *const AtomicU32) };
	let timeout = if timeout.is_null() {
		None
	} else {
		match unsafe { timeout.read().into_usec() } {
			Some(usec) if usec >= 0 => Some(usec as u64),
			_ => return -EINVAL,
		}
	};
	let flags = match Flags::from_bits(flags) {
		Some(flags) => flags,
		None => return -EINVAL,
	};

	synch::futex_wait(address, expected, timeout, flags)
}

/// Like `synch::futex_wake`, but does extra sanity checks.
///
/// Returns -EINVAL if `address` is null.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_futex_wake(address: *mut u32, count: i32) -> i32 {
	if address.is_null() {
		return -EINVAL;
	}

	let address = unsafe { &*(address as *const AtomicU32) };
	synch::futex_wake(address, count)
}