Skip to content

Commit 27fae2b

Browse files
author
Stjepan Glavina
committed
Remove thread_local_state
1 parent c99f4c4 commit 27fae2b

File tree

3 files changed

+26
-127
lines changed

3 files changed

+26
-127
lines changed

src/libstd/io/stdio.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ use sync::{Arc, Mutex, MutexGuard};
1818
use sys::stdio;
1919
use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
2020
use thread::LocalKey;
21-
#[allow(deprecated)]
22-
use thread::LocalKeyState;
2321

2422
/// Stdout used by print! and println! macros
2523
thread_local! {
@@ -670,25 +668,26 @@ pub fn set_print(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
670668
/// thread, it will just fall back to the global stream.
671669
///
672670
/// However, if the actual I/O causes an error, this function does panic.
673-
#[allow(deprecated)]
674-
fn print_to<T>(args: fmt::Arguments,
675-
local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
676-
global_s: fn() -> T,
677-
label: &str) where T: Write {
678-
let result = match local_s.state() {
679-
LocalKeyState::Uninitialized |
680-
LocalKeyState::Destroyed => global_s().write_fmt(args),
681-
LocalKeyState::Valid => {
682-
local_s.with(|s| {
683-
if let Ok(mut borrowed) = s.try_borrow_mut() {
684-
if let Some(w) = borrowed.as_mut() {
685-
return w.write_fmt(args);
686-
}
687-
}
688-
global_s().write_fmt(args)
689-
})
671+
fn print_to<T>(
672+
args: fmt::Arguments,
673+
local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
674+
global_s: fn() -> T,
675+
label: &str,
676+
)
677+
where
678+
T: Write,
679+
{
680+
let result = local_s.try_with(|s| {
681+
if let Ok(mut borrowed) = s.try_borrow_mut() {
682+
if let Some(w) = borrowed.as_mut() {
683+
return w.write_fmt(args);
684+
}
690685
}
691-
};
686+
global_s().write_fmt(args)
687+
}).unwrap_or_else(|_| {
688+
global_s().write_fmt(args)
689+
});
690+
692691
if let Err(e) = result {
693692
panic!("failed printing to {}: {}", label, e);
694693
}

src/libstd/thread/local.rs

Lines changed: 7 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -195,45 +195,6 @@ macro_rules! __thread_local_inner {
195195
}
196196
}
197197

198-
/// Indicator of the state of a thread local storage key.
199-
#[unstable(feature = "thread_local_state",
200-
reason = "state querying was recently added",
201-
issue = "27716")]
202-
#[rustc_deprecated(since = "1.26.0", reason = "use `LocalKey::try_with` instead")]
203-
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
204-
pub enum LocalKeyState {
205-
/// All keys are in this state whenever a thread starts. Keys will
206-
/// transition to the `Valid` state once the first call to [`with`] happens
207-
/// and the initialization expression succeeds.
208-
///
209-
/// Keys in the `Uninitialized` state will yield a reference to the closure
210-
/// passed to [`with`] so long as the initialization routine does not panic.
211-
///
212-
/// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
213-
Uninitialized,
214-
215-
/// Once a key has been accessed successfully, it will enter the `Valid`
216-
/// state. Keys in the `Valid` state will remain so until the thread exits,
217-
/// at which point the destructor will be run and the key will enter the
218-
/// `Destroyed` state.
219-
///
220-
/// Keys in the `Valid` state will be guaranteed to yield a reference to the
221-
/// closure passed to [`with`].
222-
///
223-
/// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
224-
Valid,
225-
226-
/// When a thread exits, the destructors for keys will be run (if
227-
/// necessary). While a destructor is running, and possibly after a
228-
/// destructor has run, a key is in the `Destroyed` state.
229-
///
230-
/// Keys in the `Destroyed` states will trigger a panic when accessed via
231-
/// [`with`].
232-
///
233-
/// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
234-
Destroyed,
235-
}
236-
237198
/// An error returned by [`LocalKey::try_with`](struct.LocalKey.html#method.try_with).
238199
#[stable(feature = "thread_local_try_with", since = "1.26.0")]
239200
pub struct AccessError {
@@ -307,51 +268,6 @@ impl<T: 'static> LocalKey<T> {
307268
(*ptr).as_ref().unwrap()
308269
}
309270

310-
/// Query the current state of this key.
311-
///
312-
/// A key is initially in the `Uninitialized` state whenever a thread
313-
/// starts. It will remain in this state up until the first call to [`with`]
314-
/// within a thread has run the initialization expression successfully.
315-
///
316-
/// Once the initialization expression succeeds, the key transitions to the
317-
/// `Valid` state which will guarantee that future calls to [`with`] will
318-
/// succeed within the thread. Some keys might skip the `Uninitialized`
319-
/// state altogether and start in the `Valid` state as an optimization
320-
/// (e.g. keys initialized with a constant expression), but no guarantees
321-
/// are made.
322-
///
323-
/// When a thread exits, each key will be destroyed in turn, and as keys are
324-
/// destroyed they will enter the `Destroyed` state just before the
325-
/// destructor starts to run. Keys may remain in the `Destroyed` state after
326-
/// destruction has completed. Keys without destructors (e.g. with types
327-
/// that are [`Copy`]), may never enter the `Destroyed` state.
328-
///
329-
/// Keys in the `Uninitialized` state can be accessed so long as the
330-
/// initialization does not panic. Keys in the `Valid` state are guaranteed
331-
/// to be able to be accessed. Keys in the `Destroyed` state will panic on
332-
/// any call to [`with`].
333-
///
334-
/// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
335-
/// [`Copy`]: ../../std/marker/trait.Copy.html
336-
#[unstable(feature = "thread_local_state",
337-
reason = "state querying was recently added",
338-
issue = "27716")]
339-
#[rustc_deprecated(since = "1.26.0", reason = "use `LocalKey::try_with` instead")]
340-
#[allow(deprecated)]
341-
pub fn state(&'static self) -> LocalKeyState {
342-
unsafe {
343-
match (self.inner)() {
344-
Some(cell) => {
345-
match *cell.get() {
346-
Some(..) => LocalKeyState::Valid,
347-
None => LocalKeyState::Uninitialized,
348-
}
349-
}
350-
None => LocalKeyState::Destroyed,
351-
}
352-
}
353-
}
354-
355271
/// Acquires a reference to the value in this TLS key.
356272
///
357273
/// This will lazily initialize the value if this thread has not referenced
@@ -527,8 +443,6 @@ pub mod os {
527443
mod tests {
528444
use sync::mpsc::{channel, Sender};
529445
use cell::{Cell, UnsafeCell};
530-
#[allow(deprecated)]
531-
use super::LocalKeyState;
532446
use thread;
533447

534448
struct Foo(Sender<()>);
@@ -563,26 +477,21 @@ mod tests {
563477
}
564478

565479
#[test]
566-
#[allow(deprecated)]
567480
fn states() {
568481
struct Foo;
569482
impl Drop for Foo {
570483
fn drop(&mut self) {
571-
assert!(FOO.state() == LocalKeyState::Destroyed);
484+
assert!(FOO.try_with(|_| ()).is_err());
572485
}
573486
}
574487
fn foo() -> Foo {
575-
assert!(FOO.state() == LocalKeyState::Uninitialized);
488+
assert!(FOO.try_with(|_| ()).is_err());
576489
Foo
577490
}
578491
thread_local!(static FOO: Foo = foo());
579492

580493
thread::spawn(|| {
581-
assert!(FOO.state() == LocalKeyState::Uninitialized);
582-
FOO.with(|_| {
583-
assert!(FOO.state() == LocalKeyState::Valid);
584-
});
585-
assert!(FOO.state() == LocalKeyState::Valid);
494+
assert!(FOO.try_with(|_| ()).is_ok());
586495
}).join().ok().unwrap();
587496
}
588497

@@ -601,7 +510,6 @@ mod tests {
601510
}
602511

603512
#[test]
604-
#[allow(deprecated)]
605513
fn circular() {
606514
struct S1;
607515
struct S2;
@@ -612,8 +520,7 @@ mod tests {
612520
impl Drop for S1 {
613521
fn drop(&mut self) {
614522
unsafe {
615-
HITS += 1;
616-
if K2.state() == LocalKeyState::Destroyed {
523+
if K2.try_with(|_| ()).is_err() {
617524
assert_eq!(HITS, 3);
618525
} else {
619526
if HITS == 1 {
@@ -629,7 +536,7 @@ mod tests {
629536
fn drop(&mut self) {
630537
unsafe {
631538
HITS += 1;
632-
assert!(K1.state() != LocalKeyState::Destroyed);
539+
assert!(K1.try_with(|_| ()).is_ok());
633540
assert_eq!(HITS, 2);
634541
K1.with(|s| *s.get() = Some(S1));
635542
}
@@ -642,14 +549,13 @@ mod tests {
642549
}
643550

644551
#[test]
645-
#[allow(deprecated)]
646552
fn self_referential() {
647553
struct S1;
648554
thread_local!(static K1: UnsafeCell<Option<S1>> = UnsafeCell::new(None));
649555

650556
impl Drop for S1 {
651557
fn drop(&mut self) {
652-
assert!(K1.state() == LocalKeyState::Destroyed);
558+
assert!(K1.try_with(|_| ()).is_err());
653559
}
654560
}
655561

@@ -664,7 +570,6 @@ mod tests {
664570
// test on macOS.
665571
#[test]
666572
#[cfg_attr(target_os = "macos", ignore)]
667-
#[allow(deprecated)]
668573
fn dtors_in_dtors_in_dtors() {
669574
struct S1(Sender<()>);
670575
thread_local!(static K1: UnsafeCell<Option<S1>> = UnsafeCell::new(None));
@@ -674,9 +579,7 @@ mod tests {
674579
fn drop(&mut self) {
675580
let S1(ref tx) = *self;
676581
unsafe {
677-
if K2.state() != LocalKeyState::Destroyed {
678-
K2.with(|s| *s.get() = Some(Foo(tx.clone())));
679-
}
582+
let _ = K2.try_with(|s| *s.get() = Some(Foo(tx.clone())));
680583
}
681584
}
682585
}

src/libstd/thread/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ use time::Duration;
192192

193193
#[stable(feature = "rust1", since = "1.0.0")]
194194
pub use self::local::{LocalKey, AccessError};
195-
#[stable(feature = "rust1", since = "1.0.0")]
196-
#[allow(deprecated)]
197-
pub use self::local::LocalKeyState;
198195

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

0 commit comments

Comments
 (0)