]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/thread_local_dtor.rs
Merge commit 'f4850f7292efa33759b4f7f9b7621268979e9914' into clippyup
[rust.git] / library / std / src / sys / unix / thread_local_dtor.rs
1 #![cfg(target_thread_local)]
2 #![unstable(feature = "thread_local_internals", issue = "none")]
3
4 //! Provides thread-local destructors without an associated "key", which
5 //! can be more efficient.
6
7 // Since what appears to be glibc 2.18 this symbol has been shipped which
8 // GCC and clang both use to invoke destructors in thread_local globals, so
9 // let's do the same!
10 //
11 // Note, however, that we run on lots older linuxes, as well as cross
12 // compiling from a newer linux to an older linux, so we also have a
13 // fallback implementation to use as well.
14 #[cfg(any(
15     target_os = "linux",
16     target_os = "fuchsia",
17     target_os = "redox",
18     target_os = "emscripten"
19 ))]
20 #[cfg_attr(target_family = "wasm", allow(unused))] // might remain unused depending on target details (e.g. wasm32-unknown-emscripten)
21 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
22     use crate::mem;
23     use crate::sys_common::thread_local_dtor::register_dtor_fallback;
24
25     extern "C" {
26         #[linkage = "extern_weak"]
27         static __dso_handle: *mut u8;
28         #[linkage = "extern_weak"]
29         static __cxa_thread_atexit_impl: *const libc::c_void;
30     }
31     if !__cxa_thread_atexit_impl.is_null() {
32         type F = unsafe extern "C" fn(
33             dtor: unsafe extern "C" fn(*mut u8),
34             arg: *mut u8,
35             dso_handle: *mut u8,
36         ) -> libc::c_int;
37         mem::transmute::<*const libc::c_void, F>(__cxa_thread_atexit_impl)(
38             dtor,
39             t,
40             &__dso_handle as *const _ as *mut _,
41         );
42         return;
43     }
44     register_dtor_fallback(t, dtor);
45 }
46
47 // This implementation is very similar to register_dtor_fallback in
48 // sys_common/thread_local.rs. The main difference is that we want to hook into
49 // macOS's analog of the above linux function, _tlv_atexit. OSX will run the
50 // registered dtors before any TLS slots get freed, and when the main thread
51 // exits.
52 //
53 // Unfortunately, calling _tlv_atexit while tls dtors are running is UB. The
54 // workaround below is to register, via _tlv_atexit, a custom DTOR list once per
55 // thread. thread_local dtors are pushed to the DTOR list without calling
56 // _tlv_atexit.
57 #[cfg(target_os = "macos")]
58 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
59     use crate::cell::Cell;
60     use crate::ptr;
61
62     #[thread_local]
63     static REGISTERED: Cell<bool> = Cell::new(false);
64     if !REGISTERED.get() {
65         _tlv_atexit(run_dtors, ptr::null_mut());
66         REGISTERED.set(true);
67     }
68
69     type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
70
71     #[thread_local]
72     static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
73     if DTORS.get().is_null() {
74         let v: Box<List> = box Vec::new();
75         DTORS.set(Box::into_raw(v));
76     }
77
78     extern "C" {
79         fn _tlv_atexit(dtor: unsafe extern "C" fn(*mut u8), arg: *mut u8);
80     }
81
82     let list: &mut List = &mut *DTORS.get();
83     list.push((t, dtor));
84
85     unsafe extern "C" fn run_dtors(_: *mut u8) {
86         let mut ptr = DTORS.replace(ptr::null_mut());
87         while !ptr.is_null() {
88             let list = Box::from_raw(ptr);
89             for (ptr, dtor) in list.into_iter() {
90                 dtor(ptr);
91             }
92             ptr = DTORS.replace(ptr::null_mut());
93         }
94     }
95 }
96
97 #[cfg(any(target_os = "vxworks", target_os = "horizon"))]
98 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
99     use crate::sys_common::thread_local_dtor::register_dtor_fallback;
100     register_dtor_fallback(t, dtor);
101 }