Skip to content

Commit

Permalink
sync: document that const_new() is not instrumented
Browse files Browse the repository at this point in the history
The mutex created by `Mutex::const_new()` does not contain the tracing
instrumentation that drives `tokio-console`.

The same is true of `Notify`, `OnceCell` (including `const_new_with()`),
`RwLock`, and `Semaphore`.

This change adds documentation to each of the `const_*` constructors,
explaining that objects created this way will not be instrumented and
therefore will not appear in `tokio-console`. A link to the non-const
variant is provided in each case.
  • Loading branch information
hds committed Sep 13, 2023
1 parent 65e7715 commit 88d5c3d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tokio/src/sync/mutex.rs
Expand Up @@ -371,13 +371,21 @@ impl<T: ?Sized> Mutex<T> {

/// Creates a new lock in an unlocked state ready for use.
///
/// When using the `tracing` [unstable feature], a `Mutex` created with
/// `const_new` will not be instrumented. As such, it will not be visible
/// in [`tokio-console`]. Instead, [`Mutex::new`] should be used to create
/// an instrumented object if that is needed.
///
/// # Examples
///
/// ```
/// use tokio::sync::Mutex;
///
/// static LOCK: Mutex<i32> = Mutex::const_new(5);
/// ```
///
/// [`tokio-console`]: https://github.com/tokio-rs/console
/// [unstable feature]: crate#unstable-features
#[cfg(not(all(loom, test)))]
pub const fn const_new(t: T) -> Self
where
Expand Down
8 changes: 8 additions & 0 deletions tokio/src/sync/notify.rs
Expand Up @@ -436,13 +436,21 @@ impl Notify {

/// Create a new `Notify`, initialized without a permit.
///
/// When using the `tracing` [unstable feature], a `Notify` created with
/// `const_new` will not be instrumented. As such, it will not be visible
/// in [`tokio-console`]. Instead, [`Notify::new`] should be used to create
/// an instrumented object if that is needed.
///
/// # Examples
///
/// ```
/// use tokio::sync::Notify;
///
/// static NOTIFY: Notify = Notify::const_new();
/// ```
///
/// [`tokio-console`]: https://github.com/tokio-rs/console
/// [unstable feature]: crate#unstable-features
#[cfg(not(all(loom, test)))]
pub const fn const_new() -> Notify {
Notify {
Expand Down
16 changes: 16 additions & 0 deletions tokio/src/sync/once_cell.rs
Expand Up @@ -153,6 +153,11 @@ impl<T> OnceCell<T> {
///
/// # Example
///
/// When using the `tracing` [unstable feature], a `OnceCell` created with
/// `const_new_with` will not be instrumented. As such, it will not be
/// visible in [`tokio-console`]. Instead, [`OnceCell::new_with`] should be
/// used to create an instrumented object if that is needed.
///
/// ```
/// use tokio::sync::OnceCell;
///
Expand All @@ -170,6 +175,9 @@ impl<T> OnceCell<T> {
/// assert_eq!(*result, 1);
/// }
/// ```
///
/// [`tokio-console`]: https://github.com/tokio-rs/console
/// [unstable feature]: crate#unstable-features
#[cfg(not(all(loom, test)))]
pub const fn const_new_with(value: T) -> Self {
OnceCell {
Expand All @@ -184,6 +192,11 @@ impl<T> OnceCell<T> {
/// Equivalent to `OnceCell::new`, except that it can be used in static
/// variables.
///
/// When using the `tracing` [unstable feature], a `OnceCell` created with
/// `const_new` will not be instrumented. As such, it will not be visible
/// in [`tokio-console`]. Instead, [`OnceCell::new`] should be used to
/// create an instrumented object if that is needed.
///
/// # Example
///
/// ```
Expand All @@ -203,6 +216,9 @@ impl<T> OnceCell<T> {
/// assert_eq!(*result, 2);
/// }
/// ```
///
/// [`tokio-console`]: https://github.com/tokio-rs/console
/// [unstable feature]: crate#unstable-features
#[cfg(not(all(loom, test)))]
pub const fn const_new() -> Self {
OnceCell {
Expand Down
8 changes: 8 additions & 0 deletions tokio/src/sync/rwlock.rs
Expand Up @@ -327,13 +327,21 @@ impl<T: ?Sized> RwLock<T> {

/// Creates a new instance of an `RwLock<T>` which is unlocked.
///
/// When using the `tracing` [unstable feature], a `RwLock` created with
/// `const_new` will not be instrumented. As such, it will not be visible
/// in [`tokio-console`]. Instead, [`RwLock::new`] should be used to create
/// an instrumented object if that is needed.
///
/// # Examples
///
/// ```
/// use tokio::sync::RwLock;
///
/// static LOCK: RwLock<i32> = RwLock::const_new(5);
/// ```
///
/// [`tokio-console`]: https://github.com/tokio-rs/console
/// [unstable feature]: crate#unstable-features
#[cfg(not(all(loom, test)))]
pub const fn const_new(value: T) -> RwLock<T>
where
Expand Down
8 changes: 8 additions & 0 deletions tokio/src/sync/semaphore.rs
Expand Up @@ -194,13 +194,21 @@ impl Semaphore {

/// Creates a new semaphore with the initial number of permits.
///
/// When using the `tracing` [unstable feature], a `Semaphore` created with
/// `const_new` will not be instrumented. As such, it will not be visible
/// in [`tokio-console`]. Instead, [`Semaphore::new`] should be used to
/// create an instrumented object if that is needed.
///
/// # Examples
///
/// ```
/// use tokio::sync::Semaphore;
///
/// static SEM: Semaphore = Semaphore::const_new(10);
/// ```
///
/// [`tokio-console`]: https://github.com/tokio-rs/console
/// [unstable feature]: crate#unstable-features
#[cfg(not(all(loom, test)))]
pub const fn const_new(permits: usize) -> Self {
Self {
Expand Down

0 comments on commit 88d5c3d

Please sign in to comment.