Skip to content

Tracking issue for std::sync::Once::is_completed #54890

Closed
@SimonSapin

Description

@SimonSapin
Contributor

Implemented in #53027

I propose to expose a way to check if a Once instance is initialized.

I need it in once_cell. OnceCell is effetively a pair of (Once, UnsafeCell<Option<T>>), which can set the T only once. Because I can't check if Once is initialized, I am forced to add an indirection and check the value of ptr instead:

https://github.com/matklad/once_cell/blob/8127a81976c3f2f4c0860562c3f14647ebc025c0/src/lib.rs#L423-L429

https://github.com/matklad/once_cell/blob/8127a81976c3f2f4c0860562c3f14647ebc025c0/src/lib.rs#L457-L461

The parking_lot's version of Once exposes the state as an enum: https://docs.rs/parking_lot/0.6.3/parking_lot/struct.Once.html#method.state.

I suggest, for now, just to add a simple bool function: this fits my use-case perfectly, exposes less implementation details, and is forward-compatible with more fine-grained state checking.

Activity

added
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
B-unstableBlocker: Implemented in the nightly compiler and unstable.
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
on Oct 7, 2018
nagisa

nagisa commented on Oct 7, 2018

@nagisa
Member

The documentation needs to be made slightly more strict.

For example:

It is also possible that immediately after is_completed returns false, some other thread finishes executing call_once.

Should be replaced with

This function returning false does not mean that Once has not been executed.

The distinction is important, because at the exact moment this function returns Once might already be executed/resolved, without it necessarily happening “immediately after”.


To avoid people shotgunning their feet off, I would suggest to add some guidance on how this should be used. For example, doing something useful based on this function returning a false is somewhat likely to be a bug. And yet, examples do not really result in such an impression.

SimonSapin

SimonSapin commented on Oct 7, 2018

@SimonSapin
ContributorAuthor

CC @matklad for nagisa’s comment above.

added 2 commits that reference this issue on Oct 9, 2018

Rollup merge of rust-lang#54891 - rust-lang:SimonSapin-patch-1, r=nagisa

5750176

Rollup merge of rust-lang#54891 - rust-lang:SimonSapin-patch-1, r=nagisa

18a78fc
added a commit that references this issue on Oct 11, 2018

Rollup merge of rust-lang#54891 - rust-lang:SimonSapin-patch-1, r=nagisa

f64e28e
added a commit that references this issue on Oct 12, 2018

Rollup merge of rust-lang#54891 - rust-lang:SimonSapin-patch-1, r=nagisa

ef7f77d
mjbshaw

mjbshaw commented on Jul 10, 2019

@mjbshaw
Contributor

It's been 9 months since this was implemented. Can this be stabilized now?

pyrrho

pyrrho commented on Oct 31, 2019

@pyrrho

Is there anything preventing this method from being stabilized? It looks like there was some bikesheding in #53027 but it's now been just past a year since initial adoption.

matklad

matklad commented on Oct 31, 2019

@matklad
Member

Note that I am personally no longer directly interested in having this method: in once_cell, I ended-up copy-pasting the whole of Once anyway. That said, this does look like straightforward addition, which allows to do more with the thing, without actually expanding it's surface area greatly.

3 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @SimonSapin@pyrrho@nagisa@mjbshaw@matklad

      Issue actions

        Tracking issue for std::sync::Once::is_completed · Issue #54890 · rust-lang/rust