Skip to content
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

docs: add countdown latch example to Semaphore #6105

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Oct 24, 2023

Motivation

Some users have requested that tokio::sync add a countdown latch
synchronization primitive. This can be implemented using the existing
Semaphore API, so rather than adding a countdown latch type, we should
add documentation examples showing how the Semaphore can be used as a
countdown latch.

Solution

This branch adds an example to the tokio::sync::Semaphore docs
demonstrating its usage as a countdown latch. This example was extracted
from #6087 (comment).

Closes #6087

Motivation

Solution

## Motivation

Some users have requested that `tokio::sync` add a countdown latch
synchronization primitive. This can be implemented using the existing
`Semaphore` API, so rather than adding a countdown latch type, we should
add documentation examples showing how the `Semaphore` can be used as a
countdown latch.

## Solution

This branch adds an example to the `tokio::sync::Semaphore` docs
demonstrating its usage as a countdown latch. This example was extracted
from #6087 (comment).

Closes #6087
@github-actions github-actions bot added the R-loom-sync Run loom sync tests on this PR label Oct 24, 2023
@Darksonn Darksonn added T-docs Topic: documentation A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Oct 24, 2023
tokio/src/sync/semaphore.rs Outdated Show resolved Hide resolved
@@ -322,7 +322,7 @@ use std::sync::Arc;
/// # #[tokio::main(flavor = "current_thread", start_paused = true)]
/// async fn main() {
/// let capacity = 5;
/// let update_interval = Duration::from_secs_f32(1.0 / capacity as f32);
/// let update_interval = Duration::from_secs_f32(1.0 / capacity as f32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an accidental change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, yeah, i'll undo that

tokio/src/sync/semaphore.rs Outdated Show resolved Hide resolved
tokio/src/sync/semaphore.rs Outdated Show resolved Hide resolved
tokio/src/sync/semaphore.rs Outdated Show resolved Hide resolved
Comment on lines +352 to +354
/// /// A token that holds a countdown latch open until it is dropped.
/// #[derive(Clone)]
/// pub struct Countdown(Arc<Semaphore>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This derive(Clone) seems a bit weird to me. It makes it difficult to make sure you have the right number of tokens. In your example, you also create one token more than you need, but the extra one in main is not dropped until after the wait completes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that's a good point. this could maybe have some kind of fn try_clone -> Option<Self> that only allows creating as many guards as there are permits, or something? i'm not sure if the additional complexity is worth including in the example or not...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just return a vector of them?

hawkw and others added 4 commits November 5, 2023 08:50
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-sync Module: tokio/sync R-loom-sync Run loom sync tests on this PR T-docs Topic: documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a countdown latch example to Semaphore
2 participants