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
16
18
// FIXME: The Rust compiler currently omits weakly function definitions (i.e.,
17
19
// __cxa_thread_atexit_impl) and its metadata from LLVM IR.
18
20
#[ no_sanitize( cfi, kcfi) ]
19
- pub unsafe fn register_dtor ( t : * mut u8 , dtor : unsafe extern "C" fn ( * mut u8 ) ) {
21
+ pub fn activate ( ) {
22
+ use crate :: cell:: Cell ;
20
23
use crate :: mem;
21
- use crate :: sys_common:: thread_local_dtor :: register_dtor_fallback ;
24
+ use crate :: sys_common:: thread_local_key :: StaticKey ;
22
25
23
26
/// This is necessary because the __cxa_thread_atexit_impl implementation
24
27
/// std links to by default may be a C or C++ implementation that was not
@@ -43,64 +46,47 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
43
46
> ;
44
47
}
45
48
46
- if let Some ( f) = __cxa_thread_atexit_impl {
47
- unsafe {
48
- f (
49
- mem:: transmute :: <
50
- unsafe extern "C" fn ( * mut u8 ) ,
51
- unsafe extern "C" fn ( * mut libc:: c_void ) ,
52
- > ( dtor) ,
53
- t. cast ( ) ,
54
- & __dso_handle as * const _ as * mut _ ,
55
- ) ;
49
+ unsafe {
50
+ if let Some ( atexit) = __cxa_thread_atexit_impl {
51
+ #[ thread_local]
52
+ static REGISTERED : Cell < bool > = Cell :: new ( false ) ;
53
+ if !REGISTERED . get ( ) {
54
+ atexit (
55
+ mem:: transmute :: <
56
+ unsafe extern "C" fn ( * mut u8 ) ,
57
+ unsafe extern "C" fn ( * mut libc:: c_void ) ,
58
+ > ( run_dtors) ,
59
+ ptr:: null_mut ( ) ,
60
+ & __dso_handle as * const _ as * mut _ ,
61
+ ) ;
62
+ REGISTERED . set ( true ) ;
63
+ }
64
+ } else {
65
+ static KEY : StaticKey = StaticKey :: new ( Some ( run_dtors) ) ;
66
+
67
+ KEY . set ( ptr:: invalid_mut ( 1 ) ) ;
56
68
}
57
- return ;
58
69
}
59
- register_dtor_fallback ( t, dtor) ;
60
70
}
61
71
62
- // This implementation is very similar to register_dtor_fallback in
63
- // sys_common/thread_local.rs. The main difference is that we want to hook into
64
- // macOS's analog of the above linux function, _tlv_atexit. OSX will run the
65
- // registered dtors before any TLS slots get freed, and when the main thread
72
+ // We hook into macOS's analog of the above linux function, _tlv_atexit. OSX
73
+ // will run `run_dtors` before any TLS slots get freed, and when the main thread
66
74
// exits.
67
- //
68
- // Unfortunately, calling _tlv_atexit while tls dtors are running is UB. The
69
- // workaround below is to register, via _tlv_atexit, a custom DTOR list once per
70
- // thread. thread_local dtors are pushed to the DTOR list without calling
71
- // _tlv_atexit.
72
75
#[ cfg( any( target_os = "macos" , target_os = "ios" , target_os = "watchos" , target_os = "tvos" ) ) ]
73
- pub unsafe fn register_dtor ( t : * mut u8 , dtor : unsafe extern "C" fn ( * mut u8 ) ) {
74
- use crate :: cell:: { Cell , RefCell } ;
75
- use crate :: ptr;
76
-
77
- #[ thread_local]
78
- static REGISTERED : Cell < bool > = Cell :: new ( false ) ;
79
-
80
- #[ thread_local]
81
- static DTORS : RefCell < Vec < ( * mut u8 , unsafe extern "C" fn ( * mut u8 ) ) > > = RefCell :: new ( Vec :: new ( ) ) ;
82
-
83
- if !REGISTERED . get ( ) {
84
- _tlv_atexit ( run_dtors, ptr:: null_mut ( ) ) ;
85
- REGISTERED . set ( true ) ;
86
- }
76
+ pub fn activate ( ) {
77
+ use crate :: cell:: Cell ;
87
78
88
79
extern "C" {
89
80
fn _tlv_atexit ( dtor : unsafe extern "C" fn ( * mut u8 ) , arg : * mut u8 ) ;
90
81
}
91
82
92
- match DTORS . try_borrow_mut ( ) {
93
- Ok ( mut dtors) => dtors. push ( ( t, dtor) ) ,
94
- Err ( _) => rtabort ! ( "global allocator may not use TLS" ) ,
95
- }
83
+ #[ thread_local]
84
+ static REGISTERED : Cell < bool > = Cell :: new ( false ) ;
96
85
97
- unsafe extern "C" fn run_dtors ( _: * mut u8 ) {
98
- let mut list = DTORS . take ( ) ;
99
- while !list. is_empty ( ) {
100
- for ( ptr, dtor) in list {
101
- dtor ( ptr) ;
102
- }
103
- list = DTORS . take ( ) ;
86
+ if !REGISTERED . get ( ) {
87
+ unsafe {
88
+ _tlv_atexit ( run_dtors, ptr:: null_mut ( ) ) ;
89
+ REGISTERED . set ( true ) ;
104
90
}
105
91
}
106
92
}
@@ -112,7 +98,12 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
112
98
target_os = "aix"
113
99
) ) ]
114
100
#[ cfg_attr( target_family = "wasm" , allow( unused) ) ] // might remain unused depending on target details (e.g. wasm32-unknown-emscripten)
115
- pub unsafe fn register_dtor ( t : * mut u8 , dtor : unsafe extern "C" fn ( * mut u8 ) ) {
116
- use crate :: sys_common:: thread_local_dtor:: register_dtor_fallback;
117
- register_dtor_fallback ( t, dtor) ;
101
+ pub fn activate ( ) {
102
+ use crate :: sys_common:: thread_local_key:: StaticKey ;
103
+
104
+ static KEY : StaticKey = StaticKey :: new ( Some ( run_dtors) ) ;
105
+
106
+ unsafe {
107
+ KEY . set ( ptr:: invalid_mut ( 1 ) ) ;
108
+ }
118
109
}
0 commit comments