Skip to content

Commit

Permalink
Fix: Semaphor creation
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlWachter committed Jun 4, 2024
1 parent 58a2e3b commit 0e7def7
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/syscalls/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ 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 @@ -16,7 +13,7 @@ pub type sem_t = *const Semaphore;
/// Returns `0` on success, `-EINVAL` if `sem` is null.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_sem_init(sem: *mut sem_t, pshared: i32, value: u32) -> i32 {
pub unsafe extern "C" fn sys_sem_init(sem: *mut *mut Semaphore, pshared: i32, value: u32) -> i32 {
if sem.is_null() || pshared != 0 {
return -EINVAL;
}
Expand All @@ -36,7 +33,7 @@ pub unsafe extern "C" fn sys_sem_init(sem: *mut sem_t, pshared: i32, value: u32)
/// Returns `0` on success, `-EINVAL` if `sem` is null.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_sem_destroy(sem: *mut sem_t) -> i32 {
pub unsafe extern "C" fn sys_sem_destroy(sem: *mut Semaphore) -> i32 {
if sem.is_null() {
return -EINVAL;
}
Expand All @@ -58,13 +55,13 @@ pub unsafe extern "C" fn sys_sem_destroy(sem: *mut sem_t) -> 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 sem_t) -> i32 {
pub unsafe extern "C" fn sys_sem_post(sem: *mut Semaphore) -> 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 @@ -77,29 +74,29 @@ pub unsafe extern "C" fn sys_sem_post(sem: *mut sem_t) -> 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 sem_t) -> i32 {
pub unsafe extern "C" fn sys_sem_trywait(sem: *mut Semaphore) -> 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 sem_t, ms: u32) -> i32 {
unsafe fn sem_timedwait(sem: *mut Semaphore, 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 @@ -114,7 +111,7 @@ unsafe fn sem_timedwait(sem: *mut sem_t, 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 sem_t, ts: *const timespec) -> i32 {
pub unsafe extern "C" fn sys_sem_timedwait(sem: *mut Semaphore, ts: *const timespec) -> i32 {
if ts.is_null() {
unsafe { sem_timedwait(sem, 0) }
} else {
Expand Down

0 comments on commit 0e7def7

Please sign in to comment.