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

std::thread::LocalKeyState: Add state Initializing #43550

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ fn print_to<T>(args: fmt::Arguments,
label: &str) where T: Write {
let result = match local_s.state() {
LocalKeyState::Uninitialized |
LocalKeyState::Initializing |
LocalKeyState::Destroyed => global_s().write_fmt(args),
LocalKeyState::Valid => {
local_s.with(|s| {
Expand Down
16 changes: 0 additions & 16 deletions src/libstd/sys/unix/fast_thread_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,3 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
// a more direct implementation.
#[cfg(target_os = "fuchsia")]
pub use sys_common::thread_local::register_dtor_fallback as register_dtor;

pub fn requires_move_before_drop() -> bool {
// The macOS implementation of TLS apparently had an odd aspect to it
// where the pointer we have may be overwritten while this destructor
// is running. Specifically if a TLS destructor re-accesses TLS it may
// trigger a re-initialization of all TLS variables, paving over at
// least some destroyed ones with initial values.
//
// This means that if we drop a TLS value in place on macOS that we could
// revert the value to its original state halfway through the
// destructor, which would be bad!
//
// Hence, we use `ptr::read` on macOS (to move to a "safe" location)
// instead of drop_in_place.
cfg!(target_os = "macos")
}
411 changes: 285 additions & 126 deletions src/libstd/thread/local.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ pub use self::local::{LocalKey, LocalKeyState, AccessError};
// where fast TLS was not available; end-user code is compiled with fast TLS
// where available, but both are needed.

#[unstable(feature = "libstd_thread_internals", issue = "0")]
#[doc(hidden)] pub use self::local::LocalKeyValue as __LocalKeyValue;
#[unstable(feature = "libstd_thread_internals", issue = "0")]
#[cfg(target_thread_local)]
#[doc(hidden)] pub use self::local::fast::Key as __FastLocalKeyInner;
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-43733-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// a custom non-`Sync` type to fake the same error.
#[cfg(not(target_thread_local))]
struct Key<T> {
_data: std::cell::UnsafeCell<Option<T>>,
_data: std::cell::UnsafeCell<std::thread::__LocalKeyValue>,
_flag: std::cell::Cell<bool>,
}

Expand All @@ -33,7 +33,7 @@ impl<T> Key<T> {
use std::thread::__FastLocalKeyInner as Key;

static __KEY: Key<()> = Key::new();
//~^ ERROR `std::cell::UnsafeCell<std::option::Option<()>>: std::marker::Sync` is not satisfied
//~^ ERROR `std::cell::UnsafeCell<std::thread::LocalKeyValue<()>>: std::marker::Sync` is not
//~| ERROR `std::cell::Cell<bool>: std::marker::Sync` is not satisfied

fn main() {}
18 changes: 10 additions & 8 deletions src/test/compile-fail/issue-43733.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@
// except according to those terms.

#![feature(const_fn, drop_types_in_const)]
#![feature(cfg_target_thread_local, thread_local_internals)]
#![feature(cfg_target_thread_local, thread_local_internals, thread_local_state)]

type Foo = std::cell::RefCell<String>;

#[cfg(target_thread_local)]
static __KEY: std::thread::__FastLocalKeyInner<Foo> =
std::thread::__FastLocalKeyInner::new();

#[cfg(not(target_thread_local))]
static __KEY: std::thread::__OsLocalKeyInner<Foo> =
std::thread::__OsLocalKeyInner::new();

fn __getit() -> std::option::Option<
&'static std::cell::UnsafeCell<
std::option::Option<Foo>>>
{
fn __get() -> &'static std::cell::UnsafeCell<std::thread::__LocalKeyValue<Foo>> {
__KEY.get() //~ ERROR invocation of unsafe method requires unsafe
}

fn __register_dtor() {
#[cfg(target_thread_local)]
__KEY.register_dtor() //~ ERROR invocation of unsafe method requires unsafe
}

static FOO: std::thread::LocalKey<Foo> =
std::thread::LocalKey::new(__getit, Default::default);
//~^ ERROR call to unsafe function requires unsafe
std::thread::LocalKey::new(__get, //~ ERROR call to unsafe function requires unsafe
__register_dtor,
Default::default);

fn main() {
FOO.with(|foo| println!("{}", foo.borrow()));
Expand Down
35 changes: 35 additions & 0 deletions src/test/run-fail/tls-init-on-init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-emscripten no threads support

// Can't include entire panic message because it'd exceed tidy's 100-character line length limit.
// error-pattern:cannot access a TLS value while it is being initialized or during or after

#![feature(thread_local_state)]

use std::thread::{self, LocalKeyState};

struct Foo;

thread_local!(static FOO: Foo = Foo::init());

impl Foo {
fn init() -> Foo {
FOO.with(|_| {});
Foo
}
}

fn main() {
thread::spawn(|| {
FOO.with(|_| {});
}).join().unwrap();
}
12 changes: 4 additions & 8 deletions src/test/run-pass/tls-init-on-init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static CNT: AtomicUsize = ATOMIC_USIZE_INIT;
impl Foo {
fn init() -> Foo {
let cnt = CNT.fetch_add(1, Ordering::SeqCst);
if cnt == 0 {
if FOO.state() == LocalKeyState::Uninitialized {
FOO.with(|_| {});
}
Foo { cnt: cnt }
Expand All @@ -34,20 +34,16 @@ impl Foo {
impl Drop for Foo {
fn drop(&mut self) {
if self.cnt == 1 {
FOO.with(|foo| assert_eq!(foo.cnt, 0));
assert_eq!(FOO.state(), LocalKeyState::Destroyed);
} else {
assert_eq!(self.cnt, 0);
match FOO.state() {
LocalKeyState::Valid => panic!("should not be in valid state"),
LocalKeyState::Uninitialized |
LocalKeyState::Destroyed => {}
}
assert_eq!(FOO.state(), LocalKeyState::Valid);
}
}
}

fn main() {
thread::spawn(|| {
FOO.with(|_| {});
Foo::init();
}).join().unwrap();
}