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 lazy_static macro #125

Merged
merged 2 commits into from Apr 10, 2020
Merged

Add lazy_static macro #125

merged 2 commits into from Apr 10, 2020

Conversation

jonhoo
Copy link
Sponsor Contributor

@jonhoo jonhoo commented Apr 9, 2020

This adds the lazy_static! macro from the lazy_static crate. We need
to re-implement it both since it establishes causality (through its use
of Once), and because the state needs to be cleared on every
execution. This implementation does that.

One unfortunate downside of this implementation is that it is inherently
unsafe. Specifically, in the context of loom, statics are, well, not
static. And yet the API of lazy_static! is such that it gives out
'static references. We cannot safely give out 'static refs into
statics that we clear on every execution.

One option here is to leak every static we allocate. The code would then
be safe (since the references would indeed be 'static), but it would
mean we leak all statics for every execution, no matter whether the
user needs them to be 'static or not.

Fixes #124.

This adds the `lazy_static!` macro from the `lazy_static` crate. We need
to re-implement it both since it establishes causality (through its use
of `Once`), and because the state needs to be cleared on every
execution. This implementation does that.

One unfortunate downside of this implementation is that it is inherently
unsafe. Specifically, in the context of loom, statics are, well, not
static. And yet the API of `lazy_static!` is such that it gives out
`'static` references. We _cannot_ safely give out `'static` refs into
statics that we clear on every execution.

One option here is to leak every static we allocate. The code would then
be safe (since the references would indeed be `'static`), but it would
mean we leak _all_ statics for _every_ execution, no matter whether the
user needs them to be `'static` or not.

Fixes tokio-rs#124.
@jonhoo jonhoo requested review from hawkw and carllerche April 9, 2020 20:55
Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Thanks 👍

@carllerche carllerche merged commit 13b7dc4 into tokio-rs:master Apr 10, 2020
@jonhoo jonhoo deleted the lazy_static branch April 11, 2020 02:03
jonhoo added a commit to jonhoo/crossbeam that referenced this pull request Apr 12, 2020
With this change, loom runs to completion on the treiber test!
I ran it with `LOOM_MAX_PREEMPTIONS=3`, and it took _forever_ (well, it
took 2h), but eventually finished with:

     ================== Iteration 112280000 ==================

    Completed in 112291514 iterations
    ok

With `LOOM_MAX_PREEMPTIONS=2` it "only" took 13 minutes, and finished
with:

     ================== Iteration 9680000 ==================

    Completed in 9690926 iterations
    ok

I updated the CI script to run with `LOOM_MAX_PREEMPTIONS=2`.

Note that this change depends on
tokio-rs/loom#125 and
tokio-rs/loom#128. The `patch` in the root
`Cargo.toml` should be removed once a new loom release is made.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Erroneous race reported with lazy_static and thread_local
2 participants