Skip to content

Commit 5b0d874

Browse files
authored
Merge pull request #928 from dhardy/libc
Make libc dependency optional
2 parents e2dc2c3 + ae4683c commit 5b0d874

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1212
### Fixes
1313
- The `Bernoulli` distribution constructors now reports an error on NaN and on
1414
`denominator == 0`. (#925)
15+
- Use `std::sync::Once` to register fork handler, avoiding possible atomicity violation (#928)
16+
17+
### Changes
18+
- Unix: make libc dependency optional; only use fork protection with std feature (#928)
1519

1620
### Additions
1721
- Implement `std::error::Error` for `BernoulliError` (#919)

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ nightly = ["simd_support"] # enables all features requiring nightly rust
2727
serde1 = [] # does nothing, deprecated
2828

2929
# Optional dependencies:
30-
std = ["rand_core/std", "rand_chacha/std", "alloc", "getrandom"]
30+
std = ["rand_core/std", "rand_chacha/std", "alloc", "getrandom", "libc"]
3131
alloc = ["rand_core/alloc"] # enables Vec and Box support (without std)
3232
# re-export optional WASM dependencies to avoid breakage:
3333
# Warning: wasm-bindgen and stdweb features will be removed in rand 0.8;
@@ -68,7 +68,7 @@ features = ["into_bits"]
6868

6969
[target.'cfg(unix)'.dependencies]
7070
# Used for fork protection (reseeding.rs)
71-
libc = { version = "0.2.22", default-features = false }
71+
libc = { version = "0.2.22", optional = true, default-features = false }
7272

7373
# Emscripten does not support 128-bit integers, which are used by ChaCha code.
7474
# We work around this by using a different RNG.

src/rngs/adapter/reseeding.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,10 @@ where
279279
}
280280

281281

282-
#[cfg(all(unix, not(target_os = "emscripten")))]
282+
#[cfg(all(unix, feature = "std", not(target_os = "emscripten")))]
283283
mod fork {
284-
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
285-
#[allow(deprecated)] // Required for compatibility with Rust < 1.24.
286-
use core::sync::atomic::{ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT};
284+
use core::sync::atomic::{AtomicUsize, Ordering};
285+
use std::sync::Once;
287286

288287
// Fork protection
289288
//
@@ -297,31 +296,27 @@ mod fork {
297296
// don't update `fork_counter`, so a reseed is attempted as soon as
298297
// possible.
299298

300-
#[allow(deprecated)]
301-
static RESEEDING_RNG_FORK_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
299+
static RESEEDING_RNG_FORK_COUNTER: AtomicUsize = AtomicUsize::new(0);
302300

303301
pub fn get_fork_counter() -> usize {
304302
RESEEDING_RNG_FORK_COUNTER.load(Ordering::Relaxed)
305303
}
306304

307-
#[allow(deprecated)]
308-
static FORK_HANDLER_REGISTERED: AtomicBool = ATOMIC_BOOL_INIT;
309-
310305
extern "C" fn fork_handler() {
311306
// Note: fetch_add is defined to wrap on overflow
312307
// (which is what we want).
313308
RESEEDING_RNG_FORK_COUNTER.fetch_add(1, Ordering::Relaxed);
314309
}
315310

316311
pub fn register_fork_handler() {
317-
if !FORK_HANDLER_REGISTERED.load(Ordering::Relaxed) {
318-
unsafe { libc::pthread_atfork(None, None, Some(fork_handler)) };
319-
FORK_HANDLER_REGISTERED.store(true, Ordering::Relaxed);
320-
}
312+
static REGISTER: Once = Once::new();
313+
REGISTER.call_once(|| unsafe {
314+
libc::pthread_atfork(None, None, Some(fork_handler));
315+
});
321316
}
322317
}
323318

324-
#[cfg(not(all(unix, not(target_os = "emscripten"))))]
319+
#[cfg(not(all(unix, feature = "std", not(target_os = "emscripten"))))]
325320
mod fork {
326321
pub fn get_fork_counter() -> usize {
327322
0

0 commit comments

Comments
 (0)