From a9c1152c4bf72132806cb76045b3464d59db07da Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 14 Nov 2014 14:20:57 -0800 Subject: 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>> = Rc::new(RefCell::new(None))) The old `local_data` module inherently contained the `Rc>>` as an implementation detail which must now be explicitly stated by users. [rfc]: https://github.com/rust-lang/rfcs/pull/461 [breaking-change] --- src/libsyntax/attr.rs | 20 +++++++++++--------- src/libsyntax/diagnostics/plugin.rs | 32 ++++++++++++-------------------- src/libsyntax/ext/mtwt.rs | 28 +++++++--------------------- src/libsyntax/parse/token.rs | 13 ++++--------- 4 files changed, 34 insertions(+), 59 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 42fdb50f87a..fdfa275549a 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -25,21 +25,20 @@ use parse::token::InternedString; use parse::token; use ptr::P; -use std::collections::HashSet; +use std::cell::{RefCell, Cell}; use std::collections::BitvSet; +use std::collections::HashSet; -local_data_key!(used_attrs: BitvSet) +thread_local!(static USED_ATTRS: RefCell = RefCell::new(BitvSet::new())) pub fn mark_used(attr: &Attribute) { - let mut used = used_attrs.replace(None).unwrap_or_else(|| BitvSet::new()); let AttrId(id) = attr.node.id; - used.insert(id); - used_attrs.replace(Some(used)); + USED_ATTRS.with(|slot| slot.borrow_mut().insert(id)); } pub fn is_used(attr: &Attribute) -> bool { let AttrId(id) = attr.node.id; - used_attrs.get().map_or(false, |used| used.contains(&id)) + USED_ATTRS.with(|slot| slot.borrow().contains(&id)) } pub trait AttrMetaMethods { @@ -167,11 +166,14 @@ pub fn mk_word_item(name: InternedString) -> P { P(dummy_spanned(MetaWord(name))) } -local_data_key!(next_attr_id: uint) +thread_local!(static NEXT_ATTR_ID: Cell = Cell::new(0)) pub fn mk_attr_id() -> AttrId { - let id = next_attr_id.replace(None).unwrap_or(0); - next_attr_id.replace(Some(id + 1)); + let id = NEXT_ATTR_ID.with(|slot| { + let r = slot.get(); + slot.set(r + 1); + r + }); AttrId(id) } diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs index 281bde3129a..5f4e675aad5 100644 --- a/src/libsyntax/diagnostics/plugin.rs +++ b/src/libsyntax/diagnostics/plugin.rs @@ -18,31 +18,23 @@ use ext::build::AstBuilder; use parse::token; use ptr::P; -local_data_key!(registered_diagnostics: RefCell>>) -local_data_key!(used_diagnostics: RefCell>) +thread_local!(static REGISTERED_DIAGNOSTICS: RefCell>> = { + RefCell::new(HashMap::new()) +}) +thread_local!(static USED_DIAGNOSTICS: RefCell> = { + RefCell::new(HashMap::new()) +}) fn with_registered_diagnostics(f: |&mut HashMap>| -> T) -> T { - match registered_diagnostics.get() { - Some(cell) => f(cell.borrow_mut().deref_mut()), - None => { - let mut map = HashMap::new(); - let value = f(&mut map); - registered_diagnostics.replace(Some(RefCell::new(map))); - value - } - } + REGISTERED_DIAGNOSTICS.with(|slot| { + f(&mut *slot.borrow_mut()) + }) } fn with_used_diagnostics(f: |&mut HashMap| -> T) -> T { - match used_diagnostics.get() { - Some(cell) => f(cell.borrow_mut().deref_mut()), - None => { - let mut map = HashMap::new(); - let value = f(&mut map); - used_diagnostics.replace(Some(RefCell::new(map))); - value - } - } + USED_DIAGNOSTICS.with(|slot| { + f(&mut *slot.borrow_mut()) + }) } pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt, diff --git a/src/libsyntax/ext/mtwt.rs b/src/libsyntax/ext/mtwt.rs index 2ddcab10cda..e6d886e28ba 100644 --- a/src/libsyntax/ext/mtwt.rs +++ b/src/libsyntax/ext/mtwt.rs @@ -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(op: |&SCTable| -> T) -> T { - local_data_key!(sctable_key: Rc) - - 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(op: |&mut ResolveTable| -> T) -> T { - local_data_key!(resolve_table_key: Rc>) - - 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()) - } - } + thread_local!(static RESOLVE_TABLE_KEY: RefCell = { + RefCell::new(HashMap::new()) + }) + + RESOLVE_TABLE_KEY.with(|slot| op(&mut *slot.borrow_mut())) } /// Resolve a syntax object to a name, per MTWT. diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 4272b57a4dc..3a3407aedba 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -560,15 +560,10 @@ pub type IdentInterner = StrInterner; // fresh one. // FIXME(eddyb) #8726 This should probably use a task-local reference. pub fn get_ident_interner() -> Rc { - local_data_key!(key: Rc<::parse::token::IdentInterner>) - match key.get() { - Some(interner) => interner.clone(), - None => { - let interner = Rc::new(mk_fresh_ident_interner()); - key.replace(Some(interner.clone())); - interner - } - } + thread_local!(static KEY: Rc<::parse::token::IdentInterner> = { + Rc::new(mk_fresh_ident_interner()) + }) + KEY.with(|k| k.clone()) } /// Represents a string stored in the task-local interner. Because the -- cgit 1.4.1-3-g733a5