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

@ -20,7 +20,6 @@ pub use self::SyntaxContext_::*;
use ast::{Ident, Mrk, Name, SyntaxContext};
use std::cell::RefCell;
use std::rc::Rc;
use std::collections::HashMap;
use std::collections::hash_map::{Occupied, Vacant};
@ -105,16 +104,8 @@ pub fn apply_renames(renames: &RenameList, ctxt: SyntaxContext) -> SyntaxContext
/// Fetch the SCTable from TLS, create one if it doesn't yet exist.
pub fn with_sctable<T>(op: |&SCTable| -> T) -> T {
local_data_key!(sctable_key: Rc<SCTable>)
match sctable_key.get() {
Some(ts) => op(&**ts),
None => {
let ts = Rc::new(new_sctable_internal());
sctable_key.replace(Some(ts.clone()));
op(&*ts)
}
}
thread_local!(static SCTABLE_KEY: SCTable = new_sctable_internal())
SCTABLE_KEY.with(|slot| op(slot))
}
// Make a fresh syntax context table with EmptyCtxt in slot zero
@ -165,16 +156,11 @@ type ResolveTable = HashMap<(Name,SyntaxContext),Name>;
// okay, I admit, putting this in TLS is not so nice:
// fetch the SCTable from TLS, create one if it doesn't yet exist.
fn with_resolve_table_mut<T>(op: |&mut ResolveTable| -> T) -> T {
local_data_key!(resolve_table_key: Rc<RefCell<ResolveTable>>)
thread_local!(static RESOLVE_TABLE_KEY: RefCell<ResolveTable> = {
RefCell::new(HashMap::new())
})
match resolve_table_key.get() {
Some(ts) => op(&mut *ts.borrow_mut()),
None => {
let ts = Rc::new(RefCell::new(HashMap::new()));
resolve_table_key.replace(Some(ts.clone()));
op(&mut *ts.borrow_mut())
}
}
RESOLVE_TABLE_KEY.with(|slot| op(&mut *slot.borrow_mut()))
}
/// Resolve a syntax object to a name, per MTWT.