From 617b61cd3af040a25f8a27e55b003705cf808f73 Mon Sep 17 00:00:00 2001 From: Peter Ammon Date: Sun, 22 Sep 2024 14:02:35 -0700 Subject: [PATCH] Clean up fd_monitor getter No need for UnsafeCell --- src/io.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/io.rs b/src/io.rs index 91566bb41..45d422346 100644 --- a/src/io.rs +++ b/src/io.rs @@ -20,9 +20,10 @@ use libc::{EAGAIN, EINTR, ENOENT, ENOTDIR, EPIPE, EWOULDBLOCK, STDOUT_FILENO}; use nix::fcntl::OFlag; use nix::sys::stat::Mode; +use once_cell::sync::Lazy; #[cfg(not(target_has_atomic = "64"))] use portable_atomic::AtomicU64; -use std::cell::{RefCell, UnsafeCell}; +use std::cell::RefCell; use std::fs::File; use std::io; use std::os::fd::{AsRawFd, IntoRawFd, OwnedFd, RawFd}; @@ -1033,14 +1034,8 @@ pub fn out_is_terminal(&self) -> bool { const OPEN_MASK: Mode = Mode::from_bits_truncate(0o666); /// Provide the fd monitor used for background fillthread operations. -fn fd_monitor() -> &'static mut FdMonitor { - // Deliberately leaked to avoid shutdown dtors. - static mut FDM: *const UnsafeCell = std::ptr::null(); - unsafe { - if FDM.is_null() { - FDM = Box::into_raw(Box::new(UnsafeCell::new(FdMonitor::new()))) - } - } - let ptr: *mut FdMonitor = unsafe { (*FDM).get() }; - unsafe { &mut *ptr } +static FD_MONITOR: Lazy = Lazy::new(FdMonitor::new); + +pub fn fd_monitor() -> &'static FdMonitor { + &FD_MONITOR }