Skip to content

Commit fdc305d

Browse files
authored
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
2 parents 324b4bc + 6911af9 commit fdc305d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

library/std/src/sys/hermit/mutex.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,17 @@ impl<T> Spinlock<T> {
4646
#[inline]
4747
fn obtain_lock(&self) {
4848
let ticket = self.queue.fetch_add(1, Ordering::SeqCst) + 1;
49+
let mut counter: u16 = 0;
4950
while self.dequeue.load(Ordering::SeqCst) != ticket {
50-
hint::spin_loop();
51+
counter += 1;
52+
if counter < 100 {
53+
hint::spin_loop();
54+
} else {
55+
counter = 0;
56+
unsafe {
57+
abi::yield_now();
58+
}
59+
}
5160
}
5261
}
5362

0 commit comments

Comments
 (0)