diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2023-05-20 14:16:36 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-20 14:16:36 +0000 |
commit | ae6699940e8fc75008f94312f189892efdcfb2f0 (patch) | |
tree | dcd38426830109db44cad09d48bdfc856fd011f4 /src | |
parent | b13b7d18e0d2f4a8c05e41576c7ebf26d6dbfb28 (diff) | |
parent | 2f60820c3d11e773826705b7204216aebbad5a6c (diff) | |
download | nix-ae6699940e8fc75008f94312f189892efdcfb2f0.zip |
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 <zasample18+github@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/features.rs | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/features.rs b/src/features.rs index 39d17604..2f07a8fc 100644 --- a/src/features.rs +++ b/src/features.rs @@ -6,6 +6,7 @@ mod os { use crate::sys::utsname::uname; use crate::Result; use std::os::unix::ffi::OsStrExt; + use std::sync::atomic::{AtomicUsize, Ordering}; // Features: // * atomic cloexec on socket: 2.6.27 @@ -72,15 +73,15 @@ mod os { } fn kernel_version() -> Result<usize> { - static mut KERNEL_VERS: usize = 0; + static KERNEL_VERS: AtomicUsize = AtomicUsize::new(0); + let mut kernel_vers = KERNEL_VERS.load(Ordering::Relaxed); - unsafe { - if KERNEL_VERS == 0 { - KERNEL_VERS = parse_kernel_version()?; - } - - Ok(KERNEL_VERS) + if kernel_vers == 0 { + kernel_vers = parse_kernel_version()?; + KERNEL_VERS.store(kernel_vers, Ordering::Relaxed); } + + Ok(kernel_vers) } /// Check if the OS supports atomic close-on-exec for sockets |