1
+ //! Ensures that thread-local destructors are run on thread exit.
2
+
1
3
#![ cfg( target_thread_local) ]
2
4
#![ unstable( feature = "thread_local_internals" , issue = "none" ) ]
3
5
4
- //! Provides thread-local destructors without an associated "key", which
5
- //! can be more efficient.
6
+ use crate :: ptr ;
7
+ use crate :: sys :: common :: thread_local :: run_dtors ;
6
8
7
9
// Since what appears to be glibc 2.18 this symbol has been shipped which
8
10
// GCC and clang both use to invoke destructors in thread_local globals, so
25
27
// FIXME: The Rust compiler currently omits weakly function definitions (i.e.,
26
28
// __cxa_thread_atexit_impl) and its metadata from LLVM IR.
27
29
#[ no_sanitize( cfi, kcfi) ]
28
- pub unsafe fn register_dtor ( t : * mut u8 , dtor : unsafe extern "C" fn ( * mut u8 ) ) {
30
+ pub fn activate ( ) {
31
+ use crate :: cell:: Cell ;
29
32
use crate :: mem;
30
- use crate :: sys_common:: thread_local_dtor :: register_dtor_fallback ;
33
+ use crate :: sys_common:: thread_local_key :: StaticKey ;
31
34
32
35
/// This is necessary because the __cxa_thread_atexit_impl implementation
33
36
/// std links to by default may be a C or C++ implementation that was not
@@ -52,64 +55,47 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
52
55
> ;
53
56
}
54
57
55
- if let Some ( f) = __cxa_thread_atexit_impl {
56
- unsafe {
57
- f (
58
- mem:: transmute :: <
59
- unsafe extern "C" fn ( * mut u8 ) ,
60
- unsafe extern "C" fn ( * mut libc:: c_void ) ,
61
- > ( dtor) ,
62
- t. cast ( ) ,
63
- & __dso_handle as * const _ as * mut _ ,
64
- ) ;
58
+ unsafe {
59
+ if let Some ( atexit) = __cxa_thread_atexit_impl {
60
+ #[ thread_local]
61
+ static REGISTERED : Cell < bool > = Cell :: new ( false ) ;
62
+ if !REGISTERED . get ( ) {
63
+ atexit (
64
+ mem:: transmute :: <
65
+ unsafe extern "C" fn ( * mut u8 ) ,
66
+ unsafe extern "C" fn ( * mut libc:: c_void ) ,
67
+ > ( run_dtors) ,
68
+ ptr:: null_mut ( ) ,
69
+ & __dso_handle as * const _ as * mut _ ,
70
+ ) ;
71
+ REGISTERED . set ( true ) ;
72
+ }
73
+ } else {
74
+ static KEY : StaticKey = StaticKey :: new ( Some ( run_dtors) ) ;
75
+
76
+ KEY . set ( ptr:: invalid_mut ( 1 ) ) ;
65
77
}
66
- return ;
67
78
}
68
- register_dtor_fallback ( t, dtor) ;
69
79
}
70
80
71
- // This implementation is very similar to register_dtor_fallback in
72
- // sys_common/thread_local.rs. The main difference is that we want to hook into
73
- // macOS's analog of the above linux function, _tlv_atexit. OSX will run the
74
- // registered dtors before any TLS slots get freed, and when the main thread
81
+ // We hook into macOS's analog of the above linux function, _tlv_atexit. OSX
82
+ // will run `run_dtors` before any TLS slots get freed, and when the main thread
75
83
// exits.
76
- //
77
- // Unfortunately, calling _tlv_atexit while tls dtors are running is UB. The
78
- // workaround below is to register, via _tlv_atexit, a custom DTOR list once per
79
- // thread. thread_local dtors are pushed to the DTOR list without calling
80
- // _tlv_atexit.
81
84
#[ cfg( any( target_os = "macos" , target_os = "ios" , target_os = "watchos" , target_os = "tvos" ) ) ]
82
- pub unsafe fn register_dtor ( t : * mut u8 , dtor : unsafe extern "C" fn ( * mut u8 ) ) {
83
- use crate :: cell:: { Cell , RefCell } ;
84
- use crate :: ptr;
85
-
86
- #[ thread_local]
87
- static REGISTERED : Cell < bool > = Cell :: new ( false ) ;
88
-
89
- #[ thread_local]
90
- static DTORS : RefCell < Vec < ( * mut u8 , unsafe extern "C" fn ( * mut u8 ) ) > > = RefCell :: new ( Vec :: new ( ) ) ;
91
-
92
- if !REGISTERED . get ( ) {
93
- _tlv_atexit ( run_dtors, ptr:: null_mut ( ) ) ;
94
- REGISTERED . set ( true ) ;
95
- }
85
+ pub fn activate ( ) {
86
+ use crate :: cell:: Cell ;
96
87
97
88
extern "C" {
98
89
fn _tlv_atexit ( dtor : unsafe extern "C" fn ( * mut u8 ) , arg : * mut u8 ) ;
99
90
}
100
91
101
- match DTORS . try_borrow_mut ( ) {
102
- Ok ( mut dtors) => dtors. push ( ( t, dtor) ) ,
103
- Err ( _) => rtabort ! ( "global allocator may not use TLS" ) ,
104
- }
92
+ #[ thread_local]
93
+ static REGISTERED : Cell < bool > = Cell :: new ( false ) ;
105
94
106
- unsafe extern "C" fn run_dtors ( _: * mut u8 ) {
107
- let mut list = DTORS . take ( ) ;
108
- while !list. is_empty ( ) {
109
- for ( ptr, dtor) in list {
110
- dtor ( ptr) ;
111
- }
112
- list = DTORS . take ( ) ;
95
+ if !REGISTERED . get ( ) {
96
+ unsafe {
97
+ _tlv_atexit ( run_dtors, ptr:: null_mut ( ) ) ;
98
+ REGISTERED . set ( true ) ;
113
99
}
114
100
}
115
101
}
@@ -121,7 +107,12 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
121
107
target_os = "aix"
122
108
) ) ]
123
109
#[ cfg_attr( target_family = "wasm" , allow( unused) ) ] // might remain unused depending on target details (e.g. wasm32-unknown-emscripten)
124
- pub unsafe fn register_dtor ( t : * mut u8 , dtor : unsafe extern "C" fn ( * mut u8 ) ) {
125
- use crate :: sys_common:: thread_local_dtor:: register_dtor_fallback;
126
- register_dtor_fallback ( t, dtor) ;
110
+ pub fn activate ( ) {
111
+ use crate :: sys_common:: thread_local_key:: StaticKey ;
112
+
113
+ static KEY : StaticKey = StaticKey :: new ( Some ( run_dtors) ) ;
114
+
115
+ unsafe {
116
+ KEY . set ( ptr:: invalid_mut ( 1 ) ) ;
117
+ }
127
118
}
0 commit comments