Skip to content

Commit 1795ae4

Browse files
committed
add #[thread_local] attribute
This provides a building block for fast thread-local storage. It does not change the safety semantics of `static mut`. Closes #10310
1 parent e03f17e commit 1795ae4

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

doc/rust.md

+2
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,8 @@ names are effectively reserved. Some significant attributes include:
17541754
* The `deriving` attribute, for automatically generating
17551755
implementations of certain traits.
17561756
* The `static_assert` attribute, for asserting that a static bool is true at compiletime
1757+
* The `thread_local` attribute, for defining a `static mut` as a thread-local. Note that this is
1758+
only a low-level building block, and is not local to a *task*, nor does it provide safety.
17571759

17581760
Other attributes may be added or removed during development of the language.
17591761

src/librustc/lib/llvm.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,12 @@ pub fn SetUnnamedAddr(Global: ValueRef, Unnamed: bool) {
17491749
}
17501750
}
17511751

1752+
pub fn set_thread_local(global: ValueRef, is_thread_local: bool) {
1753+
unsafe {
1754+
llvm::LLVMSetThreadLocal(global, is_thread_local as Bool);
1755+
}
1756+
}
1757+
17521758
pub fn ConstICmp(Pred: IntPredicate, V1: ValueRef, V2: ValueRef) -> ValueRef {
17531759
unsafe {
17541760
llvm::LLVMConstICmp(Pred as c_ushort, V1, V2)

src/librustc/middle/lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ static obsolete_attrs: &'static [(&'static str, &'static str)] = &[
816816
static other_attrs: &'static [&'static str] = &[
817817
// item-level
818818
"address_insignificant", // can be crate-level too
819+
"thread_local", // for statics
819820
"allow", "deny", "forbid", "warn", // lint options
820821
"deprecated", "experimental", "unstable", "stable", "locked", "frozen", //item stability
821822
"crate_map", "cfg", "doc", "export_name", "link_section", "no_freeze",

src/librustc/middle/trans/base.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,10 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
25432543
inlineable = true;
25442544
}
25452545

2546+
if attr::contains_name(i.attrs, "thread_local") {
2547+
lib::llvm::set_thread_local(g, true);
2548+
}
2549+
25462550
if !inlineable {
25472551
debug!("{} not inlined", sym);
25482552
ccx.non_inlineable_statics.insert(id);

0 commit comments

Comments
 (0)