Skip to content

Commit b2c0707

Browse files
committed
Auto merge of #43158 - PlasmaPower:thread-local-try-with, r=alexcrichton
Thread local try with rust-lang/rfcs#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.
2 parents 06ffdeb + a301f84 commit b2c0707

File tree

4 files changed

+95
-18
lines changed

4 files changed

+95
-18
lines changed

src/libstd/sys_common/thread_info.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use cell::RefCell;
1414
use thread::Thread;
15-
use thread::LocalKeyState;
1615

1716
struct ThreadInfo {
1817
stack_guard: Option<usize>,
@@ -23,19 +22,15 @@ thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(N
2322

2423
impl ThreadInfo {
2524
fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
26-
if THREAD_INFO.state() == LocalKeyState::Destroyed {
27-
return None
28-
}
29-
30-
THREAD_INFO.with(move |c| {
25+
THREAD_INFO.try_with(move |c| {
3126
if c.borrow().is_none() {
3227
*c.borrow_mut() = Some(ThreadInfo {
3328
stack_guard: None,
3429
thread: Thread::new(None),
3530
})
3631
}
37-
Some(f(c.borrow_mut().as_mut().unwrap()))
38-
})
32+
f(c.borrow_mut().as_mut().unwrap())
33+
}).ok()
3934
}
4035
}
4136

src/libstd/thread/local.rs

+54-9
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,32 @@ pub enum LocalKeyState {
232232
Destroyed,
233233
}
234234

235+
/// An error returned by [`LocalKey::try_with`](struct.LocalKey.html#method.try_with).
236+
#[unstable(feature = "thread_local_state",
237+
reason = "state querying was recently added",
238+
issue = "27716")]
239+
pub struct AccessError {
240+
_private: (),
241+
}
242+
243+
#[unstable(feature = "thread_local_state",
244+
reason = "state querying was recently added",
245+
issue = "27716")]
246+
impl fmt::Debug for AccessError {
247+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
248+
f.debug_struct("AccessError").finish()
249+
}
250+
}
251+
252+
#[unstable(feature = "thread_local_state",
253+
reason = "state querying was recently added",
254+
issue = "27716")]
255+
impl fmt::Display for AccessError {
256+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
257+
fmt::Display::fmt("already destroyed", f)
258+
}
259+
}
260+
235261
impl<T: 'static> LocalKey<T> {
236262
#[doc(hidden)]
237263
#[unstable(feature = "thread_local_internals",
@@ -258,15 +284,8 @@ impl<T: 'static> LocalKey<T> {
258284
#[stable(feature = "rust1", since = "1.0.0")]
259285
pub fn with<F, R>(&'static self, f: F) -> R
260286
where F: FnOnce(&T) -> R {
261-
unsafe {
262-
let slot = (self.inner)();
263-
let slot = slot.expect("cannot access a TLS value during or \
264-
after it is destroyed");
265-
f(match *slot.get() {
266-
Some(ref inner) => inner,
267-
None => self.init(slot),
268-
})
269-
}
287+
self.try_with(f).expect("cannot access a TLS value during or \
288+
after it is destroyed")
270289
}
271290

272291
unsafe fn init(&self, slot: &UnsafeCell<Option<T>>) -> &T {
@@ -331,6 +350,32 @@ impl<T: 'static> LocalKey<T> {
331350
}
332351
}
333352
}
353+
354+
/// Acquires a reference to the value in this TLS key.
355+
///
356+
/// This will lazily initialize the value if this thread has not referenced
357+
/// this key yet. If the key has been destroyed (which may happen if this is called
358+
/// in a destructor), this function will return a ThreadLocalError.
359+
///
360+
/// # Panics
361+
///
362+
/// This function will still `panic!()` if the key is uninitialized and the
363+
/// key's initializer panics.
364+
#[unstable(feature = "thread_local_state",
365+
reason = "state querying was recently added",
366+
issue = "27716")]
367+
pub fn try_with<F, R>(&'static self, f: F) -> Result<R, AccessError>
368+
where F: FnOnce(&T) -> R {
369+
unsafe {
370+
let slot = (self.inner)().ok_or(AccessError {
371+
_private: (),
372+
})?;
373+
Ok(f(match *slot.get() {
374+
Some(ref inner) => inner,
375+
None => self.init(slot),
376+
}))
377+
}
378+
}
334379
}
335380

336381
#[doc(hidden)]

src/libstd/thread/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ use time::Duration;
159159
#[macro_use] mod local;
160160

161161
#[stable(feature = "rust1", since = "1.0.0")]
162-
pub use self::local::{LocalKey, LocalKeyState};
162+
pub use self::local::{LocalKey, LocalKeyState, AccessError};
163163

164164
// The types used by the thread_local! macro to access TLS keys. Note that there
165165
// are two types, the "OS" type and the "fast" type. The OS thread local key

src/test/run-pass/tls-try-with.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-emscripten no threads support
12+
13+
#![feature(thread_local_state)]
14+
15+
use std::thread;
16+
17+
static mut DROP_RUN: bool = false;
18+
19+
struct Foo;
20+
21+
thread_local!(static FOO: Foo = Foo {});
22+
23+
impl Drop for Foo {
24+
fn drop(&mut self) {
25+
assert!(FOO.try_with(|_| panic!("`try_with` closure run")).is_err());
26+
unsafe { DROP_RUN = true; }
27+
}
28+
}
29+
30+
fn main() {
31+
thread::spawn(|| {
32+
assert_eq!(FOO.try_with(|_| {
33+
132
34+
}).expect("`try_with` failed"), 132);
35+
}).join().unwrap();
36+
assert!(unsafe { DROP_RUN });
37+
}

0 commit comments

Comments
 (0)