-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #3739 - joboet:macos_tls_dtors, r=RalfJung
Implement support for multiple TLS destructors on macOS I want to get rid of [this `#[cfg]` block](https://github.com/rust-lang/rust/blob/98dcbae5c9ac615d5acfbf42d42b19a77cb9ec11/library/std/src/thread/mod.rs#L195-L211) in `std`, but it is currently required for miri, as it does not support multiple macOS TLS destructors. This is not true for the platform itself, however, as can be observed in the [implementation](https://github.com/apple-oss-distributions/dyld/blob/d552c40cd1de105f0ec95008e0e0c0972de43456/dyld/DyldRuntimeState.cpp#L2239).
- Loading branch information
Showing
4 changed files
with
87 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//@only-target-darwin | ||
|
||
use std::thread; | ||
|
||
extern "C" { | ||
fn _tlv_atexit(dtor: unsafe extern "C" fn(*mut u8), arg: *mut u8); | ||
} | ||
|
||
fn register<F>(f: F) | ||
where | ||
F: FnOnce() + 'static, | ||
{ | ||
// This will receive the pointer passed into `_tlv_atexit`, which is the | ||
// original `f` but boxed up. | ||
unsafe extern "C" fn run<F>(ptr: *mut u8) | ||
where | ||
F: FnOnce() + 'static, | ||
{ | ||
let f = unsafe { Box::from_raw(ptr as *mut F) }; | ||
f() | ||
} | ||
|
||
unsafe { | ||
_tlv_atexit(run::<F>, Box::into_raw(Box::new(f)) as *mut u8); | ||
} | ||
} | ||
|
||
fn main() { | ||
thread::spawn(|| { | ||
register(|| println!("dtor 2")); | ||
register(|| println!("dtor 1")); | ||
println!("exiting thread"); | ||
}) | ||
.join() | ||
.unwrap(); | ||
|
||
println!("exiting main"); | ||
register(|| println!("dtor 5")); | ||
register(|| { | ||
println!("registering dtor in dtor 3"); | ||
register(|| println!("dtor 4")); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
exiting thread | ||
dtor 1 | ||
dtor 2 | ||
exiting main | ||
registering dtor in dtor 3 | ||
dtor 4 | ||
dtor 5 |