Skip to content

Commit

Permalink
stage0 fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
durka committed Apr 21, 2017
1 parent 5fde1e2 commit 62e69cd
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/libstd/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
}
}

#[cfg(not(stage0))]
/// Declare a new thread local storage key of type `std::thread::LocalKey`.
///
/// # Syntax
Expand Down Expand Up @@ -145,6 +146,7 @@ macro_rules! thread_local {
);
}

#[cfg(not(stage0))]
#[doc(hidden)]
#[unstable(feature = "thread_local_internals",
reason = "should not be necessary",
Expand Down Expand Up @@ -177,6 +179,71 @@ macro_rules! __thread_local_inner {
}
}

#[cfg(stage0)]
/// Declare a new thread local storage key of type `std::thread::LocalKey`.
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
macro_rules! thread_local {
// rule 0: empty (base case for the recursion)
() => {};

// rule 1: process multiple declarations where the first one is private
($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
thread_local!($(#[$attr])* static $name: $t = $init); // go to rule 2
thread_local!($($rest)*);
);

// rule 2: handle a single private declaration
($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr) => (
$(#[$attr])* static $name: $crate::thread::LocalKey<$t> =
__thread_local_inner!($t, $init);
);

// rule 3: handle multiple declarations where the first one is public
($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
thread_local!($(#[$attr])* pub static $name: $t = $init); // go to rule 4
thread_local!($($rest)*);
);

// rule 4: handle a single public declaration
($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr) => (
$(#[$attr])* pub static $name: $crate::thread::LocalKey<$t> =
__thread_local_inner!($t, $init);
);
}

#[cfg(stage0)]
#[doc(hidden)]
#[unstable(feature = "thread_local_internals",
reason = "should not be necessary",
issue = "0")]
#[macro_export]
#[allow_internal_unstable]
macro_rules! __thread_local_inner {
($t:ty, $init:expr) => {{
fn __init() -> $t { $init }

fn __getit() -> $crate::option::Option<
&'static $crate::cell::UnsafeCell<
$crate::option::Option<$t>>>
{
#[thread_local]
#[cfg(target_thread_local)]
static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
$crate::thread::__FastLocalKeyInner::new();

#[cfg(not(target_thread_local))]
static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
$crate::thread::__OsLocalKeyInner::new();

__KEY.get()
}

$crate::thread::LocalKey::new(__getit, __init)
}}
}

/// Indicator of the state of a thread local storage key.
#[unstable(feature = "thread_local_state",
reason = "state querying was recently added",
Expand Down

0 comments on commit 62e69cd

Please sign in to comment.