Skip to content

Commit

Permalink
Rename some variables
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Sep 15, 2024
1 parent 8617515 commit 5f29d42
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
24 changes: 12 additions & 12 deletions rust_crate/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ impl<T> SignalSender<T> {
/// message, it will be woken up. This method does not fail if the mutex
/// is poisoned but simply ignores the failure.
pub fn send(&self, msg: T) {
let mut inner = match self.inner.lock() {
let mut guard = match self.inner.lock() {
Ok(inner) => inner,
Err(poisoned) => poisoned.into_inner(),
};

// Enqueue the message
inner.queue.push_back(msg);
guard.queue.push_back(msg);
// Wake up the previous receiver making it receive `None`, if any
if let Some(waker) = inner.waker.take() {
if let Some(waker) = guard.waker.take() {
waker.wake();
}
}
Expand All @@ -69,16 +69,16 @@ impl<T> Clone for SignalReceiver<T> {
/// original receiver will no longer receive messages after this clone.
/// This ensures only the most recent receiver can access the message queue.
fn clone(&self) -> Self {
let mut inner = match self.inner.lock() {
let mut guard = match self.inner.lock() {
Ok(inner) => inner,
Err(poisoned) => poisoned.into_inner(),
};
let new_receiver = SignalReceiver {
inner: self.inner.clone(),
id: inner.active_receiver_id + 1, // Increment ID for new receiver
id: guard.active_receiver_id + 1, // Increment ID for new receiver
};
inner.active_receiver_id = new_receiver.id;
if let Some(waker) = inner.waker.take() {
guard.active_receiver_id = new_receiver.id;
if let Some(waker) = guard.waker.take() {
waker.wake();
}
new_receiver
Expand All @@ -101,22 +101,22 @@ impl<T> Future for RecvFuture<T> {
/// a message is sent. If this receiver is not the active receiver, it will
/// return `None`.
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut inner = match self.inner.lock() {
let mut guard = match self.inner.lock() {
Ok(inner) => inner,
Err(poisoned) => poisoned.into_inner(),
};

// Only allow the current active receiver to receive messages
if inner.active_receiver_id == self.receiver_id {
if let Some(msg) = inner.queue.pop_front() {
if guard.active_receiver_id == self.receiver_id {
if let Some(msg) = guard.queue.pop_front() {
// Check if more messages are in the queue
if !inner.queue.is_empty() {
if !guard.queue.is_empty() {
// If so, wake the current task immediately
cx.waker().wake_by_ref();
}
Poll::Ready(Some(msg))
} else {
inner.waker = Some(cx.waker().clone());
guard.waker = Some(cx.waker().clone());
Poll::Pending
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions rust_crate/src/shutdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ impl Event {
/// This will wake up any threads or async tasks.
#[cfg(not(target_family = "wasm"))]
pub fn set(&self) {
let mut inner = match self.inner.lock() {
let mut guard = match self.inner.lock() {
Ok(inner) => inner,
Err(poisoned) => poisoned.into_inner(),
};
inner.flag = true; // Set the flag
inner.session += 1; // Increment the session count
guard.flag = true; // Set the flag
guard.session += 1; // Increment the session count

// Wake all threads and async tasks when the event is set
self.condvar.notify_all();
for waker in inner.wakers.drain(..) {
for waker in guard.wakers.drain(..) {
waker.wake();
}
}
Expand All @@ -64,11 +64,11 @@ impl Event {
/// block until the flag is set again.
#[cfg(all(not(target_family = "wasm"), debug_assertions))]
pub fn clear(&self) {
let mut inner = match self.inner.lock() {
let mut guard = match self.inner.lock() {
Ok(inner) => inner,
Err(poisoned) => poisoned.into_inner(),
};
inner.flag = false; // Clear the flag
guard.flag = false; // Clear the flag
}

/// Blocks the current thread until the flag is set to `true`.
Expand Down

0 comments on commit 5f29d42

Please sign in to comment.