Browse Source

Fix thread starting before pte_osThreadStart

Jeremy Soller 6 years ago
parent
commit
17372b4f69
1 changed files with 22 additions and 18 deletions
  1. 22 18
      src/platform/pte.rs

+ 22 - 18
src/platform/pte.rs

@@ -63,27 +63,31 @@ pub unsafe extern "C" fn pte_osThreadCreate(
     ppte_osThreadHandle: *mut pte_osThreadHandle,
 ) -> pte_osResult {
     // XXX error handling
-    let id = Sys::pte_clone();
-    if id < 0 {
-        return PTE_OS_GENERAL_FAILURE;
-    }
 
-    let mutex = Box::into_raw(Box::new(0));
-
-    if id == 0 {
-        // Wait until pte_osThreadStart
-        pte_osMutexLock(mutex);
-        entryPoint(argv);
-        pte_osThreadExit();
-    } else {
-        pte_osMutexLock(&mut pid_mutexes_lock);
-        if pid_mutexes.is_none() {
-            pid_mutexes = Some(BTreeMap::new());
+    // Create a locked mutex, unlocked by pte_osThreadStart
+    let mutex = Box::into_raw(Box::new(2));
+    {
+        let id = Sys::pte_clone();
+        if id < 0 {
+            return PTE_OS_GENERAL_FAILURE;
+        }
+
+        if id == 0 {
+            // Wait until pte_osThreadStart
+            pte_osMutexLock(mutex);
+            entryPoint(argv);
+            pte_osThreadExit();
+        } else {
+            pte_osMutexLock(&mut pid_mutexes_lock);
+            if pid_mutexes.is_none() {
+                pid_mutexes = Some(BTreeMap::new());
+            }
+            pid_mutexes.as_mut().unwrap().insert(id, mutex);
+            pte_osMutexUnlock(&mut pid_mutexes_lock);
+            *ppte_osThreadHandle = id;
         }
-        pid_mutexes.as_mut().unwrap().insert(id, mutex);
-        pte_osMutexUnlock(&mut pid_mutexes_lock);
-        *ppte_osThreadHandle = id;
     }
+
     PTE_OS_OK
 }