Skip to content

Commit 62e69cd

Browse files
committed
stage0 fallback
1 parent 5fde1e2 commit 62e69cd

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/libstd/thread/local.rs

+67
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
106106
}
107107
}
108108

109+
#[cfg(not(stage0))]
109110
/// Declare a new thread local storage key of type `std::thread::LocalKey`.
110111
///
111112
/// # Syntax
@@ -145,6 +146,7 @@ macro_rules! thread_local {
145146
);
146147
}
147148

149+
#[cfg(not(stage0))]
148150
#[doc(hidden)]
149151
#[unstable(feature = "thread_local_internals",
150152
reason = "should not be necessary",
@@ -177,6 +179,71 @@ macro_rules! __thread_local_inner {
177179
}
178180
}
179181

182+
#[cfg(stage0)]
183+
/// Declare a new thread local storage key of type `std::thread::LocalKey`.
184+
#[macro_export]
185+
#[stable(feature = "rust1", since = "1.0.0")]
186+
#[allow_internal_unstable]
187+
macro_rules! thread_local {
188+
// rule 0: empty (base case for the recursion)
189+
() => {};
190+
191+
// rule 1: process multiple declarations where the first one is private
192+
($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
193+
thread_local!($(#[$attr])* static $name: $t = $init); // go to rule 2
194+
thread_local!($($rest)*);
195+
);
196+
197+
// rule 2: handle a single private declaration
198+
($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr) => (
199+
$(#[$attr])* static $name: $crate::thread::LocalKey<$t> =
200+
__thread_local_inner!($t, $init);
201+
);
202+
203+
// rule 3: handle multiple declarations where the first one is public
204+
($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
205+
thread_local!($(#[$attr])* pub static $name: $t = $init); // go to rule 4
206+
thread_local!($($rest)*);
207+
);
208+
209+
// rule 4: handle a single public declaration
210+
($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr) => (
211+
$(#[$attr])* pub static $name: $crate::thread::LocalKey<$t> =
212+
__thread_local_inner!($t, $init);
213+
);
214+
}
215+
216+
#[cfg(stage0)]
217+
#[doc(hidden)]
218+
#[unstable(feature = "thread_local_internals",
219+
reason = "should not be necessary",
220+
issue = "0")]
221+
#[macro_export]
222+
#[allow_internal_unstable]
223+
macro_rules! __thread_local_inner {
224+
($t:ty, $init:expr) => {{
225+
fn __init() -> $t { $init }
226+
227+
fn __getit() -> $crate::option::Option<
228+
&'static $crate::cell::UnsafeCell<
229+
$crate::option::Option<$t>>>
230+
{
231+
#[thread_local]
232+
#[cfg(target_thread_local)]
233+
static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
234+
$crate::thread::__FastLocalKeyInner::new();
235+
236+
#[cfg(not(target_thread_local))]
237+
static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
238+
$crate::thread::__OsLocalKeyInner::new();
239+
240+
__KEY.get()
241+
}
242+
243+
$crate::thread::LocalKey::new(__getit, __init)
244+
}}
245+
}
246+
180247
/// Indicator of the state of a thread local storage key.
181248
#[unstable(feature = "thread_local_state",
182249
reason = "state querying was recently added",

0 commit comments

Comments
 (0)