2016-09-30 20:13:58 +00:00
|
|
|
#![cfg(target_thread_local)]
|
2019-12-21 13:16:18 +02:00
|
|
|
#![unstable(feature = "thread_local_internals", issue = "none")]
|
2016-09-30 20:13:58 +00:00
|
|
|
|
2020-07-12 11:37:11 +02:00
|
|
|
//! Provides thread-local destructors without an associated "key", which
|
|
|
|
//! can be more efficient.
|
|
|
|
|
2016-09-30 20:13:58 +00:00
|
|
|
// Since what appears to be glibc 2.18 this symbol has been shipped which
|
|
|
|
// GCC and clang both use to invoke destructors in thread_local globals, so
|
|
|
|
// let's do the same!
|
|
|
|
//
|
|
|
|
// Note, however, that we run on lots older linuxes, as well as cross
|
|
|
|
// compiling from a newer linux to an older linux, so we also have a
|
|
|
|
// fallback implementation to use as well.
|
2019-11-27 10:28:39 -08:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "redox",
|
|
|
|
target_os = "emscripten"
|
|
|
|
))]
|
|
|
|
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::mem;
|
2020-07-12 11:37:11 +02:00
|
|
|
use crate::sys_common::thread_local_dtor::register_dtor_fallback;
|
2016-09-30 20:13:58 +00:00
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
extern "C" {
|
2016-09-30 20:13:58 +00:00
|
|
|
#[linkage = "extern_weak"]
|
|
|
|
static __dso_handle: *mut u8;
|
|
|
|
#[linkage = "extern_weak"]
|
|
|
|
static __cxa_thread_atexit_impl: *const libc::c_void;
|
|
|
|
}
|
|
|
|
if !__cxa_thread_atexit_impl.is_null() {
|
2019-11-27 10:28:39 -08:00
|
|
|
type F = unsafe extern "C" fn(
|
|
|
|
dtor: unsafe extern "C" fn(*mut u8),
|
|
|
|
arg: *mut u8,
|
|
|
|
dso_handle: *mut u8,
|
|
|
|
) -> libc::c_int;
|
|
|
|
mem::transmute::<*const libc::c_void, F>(__cxa_thread_atexit_impl)(
|
|
|
|
dtor,
|
|
|
|
t,
|
|
|
|
&__dso_handle as *const _ as *mut _,
|
|
|
|
);
|
|
|
|
return;
|
2016-09-30 20:13:58 +00:00
|
|
|
}
|
|
|
|
register_dtor_fallback(t, dtor);
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:09:06 -08:00
|
|
|
// This implementation is very similar to register_dtor_fallback in
|
|
|
|
// sys_common/thread_local.rs. The main difference is that we want to hook into
|
|
|
|
// macOS's analog of the above linux function, _tlv_atexit. OSX will run the
|
|
|
|
// registered dtors before any TLS slots get freed, and when the main thread
|
|
|
|
// exits.
|
|
|
|
//
|
|
|
|
// Unfortunately, calling _tlv_atexit while tls dtors are running is UB. The
|
|
|
|
// workaround below is to register, via _tlv_atexit, a custom DTOR list once per
|
|
|
|
// thread. thread_local dtors are pushed to the DTOR list without calling
|
|
|
|
// _tlv_atexit.
|
2016-09-30 20:13:58 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
2019-11-27 10:28:39 -08:00
|
|
|
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::cell::Cell;
|
|
|
|
use crate::ptr;
|
2019-01-15 20:09:06 -08:00
|
|
|
|
|
|
|
#[thread_local]
|
|
|
|
static REGISTERED: Cell<bool> = Cell::new(false);
|
|
|
|
if !REGISTERED.get() {
|
|
|
|
_tlv_atexit(run_dtors, ptr::null_mut());
|
|
|
|
REGISTERED.set(true);
|
|
|
|
}
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
|
2019-01-15 20:09:06 -08:00
|
|
|
|
|
|
|
#[thread_local]
|
|
|
|
static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
|
|
|
|
if DTORS.get().is_null() {
|
|
|
|
let v: Box<List> = box Vec::new();
|
|
|
|
DTORS.set(Box::into_raw(v));
|
|
|
|
}
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
extern "C" {
|
|
|
|
fn _tlv_atexit(dtor: unsafe extern "C" fn(*mut u8), arg: *mut u8);
|
2016-09-30 20:13:58 +00:00
|
|
|
}
|
2019-01-15 20:09:06 -08:00
|
|
|
|
|
|
|
let list: &mut List = &mut *DTORS.get();
|
|
|
|
list.push((t, dtor));
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
unsafe extern "C" fn run_dtors(_: *mut u8) {
|
2019-01-15 20:09:06 -08:00
|
|
|
let mut ptr = DTORS.replace(ptr::null_mut());
|
|
|
|
while !ptr.is_null() {
|
|
|
|
let list = Box::from_raw(ptr);
|
|
|
|
for (ptr, dtor) in list.into_iter() {
|
|
|
|
dtor(ptr);
|
|
|
|
}
|
|
|
|
ptr = DTORS.replace(ptr::null_mut());
|
|
|
|
}
|
|
|
|
}
|
2016-09-30 20:13:58 +00:00
|
|
|
}
|