Skip to content

Commit 0b17592

Browse files
committed
[WIP] Tls support
1 parent 6156f48 commit 0b17592

14 files changed

+140
-89
lines changed

Cargo.lock

+22-41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ default-features = false
3030
features = ["write"] # We don't need read support
3131

3232
# Uncomment to use local checkout of cranelift
33-
#[patch."https://github.com/bytecodealliance/cranelift/"]
34-
#cranelift-codegen = { path = "../cranelift/cranelift-codegen" }
35-
#cranelift-frontend = { path = "../cranelift/cranelift-frontend" }
36-
#cranelift-module = { path = "../cranelift/cranelift-module" }
37-
#cranelift-simplejit = { path = "../cranelift/cranelift-simplejit" }
38-
#cranelift-object = { path = "../cranelift/cranelift-object" }
33+
[patch."https://github.com/bytecodealliance/cranelift/"]
34+
cranelift-codegen = { path = "../cranelift/cranelift-codegen", default-features = false, features = ["std"] }
35+
cranelift-frontend = { path = "../cranelift/cranelift-frontend", default-features = false, features = ["std"] }
36+
cranelift-module = { path = "../cranelift/cranelift-module" }
37+
cranelift-simplejit = { path = "../cranelift/cranelift-simplejit" }
38+
cranelift-object = { path = "../cranelift/cranelift-object" }
3939

4040
#[patch.crates-io]
4141
#gimli = { path = "../" }

crate_patches/regex.patch

-34
This file was deleted.

example/mini_core.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(
22
no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types,
3-
untagged_unions, decl_macro, rustc_attrs, transparent_unions, optin_builtin_traits
3+
untagged_unions, decl_macro, rustc_attrs, transparent_unions, optin_builtin_traits,
4+
thread_local,
45
)]
56
#![no_core]
67
#![allow(dead_code)]
@@ -551,3 +552,11 @@ struct PanicLocation {
551552
line: u32,
552553
column: u32,
553554
}
555+
556+
#[no_mangle]
557+
pub fn get_tls() -> u8 {
558+
#[thread_local]
559+
static A: u8 = 42;
560+
561+
A
562+
}

example/mini_core_hello_world.rs

+73-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Adapted from https://github.com/sunfishcode/mir2cranelift/blob/master/rust-examples/nocore-hello-world.rs
22

3-
#![feature(no_core, unboxed_closures, start, lang_items, box_syntax, slice_patterns, never_type, linkage, extern_types)]
3+
#![feature(
4+
no_core, unboxed_closures, start, lang_items, box_syntax, slice_patterns, never_type, linkage,
5+
extern_types, thread_local
6+
)]
47
#![no_core]
5-
#![allow(dead_code)]
8+
#![allow(dead_code, non_camel_case_types)]
69

710
extern crate mini_core;
811

@@ -276,6 +279,74 @@ fn main() {
276279
extern_nullptr as *const ();
277280
let slice_ptr = &[] as *const [u8];
278281
slice_ptr as *const u8;
282+
283+
test_tls();
284+
}
285+
286+
#[repr(C)]
287+
enum c_void {
288+
_1,
289+
_2,
290+
}
291+
292+
type c_int = i32;
293+
type c_ulong = u64;
294+
295+
type pthread_t = c_ulong;
296+
297+
#[repr(C)]
298+
struct pthread_attr_t {
299+
__size: [u64; 7],
300+
}
301+
302+
#[link(name = "pthread")]
303+
extern "C" {
304+
fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int;
305+
306+
fn pthread_create(
307+
native: *mut pthread_t,
308+
attr: *const pthread_attr_t,
309+
f: extern "C" fn(_: *mut c_void) -> *mut c_void,
310+
value: *mut c_void
311+
) -> c_int;
312+
313+
fn pthread_join(
314+
native: pthread_t,
315+
value: *mut *mut c_void
316+
) -> c_int;
317+
}
318+
319+
#[thread_local]
320+
static mut TLS: u8 = 42;
321+
322+
extern "C" fn mutate_tls(_: *mut c_void) -> *mut c_void {
323+
unsafe { TLS = 0; }
324+
0 as *mut c_void
325+
}
326+
327+
fn test_tls() {
328+
unsafe {
329+
let mut attr: pthread_attr_t = intrinsics::init();
330+
let mut thread: pthread_t = 0;
331+
332+
assert_eq!(TLS, 42);
333+
334+
if pthread_attr_init(&mut attr) != 0 {
335+
assert!(false);
336+
}
337+
338+
if pthread_create(&mut thread, &attr, mutate_tls, 0 as *mut c_void) != 0 {
339+
assert!(false);
340+
}
341+
342+
let mut res = 0 as *mut c_void;
343+
pthread_join(thread, &mut res);
344+
345+
// TLS of main thread must not have been changed by the other thread.
346+
assert_eq!(TLS, 42);
347+
348+
puts("TLS works!\n\0" as *const str as *const u8);
349+
}
279350
}
280351

281352
// Copied ui/issues/issue-61696.rs

example/std_example.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ fn main() {
1212
let stderr = ::std::io::stderr();
1313
let mut stderr = stderr.lock();
1414

15+
std::thread::spawn(move || {
16+
println!("Hello from another thread!");
17+
});
18+
1519
writeln!(stderr, "some {} text", "<unknown>").unwrap();
1620

1721
let _ = std::process::Command::new("true").env("c", "d").spawn();

patches/0018-Add-FnBox-back.patch

+3-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ index 143cf2f..a6e8faf 100644
9595
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
9696
- pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
9797
+ pub unsafe fn new(stack: usize, p: Box<dyn FnBox()>) -> io::Result<Thread> {
98-
panic!("Warning: Threads are not yet fully supported, because cranelift doesn't support atomics.");
99-
98+
- panic!("Warning: Threads are not yet fully supported, because cranelift doesn't support atomics.");
99+
+ println!("Spawned thread");
100+
100101
let p = box p;
101102
let mut native: libc::pthread_t = mem::zeroed();
102103
diff --git a/src/libstd/sys_common/at_exit_imp.rs b/src/libstd/sys_common/at_exit_imp.rs

prepare.sh

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ git clone https://github.com/rust-lang/regex.git || echo "rust-lang/regex has al
99
pushd regex
1010
git checkout -- .
1111
git checkout 341f207c1071f7290e3f228c710817c280c8dca1
12-
git apply ../crate_patches/regex.patch
1312
popd
1413

1514
git clone https://github.com/ebobby/simple-raytracer || echo "ebobby/simple-raytracer has already been cloned"

src/atomic_shim.rs

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn init_global_lock(module: &mut Module<impl Backend>, bcx: &mut FunctionBui
2222
"__cg_clif_global_atomic_mutex",
2323
Linkage::Export,
2424
true,
25+
false,
2526
Some(16),
2627
).unwrap();
2728
module.define_data(atomic_mutex, &data_ctx).unwrap();
@@ -50,6 +51,7 @@ pub fn lock_global_lock(fx: &mut FunctionCx<'_, '_, impl Backend>) {
5051
"__cg_clif_global_atomic_mutex",
5152
Linkage::Import,
5253
true,
54+
false,
5355
None,
5456
).unwrap();
5557

@@ -74,6 +76,7 @@ pub fn unlock_global_lock(fx: &mut FunctionCx<'_, '_, impl Backend>) {
7476
"__cg_clif_global_atomic_mutex",
7577
Linkage::Import,
7678
true,
79+
false,
7780
None,
7881
).unwrap();
7982

src/constant.rs

+4
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ fn data_id_for_alloc_id<B: Backend>(
247247
&format!("__alloc_{}", alloc_id.0),
248248
Linkage::Local,
249249
false,
250+
false,
250251
Some(align.bytes() as u8),
251252
)
252253
.unwrap()
@@ -273,11 +274,14 @@ fn data_id_for_static(
273274
.pref
274275
.bytes();
275276

277+
let attrs = tcx.codegen_fn_attrs(def_id);
278+
276279
let data_id = module
277280
.declare_data(
278281
&*symbol_name,
279282
linkage,
280283
is_mutable,
284+
attrs.flags.contains(rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags::THREAD_LOCAL),
281285
Some(align.try_into().unwrap()),
282286
)
283287
.unwrap();

0 commit comments

Comments
 (0)