]> git.lizzy.rs Git - rust.git/commitdiff
Make mk_attr_id thread safe
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Sun, 3 Dec 2017 13:03:28 +0000 (14:03 +0100)
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Thu, 21 Dec 2017 18:21:39 +0000 (19:21 +0100)
src/libsyntax/attr.rs
src/libsyntax/lib.rs

index e5e95002e107933e12be56e58fbf4d833cb17e14..cc72ff7d15d2baa615cb044a2c63b566c20f3e96 100644 (file)
@@ -31,7 +31,7 @@
 use tokenstream::{TokenStream, TokenTree, Delimited};
 use util::ThinVec;
 
-use std::cell::{RefCell, Cell};
+use std::cell::RefCell;
 use std::iter;
 
 thread_local! {
@@ -419,16 +419,14 @@ pub fn mk_spanned_word_item(sp: Span, name: Name) -> MetaItem {
     MetaItem { span: sp, name: name, node: MetaItemKind::Word }
 }
 
+pub fn mk_attr_id() -> AttrId {
+    use std::sync::atomic::AtomicUsize;
+    use std::sync::atomic::Ordering;
 
+    static NEXT_ATTR_ID: AtomicUsize = AtomicUsize::new(0);
 
-thread_local! { static NEXT_ATTR_ID: Cell<usize> = Cell::new(0) }
-
-pub fn mk_attr_id() -> AttrId {
-    let id = NEXT_ATTR_ID.with(|slot| {
-        let r = slot.get();
-        slot.set(r + 1);
-        r
-    });
+    let id = NEXT_ATTR_ID.fetch_add(1, Ordering::SeqCst);
+    assert!(id != ::std::usize::MAX);
     AttrId(id)
 }
 
index 44383233a8af190b505944f6ed57dba8df52ab65..0b51f2e9814567f0621bc1adadbe173c3c7ab7af 100644 (file)
@@ -24,6 +24,7 @@
 #![feature(rustc_diagnostic_macros)]
 #![feature(match_default_bindings)]
 #![feature(i128_type)]
+#![feature(const_atomic_usize_new)]
 
 // See librustc_cratesio_shim/Cargo.toml for a comment explaining this.
 #[allow(unused_extern_crates)]