Skip to content

Commit

Permalink
Rollup merge of #91176 - hermitcore:spin, r=kennytm
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matthiaskrgr authored Nov 26, 2021
2 parents 324b4bc + 6911af9 commit fdc305d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion library/std/src/sys/hermit/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
}

Expand Down

0 comments on commit fdc305d

Please sign in to comment.