If the thread does not get the lock in the short term, yield the CPU

Reduces the amount of wasted processor cycles
This commit is contained in:
Stefan Lankes 2021-11-24 15:59:28 +01:00
parent 982c552c90
commit 644b445428

View file

@ -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();
}
}
}
}