Commit 1b16d8db authored by Stefan Lankes's avatar Stefan Lankes Committed by Stefan Lankes
Browse files

introduce a nicer interface to the semaphore

- acquire don't longer expect an absolute time as argument
- a maximum delay from calling the function is passed
parent e4212aa3
......@@ -17,8 +17,7 @@ pub fn netwait(millis: Option<u64>) {
match millis {
Some(ms) => {
if ms > 0 {
let delay = Some(crate::arch::processor::get_timer_ticks() + ms * 1000);
NET_SEM.acquire(delay);
NET_SEM.acquire(Some(ms));
} else {
NET_SEM.try_acquire();
}
......
......@@ -69,11 +69,11 @@ impl Semaphore {
}
/// Acquires a resource of this semaphore, blocking the current thread until
/// it can do so or until the wakeup time has elapsed.
/// it can do so or until the wakeup time (in ms) has elapsed.
///
/// This method will block until the internal count of the semaphore is at
/// least 1.
pub fn acquire(&self, wakeup_time: Option<u64>) -> bool {
pub fn acquire(&self, time: Option<u64>) -> bool {
// Reset last_wakeup_reason.
let core_scheduler = core_scheduler();
core_scheduler.set_current_task_wakeup_reason(WakeupReason::Custom);
......@@ -96,6 +96,11 @@ impl Semaphore {
return false;
}
let wakeup_time = match time {
Some(ms) => Some(crate::arch::processor::get_timer_ticks() + ms * 1000),
_ => None,
};
// We couldn't acquire the semaphore.
// Block the current task and add it to the wakeup queue.
core_scheduler.block_current_task(wakeup_time);
......
......@@ -86,16 +86,15 @@ fn __sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 {
return -EINVAL;
}
// Calculate the absolute wakeup time in processor timer ticks out of the relative timeout in milliseconds.
let wakeup_time = if ms > 0 {
Some(arch::processor::get_timer_ticks() + u64::from(ms) * 1000)
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 };
if semaphore.acquire(wakeup_time) {
if semaphore.acquire(delay) {
0
} else {
-ETIME
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment