From 5ae7b7efe777af393e486129827850098fcc3daf Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Wed, 23 Dec 2020 12:20:19 -0700
Subject: [PATCH] Use new semaphore to prevent spinning

---
 src/platform/pte.rs | 28 ++++------------------------
 1 file changed, 4 insertions(+), 24 deletions(-)

diff --git a/src/platform/pte.rs b/src/platform/pte.rs
index ea0d209be..39e004582 100644
--- a/src/platform/pte.rs
+++ b/src/platform/pte.rs
@@ -16,15 +16,10 @@ use crate::{
         types::{c_int, c_uint, c_void, pid_t, size_t},
         Pal, Sys,
     },
-    sync::Mutex,
+    sync::{Mutex, Semaphore},
     ALLOCATOR,
 };
 
-pub struct Semaphore {
-    lock: Mutex<()>,
-    count: i32,
-}
-
 type pte_osThreadHandle = pid_t;
 type pte_osMutexHandle = *mut Mutex<()>;
 type pte_osSemaphoreHandle = *mut Semaphore;
@@ -324,10 +319,7 @@ pub unsafe extern "C" fn pte_osSemaphoreCreate(
     initialValue: c_int,
     pHandle: *mut pte_osSemaphoreHandle,
 ) -> 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
 }
 
@@ -342,9 +334,7 @@ pub unsafe extern "C" fn pte_osSemaphorePost(
     handle: pte_osSemaphoreHandle,
     count: c_int,
 ) -> pte_osResult {
-    let semaphore = &mut *handle;
-    let _guard = semaphore.lock.lock();
-    intrinsics::atomic_xadd(&mut semaphore.count, 1);
+    (*handle).post();
     PTE_OS_OK
 }
 
@@ -354,17 +344,7 @@ pub unsafe extern "C" fn pte_osSemaphorePend(
     pTimeout: *mut c_uint,
 ) -> pte_osResult {
     //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
 }
 
-- 
GitLab