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:
parent
982c552c90
commit
644b445428
1 changed files with 10 additions and 1 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue