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
This commit is contained in:
Matthias Krüger 2021-11-26 16:02:24 +01:00 committed by GitHub
commit fdc305d58d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -46,8 +46,17 @@ pub const fn new(user_data: 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();
}
}
}
}