Skip to content

Commit

Permalink
improve impl
Browse files Browse the repository at this point in the history
  • Loading branch information
MikailBag committed Sep 9, 2020
1 parent 0177e3c commit 623ab17
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
19 changes: 1 addition & 18 deletions tokio/src/sync/mpsc/chan.rs
Expand Up @@ -439,24 +439,7 @@ impl Semaphore for (crate::sync::semaphore_ll::Semaphore, usize) {
}

fn is_closed(&self) -> bool {
// TODO find more efficient way
struct NoopWaker;
impl crate::util::Wake for NoopWaker {
fn wake(self: std::sync::Arc<Self>) {}
fn wake_by_ref(_arc_self: &std::sync::Arc<Self>) {}
}
let waker = std::sync::Arc::new(NoopWaker);
let waker = crate::util::waker_ref(&waker);
let mut noop_cx = std::task::Context::from_waker(&*waker);
let mut permit = Permit::new();
match permit.poll_acquire(&mut noop_cx, 1, &self.0) {
Poll::Ready(Err(_)) => true,
Poll::Ready(Ok(())) => {
permit.release(1, &self.0);
false
}
Poll::Pending => false,
}
self.0.is_closed()
}

fn is_idle(&self) -> bool {
Expand Down
6 changes: 6 additions & 0 deletions tokio/src/sync/semaphore_ll.rs
Expand Up @@ -177,6 +177,12 @@ impl Semaphore {
curr.available_permits()
}

/// Checks if semaphore was closed (i.e. if `close` method was called)
pub(crate) fn is_closed(&self) -> bool {
let curr = SemState(self.state.load(Acquire));
curr.is_closed()
}

/// Tries to acquire the requested number of permits, registering the waiter
/// if not enough permits are available.
fn poll_acquire(
Expand Down

0 comments on commit 623ab17

Please sign in to comment.