|
1 | | -use crate::ffi::OsString; |
| 1 | +use crate::ffi::{c_char, CStr, OsString}; |
2 | 2 | use crate::fmt; |
| 3 | +use crate::os::unix::ffi::OsStringExt; |
| 4 | +use crate::ptr; |
| 5 | +use crate::sync::atomic::{ |
| 6 | + AtomicIsize, AtomicPtr, |
| 7 | + Ordering::{Acquire, Relaxed, Release}, |
| 8 | +}; |
3 | 9 | use crate::vec; |
4 | 10 |
|
| 11 | +static ARGC: AtomicIsize = AtomicIsize::new(0); |
| 12 | +static ARGV: AtomicPtr<*const u8> = AtomicPtr::new(ptr::null_mut()); |
| 13 | + |
5 | 14 | /// One-time global initialization. |
6 | 15 | pub unsafe fn init(argc: isize, argv: *const *const u8) { |
7 | | - imp::init(argc, argv) |
8 | | -} |
9 | | - |
10 | | -/// One-time global cleanup. |
11 | | -pub unsafe fn cleanup() { |
12 | | - imp::cleanup() |
| 16 | + ARGC.store(argc, Relaxed); |
| 17 | + // Use release ordering here to broadcast writes by the OS. |
| 18 | + ARGV.store(argv as *mut *const u8, Release); |
13 | 19 | } |
14 | 20 |
|
15 | 21 | /// Returns the command line arguments |
16 | 22 | pub fn args() -> Args { |
17 | | - imp::args() |
| 23 | + // Synchronize with the store above. |
| 24 | + let argv = ARGV.load(Acquire); |
| 25 | + // If argv has not been initialized yet, do not return any arguments. |
| 26 | + let argc = if argv.is_null() { 0 } else { ARGC.load(Relaxed) }; |
| 27 | + let args: Vec<OsString> = (0..argc) |
| 28 | + .map(|i| unsafe { |
| 29 | + let cstr = CStr::from_ptr(*argv.offset(i) as *const c_char); |
| 30 | + OsStringExt::from_vec(cstr.to_bytes().to_vec()) |
| 31 | + }) |
| 32 | + .collect(); |
| 33 | + |
| 34 | + Args { iter: args.into_iter() } |
18 | 35 | } |
19 | 36 |
|
20 | 37 | pub struct Args { |
@@ -51,44 +68,3 @@ impl DoubleEndedIterator for Args { |
51 | 68 | self.iter.next_back() |
52 | 69 | } |
53 | 70 | } |
54 | | - |
55 | | -mod imp { |
56 | | - use super::Args; |
57 | | - use crate::ffi::{CStr, OsString}; |
58 | | - use crate::os::unix::ffi::OsStringExt; |
59 | | - use crate::ptr; |
60 | | - |
61 | | - use crate::sys_common::mutex::StaticMutex; |
62 | | - |
63 | | - static mut ARGC: isize = 0; |
64 | | - static mut ARGV: *const *const u8 = ptr::null(); |
65 | | - static LOCK: StaticMutex = StaticMutex::new(); |
66 | | - |
67 | | - pub unsafe fn init(argc: isize, argv: *const *const u8) { |
68 | | - let _guard = LOCK.lock(); |
69 | | - ARGC = argc; |
70 | | - ARGV = argv; |
71 | | - } |
72 | | - |
73 | | - pub unsafe fn cleanup() { |
74 | | - let _guard = LOCK.lock(); |
75 | | - ARGC = 0; |
76 | | - ARGV = ptr::null(); |
77 | | - } |
78 | | - |
79 | | - pub fn args() -> Args { |
80 | | - Args { iter: clone().into_iter() } |
81 | | - } |
82 | | - |
83 | | - fn clone() -> Vec<OsString> { |
84 | | - unsafe { |
85 | | - let _guard = LOCK.lock(); |
86 | | - (0..ARGC) |
87 | | - .map(|i| { |
88 | | - let cstr = CStr::from_ptr(*ARGV.offset(i) as *const i8); |
89 | | - OsStringExt::from_vec(cstr.to_bytes().to_vec()) |
90 | | - }) |
91 | | - .collect() |
92 | | - } |
93 | | - } |
94 | | -} |
0 commit comments