@@ -99,37 +99,60 @@ pub struct LocalKey<T> {
9999
100100/// Declare a new thread local storage key of type `std::thread::LocalKey`.
101101///
102- /// See [LocalKey documentation](thread/struct.LocalKey.html) for more information.
102+ /// See [LocalKey documentation](thread/struct.LocalKey.html) for more
103+ /// information.
103104#[ macro_export]
104105#[ stable( feature = "rust1" , since = "1.0.0" ) ]
105106#[ allow_internal_unstable]
107+ #[ cfg( not( no_elf_tls) ) ]
106108macro_rules! thread_local {
107109 ( static $name: ident: $t: ty = $init: expr) => (
108- static $name: :: std:: thread:: LocalKey <$t> = {
109- #[ cfg_attr( all( any( target_os = "macos" , target_os = "linux" ) ,
110- not( target_arch = "aarch64" ) ) ,
111- thread_local) ]
112- static __KEY: :: std:: thread:: __LocalKeyInner<$t> =
113- :: std:: thread:: __LocalKeyInner:: new( ) ;
114- fn __init( ) -> $t { $init }
115- fn __getit( ) -> & ' static :: std:: thread:: __LocalKeyInner<$t> { & __KEY }
116- :: std:: thread:: LocalKey :: new( __getit, __init)
117- } ;
110+ static $name: :: std:: thread:: LocalKey <$t> =
111+ __thread_local_inner!( $t, $init,
112+ #[ cfg_attr( all( any( target_os = "macos" , target_os = "linux" ) ,
113+ not( target_arch = "aarch64" ) ) ,
114+ thread_local) ] ) ;
118115 ) ;
119116 ( pub static $name: ident: $t: ty = $init: expr) => (
120- pub static $name: :: std:: thread:: LocalKey <$t> = {
121- #[ cfg_attr( all( any( target_os = "macos" , target_os = "linux" ) ,
122- not( target_arch = "aarch64" ) ) ,
123- thread_local) ]
124- static __KEY: :: std:: thread:: __LocalKeyInner<$t> =
125- :: std:: thread:: __LocalKeyInner:: new( ) ;
126- fn __init( ) -> $t { $init }
127- fn __getit( ) -> & ' static :: std:: thread:: __LocalKeyInner<$t> { & __KEY }
128- :: std:: thread:: LocalKey :: new( __getit, __init)
129- } ;
117+ pub static $name: :: std:: thread:: LocalKey <$t> =
118+ __thread_local_inner!( $t, $init,
119+ #[ cfg_attr( all( any( target_os = "macos" , target_os = "linux" ) ,
120+ not( target_arch = "aarch64" ) ) ,
121+ thread_local) ] ) ;
130122 ) ;
131123}
132124
125+ #[ macro_export]
126+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
127+ #[ allow_internal_unstable]
128+ #[ cfg( no_elf_tls) ]
129+ macro_rules! thread_local {
130+ ( static $name: ident: $t: ty = $init: expr) => (
131+ static $name: :: std:: thread:: LocalKey <$t> =
132+ __thread_local_inner!( $t, $init, #[ ] ) ;
133+ ) ;
134+ ( pub static $name: ident: $t: ty = $init: expr) => (
135+ pub static $name: :: std:: thread:: LocalKey <$t> =
136+ __thread_local_inner!( $t, $init, #[ ] ) ;
137+ ) ;
138+ }
139+
140+ #[ doc( hidden) ]
141+ #[ unstable( feature = "thread_local_internals" ,
142+ reason = "should not be necessary" ) ]
143+ #[ macro_export]
144+ #[ allow_internal_unstable]
145+ macro_rules! __thread_local_inner {
146+ ( $t: ty, $init: expr, #[ $( $attr: meta) ,* ] ) => { {
147+ $( #[ $attr] ) *
148+ static __KEY: :: std:: thread:: __LocalKeyInner<$t> =
149+ :: std:: thread:: __LocalKeyInner:: new( ) ;
150+ fn __init( ) -> $t { $init }
151+ fn __getit( ) -> & ' static :: std:: thread:: __LocalKeyInner<$t> { & __KEY }
152+ :: std:: thread:: LocalKey :: new( __getit, __init)
153+ } }
154+ }
155+
133156/// Indicator of the state of a thread local storage key.
134157#[ unstable( feature = "std_misc" ,
135158 reason = "state querying was recently added" ) ]
@@ -163,7 +186,10 @@ pub enum LocalKeyState {
163186
164187impl < T : ' static > LocalKey < T > {
165188 #[ doc( hidden) ]
166- pub const fn new ( inner : fn ( ) -> & ' static __KeyInner < T > , init : fn ( ) -> T ) -> LocalKey < T > {
189+ #[ unstable( feature = "thread_local_internals" ,
190+ reason = "recently added to create a key" ) ]
191+ pub const fn new ( inner : fn ( ) -> & ' static __KeyInner < T > ,
192+ init : fn ( ) -> T ) -> LocalKey < T > {
167193 LocalKey {
168194 inner : inner,
169195 init : init
@@ -240,7 +266,9 @@ impl<T: 'static> LocalKey<T> {
240266 }
241267}
242268
243- #[ cfg( all( any( target_os = "macos" , target_os = "linux" ) , not( target_arch = "aarch64" ) ) ) ]
269+ #[ cfg( all( any( target_os = "macos" , target_os = "linux" ) ,
270+ not( target_arch = "aarch64" ) ,
271+ not( no_elf_tls) ) ) ]
244272#[ doc( hidden) ]
245273mod imp {
246274 use prelude:: v1:: * ;
@@ -371,7 +399,9 @@ mod imp {
371399 }
372400}
373401
374- #[ cfg( any( not( any( target_os = "macos" , target_os = "linux" ) ) , target_arch = "aarch64" ) ) ]
402+ #[ cfg( any( not( any( target_os = "macos" , target_os = "linux" ) ) ,
403+ target_arch = "aarch64" ,
404+ no_elf_tls) ) ]
375405#[ doc( hidden) ]
376406mod imp {
377407 use prelude:: v1:: * ;
0 commit comments