Skip to content

Commit 5a3bc7f

Browse files
committed
use existing global lock to protect against fork
1 parent 3bc9ca2 commit 5a3bc7f

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ libc = { version = "0.2", default-features = false }
2525
# `src/tools/rustc-std-workspace` folder
2626
core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' }
2727
compiler_builtins = { version = '0.1.0', optional = true }
28-
once_cell = { version = '1.8', default-features= false, optional = true }
2928

3029
[dev-dependencies]
3130
rand = "0.3"
@@ -41,7 +40,4 @@ global = []
4140
# Enable very expensive debug checks in this crate
4241
debug = []
4342

44-
# makes allocations safe for use during `fork(2)`
45-
fork-safe = ["once_cell"]
46-
4743
rustc-dep-of-std = ['core', 'compiler_builtins/rustc-dep-of-std']

src/linux.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,23 @@ unsafe impl Allocator for System {
7575

7676
#[cfg(feature = "global")]
7777
pub 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")]

src/macos.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,23 @@ unsafe impl Allocator for System {
6363

6464
#[cfg(feature = "global")]
6565
pub 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")]

0 commit comments

Comments
 (0)