From 512878a3e97e1cf06b3432e5ff711e88e8ce26cc Mon Sep 17 00:00:00 2001
From: Martin Geisler <mgeisler@google.com>
Date: Mon, 17 Jun 2024 12:31:44 +0200
Subject: [PATCH] wip: async dining philosophers simplification

---
 .../async-exercises/dining-philosophers.rs    | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)

diff --git a/src/concurrency/async-exercises/dining-philosophers.rs b/src/concurrency/async-exercises/dining-philosophers.rs
index 12cb08a9..bf67e720 100644
--- a/src/concurrency/async-exercises/dining-philosophers.rs
+++ b/src/concurrency/async-exercises/dining-philosophers.rs
@@ -44,22 +44,11 @@ impl Philosopher {
         // Keep trying until we have both forks
         // ANCHOR_END: Philosopher-eat
         let (_left_fork, _right_fork) = loop {
+            tokio::task::yield_now().await;
             // Pick up forks...
-            let left_fork = self.left_fork.try_lock();
-            let right_fork = self.right_fork.try_lock();
-            let Ok(left_fork) = left_fork else {
-                // 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;
+            match (self.left_fork.try_lock(), self.right_fork.try_lock()) {
+                (Ok(left_fork), Ok(right_fork)) => (left_fork, right_fork),
+                (_, _) => continue,
             };
             break (left_fork, right_fork);
         };