File tree Expand file tree Collapse file tree 3 files changed +20
-26
lines changed
Expand file tree Collapse file tree 3 files changed +20
-26
lines changed Original file line number Diff line number Diff line change @@ -25,7 +25,6 @@ libc = { version = "0.2", default-features = false }
2525# `src/tools/rustc-std-workspace` folder
2626core = { version = ' 1.0.0' , optional = true , package = ' rustc-std-workspace-core' }
2727compiler_builtins = { version = ' 0.1.0' , optional = true }
28- once_cell = { version = ' 1.8' , default-features = false , optional = true }
2928
3029[dev-dependencies ]
3130rand = " 0.3"
@@ -41,7 +40,4 @@ global = []
4140# Enable very expensive debug checks in this crate
4241debug = []
4342
44- # makes allocations safe for use during `fork(2)`
45- fork-safe = [" once_cell" ]
46-
4743rustc-dep-of-std = [' core' , ' compiler_builtins/rustc-dep-of-std' ]
Original file line number Diff line number Diff line change @@ -75,24 +75,23 @@ unsafe impl Allocator for System {
7575
7676#[ cfg( feature = "global" ) ]
7777pub fn acquire_global_lock ( ) {
78- fn _acquire_global_lock ( ) {
79- unsafe { assert_eq ! ( libc:: pthread_mutex_lock( & mut LOCK ) , 0 ) }
80- }
78+ static mut FORK_PROTECTED : bool = false ;
8179
82- #[ cfg( feature = "fork-safe" ) ]
8380 unsafe {
84- static EXEC_ONCE : once_cell:: sync:: OnceCell ( ) ;
85-
86- EXEC_ONCE . get_or_init ( || {
81+ assert_eq ! ( libc:: pthread_mutex_lock( & mut LOCK ) , 0 ) ;
82+ if !FORK_PROTECTED {
83+ // ensures that if a process forks,
84+ // it will acquire the lock before any other thread,
85+ // protecting it from deadlock,
86+ // when the child is created with only that thread.
8787 libc:: pthread_atfork (
8888 _acquire_global_lock,
8989 release_global_lock,
9090 release_global_lock,
91- )
92- } ) ;
91+ ) ;
92+ FORK_PROTECTED = true ;
93+ }
9394 }
94-
95- _acquire_global_lock ( ) ;
9695}
9796
9897#[ cfg( feature = "global" ) ]
Original file line number Diff line number Diff line change @@ -63,24 +63,23 @@ unsafe impl Allocator for System {
6363
6464#[ cfg( feature = "global" ) ]
6565pub fn acquire_global_lock ( ) {
66- fn _acquire_global_lock ( ) {
67- unsafe { assert_eq ! ( libc:: pthread_mutex_lock( & mut LOCK ) , 0 ) }
68- }
66+ static mut FORK_PROTECTED : bool = false ;
6967
70- #[ cfg( feature = "fork-safe" ) ]
7168 unsafe {
72- static EXEC_ONCE : once_cell:: sync:: OnceCell ( ) ;
73-
74- EXEC_ONCE . get_or_init ( || {
69+ assert_eq ! ( libc:: pthread_mutex_lock( & mut LOCK ) , 0 ) ;
70+ if !FORK_PROTECTED {
71+ // ensures that if a process forks,
72+ // it will acquire the lock before any other thread,
73+ // protecting it from deadlock,
74+ // when the child is created with only that thread.
7575 libc:: pthread_atfork (
7676 _acquire_global_lock,
7777 release_global_lock,
7878 release_global_lock,
79- )
80- } ) ;
79+ ) ;
80+ FORK_PROTECTED = true ;
81+ }
8182 }
82-
83- _acquire_global_lock ( ) ;
8483}
8584
8685#[ cfg( feature = "global" ) ]
You can’t perform that action at this time.
0 commit comments