std: Add a new top-level thread_local module

This commit removes the `std::local_data` module in favor of a new
`std::thread_local` module providing thread local storage. The module provides
two variants of TLS: one which owns its contents and one which is based on
scoped references. Each implementation has pros and cons listed in the
documentation.

Both flavors have accessors through a function called `with` which yield a
reference to a closure provided. Both flavors also panic if a reference cannot
be yielded and provide a function to test whether an access would panic or not.
This is an implementation of [RFC 461][rfc] and full details can be found in
that RFC.

This is a breaking change due to the removal of the `std::local_data` module.
All users can migrate to the new thread local system like so:

    thread_local!(static FOO: Rc<RefCell<Option<T>>> = Rc::new(RefCell::new(None)))

The old `local_data` module inherently contained the `Rc<RefCell<Option<T>>>` as
an implementation detail which must now be explicitly stated by users.

[rfc]: https://github.com/rust-lang/rfcs/pull/461
[breaking-change]
This commit is contained in:
Alex Crichton 2014-11-14 14:20:57 -08:00
parent 4e5259503c
commit a9c1152c4b
38 changed files with 1810 additions and 1151 deletions

View file

@ -15,6 +15,7 @@
extern crate rustc;
use std::any::Any;
use std::cell::RefCell;
use rustc::plugin::Registry;
struct Foo {
@ -27,7 +28,7 @@ impl Drop for Foo {
#[plugin_registrar]
pub fn registrar(_: &mut Registry) {
local_data_key!(foo: Box<Any+Send>);
foo.replace(Some(box Foo { foo: 10 } as Box<Any+Send>));
thread_local!(static FOO: RefCell<Option<Box<Any+Send>>> = RefCell::new(None));
FOO.with(|s| *s.borrow_mut() = Some(box Foo { foo: 10 } as Box<Any+Send>));
}

View file

@ -1,16 +0,0 @@
// Copyright 2012 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.
// Testing that we can't store a reference in task-local storage
local_data_key!(key: Box<&int>)
//~^ ERROR missing lifetime specifier
fn main() {}

View file

@ -11,10 +11,10 @@
// check that the local data keys are private by default.
mod bar {
local_data_key!(baz: f64)
thread_local!(static baz: f64 = 0.0)
}
fn main() {
bar::baz.replace(Some(-10.0));
bar::baz.with(|_| ());
//~^ ERROR static `baz` is private
}

View file

@ -1,26 +0,0 @@
// Copyright 2013 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.
local_data_key!(foo: int)
mod bar {
local_data_key!(pub baz: f64)
}
pub fn main() {
assert!(foo.get().is_none());
assert!(bar::baz.get().is_none());
foo.replace(Some(3));
bar::baz.replace(Some(-10.0));
assert_eq!(*foo.get().unwrap(), 3);
assert_eq!(*bar::baz.get().unwrap(), -10.0);
}

View file

@ -1,33 +0,0 @@
// Copyright 2014 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.
use std::task;
static mut DROPS: uint = 0;
struct Foo;
impl Drop for Foo {
fn drop(&mut self) {
unsafe { DROPS += 1; }
panic!()
}
}
fn main() {
let _ = task::try(proc() {
local_data_key!(foo: Foo);
foo.replace(Some(Foo));
});
unsafe {
assert_eq!(DROPS, 1);
}
}

View file

@ -17,8 +17,6 @@ use std::rt;
use rustrt::unwind::try;
local_data_key!(foo: int)
#[start]
fn start(argc: int, argv: *const *const u8) -> int {
if argc > 1 {
@ -30,8 +28,6 @@ fn start(argc: int, argv: *const *const u8) -> int {
4 => assert!(try(|| panic!()).is_err()),
5 => assert!(try(|| spawn(proc() {})).is_err()),
6 => assert!(Command::new("test").spawn().is_err()),
7 => assert!(foo.get().is_none()),
8 => assert!(try(|| { foo.replace(Some(3)); }).is_err()),
_ => panic!()
}
}
@ -57,10 +53,6 @@ fn main() {
pass(Command::new(me).arg(x).output().unwrap());
let x: &[u8] = &[6u8];
pass(Command::new(me).arg(x).output().unwrap());
let x: &[u8] = &[7u8];
pass(Command::new(me).arg(x).output().unwrap());
let x: &[u8] = &[8u8];
pass(Command::new(me).arg(x).output().unwrap());
}
fn pass(output: ProcessOutput) {