Skip to content

Commit

Permalink
Restored POSIX compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlWachter committed Jun 4, 2024
1 parent fdca6aa commit 9468cb3
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/syscalls/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use crate::synch::semaphore::Semaphore;
use crate::syscalls::{sys_clock_gettime, CLOCK_REALTIME};
use crate::time::timespec;

#[allow(non_camel_case_types)]
pub type sem_t = *const Semaphore;

/// Create a new, unnamed semaphore.
///
/// This function can be used to get the raw memory location of a semaphore.
Expand All @@ -13,7 +16,7 @@ use crate::time::timespec;
/// Returns `0` on success, `-EINVAL` if `sem` is null.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_sem_init(sem: *mut *mut Semaphore, pshared: i32, value: u32) -> i32 {
pub unsafe extern "C" fn sys_sem_init(sem: *mut sem_t, pshared: i32, value: u32) -> i32 {
if sem.is_null() || pshared != 0 {
return -EINVAL;
}
Expand All @@ -33,15 +36,15 @@ pub unsafe extern "C" fn sys_sem_init(sem: *mut *mut Semaphore, pshared: i32, va
/// Returns `0` on success, `-EINVAL` if `sem` is null.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_sem_destroy(sem: *mut Semaphore) -> i32 {
pub unsafe extern "C" fn sys_sem_destroy(sem: *mut sem_t) -> i32 {
if sem.is_null() {
return -EINVAL;
}

// Consume the pointer to the raw memory into a Box again
// and drop the Box to free the associated memory.
unsafe {
drop(Box::from_raw(sem));
drop(Box::from_raw((*sem).cast_mut()));
}
0
}
Expand All @@ -55,13 +58,13 @@ pub unsafe extern "C" fn sys_sem_destroy(sem: *mut Semaphore) -> i32 {
/// Returns `0` on success, or `-EINVAL` if `sem` is null.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_sem_post(sem: *mut Semaphore) -> i32 {
pub unsafe extern "C" fn sys_sem_post(sem: *mut sem_t) -> i32 {
if sem.is_null() {
return -EINVAL;
}

// Get a reference to the given semaphore and release it.
let semaphore = unsafe { &*sem };
let semaphore = unsafe { &**sem };
semaphore.release();
0
}
Expand All @@ -74,29 +77,29 @@ pub unsafe extern "C" fn sys_sem_post(sem: *mut Semaphore) -> i32 {
/// Returns `0` on lock acquire, `-EINVAL` if `sem` is null, or `-ECANCELED` if the decrement fails.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_sem_trywait(sem: *mut Semaphore) -> i32 {
pub unsafe extern "C" fn sys_sem_trywait(sem: *mut sem_t) -> i32 {
if sem.is_null() {
return -EINVAL;
}

// Get a reference to the given semaphore and acquire it in a non-blocking fashion.
let semaphore = unsafe { &*sem };
let semaphore = unsafe { &**sem };
if semaphore.try_acquire() {
0
} else {
-ECANCELED
}
}

unsafe fn sem_timedwait(sem: *mut Semaphore, ms: u32) -> i32 {
unsafe fn sem_timedwait(sem: *mut sem_t, ms: u32) -> i32 {
if sem.is_null() {
return -EINVAL;
}

let delay = if ms > 0 { Some(u64::from(ms)) } else { None };

// Get a reference to the given semaphore and wait until we have acquired it or the wakeup time has elapsed.
let semaphore = unsafe { &*sem };
let semaphore = unsafe { &**sem };
if semaphore.acquire(delay) {
0
} else {
Expand All @@ -111,7 +114,7 @@ unsafe fn sem_timedwait(sem: *mut Semaphore, ms: u32) -> i32 {
/// Returns `0` on lock acquire, `-EINVAL` if sem is null, or `-ETIME` on timeout.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_sem_timedwait(sem: *mut Semaphore, ts: *const timespec) -> i32 {
pub unsafe extern "C" fn sys_sem_timedwait(sem: *mut sem_t, ts: *const timespec) -> i32 {
if ts.is_null() {
unsafe { sem_timedwait(sem, 0) }
} else {
Expand Down

0 comments on commit 9468cb3

Please sign in to comment.