Skip to content

Commit 8b5549d

Browse files
committed
Fix @alexcrichton comments
1 parent a45c8b0 commit 8b5549d

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/libstd/io/stdio.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use io::{self, Initializer, BufReader, LineWriter};
1717
use sync::{Arc, Mutex, MutexGuard};
1818
use sys::stdio;
1919
use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
20-
use thread::LocalKey;
20+
use thread::{LocalKey, LocalKeyState};
2121

2222
/// Stdout used by print! and println! macros
2323
thread_local! {
@@ -674,14 +674,20 @@ fn print_to<T>(args: fmt::Arguments,
674674
local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
675675
global_s: fn() -> T,
676676
label: &str) where T: Write {
677-
let result = local_s.try_with(|s| {
678-
if let Ok(mut borrowed) = s.try_borrow_mut() {
679-
if let Some(w) = borrowed.as_mut() {
680-
return w.write_fmt(args);
681-
}
677+
let result = match local_s.state() {
678+
LocalKeyState::Uninitialized |
679+
LocalKeyState::Destroyed => global_s().write_fmt(args),
680+
LocalKeyState::Valid => {
681+
local_s.with(|s| {
682+
if let Ok(mut borrowed) = s.try_borrow_mut() {
683+
if let Some(w) = borrowed.as_mut() {
684+
return w.write_fmt(args);
685+
}
686+
}
687+
global_s().write_fmt(args)
688+
})
682689
}
683-
global_s().write_fmt(args)
684-
}).unwrap_or_else(|_| global_s().write_fmt(args));
690+
};
685691
if let Err(e) = result {
686692
panic!("failed printing to {}: {}", label, e);
687693
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
use std::thread;
1616

17+
static mut DROP_RUN: bool = false;
18+
1719
struct Foo;
1820

1921
thread_local!(static FOO: Foo = Foo {});
2022

2123
impl Drop for Foo {
2224
fn drop(&mut self) {
2325
assert!(FOO.try_with(|_| panic!("`try_with` closure run")).is_err());
26+
unsafe { DROP_RUN = true; }
2427
}
2528
}
2629

@@ -30,4 +33,5 @@ fn main() {
3033
132
3134
}).expect("`try_with` failed"), 132);
3235
}).join().unwrap();
36+
assert!(unsafe { DROP_RUN });
3337
}

0 commit comments

Comments
 (0)