Skip to main content

hermit/syscalls/
recmutex.rs

1use alloc::boxed::Box;
2
3use crate::errno::Errno;
4use crate::synch::recmutex::RecursiveMutex;
5
6#[hermit_macro::system(errno)]
7#[unsafe(no_mangle)]
8pub unsafe extern "C" fn sys_recmutex_init(recmutex: *mut *mut RecursiveMutex) -> i32 {
9	if recmutex.is_null() {
10		return -i32::from(Errno::Inval);
11	}
12
13	// Create a new boxed recursive mutex and return a pointer to the raw memory.
14	let boxed_mutex = Box::new(RecursiveMutex::new());
15	unsafe {
16		*recmutex = Box::into_raw(boxed_mutex);
17	}
18
19	0
20}
21
22#[hermit_macro::system(errno)]
23#[unsafe(no_mangle)]
24pub unsafe extern "C" fn sys_recmutex_destroy(recmutex: *mut RecursiveMutex) -> i32 {
25	if recmutex.is_null() {
26		return -i32::from(Errno::Inval);
27	}
28
29	// Consume the pointer to the raw memory into a Box again
30	// and drop the Box to free the associated memory.
31	unsafe {
32		drop(Box::from_raw(recmutex));
33	}
34
35	0
36}
37
38#[hermit_macro::system(errno)]
39#[unsafe(no_mangle)]
40pub unsafe extern "C" fn sys_recmutex_lock(recmutex: *mut RecursiveMutex) -> i32 {
41	if recmutex.is_null() {
42		return -i32::from(Errno::Inval);
43	}
44
45	let mutex = unsafe { &*recmutex };
46	mutex.acquire();
47
48	0
49}
50
51#[hermit_macro::system(errno)]
52#[unsafe(no_mangle)]
53pub unsafe extern "C" fn sys_recmutex_unlock(recmutex: *mut RecursiveMutex) -> i32 {
54	if recmutex.is_null() {
55		return -i32::from(Errno::Inval);
56	}
57
58	let mutex = unsafe { &*recmutex };
59	mutex.release();
60
61	0
62}