|
1 | 1 | // Adapted from https://github.com/sunfishcode/mir2cranelift/blob/master/rust-examples/nocore-hello-world.rs
|
2 | 2 |
|
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 | +)] |
4 | 7 | #![no_core]
|
5 |
| -#![allow(dead_code)] |
| 8 | +#![allow(dead_code, non_camel_case_types)] |
6 | 9 |
|
7 | 10 | extern crate mini_core;
|
8 | 11 |
|
@@ -276,6 +279,78 @@ fn main() {
|
276 | 279 | extern_nullptr as *const ();
|
277 | 280 | let slice_ptr = &[] as *const [u8];
|
278 | 281 | slice_ptr as *const u8;
|
| 282 | + |
| 283 | + #[cfg(not(jit))] |
| 284 | + test_tls(); |
| 285 | +} |
| 286 | + |
| 287 | +#[repr(C)] |
| 288 | +enum c_void { |
| 289 | + _1, |
| 290 | + _2, |
| 291 | +} |
| 292 | + |
| 293 | +type c_int = i32; |
| 294 | +type c_ulong = u64; |
| 295 | + |
| 296 | +type pthread_t = c_ulong; |
| 297 | + |
| 298 | +#[repr(C)] |
| 299 | +struct pthread_attr_t { |
| 300 | + __size: [u64; 7], |
| 301 | +} |
| 302 | + |
| 303 | +#[link(name = "pthread")] |
| 304 | +extern "C" { |
| 305 | + fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int; |
| 306 | + |
| 307 | + fn pthread_create( |
| 308 | + native: *mut pthread_t, |
| 309 | + attr: *const pthread_attr_t, |
| 310 | + f: extern "C" fn(_: *mut c_void) -> *mut c_void, |
| 311 | + value: *mut c_void |
| 312 | + ) -> c_int; |
| 313 | + |
| 314 | + fn pthread_join( |
| 315 | + native: pthread_t, |
| 316 | + value: *mut *mut c_void |
| 317 | + ) -> c_int; |
| 318 | +} |
| 319 | + |
| 320 | +#[thread_local] |
| 321 | +#[cfg(not(jit))] |
| 322 | +static mut TLS: u8 = 42; |
| 323 | + |
| 324 | +#[cfg(not(jit))] |
| 325 | +extern "C" fn mutate_tls(_: *mut c_void) -> *mut c_void { |
| 326 | + unsafe { TLS = 0; } |
| 327 | + 0 as *mut c_void |
| 328 | +} |
| 329 | + |
| 330 | +#[cfg(not(jit))] |
| 331 | +fn test_tls() { |
| 332 | + unsafe { |
| 333 | + let mut attr: pthread_attr_t = intrinsics::init(); |
| 334 | + let mut thread: pthread_t = 0; |
| 335 | + |
| 336 | + assert_eq!(TLS, 42); |
| 337 | + |
| 338 | + if pthread_attr_init(&mut attr) != 0 { |
| 339 | + assert!(false); |
| 340 | + } |
| 341 | + |
| 342 | + if pthread_create(&mut thread, &attr, mutate_tls, 0 as *mut c_void) != 0 { |
| 343 | + assert!(false); |
| 344 | + } |
| 345 | + |
| 346 | + let mut res = 0 as *mut c_void; |
| 347 | + pthread_join(thread, &mut res); |
| 348 | + |
| 349 | + // TLS of main thread must not have been changed by the other thread. |
| 350 | + assert_eq!(TLS, 42); |
| 351 | + |
| 352 | + puts("TLS works!\n\0" as *const str as *const u8); |
| 353 | + } |
279 | 354 | }
|
280 | 355 |
|
281 | 356 | // Copied ui/issues/issue-61696.rs
|
|
0 commit comments