From 4b81007ef6b6b5f8ae56032cad7bf0232ad8a5be Mon Sep 17 00:00:00 2001 From: Danny Kim Date: Sun, 18 May 2025 22:49:40 +0900 Subject: [PATCH 1/2] Remove `EventBlocking` --- rust_crate/src/shutdown.rs | 46 +++++++++----------------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/rust_crate/src/shutdown.rs b/rust_crate/src/shutdown.rs index 9d46a7f9..1410f79a 100644 --- a/rust_crate/src/shutdown.rs +++ b/rust_crate/src/shutdown.rs @@ -92,8 +92,17 @@ impl Event { /// this method will return immediately. /// Otherwise, it will block until `set` is called by another thread. pub fn wait(&self) { - let blocking = EventBlocking::new(self.inner.clone(), self.condvar.clone()); - blocking.wait(); + // Lock the inner state and wait on the condition variable + let mut guard = self.inner.lock().recover(); + let started_session = guard.session; + loop { + // Check if the condition is met + if guard.flag || guard.session != started_session { + break; + } + // Wait on the condition variable and reassign the guard + guard = self.condvar.wait(guard).recover(); + } } } @@ -114,39 +123,6 @@ impl EventInner { } } -/// Struct to handle waiting with session tracking. -#[cfg(not(target_family = "wasm"))] -struct EventBlocking { - inner: Arc>, - condvar: Arc, - started_session: usize, -} - -#[cfg(not(target_family = "wasm"))] -impl EventBlocking { - pub fn new(inner: Arc>, condvar: Arc) -> Self { - let guard = inner.lock().recover(); - EventBlocking { - inner: inner.clone(), - condvar, - started_session: guard.session, - } - } - - pub fn wait(&self) { - // Lock the inner state and wait on the condition variable - let mut guard = self.inner.lock().recover(); - loop { - // Check if the condition is met - if guard.flag || guard.session != self.started_session { - break; - } - // Wait on the condition variable and reassign the guard - guard = self.condvar.wait(guard).recover(); - } - } -} - /// Future that resolves when the `Event` flag is set to `true`. pub struct EventFuture { started_session: usize, From 7056e9b66db22186b0b78fe29f40978ac57cc7ff Mon Sep 17 00:00:00 2001 From: Danny Kim Date: Sun, 18 May 2025 22:54:48 +0900 Subject: [PATCH 2/2] Remove an unneeded constructor --- rust_crate/src/shutdown.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/rust_crate/src/shutdown.rs b/rust_crate/src/shutdown.rs index 1410f79a..4fbd198b 100644 --- a/rust_crate/src/shutdown.rs +++ b/rust_crate/src/shutdown.rs @@ -44,8 +44,13 @@ pub struct Event { impl Event { /// Creates a new `Event` with the initial flag state. pub fn new() -> Self { + let inner = EventInner { + flag: false, + session: 0, + wakers: Vec::new(), + }; Event { - inner: Arc::new(Mutex::new(EventInner::new())), + inner: Arc::new(Mutex::new(inner)), #[cfg(not(target_family = "wasm"))] condvar: Arc::new(Condvar::new()), } @@ -113,16 +118,6 @@ struct EventInner { wakers: Vec, // List of wakers to be notified } -impl EventInner { - pub fn new() -> Self { - EventInner { - flag: false, - session: 0, - wakers: Vec::new(), - } - } -} - /// Future that resolves when the `Event` flag is set to `true`. pub struct EventFuture { started_session: usize,