Skip to content

Commit 8e7c488

Browse files
committed
stage0 fallback
1 parent 8cce5bc commit 8e7c488

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/libstd/thread/local.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
110110
}
111111
}
112112

113+
#[cfg(not(stage0))]
113114
/// Declare a new thread local storage key of type [`std::thread::LocalKey`].
114115
///
115116
/// # Syntax
@@ -151,6 +152,7 @@ macro_rules! thread_local {
151152
);
152153
}
153154

155+
#[cfg(not(stage0))]
154156
#[doc(hidden)]
155157
#[unstable(feature = "thread_local_internals",
156158
reason = "should not be necessary",
@@ -183,6 +185,71 @@ macro_rules! __thread_local_inner {
183185
}
184186
}
185187

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

0 commit comments

Comments
 (0)