Skip to content

Commit 684b5fb

Browse files
committed
std: Add an option to disable ELF based TLS
This commit adds a ./configure option called `--disable-elf-tls` which disables ELF based TLS (that which is communicated to LLVM) on platforms which already support it. OSX 10.6 does not support this form of TLS, and some users of Rust need to target 10.6 and are unable to do so due to the usage of TLS. The standard library will continue to use ELF based TLS on OSX by default (as the officially supported platform is 10.7+), but this adds an option to compile the standard library in a way that is compatible with 10.6. Conflicts: src/libstd/thread/local.rs
1 parent 25cc37e commit 684b5fb

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

configure

+1
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ valopt musl-root "/usr/local" "MUSL root installation directory"
593593
opt_nosave manage-submodules 1 "let the build manage the git submodules"
594594
opt_nosave clang 0 "prefer clang to gcc for building the runtime"
595595
opt_nosave jemalloc 1 "build liballoc with jemalloc"
596+
opt elf-tls 1 "elf thread local storage on platforms where supported"
596597

597598
valopt_nosave prefix "/usr/local" "set installation prefix"
598599
valopt_nosave local-rust-root "/usr/local" "set prefix for local rust binary"

mk/crates.mk

+4
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,7 @@ TOOL_INPUTS_$(1) := $$(call rwildcard,$$(dir $$(TOOL_SOURCE_$(1))),*.rs)
153153
endef
154154

155155
$(foreach crate,$(TOOLS),$(eval $(call RUST_TOOL,$(crate))))
156+
157+
ifdef CFG_DISABLE_ELF_TLS
158+
RUSTFLAGS_std := --cfg no_elf_tls
159+
endif

src/libstd/thread/local.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ macro_rules! thread_local {
150150
#[macro_export]
151151
#[doc(hidden)]
152152
#[allow_internal_unstable]
153+
#[cfg(not(no_elf_tls))]
153154
macro_rules! __thread_local_inner {
154155
(static $name:ident: $t:ty = $init:expr) => (
155156
#[cfg_attr(all(any(target_os = "macos", target_os = "linux"),
@@ -193,6 +194,37 @@ macro_rules! __thread_local_inner {
193194
});
194195
}
195196

197+
#[macro_export]
198+
#[doc(hidden)]
199+
#[allow_internal_unstable]
200+
#[cfg(no_elf_tls)]
201+
macro_rules! __thread_local_inner {
202+
(static $name:ident: $t:ty = $init:expr) => (
203+
static $name: ::std::thread::__local::KeyInner<$t> =
204+
__thread_local_inner!($init, $t);
205+
);
206+
(pub static $name:ident: $t:ty = $init:expr) => (
207+
pub static $name: ::std::thread::__local::KeyInner<$t> =
208+
__thread_local_inner!($init, $t);
209+
);
210+
($init:expr, $t:ty) => ({
211+
#[allow(trivial_casts)]
212+
const _INIT: ::std::thread::__local::KeyInner<$t> = {
213+
::std::thread::__local::KeyInner {
214+
inner: ::std::cell::UnsafeCell { value: $init },
215+
os: ::std::thread::__local::OsStaticKey {
216+
inner: ::std::thread::__local::OS_INIT_INNER,
217+
dtor: ::std::option::Option::Some(
218+
::std::thread::__local::destroy_value::<$t>
219+
),
220+
},
221+
}
222+
};
223+
224+
_INIT
225+
});
226+
}
227+
196228
/// Indicator of the state of a thread local storage key.
197229
#[unstable(feature = "std_misc",
198230
reason = "state querying was recently added")]
@@ -295,7 +327,9 @@ impl<T: 'static> LocalKey<T> {
295327
}
296328
}
297329

298-
#[cfg(all(any(target_os = "macos", target_os = "linux"), not(target_arch = "aarch64")))]
330+
#[cfg(all(any(target_os = "macos", target_os = "linux"),
331+
not(target_arch = "aarch64"),
332+
not(no_elf_tls)))]
299333
#[doc(hidden)]
300334
mod imp {
301335
use prelude::v1::*;
@@ -427,7 +461,9 @@ mod imp {
427461
}
428462
}
429463

430-
#[cfg(any(not(any(target_os = "macos", target_os = "linux")), target_arch = "aarch64"))]
464+
#[cfg(any(not(any(target_os = "macos", target_os = "linux")),
465+
target_arch = "aarch64",
466+
no_elf_tls))]
431467
#[doc(hidden)]
432468
mod imp {
433469
use prelude::v1::*;

src/libstd/thread/scoped_tls.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ pub struct ScopedKey<T> { #[doc(hidden)] pub inner: __impl::KeyInner<T> }
6767
/// This macro declares a `static` item on which methods are used to get and
6868
/// set the value stored within.
6969
///
70-
/// See [ScopedKey documentation](thread/struct.ScopedKey.html) for more information.
70+
/// See [ScopedKey documentation](thread/struct.ScopedKey.html) for more
71+
/// information.
7172
#[macro_export]
7273
#[allow_internal_unstable]
74+
#[cfg(not(no_elf_tls))]
7375
macro_rules! scoped_thread_local {
7476
(static $name:ident: $t:ty) => (
7577
__scoped_thread_local_inner!(static $name: $t);
@@ -133,6 +135,20 @@ macro_rules! __scoped_thread_local_inner {
133135
})
134136
}
135137

138+
#[macro_export]
139+
#[allow_internal_unstable]
140+
#[cfg(no_elf_tls)]
141+
macro_rules! scoped_thread_local {
142+
(static $name:ident: $t:ty) => (
143+
static $name: ::std::thread::ScopedKey<$t> =
144+
::std::thread::ScopedKey::new();
145+
);
146+
(pub static $name:ident: $t:ty) => (
147+
pub static $name: ::std::thread::ScopedKey<$t> =
148+
::std::thread::ScopedKey::new();
149+
);
150+
}
151+
136152
#[unstable(feature = "scoped_tls",
137153
reason = "scoped TLS has yet to have wide enough use to fully consider \
138154
stabilizing its interface")]
@@ -229,7 +245,8 @@ impl<T> ScopedKey<T> {
229245
target_os = "android",
230246
target_os = "ios",
231247
target_os = "openbsd",
232-
target_arch = "aarch64")))]
248+
target_arch = "aarch64",
249+
no_elf_tls)))]
233250
mod imp {
234251
use std::cell::UnsafeCell;
235252

@@ -251,7 +268,8 @@ mod imp {
251268
target_os = "android",
252269
target_os = "ios",
253270
target_os = "openbsd",
254-
target_arch = "aarch64"))]
271+
target_arch = "aarch64",
272+
no_elf_tls))]
255273
mod imp {
256274
use marker;
257275
use std::cell::Cell;

0 commit comments

Comments
 (0)