Add docs for semaphore.h

This commit is contained in:
Peter Limkilde Svendsen
2024-10-31 23:58:52 +00:00
committed by Jeremy Soller
parent 5135a43e9e
commit 8367249c1a
+46 -32
View File
@@ -1,5 +1,10 @@
//! `semaphore.h` implementation.
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/semaphore.h.html>.
use crate::platform::types::*;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/semaphore.h.html>.
// TODO: Statically verify size and align
#[repr(C)]
#[derive(Clone, Copy)]
@@ -9,6 +14,28 @@ pub union sem_t {
}
pub type RlctSempahore = crate::sync::Semaphore;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_close.html>.
// #[no_mangle]
pub unsafe extern "C" fn sem_close(sem: *mut sem_t) -> c_int {
todo!("named semaphores")
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_destroy.html>.
#[no_mangle]
pub unsafe extern "C" fn sem_destroy(sem: *mut sem_t) -> c_int {
core::ptr::drop_in_place(sem.cast::<RlctSempahore>());
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_getvalue.html>.
#[no_mangle]
pub unsafe extern "C" fn sem_getvalue(sem: *mut sem_t, sval: *mut c_int) -> c_int {
sval.write(get(sem).value() as c_int);
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_init.html>.
#[no_mangle]
pub unsafe extern "C" fn sem_init(sem: *mut sem_t, _pshared: c_int, value: c_uint) -> c_int {
sem.cast::<RlctSempahore>().write(RlctSempahore::new(value));
@@ -16,12 +43,7 @@ pub unsafe extern "C" fn sem_init(sem: *mut sem_t, _pshared: c_int, value: c_uin
0
}
#[no_mangle]
pub unsafe extern "C" fn sem_destroy(sem: *mut sem_t) -> c_int {
core::ptr::drop_in_place(sem.cast::<RlctSempahore>());
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_open.html>.
// TODO: va_list
// #[no_mangle]
pub unsafe extern "C" fn sem_open(
@@ -31,30 +53,7 @@ pub unsafe extern "C" fn sem_open(
todo!("named semaphores")
}
// #[no_mangle]
pub unsafe extern "C" fn sem_close(sem: *mut sem_t) -> c_int {
todo!("named semaphores")
}
// #[no_mangle]
pub unsafe extern "C" fn sem_unlink(name: *const c_char) -> c_int {
todo!("named semaphores")
}
#[no_mangle]
pub unsafe extern "C" fn sem_wait(sem: *mut sem_t) -> c_int {
get(sem).wait(None);
0
}
#[no_mangle]
pub unsafe extern "C" fn sem_trywait(sem: *mut sem_t) -> c_int {
get(sem).try_wait();
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_post.html>.
#[no_mangle]
pub unsafe extern "C" fn sem_post(sem: *mut sem_t) -> c_int {
get(sem).post(1);
@@ -62,9 +61,24 @@ pub unsafe extern "C" fn sem_post(sem: *mut sem_t) -> c_int {
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_trywait.html>.
#[no_mangle]
pub unsafe extern "C" fn sem_getvalue(sem: *mut sem_t, sval: *mut c_int) -> c_int {
sval.write(get(sem).value() as c_int);
pub unsafe extern "C" fn sem_trywait(sem: *mut sem_t) -> c_int {
get(sem).try_wait();
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_unlink.html>.
// #[no_mangle]
pub unsafe extern "C" fn sem_unlink(name: *const c_char) -> c_int {
todo!("named semaphores")
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_trywait.html>.
#[no_mangle]
pub unsafe extern "C" fn sem_wait(sem: *mut sem_t) -> c_int {
get(sem).wait(None);
0
}