Skip to content
Snippets Groups Projects
Verified Commit 5ae7b7ef authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Use new semaphore to prevent spinning

parent 2f69f0e7
No related branches found
No related tags found
No related merge requests found
Pipeline #8848 failed
...@@ -16,15 +16,10 @@ use crate::{ ...@@ -16,15 +16,10 @@ use crate::{
types::{c_int, c_uint, c_void, pid_t, size_t}, types::{c_int, c_uint, c_void, pid_t, size_t},
Pal, Sys, Pal, Sys,
}, },
sync::Mutex, sync::{Mutex, Semaphore},
ALLOCATOR, ALLOCATOR,
}; };
pub struct Semaphore {
lock: Mutex<()>,
count: i32,
}
type pte_osThreadHandle = pid_t; type pte_osThreadHandle = pid_t;
type pte_osMutexHandle = *mut Mutex<()>; type pte_osMutexHandle = *mut Mutex<()>;
type pte_osSemaphoreHandle = *mut Semaphore; type pte_osSemaphoreHandle = *mut Semaphore;
...@@ -324,10 +319,7 @@ pub unsafe extern "C" fn pte_osSemaphoreCreate( ...@@ -324,10 +319,7 @@ pub unsafe extern "C" fn pte_osSemaphoreCreate(
initialValue: c_int, initialValue: c_int,
pHandle: *mut pte_osSemaphoreHandle, pHandle: *mut pte_osSemaphoreHandle,
) -> pte_osResult { ) -> pte_osResult {
*pHandle = Box::into_raw(Box::new(Semaphore { *pHandle = Box::into_raw(Box::new(Semaphore::new(initialValue)));
lock: Mutex::new(()),
count: initialValue,
}));
PTE_OS_OK PTE_OS_OK
} }
...@@ -342,9 +334,7 @@ pub unsafe extern "C" fn pte_osSemaphorePost( ...@@ -342,9 +334,7 @@ pub unsafe extern "C" fn pte_osSemaphorePost(
handle: pte_osSemaphoreHandle, handle: pte_osSemaphoreHandle,
count: c_int, count: c_int,
) -> pte_osResult { ) -> pte_osResult {
let semaphore = &mut *handle; (*handle).post();
let _guard = semaphore.lock.lock();
intrinsics::atomic_xadd(&mut semaphore.count, 1);
PTE_OS_OK PTE_OS_OK
} }
...@@ -354,17 +344,7 @@ pub unsafe extern "C" fn pte_osSemaphorePend( ...@@ -354,17 +344,7 @@ pub unsafe extern "C" fn pte_osSemaphorePend(
pTimeout: *mut c_uint, pTimeout: *mut c_uint,
) -> pte_osResult { ) -> pte_osResult {
//TODO: pTimeout //TODO: pTimeout
let semaphore = &mut *handle; (*handle).wait();
loop {
{
let _guard = semaphore.lock.lock();
if intrinsics::atomic_load(&semaphore.count) > 0 {
intrinsics::atomic_xsub(&mut semaphore.count, 1);
break;
}
}
Sys::sched_yield();
}
PTE_OS_OK PTE_OS_OK
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment