about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-11-26 16:02:24 +0100
committerGitHub <noreply@github.com>2021-11-26 16:02:24 +0100
commitfdc305d58d7cf93703a47118538f04b2b53c2873 (patch)
tree2452769988305e555838f77d54e30166fba66c05
parent324b4bcb3caaaa70476d65c86e8d399f09145bab (diff)
parent6911af9d06ef4f92659326ce426c6aefc7b47282 (diff)
downloadrust-fdc305d58d7cf93703a47118538f04b2b53c2873.tar.gz
rust-fdc305d58d7cf93703a47118538f04b2b53c2873.zip
Rollup merge of #91176 - hermitcore:spin, r=kennytm
If the thread does not get the lock in the short term, yield the CPU

Reduces on [RustyHermit](https://github.com/hermitcore/rusty-hermit) 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..415cbba101c 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 += 1;
+            if counter < 100 {
+                hint::spin_loop();
+            } else {
+                counter = 0;
+                unsafe {
+                    abi::yield_now();
+                }
+            }
         }
     }