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

Add broadcast::Sender::len #5343

Merged
merged 6 commits into from Jan 12, 2023
Merged

Add broadcast::Sender::len #5343

merged 6 commits into from Jan 12, 2023

Conversation

sfackler
Copy link
Contributor

@sfackler sfackler commented Jan 4, 2023

Since the head index for each receiver isn't stored in a location accessible from the sender, we need to instead binary search for the last value with remaining readers.

Closes #5334

@github-actions github-actions bot added the R-loom Run loom tests on this PR label Jan 4, 2023
@sfackler
Copy link
Contributor Author

sfackler commented Jan 4, 2023

The binary search can race with updates to rem by receivers but I think it should produce a consistent value anyway? Or at least a value that was correct at some point during the call.

while low < high {
let mid = low + (high - low) / 2;
let idx = base_idx.wrapping_add(mid) & self.shared.mask;
if self.shared.buffer[idx].read().unwrap().rem.load(SeqCst) == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this can probably be a relaxed load rather than sequentially consistent, but haven't thought too carefully about it.

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Jan 4, 2023
@sfackler
Copy link
Contributor Author

Ping!

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Sorry about the delay. It looks correct, but its rather complicated, so I would like to see a more comprehensive test. Perhaps something that randomly chooses between "recv on chan 1, recv on chan 2, send a message" and checks that the length is correct between each iteration, for 1000 iterations?

@sfackler
Copy link
Contributor Author

Will do!

@sfackler
Copy link
Contributor Author

Updated with a test that performs random operations and compares Sender::len against Receiver::len.

tokio/tests/sync_broadcast.rs Outdated Show resolved Hide resolved
tokio/tests/sync_broadcast.rs Outdated Show resolved Hide resolved
tokio/src/sync/broadcast.rs Show resolved Hide resolved
@sfackler
Copy link
Contributor Author

Updated!

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

LGTM.

@sfackler sfackler merged commit c390a62 into tokio-rs:master Jan 12, 2023
@sfackler sfackler deleted the sender-len branch January 12, 2023 18:53
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this pull request Feb 2, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [tokio](https://tokio.rs) ([source](https://github.com/tokio-rs/tokio)) | dependencies | minor | `1.24.2` -> `1.25.0` |
| [tokio](https://tokio.rs) ([source](https://github.com/tokio-rs/tokio)) | dev-dependencies | minor | `1.24.2` -> `1.25.0` |

---

### Release Notes

<details>
<summary>tokio-rs/tokio</summary>

### [`v1.25.0`](https://github.com/tokio-rs/tokio/releases/tag/tokio-1.25.0): Tokio v1.25.0

##### 1.25.0 (January 28, 2023)

##### Fixed

-   rt: fix runtime metrics reporting ([#&#8203;5330])

##### Added

-   sync: add `broadcast::Sender::len` ([#&#8203;5343])

##### Changed

-   fs: increase maximum read buffer size to 2MiB ([#&#8203;5397])

[#&#8203;5330]: tokio-rs/tokio#5330

[#&#8203;5343]: tokio-rs/tokio#5343

[#&#8203;5397]: tokio-rs/tokio#5397

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMTYuMSIsInVwZGF0ZWRJblZlciI6IjM0LjExOS4yIn0=-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1761
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
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 Run loom tests on this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add broadcast::Sender::len
2 participants