@@ -10,15 +10,10 @@ use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
1010use crate :: tokenstream:: { LazyAttrTokenStream , TokenStream } ;
1111use crate :: util:: comments;
1212use crate :: util:: literal:: escape_string_symbol;
13- use rustc_data_structures:: sync:: WorkerLocal ;
1413use rustc_index:: bit_set:: GrowableBitSet ;
1514use rustc_span:: symbol:: { sym, Ident , Symbol } ;
1615use rustc_span:: Span ;
17- use std:: cell:: Cell ;
1816use std:: iter;
19- #[ cfg( debug_assertions) ]
20- use std:: ops:: BitXor ;
21- #[ cfg( debug_assertions) ]
2217use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
2318use thin_vec:: { thin_vec, ThinVec } ;
2419
@@ -40,39 +35,16 @@ impl MarkedAttrs {
4035 }
4136}
4237
43- pub struct AttrIdGenerator ( WorkerLocal < Cell < u32 > > ) ;
44-
45- #[ cfg( debug_assertions) ]
46- static MAX_ATTR_ID : AtomicU32 = AtomicU32 :: new ( u32:: MAX ) ;
38+ pub struct AttrIdGenerator ( AtomicU32 ) ;
4739
4840impl AttrIdGenerator {
4941 pub fn new ( ) -> Self {
50- // We use `(index as u32).reverse_bits()` to initialize the
51- // starting value of AttrId in each worker thread.
52- // The `index` is the index of the worker thread.
53- // This ensures that the AttrId generated in each thread is unique.
54- AttrIdGenerator ( WorkerLocal :: new ( |index| {
55- let index: u32 = index. try_into ( ) . unwrap ( ) ;
56-
57- #[ cfg( debug_assertions) ]
58- {
59- let max_id = ( ( index + 1 ) . next_power_of_two ( ) - 1 ) . bitxor ( u32:: MAX ) . reverse_bits ( ) ;
60- MAX_ATTR_ID . fetch_min ( max_id, Ordering :: Release ) ;
61- }
62-
63- Cell :: new ( index. reverse_bits ( ) )
64- } ) )
42+ AttrIdGenerator ( AtomicU32 :: new ( 0 ) )
6543 }
6644
6745 pub fn mk_attr_id ( & self ) -> AttrId {
68- let id = self . 0 . get ( ) ;
69-
70- // Ensure the assigned attr_id does not overlap the bits
71- // representing the number of threads.
72- #[ cfg( debug_assertions) ]
73- assert ! ( id <= MAX_ATTR_ID . load( Ordering :: Acquire ) ) ;
74-
75- self . 0 . set ( id + 1 ) ;
46+ let id = self . 0 . fetch_add ( 1 , Ordering :: Relaxed ) ;
47+ assert ! ( id != u32 :: MAX ) ;
7648 AttrId :: from_u32 ( id)
7749 }
7850}
0 commit comments