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

Tracking issue for Scoped TLS #27715

Closed
alexcrichton opened this issue Aug 12, 2015 · 5 comments
Closed

Tracking issue for Scoped TLS #27715

alexcrichton opened this issue Aug 12, 2015 · 5 comments
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@alexcrichton
Copy link
Member

This is a tracking issue for the unstable scoped_tls feature in the standard library. The known open questions about this API are:

  • Is it useful enough to be stabilized? It doesn't look like there's been much usage yet.
  • Currently the API only accepts sized type parameters, but it in theory could accept more (although the OS doesn't technically support this). A decision should be made either way.
  • Are we rock-solid that the implementation is sound?
@alexcrichton alexcrichton added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. B-unstable Blocker: Implemented in the nightly compiler and unstable. labels Aug 12, 2015
@nagisa
Copy link
Member

nagisa commented Aug 12, 2015

This reminds me of a Reader monad and its local method a lot. Specialisation of this monad to only work in TLS context sounds really obscure to me. Could this be generalised?

@Diggsey
Copy link
Contributor

Diggsey commented Aug 12, 2015

The stabilization of scoped TLS precludes the safe implementation of couroutines/fibers/green threads in external libraries.

Admittedly there are also problems with normal TLS, but they seem less terminal, and can probably be limited to causing unexpected behaviour or panics rather than outright memory unsafety with some changes.

Possible solutions:

  • Formalize the notion of a call-stack/context within the standard library, and provide unsafe methods to "swap" the current context with another. External libraries can then build safe interfaces around this. Rename "scoped thread-local storage" to "scoped context-local storage" or similar.
    Either do the same thing for normal TLS, or also provide the means for external libraries to detect whether code is currently executing within a "LocalKey::with()" call, so that they can panic if a context switch occurs there.
  • Keep a per-thread token which is initially left unset. Each type of threading is given a unique token, external libraries being able to acquire unique tokens from the standard library. Once a thread's token has been set, it can never be changed, and attempting to do so will cause it to panic.
    Any use of TLS will attempt to set the thread's token to "threaded-only". Similarly, for any use of coroutines/fibers, the external library must attempt to set its own special token.
  • Make all libraries (including std) which use TLS be effectively generic over the threading model. Only when the code is compiled into an executable is the threading model fixed. If the final executable uses only native threads, then all TLS use gets compiled down to fast, native TLS. If the final program uses fibers, then use fiber local storage. One way this could be achieved is by leaving undefined symbols in all libraries which are only supplied when the final program is linked.
    This would preclude the use of multiple threading models within the same program.
  • Introduce some form of static analysis and a set of attributes, such that the compiler can prove at compile-time that different threading-models don't mix.

All of the options have significant downsides, either due to complexity or runtime performance.

@alexcrichton
Copy link
Member Author

🔔 This issue is now entering its cycle final comment period to be deprecation in 1.8 🔔

Scoped TLS can currently be built externally on crates.io if truly necessary, and otherwise it seems to be getting far less usage than originally anticipated.

@alexcrichton alexcrichton added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed I-nominated labels Jan 29, 2016
@nikomatsakis
Copy link
Contributor

Even though I was an initial advocate for (and proposer of) scoped TLS, I think it's good to deprecate it. One major limitation that I didn't fully appreciate at the time is that the T of the variable must meet T: 'static, which tends to rule out a lot of the uses I've had in mind. At least without some unsafety. e.g., in the compiler we do some tricks to put a &ty::ctxt<'tcx> in there.

@alexcrichton
Copy link
Member Author

The libs team discussed this during triage yesterday and the conclusion was to deprecate

alexcrichton added a commit to alexcrichton/rust that referenced this issue Feb 29, 2016
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:

Stabilized

* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`

Deprecated

* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`

Closes rust-lang#27714
Closes rust-lang#27715
Closes rust-lang#27746
Closes rust-lang#27748
Closes rust-lang#27908
Closes rust-lang#29866
bors added a commit that referenced this issue Feb 29, 2016
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:

Stabilized

* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`

Deprecated

* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`

Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
Closes #28235
Closes #29720
dlrobertson pushed a commit to dlrobertson/rust that referenced this issue Nov 29, 2018
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:

Stabilized

* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`

Deprecated

* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`

Closes rust-lang#27714
Closes rust-lang#27715
Closes rust-lang#27746
Closes rust-lang#27748
Closes rust-lang#27908
Closes rust-lang#29866
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants