Skip to content

Commit c6e7a1c

Browse files
committed
Get rid of a warning
While the original code followed similar safety reasoning as the new one, the old one generated a deny-by-default (in new editions) warning.
1 parent 8f80afb commit c6e7a1c

File tree

1 file changed

+16
-8
lines changed
  • signal-hook-registry/src

1 file changed

+16
-8
lines changed

signal-hook-registry/src/lib.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ use std::io::Error;
7272
use std::mem;
7373
#[cfg(not(windows))]
7474
use std::ptr;
75+
use std::sync::atomic::{AtomicPtr, Ordering};
7576
// Once::new is now a const-fn. But it is not stable in all the rustc versions we want to support
7677
// yet.
7778
#[allow(deprecated)]
@@ -171,20 +172,20 @@ impl Slot {
171172
// There doesn't seem to be a way to make Cargo force the dependency for only one target
172173
// (it doesn't compile the ones it doesn't need, but it stills considers the other targets
173174
// for version resolution).
174-
//
175+
//
175176
// Therefore, we let the user have freedom - if they want AIX, they can upgrade to new
176177
// enough libc. If they want ancient rustc, they can force older versions of libc.
177178
//
178179
// See #169.
179180

180181
new.sa_sigaction = handler as usize; // If it doesn't compile on AIX, upgrade the libc dependency
181-
182+
182183
// Android is broken and uses different int types than the rest (and different depending on
183184
// the pointer width). This converts the flags to the proper type no matter what it is on
184185
// the given platform.
185186
#[cfg(target_os = "nto")]
186187
let flags = 0;
187-
// SA_RESTART is supported by qnx https://www.qnx.com/support/knowledgebase.html?id=50130000000SmiD
188+
// SA_RESTART is supported by qnx https://www.qnx.com/support/knowledgebase.html?id=50130000000SmiD
188189
#[cfg(not(target_os = "nto"))]
189190
let flags = libc::SA_RESTART;
190191
#[allow(unused_assignments)]
@@ -296,23 +297,30 @@ struct GlobalData {
296297
race_fallback: HalfLock<Option<Prev>>,
297298
}
298299

299-
static mut GLOBAL_DATA: Option<GlobalData> = None;
300+
static GLOBAL_DATA: AtomicPtr<GlobalData> = AtomicPtr::new(ptr::null_mut());
300301
#[allow(deprecated)]
301302
static GLOBAL_INIT: Once = ONCE_INIT;
302303

303304
impl GlobalData {
304305
fn get() -> &'static Self {
305-
unsafe { GLOBAL_DATA.as_ref().unwrap() }
306+
let data = GLOBAL_DATA.load(Ordering::Acquire);
307+
// # Safety
308+
//
309+
// * The data actually does live forever - created by Box::into_raw.
310+
// * It is _never_ modified (apart for interior mutability, but that one is fine).
311+
unsafe { data.as_ref().expect("We shall be set up already") }
306312
}
307313
fn ensure() -> &'static Self {
308-
GLOBAL_INIT.call_once(|| unsafe {
309-
GLOBAL_DATA = Some(GlobalData {
314+
GLOBAL_INIT.call_once(|| {
315+
let data = Box::into_raw(Box::new(GlobalData {
310316
data: HalfLock::new(SignalData {
311317
signals: HashMap::new(),
312318
next_id: 1,
313319
}),
314320
race_fallback: HalfLock::new(None),
315-
});
321+
}));
322+
let old = GLOBAL_DATA.swap(data, Ordering::Release);
323+
assert!(old.is_null());
316324
});
317325
Self::get()
318326
}

0 commit comments

Comments
 (0)