Skip to content

Commit 9bb11ba

Browse files
committed
Remove an unnecessary Mutex around argument initialization.
In the command-line argument initialization code, remove the Mutex around the `ARGV` and `ARGC` variables, and simply check whether ARGV is non-null before dereferencing it. This way, if either of ARGV or ARGC is not initialized, we'll get an empty argument list. This allows simple cdylibs to avoid having `pthread_mutex_lock`/`pthread_mutex_unlock` appear in their symbol tables if they don't otherwise use threads.
1 parent 46010c4 commit 9bb11ba

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

library/std/src/sys/unix/args.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,10 @@ mod imp {
7777
use crate::ptr;
7878
use crate::sync::atomic::{AtomicIsize, AtomicPtr, Ordering};
7979

80-
use crate::sys_common::mutex::StaticMutex;
81-
8280
static ARGC: AtomicIsize = AtomicIsize::new(0);
8381
static ARGV: AtomicPtr<*const u8> = AtomicPtr::new(ptr::null_mut());
84-
// We never call `ENV_LOCK.init()`, so it is UB to attempt to
85-
// acquire this mutex reentrantly!
86-
static LOCK: StaticMutex = StaticMutex::new();
8782

8883
unsafe fn really_init(argc: isize, argv: *const *const u8) {
89-
let _guard = LOCK.lock();
9084
ARGC.store(argc, Ordering::Relaxed);
9185
ARGV.store(argv as *mut _, Ordering::Relaxed);
9286
}
@@ -128,9 +122,14 @@ mod imp {
128122

129123
fn clone() -> Vec<OsString> {
130124
unsafe {
131-
let _guard = LOCK.lock();
132-
let argc = ARGC.load(Ordering::Relaxed);
125+
// Load ARGC and ARGV without a lock. If the store to either ARGV or
126+
// ARGC isn't visible yet, we'll return an empty argument list.
133127
let argv = ARGV.load(Ordering::Relaxed);
128+
let argc = if argv.is_null() {
129+
0
130+
} else {
131+
ARGC.load(Ordering::Relaxed)
132+
};
134133
(0..argc)
135134
.map(|i| {
136135
let cstr = CStr::from_ptr(*argv.offset(i) as *const libc::c_char);

0 commit comments

Comments
 (0)