Skip to content

Conversation

@srxg
Copy link
Contributor

@srxg srxg commented Aug 28, 2025

This PR:

  1. Introduces NotifyGuard, an internal type that wraps a lock on Notify’s waiter list and provides a method to wake all waiters while holding it.
  2. removes the dedicated Mutex<()> from SetOnce by having it re-use the mutex already owned by Notify, via NotifyGuard.

Motivation

The goal is to remove the extra mutex inside SetOnce by having it reuse Notify's mutex. This avoids duplicate synchronisation and reduces memory footprint.

Solution

SetOnce now acquires the lock through Notify::lock, instead of holding its own mutex.

@github-actions github-actions bot added the R-loom-sync Run loom sync tests on this PR label Aug 28, 2025
@ADD-SP ADD-SP added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Aug 28, 2025
Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

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

I believe that eliminating the extra Mutex would improve both performance and code complexity.

it would be better for us to carefully consider how to achieve these benefits. The performance (including CPU and memory) penalty caused by the extra Mutex is not significant, which means that we should focus on reducing the code complexity.

The ideal case is that eliminating the extra Mutex and make people like me can understand the logic of critical code path in a few minutes.

srxg and others added 2 commits September 1, 2025 13:32
Remove redundant pending check

Co-authored-by: Alice Ryhl <aliceryhl@google.com>
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.

Looks good to me.

Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

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

Thanks!

@Darksonn Darksonn merged commit 37ca2f0 into tokio-rs:master Sep 3, 2025
90 checks passed
@Darksonn Darksonn mentioned this pull request Oct 14, 2025
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants