diff options
| author | bors <bors@rust-lang.org> | 2017-07-13 10:50:23 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-07-13 10:50:23 +0000 |
| commit | b2c0707872082c890f332178f59fd02eea5b98f3 (patch) | |
| tree | e6c9eaa93dfbb471c9aaa96965d6265c1a3965de /src/libstd | |
| parent | 06ffdeb7816ae62175febdfa18534290bf5ad573 (diff) | |
| parent | a301f84b6afc30c58dede5ddd804890f7a6f20a5 (diff) | |
| download | rust-b2c0707872082c890f332178f59fd02eea5b98f3.tar.gz rust-b2c0707872082c890f332178f59fd02eea5b98f3.zip | |
Auto merge of #43158 - PlasmaPower:thread-local-try-with, r=alexcrichton
Thread local try with https://github.com/rust-lang/rfcs/pull/2030 was turned into this PR (the RFC was closed, but it looks like just a PR should be good). See also: state stabilization issue: #27716 `try_with` is used in two places in std: stdio and thread_info. In stdio, it would be better if the result was passed to the closure, but in thread_info, it's better as is where the result is returned from the function call. I'm not sure which is better, but I prefer the current way as it better represents the scope.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/sys_common/thread_info.rs | 11 | ||||
| -rw-r--r-- | src/libstd/thread/local.rs | 63 | ||||
| -rw-r--r-- | src/libstd/thread/mod.rs | 2 |
3 files changed, 58 insertions, 18 deletions
diff --git a/src/libstd/sys_common/thread_info.rs b/src/libstd/sys_common/thread_info.rs index 5ed48ee4558..2abb8afa828 100644 --- a/src/libstd/sys_common/thread_info.rs +++ b/src/libstd/sys_common/thread_info.rs @@ -12,7 +12,6 @@ use cell::RefCell; use thread::Thread; -use thread::LocalKeyState; struct ThreadInfo { stack_guard: Option<usize>, @@ -23,19 +22,15 @@ thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(N impl ThreadInfo { fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R { - if THREAD_INFO.state() == LocalKeyState::Destroyed { - return None - } - - THREAD_INFO.with(move |c| { + THREAD_INFO.try_with(move |c| { if c.borrow().is_none() { *c.borrow_mut() = Some(ThreadInfo { stack_guard: None, thread: Thread::new(None), }) } - Some(f(c.borrow_mut().as_mut().unwrap())) - }) + f(c.borrow_mut().as_mut().unwrap()) + }).ok() } } diff --git a/src/libstd/thread/local.rs b/src/libstd/thread/local.rs index dad21473eae..28e8f72ac64 100644 --- a/src/libstd/thread/local.rs +++ b/src/libstd/thread/local.rs @@ -232,6 +232,32 @@ pub enum LocalKeyState { Destroyed, } +/// An error returned by [`LocalKey::try_with`](struct.LocalKey.html#method.try_with). +#[unstable(feature = "thread_local_state", + reason = "state querying was recently added", + issue = "27716")] +pub struct AccessError { + _private: (), +} + +#[unstable(feature = "thread_local_state", + reason = "state querying was recently added", + issue = "27716")] +impl fmt::Debug for AccessError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("AccessError").finish() + } +} + +#[unstable(feature = "thread_local_state", + reason = "state querying was recently added", + issue = "27716")] +impl fmt::Display for AccessError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt("already destroyed", f) + } +} + impl<T: 'static> LocalKey<T> { #[doc(hidden)] #[unstable(feature = "thread_local_internals", @@ -258,15 +284,8 @@ impl<T: 'static> LocalKey<T> { #[stable(feature = "rust1", since = "1.0.0")] pub fn with<F, R>(&'static self, f: F) -> R where F: FnOnce(&T) -> R { - unsafe { - let slot = (self.inner)(); - let slot = slot.expect("cannot access a TLS value during or \ - after it is destroyed"); - f(match *slot.get() { - Some(ref inner) => inner, - None => self.init(slot), - }) - } + self.try_with(f).expect("cannot access a TLS value during or \ + after it is destroyed") } unsafe fn init(&self, slot: &UnsafeCell<Option<T>>) -> &T { @@ -331,6 +350,32 @@ impl<T: 'static> LocalKey<T> { } } } + + /// Acquires a reference to the value in this TLS key. + /// + /// This will lazily initialize the value if this thread has not referenced + /// this key yet. If the key has been destroyed (which may happen if this is called + /// in a destructor), this function will return a ThreadLocalError. + /// + /// # Panics + /// + /// This function will still `panic!()` if the key is uninitialized and the + /// key's initializer panics. + #[unstable(feature = "thread_local_state", + reason = "state querying was recently added", + issue = "27716")] + pub fn try_with<F, R>(&'static self, f: F) -> Result<R, AccessError> + where F: FnOnce(&T) -> R { + unsafe { + let slot = (self.inner)().ok_or(AccessError { + _private: (), + })?; + Ok(f(match *slot.get() { + Some(ref inner) => inner, + None => self.init(slot), + })) + } + } } #[doc(hidden)] diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index f4fe52ca3b3..07a3a01ce86 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -159,7 +159,7 @@ use time::Duration; #[macro_use] mod local; #[stable(feature = "rust1", since = "1.0.0")] -pub use self::local::{LocalKey, LocalKeyState}; +pub use self::local::{LocalKey, LocalKeyState, AccessError}; // The types used by the thread_local! macro to access TLS keys. Note that there // are two types, the "OS" type and the "fast" type. The OS thread local key |
