From 31bec788f46c73ab14c72868dc6141141320a058 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 8 Aug 2018 18:12:33 +0200 Subject: [PATCH] avoid using the word 'initialized' to talk about that non-reentrant-capable state of the mutex --- src/libstd/io/lazy.rs | 3 +-- src/libstd/sys/unix/args.rs | 2 +- src/libstd/sys/unix/os.rs | 2 +- src/libstd/sys_common/at_exit_imp.rs | 2 +- src/libstd/sys_common/mutex.rs | 4 +++- src/libstd/sys_common/thread_local.rs | 2 +- src/libstd/thread/mod.rs | 2 +- 7 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/libstd/io/lazy.rs b/src/libstd/io/lazy.rs index 5743ea51af3..4fb367fb6ba 100644 --- a/src/libstd/io/lazy.rs +++ b/src/libstd/io/lazy.rs @@ -15,6 +15,7 @@ use sys_common::mutex::Mutex; pub struct Lazy { + // We never call `lock.init()`, so it is UB to attempt to acquire this mutex reentrantly! lock: Mutex, ptr: Cell<*mut Arc>, init: fn() -> Arc, @@ -29,8 +30,6 @@ impl Lazy { /// Safety: `init` must not call `get` on the variable that is being /// initialized. pub const unsafe fn new(init: fn() -> Arc) -> Lazy { - // `lock` is never initialized fully, so it is UB to attempt to - // acquire this mutex reentrantly! Lazy { lock: Mutex::new(), ptr: Cell::new(ptr::null_mut()), diff --git a/src/libstd/sys/unix/args.rs b/src/libstd/sys/unix/args.rs index 220bd11b1f1..c3c033dfbc7 100644 --- a/src/libstd/sys/unix/args.rs +++ b/src/libstd/sys/unix/args.rs @@ -80,7 +80,7 @@ mod imp { static mut ARGC: isize = 0; static mut ARGV: *const *const u8 = ptr::null(); - // `ENV_LOCK` is never initialized fully, so it is UB to attempt to + // We never call `ENV_LOCK.init()`, so it is UB to attempt to // acquire this mutex reentrantly! static LOCK: Mutex = Mutex::new(); diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index 3d98b2efdf1..08c3e154978 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -33,7 +33,7 @@ use vec; const TMPBUF_SZ: usize = 128; -// `ENV_LOCK` is never initialized fully, so it is UB to attempt to +// We never call `ENV_LOCK.init()`, so it is UB to attempt to // acquire this mutex reentrantly! static ENV_LOCK: Mutex = Mutex::new(); diff --git a/src/libstd/sys_common/at_exit_imp.rs b/src/libstd/sys_common/at_exit_imp.rs index 85679837312..76e5df2c865 100644 --- a/src/libstd/sys_common/at_exit_imp.rs +++ b/src/libstd/sys_common/at_exit_imp.rs @@ -23,7 +23,7 @@ // on poisoning and this module needs to operate at a lower level than requiring // the thread infrastructure to be in place (useful on the borders of // initialization/destruction). -// `LOCK` is never initialized fully, so it is UB to attempt to +// We never call `LOCK.init()`, so it is UB to attempt to // acquire this mutex reentrantly! static LOCK: Mutex = Mutex::new(); static mut QUEUE: *mut Queue = ptr::null_mut(); diff --git a/src/libstd/sys_common/mutex.rs b/src/libstd/sys_common/mutex.rs index 74e1defd9f4..c6d531c7a1a 100644 --- a/src/libstd/sys_common/mutex.rs +++ b/src/libstd/sys_common/mutex.rs @@ -32,7 +32,9 @@ pub const fn new() -> Mutex { Mutex(imp::Mutex::new()) } /// Prepare the mutex for use. /// /// This should be called once the mutex is at a stable memory address. - /// Behavior is undefined unless this is called before any other operation. + /// If called, this must be the very first thing that happens to the mutex. + /// Calling it in parallel with or after any operation (including another + /// `init()`) is undefined behavior. #[inline] pub unsafe fn init(&mut self) { self.0.init() } diff --git a/src/libstd/sys_common/thread_local.rs b/src/libstd/sys_common/thread_local.rs index 9db7d732698..bb72cb0930a 100644 --- a/src/libstd/sys_common/thread_local.rs +++ b/src/libstd/sys_common/thread_local.rs @@ -161,7 +161,7 @@ unsafe fn lazy_init(&self) -> usize { // Additionally a 0-index of a tls key hasn't been seen on windows, so // we just simplify the whole branch. if imp::requires_synchronized_create() { - // `INIT_LOCK` is never initialized fully, so it is UB to attempt to + // We never call `INIT_LOCK.init()`, so it is UB to attempt to // acquire this mutex reentrantly! static INIT_LOCK: Mutex = Mutex::new(); let _guard = INIT_LOCK.lock(); diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 0078a05e597..61c6084a250 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -940,7 +940,7 @@ pub fn park_timeout(dur: Duration) { impl ThreadId { // Generate a new unique thread ID. fn new() -> ThreadId { - // `GUARD` is never initialized fully, so it is UB to attempt to + // We never call `GUARD.init()`, so it is UB to attempt to // acquire this mutex reentrantly! static GUARD: mutex::Mutex = mutex::Mutex::new(); static mut COUNTER: u64 = 0; -- GitLab