about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStefan Lankes <slankes@eonerc.rwth-aachen.de>2021-11-24 15:59:28 +0100
committerStefan Lankes <slankes@eonerc.rwth-aachen.de>2021-11-24 15:59:28 +0100
commit644b445428845a4e662c15059193dfa492bc0c44 (patch)
tree6f5938a1d2ef28506a3f4e151985c293719fddf9
parent982c552c908d179eaa38b6ef152ad3fa30268778 (diff)
downloadrust-644b445428845a4e662c15059193dfa492bc0c44.tar.gz
rust-644b445428845a4e662c15059193dfa492bc0c44.zip
If the thread does not get the lock in the short term, yield the CPU
Reduces the amount of wasted processor cycles
-rw-r--r--library/std/src/sys/hermit/mutex.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/library/std/src/sys/hermit/mutex.rs b/library/std/src/sys/hermit/mutex.rs
index 691e7e07902..79692ef2442 100644
--- a/library/std/src/sys/hermit/mutex.rs
+++ b/library/std/src/sys/hermit/mutex.rs
@@ -46,8 +46,17 @@ impl<T> Spinlock<T> {
     #[inline]
     fn obtain_lock(&self) {
         let ticket = self.queue.fetch_add(1, Ordering::SeqCst) + 1;
+        let mut counter: u16 = 0;
         while self.dequeue.load(Ordering::SeqCst) != ticket {
-            hint::spin_loop();
+            counter = counter + 1;
+            if counter < 100 {
+                hint::spin_loop();
+            } else {
+                counter = 0;
+                unsafe {
+                    abi::yield_now();
+                }
+            }
         }
     }