Skip to content

Commit ae66999

Browse files
bors[bot]zachs18
andauthored
Merge #2029
2029: Remove 'static mut' usage in features::os::kernel_version. r=asomers a=zachs18 Resolves #2028 Note that this is (AFAICT) the first use of `Atomic*` types in `nix` (other than tests). However, this shouldn't be a portability issue, since `nix` is not `#![no_std]`, and (IIUC) [`std` requires](https://doc.rust-lang.org/std/sync/atomic/#portability) at least loads and stores of pointer-sized atomics (i.e. `AtomicUsize`), which is all this PR uses. Co-authored-by: Zachary S <[email protected]>
2 parents b13b7d1 + 2f60820 commit ae66999

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/features.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod os {
66
use crate::sys::utsname::uname;
77
use crate::Result;
88
use std::os::unix::ffi::OsStrExt;
9+
use std::sync::atomic::{AtomicUsize, Ordering};
910

1011
// Features:
1112
// * atomic cloexec on socket: 2.6.27
@@ -72,15 +73,15 @@ mod os {
7273
}
7374

7475
fn kernel_version() -> Result<usize> {
75-
static mut KERNEL_VERS: usize = 0;
76+
static KERNEL_VERS: AtomicUsize = AtomicUsize::new(0);
77+
let mut kernel_vers = KERNEL_VERS.load(Ordering::Relaxed);
7678

77-
unsafe {
78-
if KERNEL_VERS == 0 {
79-
KERNEL_VERS = parse_kernel_version()?;
80-
}
81-
82-
Ok(KERNEL_VERS)
79+
if kernel_vers == 0 {
80+
kernel_vers = parse_kernel_version()?;
81+
KERNEL_VERS.store(kernel_vers, Ordering::Relaxed);
8382
}
83+
84+
Ok(kernel_vers)
8485
}
8586

8687
/// Check if the OS supports atomic close-on-exec for sockets

0 commit comments

Comments
 (0)