-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose Semaphore::close
#3065
Expose Semaphore::close
#3065
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -41,15 +41,21 @@ struct Waitlist { | |||||||||
closed: bool, | ||||||||||
} | ||||||||||
|
||||||||||
/// Error returned by `Semaphore::try_acquire`. | ||||||||||
/// Error returned from the `Semaphore::try_acquire` function. | ||||||||||
#[derive(Debug)] | ||||||||||
pub(crate) enum TryAcquireError { | ||||||||||
pub enum TryAcquireError { | ||||||||||
/// The semaphore has been closed and cannot issue new permits | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth linking to the
Suggested change
|
||||||||||
Closed, | ||||||||||
|
||||||||||
/// The has no available permits. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The semaphore |
||||||||||
NoPermits, | ||||||||||
} | ||||||||||
/// Error returned by `Semaphore::acquire`. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this one? |
||||||||||
/// | ||||||||||
/// An `acquire` operation can only fail if the semaphore has been | ||||||||||
/// closed. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be worth linking to the close method here too?
Suggested change
|
||||||||||
#[derive(Debug)] | ||||||||||
pub(crate) struct AcquireError(()); | ||||||||||
pub struct AcquireError(()); | ||||||||||
|
||||||||||
pub(crate) struct Acquire<'a> { | ||||||||||
node: Waiter, | ||||||||||
|
@@ -164,8 +170,6 @@ impl Semaphore { | |||||||||
|
||||||||||
/// Closes the semaphore. This prevents the semaphore from issuing new | ||||||||||
/// permits and notifies all pending waiters. | ||||||||||
// This will be used once the bounded MPSC is updated to use the new | ||||||||||
// semaphore implementation. | ||||||||||
pub(crate) fn close(&self) { | ||||||||||
let mut waiters = self.waiters.lock(); | ||||||||||
// If the semaphore's permits counter has enough permits for an | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||||||||||
use super::batch_semaphore as ll; // low level implementation | ||||||||||||||
use super::{AcquireError, TryAcquireError}; | ||||||||||||||
use std::sync::Arc; | ||||||||||||||
|
||||||||||||||
/// Counting semaphore performing asynchronous permit acquisition. | ||||||||||||||
|
@@ -42,15 +43,6 @@ pub struct OwnedSemaphorePermit { | |||||||||||||
permits: u32, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Error returned from the [`Semaphore::try_acquire`] function. | ||||||||||||||
/// | ||||||||||||||
/// A `try_acquire` operation can only fail if the semaphore has no available | ||||||||||||||
/// permits. | ||||||||||||||
/// | ||||||||||||||
/// [`Semaphore::try_acquire`]: Semaphore::try_acquire | ||||||||||||||
#[derive(Debug)] | ||||||||||||||
pub struct TryAcquireError(()); | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
#[cfg(not(loom))] | ||||||||||||||
fn bounds() { | ||||||||||||||
|
@@ -96,21 +88,21 @@ impl Semaphore { | |||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Acquires permit from the semaphore. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nice if this comment said something about errors, like
Suggested change
or something? |
||||||||||||||
pub async fn acquire(&self) -> SemaphorePermit<'_> { | ||||||||||||||
self.ll_sem.acquire(1).await.unwrap(); | ||||||||||||||
SemaphorePermit { | ||||||||||||||
pub async fn acquire(&self) -> Result<SemaphorePermit<'_>, AcquireError> { | ||||||||||||||
self.ll_sem.acquire(1).await?; | ||||||||||||||
Ok(SemaphorePermit { | ||||||||||||||
sem: &self, | ||||||||||||||
permits: 1, | ||||||||||||||
} | ||||||||||||||
}) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Acquires `n` permits from the semaphore | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and maybe this should say something like
Suggested change
|
||||||||||||||
pub async fn acquire_many(&self, n: u32) -> SemaphorePermit<'_> { | ||||||||||||||
self.ll_sem.acquire(n).await.unwrap(); | ||||||||||||||
SemaphorePermit { | ||||||||||||||
pub async fn acquire_many(&self, n: u32) -> Result<SemaphorePermit<'_>, AcquireError> { | ||||||||||||||
self.ll_sem.acquire(n).await?; | ||||||||||||||
Ok(SemaphorePermit { | ||||||||||||||
sem: &self, | ||||||||||||||
permits: n, | ||||||||||||||
} | ||||||||||||||
}) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Tries to acquire a permit from the semaphore. | ||||||||||||||
|
@@ -120,7 +112,7 @@ impl Semaphore { | |||||||||||||
sem: self, | ||||||||||||||
permits: 1, | ||||||||||||||
}), | ||||||||||||||
Err(_) => Err(TryAcquireError(())), | ||||||||||||||
Err(e) => Err(e), | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
@@ -131,7 +123,7 @@ impl Semaphore { | |||||||||||||
sem: self, | ||||||||||||||
permits: n, | ||||||||||||||
}), | ||||||||||||||
Err(_) => Err(TryAcquireError(())), | ||||||||||||||
Err(e) => Err(e), | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
@@ -140,12 +132,12 @@ impl Semaphore { | |||||||||||||
/// The semaphore must be wrapped in an [`Arc`] to call this method. | ||||||||||||||
/// | ||||||||||||||
hawkw marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
/// [`Arc`]: std::sync::Arc | ||||||||||||||
pub async fn acquire_owned(self: Arc<Self>) -> OwnedSemaphorePermit { | ||||||||||||||
self.ll_sem.acquire(1).await.unwrap(); | ||||||||||||||
OwnedSemaphorePermit { | ||||||||||||||
pub async fn acquire_owned(self: Arc<Self>) -> Result<OwnedSemaphorePermit, AcquireError> { | ||||||||||||||
self.ll_sem.acquire(1).await?; | ||||||||||||||
Ok(OwnedSemaphorePermit { | ||||||||||||||
sem: self, | ||||||||||||||
permits: 1, | ||||||||||||||
} | ||||||||||||||
}) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Tries to acquire a permit from the semaphore. | ||||||||||||||
|
@@ -159,9 +151,16 @@ impl Semaphore { | |||||||||||||
sem: self, | ||||||||||||||
permits: 1, | ||||||||||||||
}), | ||||||||||||||
Err(_) => Err(TryAcquireError(())), | ||||||||||||||
Err(e) => Err(e), | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Closes the semaphore. | ||||||||||||||
/// | ||||||||||||||
/// This prevents the semaphore from issuing new permits and notifies all pending waiters. | ||||||||||||||
hawkw marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
pub fn close(&self) { | ||||||||||||||
carllerche marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
self.ll_sem.close(); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
impl<'a> SemaphorePermit<'a> { | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably ought to be a link?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hawkw Correct me if I am wrong, but it seems we cannot link to non public methods. Having this type be located in
batch_semaphore
and linking to the method insemaphore
seems a bit odd. Am I missing something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type is exposed in the public API. Users will only ever see it via the public
Semaphore
type's methods; they shouldn't need to be aware ofbatch_semaphore
, which is an implementation detail. I think it's correct for this to link to the public methods for readers of the documentation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Thank you !