瀏覽代碼

Use new semaphore to prevent spinning

Jeremy Soller 4 年之前
父節點
當前提交
5ae7b7efe7
共有 1 個文件被更改,包括 4 次插入24 次删除
  1. 4 24
      src/platform/pte.rs

+ 4 - 24
src/platform/pte.rs

@@ -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(
     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 {
-        lock: Mutex::new(()),
-        count: initialValue,
-    }));
+    *pHandle = Box::into_raw(Box::new(Semaphore::new(initialValue)));
     PTE_OS_OK
     PTE_OS_OK
 }
 }
 
 
@@ -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;
-    let _guard = semaphore.lock.lock();
-    intrinsics::atomic_xadd(&mut semaphore.count, 1);
+    (*handle).post();
     PTE_OS_OK
     PTE_OS_OK
 }
 }
 
 
@@ -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;
-    loop {
-        {
-            let _guard = semaphore.lock.lock();
-            if intrinsics::atomic_load(&semaphore.count) > 0 {
-                intrinsics::atomic_xsub(&mut semaphore.count, 1);
-                break;
-            }
-        }
-        Sys::sched_yield();
-    }
+    (*handle).wait();
     PTE_OS_OK
     PTE_OS_OK
 }
 }