3232use crate :: thread;
3333#[ cfg( feature = "alloc" ) ]
3434use alloc:: boxed:: Box ;
35+ #[ cfg( all( feature = "alloc" , not( feature = "thread" ) ) ) ]
36+ use core:: cell:: UnsafeCell ;
3537use linux_raw_sys:: ctypes:: c_int;
3638#[ cfg( all( feature = "alloc" , feature = "thread" ) ) ]
3739use rustix_futex_sync:: Mutex ;
@@ -231,7 +233,7 @@ static DTORS: Mutex<smallvec::SmallVec<[Box<dyn FnOnce() + Send>; 32]>> =
231233
232234/// A type for `DTORS` in the single-threaded case that we can mark as `Sync`.
233235#[ cfg( all( feature = "alloc" , not( feature = "thread" ) ) ) ]
234- struct Dtors ( smallvec:: SmallVec < [ Box < dyn FnOnce ( ) + Send > ; 32 ] > ) ;
236+ struct Dtors ( UnsafeCell < smallvec:: SmallVec < [ Box < dyn FnOnce ( ) + Send > ; 32 ] > > ) ;
235237
236238/// SAFETY: With `feature = "take-charge"`, we can assume that Origin is
237239/// responsible for creating all threads in the program, and with
@@ -242,7 +244,7 @@ unsafe impl Sync for Dtors {}
242244
243245/// The single-threaded version of `DTORS`.
244246#[ cfg( all( feature = "alloc" , not( feature = "thread" ) ) ) ]
245- static mut DTORS : Dtors = Dtors ( smallvec:: SmallVec :: new_const ( ) ) ;
247+ static DTORS : Dtors = Dtors ( UnsafeCell :: new ( smallvec:: SmallVec :: new_const ( ) ) ) ;
246248
247249/// Register a function to be called when [`exit`] is called.
248250#[ cfg( feature = "alloc" ) ]
@@ -252,7 +254,7 @@ pub fn at_exit(func: Box<dyn FnOnce() + Send>) {
252254 let mut dtors = DTORS . lock ( ) ;
253255 // SAFETY: See the safety comments on the `unsafe impl Sync for Dtors`.
254256 #[ cfg( not( feature = "thread" ) ) ]
255- let dtors = unsafe { & mut DTORS . 0 } ;
257+ let dtors = unsafe { & mut * DTORS . 0 . get ( ) } ;
256258
257259 dtors. push ( func) ;
258260}
@@ -273,7 +275,7 @@ pub fn exit(status: c_int) -> ! {
273275 let mut dtors = DTORS . lock ( ) ;
274276 // SAFETY: See the safety comments on the `unsafe impl Sync for Dtors`.
275277 #[ cfg( not( feature = "thread" ) ) ]
276- let dtors = unsafe { & mut DTORS . 0 } ;
278+ let dtors = unsafe { & mut * DTORS . 0 . get ( ) } ;
277279
278280 if let Some ( func) = dtors. pop ( ) {
279281 #[ cfg( feature = "log" ) ]
0 commit comments