You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-16 22:27:34 +02:00
wip: async dining philosophers simplification
This commit is contained in:
@ -44,22 +44,11 @@ impl Philosopher {
|
|||||||
// Keep trying until we have both forks
|
// Keep trying until we have both forks
|
||||||
// ANCHOR_END: Philosopher-eat
|
// ANCHOR_END: Philosopher-eat
|
||||||
let (_left_fork, _right_fork) = loop {
|
let (_left_fork, _right_fork) = loop {
|
||||||
|
tokio::task::yield_now().await;
|
||||||
// Pick up forks...
|
// Pick up forks...
|
||||||
let left_fork = self.left_fork.try_lock();
|
match (self.left_fork.try_lock(), self.right_fork.try_lock()) {
|
||||||
let right_fork = self.right_fork.try_lock();
|
(Ok(left_fork), Ok(right_fork)) => (left_fork, right_fork),
|
||||||
let Ok(left_fork) = left_fork else {
|
(_, _) => continue,
|
||||||
// If we didn't get the left fork, drop the right fork if we
|
|
||||||
// have it and let other tasks make progress.
|
|
||||||
drop(right_fork);
|
|
||||||
time::sleep(time::Duration::from_millis(1)).await;
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
let Ok(right_fork) = right_fork else {
|
|
||||||
// If we didn't get the right fork, drop the left fork and let
|
|
||||||
// other tasks make progress.
|
|
||||||
drop(left_fork);
|
|
||||||
time::sleep(time::Duration::from_millis(1)).await;
|
|
||||||
continue;
|
|
||||||
};
|
};
|
||||||
break (left_fork, right_fork);
|
break (left_fork, right_fork);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user