about summary refs log tree commit diff
path: root/library/std/src/sys
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-07-21 13:54:22 +0000
committerbors <bors@rust-lang.org>2021-07-21 13:54:22 +0000
commit05f2326c0570fdd64f53532a089bbbc361d190e6 (patch)
tree4b1f69fd54478ba6be8edf9cefdacda297a8ba01 /library/std/src/sys
parentcf932aa584f2ba1a1744f3ef0b21f14387276fb6 (diff)
parent3a8bc0d7da1c675e3c56e9f703eb5fdc20874b2f (diff)
downloadrust-05f2326c0570fdd64f53532a089bbbc361d190e6.tar.gz
rust-05f2326c0570fdd64f53532a089bbbc361d190e6.zip
Auto merge of #87347 - GuillaumeGomez:rollup-ke92xxc, r=GuillaumeGomez
Rollup of 9 pull requests

Successful merges:

 - #87187 (Fix NixOS detection)
 - #87206 (avoid temporary vectors/reuse iterators)
 - #87230 (Fix docblock <table> overflow)
 - #87273 (Recognize bounds on impls as const bounds)
 - #87279 (Add comments explaining the unix command-line argument support.)
 - #87301 (Fix typo in compile.rs)
 - #87311 (Get back the more precise suggestion spans of old regionck)
 - #87321 (Add long explanation for E0722)
 - #87342 (Add long explanation for E0757)

Failed merges:

 - #87270 (Don't display <table> in item summary)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/std/src/sys')
-rw-r--r--library/std/src/sys/unix/args.rs18
1 files changed, 16 insertions, 2 deletions
diff --git a/library/std/src/sys/unix/args.rs b/library/std/src/sys/unix/args.rs
index ad93fa610c4..0bd1ea64577 100644
--- a/library/std/src/sys/unix/args.rs
+++ b/library/std/src/sys/unix/args.rs
@@ -77,10 +77,18 @@ mod imp {
     use crate::ptr;
     use crate::sync::atomic::{AtomicIsize, AtomicPtr, Ordering};
 
+    // The system-provided argc and argv, which we store in static memory
+    // here so that we can defer the work of parsing them until its actually
+    // needed.
+    //
+    // Note that we never mutate argv/argc, the argv array, or the argv
+    // strings, which allows the code in this file to be very simple.
     static ARGC: AtomicIsize = AtomicIsize::new(0);
     static ARGV: AtomicPtr<*const u8> = AtomicPtr::new(ptr::null_mut());
 
     unsafe fn really_init(argc: isize, argv: *const *const u8) {
+        // These don't need to be ordered with each other or other stores,
+        // because they only hold the unmodified system-provide argv/argc.
         ARGC.store(argc, Ordering::Relaxed);
         ARGV.store(argv as *mut _, Ordering::Relaxed);
     }
@@ -122,8 +130,14 @@ mod imp {
 
     fn clone() -> Vec<OsString> {
         unsafe {
-            // Load ARGC and ARGV without a lock. If the store to either ARGV or
-            // ARGC isn't visible yet, we'll return an empty argument list.
+            // Load ARGC and ARGV, which hold the unmodified system-provided
+            // argc/argv, so we can read the pointed-to memory without atomics
+            // or synchronization.
+            //
+            // If either ARGC or ARGV is still zero or null, then either there
+            // really are no arguments, or someone is asking for `args()`
+            // before initialization has completed, and we return an empty
+            // list.
             let argv = ARGV.load(Ordering::Relaxed);
             let argc = if argv.is_null() { 0 } else { ARGC.load(Ordering::Relaxed) };
             (0..argc)