summary refs log tree commit diff
path: root/library/std/src/sys_common
AgeCommit message (Collapse)AuthorLines
2021-04-27Override `clone_from` method for PathBuf and OsStringBenoît du Garreau-1/+13
2021-04-25Auto merge of #84115 - CDirkx:rt, r=m-ou-sebors-127/+66
Rework `init` and `cleanup` This PR reworks the code in `std` that runs before and after `main` and centralizes this code respectively in the functions `init` and `cleanup` in both `sys_common` and `sys`. This makes is easy to see what code is executed during initialization and cleanup on each platform just by looking at e.g. `sys::windows::init`. Full list of changes: - new module `rt` in `sys_common` to contain `init` and `cleanup` and the runtime macros. - `at_exit` and the mechanism to register exit handlers has been completely removed. In practice this was only used for closing sockets on windows and flushing stdout, which have been moved to `cleanup`. - <s>On windows `alloc` and `net` initialization is now done in `init`, this saves a runtime check in every allocation and network use.</s>
2021-04-24Rollup merge of #84387 - CDirkx:poison, r=m-ou-seYuki Okushi-258/+0
Move `sys_common::poison` to `sync::poison` `sys_common` should not contain publicly exported types, only platform-independent abstractions on top of `sys`, which `sys_common::poison` is not. There is thus no reason for the module to not live under `sync`. Part of #84187.
2021-04-22Rollup merge of #84402 - CDirkx:rwlock, r=dtolnayDylan DPC-59/+0
Move `sys_common::rwlock::StaticRWLock` etc. to `sys::unix::rwlock` This moves `sys_common::rwlock::StaticRwLock`, `RWLockReadGuard` and `RWLockWriteGuard` to `sys::unix::rwlock`. They are already `#[cfg(unix)]` and don't need to be in `sys_common`.
2021-04-22Remove `Once` from `init`Christiaan Dirkx-5/+6
2021-04-22Document that `init` and `cleanup` are not guaranteed to runChristiaan Dirkx-2/+3
2021-04-22Apply suggestions from reviewChristiaan Dirkx-2/+2
2021-04-22Move most init to `sys::init`Christiaan Dirkx-6/+1
2021-04-22Move all cleanup to `sys::cleanup`Christiaan Dirkx-2/+0
2021-04-22Rework `at_exit` to `cleanup`Christiaan Dirkx-127/+71
2021-04-22Move `sys_common::poison` to `sync::poison`Christiaan Dirkx-258/+0
2021-04-21Move `sys_common::rwlock::StaticRWLock` etc. to `sys::unix::rwlock`Christiaan Dirkx-59/+0
2021-04-21Replace all `fmt.pad` with `debug_struct`Christiaan Dirkx-1/+1
2021-04-14Update documentationChristiaan Dirkx-3/+5
2021-04-14Move `std::sys_common::alloc` to `std::sys::common`Christiaan Dirkx-49/+0
2021-03-27Improve fs error open_from unixIvan Tham-4/+6
Consistency for #79399 Suggested by JohnTitor Improve fs error invaild input for sys_common The text was duplicated from unix.
2021-03-24Rollup merge of #83353 - m-ou-se:io-error-avoid-alloc, r=nagisaDylan DPC-4/+4
Add internal io::Error::new_const to avoid allocations. This makes it possible to have a io::Error containing a message with zero allocations, and uses that everywhere to avoid the *three* allocations involved in `io::Error::new(kind, "message")`. The function signature isn't perfect, because it needs a reference to the `&str`. So for now, this is just a `pub(crate)` function. Later, we'll be able to use `fn new_const<MSG: &'static str>(kind: ErrorKind)` to make that a bit better. (Then we'll also be able to use some ZST trickery if that would result in more efficient code.) See https://github.com/rust-lang/rust/issues/83352
2021-03-21Use io::Error::new_const everywhere to avoid allocations.Mara Bos-4/+4
2021-03-15Auto merge of #83121 - the8472:env-rwlock-2, r=joshtriplettbors-0/+59
use RWlock when accessing os::env (take 2) This reverts commit acdca316c3d42299d31c1b47eb792006ffdfc29c (#82877) i.e. redoes #81850 since the invalid unlock attempts in the child process have been fixed in #82949 r? `@joshtriplett`
2021-03-14Revert "Revert "use RWlock when accessing os::env #81850""The8472-0/+59
This reverts commit acdca316c3d42299d31c1b47eb792006ffdfc29c.
2021-03-14Fix a typo in thread_local_dtor.rsMotoki Ikeda-1/+1
2021-03-07Revert "use RWlock when accessing os::env #81850"Eric Huss-59/+0
This reverts commit 354f19cf2475148994954b6783341620c7445071, reversing changes made to 0cfba2fd090834c909d5ed9deccdee8170da791b.
2021-02-24library: Normalize safety-for-unsafe-block commentsMiguel Ojeda-2/+2
Almost all safety comments are of the form `// SAFETY:`, so normalize the rest and fix a few of them that should have been a `/// # Safety` section instead. Furthermore, make `tidy` only allow the uppercase form. While currently `tidy` only checks `core`, it is a good idea to prevent `core` from drifting to non-uppercase comments, so that later we can start checking `alloc` etc. too. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2021-02-15Rollup merge of #81975 - Amanieu:seal2, r=m-ou-seJonas Schievink-2/+9
Seal the CommandExt, OsStrExt and OsStringExt traits A crater run (https://github.com/rust-lang/rust/pull/81213#issuecomment-767651811) has shown that this does not break any existing code. This also unblocks #77728. Based on #81213. r? ````@m-ou-se```` cc ````@lygstate````
2021-02-10Seal the CommandExt, OsStrExt and OsStringExt traitsAmanieu d'Antras-2/+9
2021-02-09split guard into read and write typesThe8472-15/+14
2021-02-08introduce StaticRWLock wrapper to make methods safeThe8472-49/+60
2021-02-07silence dead code warnings on windowsThe8472-0/+5
2021-02-07use rwlock for accessing ENVThe8472-0/+44
2021-01-06Optimize away some path lookups in the generic `fs::copy` implementation.Dan Gohman-4/+6
This also eliminates a use of a `Path` convenience function, in support of #80741, refactoring `std::path` to focus on pure data structures and algorithms.
2020-12-22Migrate standard library away from compare_and_swapLinus Färnstrand-8/+8
2020-12-14Auto merge of #77618 - fusion-engineering-forks:windows-parker, r=Amanieubors-0/+2
Add fast futex-based thread parker for Windows. This adds a fast futex-based thread parker for Windows. It either uses WaitOnAddress+WakeByAddressSingle or NT Keyed Events (NtWaitForKeyedEvent+NtReleaseKeyedEvent), depending on which is available. Together, this makes this thread parker work for Windows XP and up. Before this change, park()/unpark() did not work on Windows XP: it needs condition variables, which only exist since Windows Vista. --- Unfortunately, NT Keyed Events are an undocumented Windows API. However: - This API is relatively simple with obvious behaviour, and there are several (unofficial) articles documenting the details. [1] - parking_lot has been using this API for years (on Windows versions before Windows 8). [2] Many big projects extensively use parking_lot, such as servo and the Rust compiler itself. - It is the underlying API used by Windows SRW locks and Windows critical sections. [3] [4] - The source code of the implementations of Wine, ReactOs, and Windows XP are available and match the expected behaviour. - The main risk with an undocumented API is that it might change in the future. But since we only use it for older versions of Windows, that's not a problem. - Even if these functions do not block or wake as we expect (which is unlikely, see all previous points), this implementation would still be memory safe. The NT Keyed Events API is only used to sleep/block in the right place. [1]\: http://www.locklessinc.com/articles/keyed_events/ [2]\: https://github.com/Amanieu/parking_lot/commit/43abbc964e [3]\: https://docs.microsoft.com/en-us/archive/msdn-magazine/2012/november/windows-with-c-the-evolution-of-synchronization-in-windows-and-c [4]\: Windows Internals, Part 1, ISBN 9780735671300 --- The choice of fallback API is inspired by parking_lot(_core), but the implementation of this thread parker is different. While parking_lot has no use for a fast path (park() directly returning if unpark() was already called), this implementation has a fast path that returns without even checking which waiting/waking API to use, as the same atomic variable with compatible states is used in all cases.
2020-12-10Rollup merge of #79809 - Eric-Arellano:split-once, r=matkladTyler Mandry-4/+1
Dogfood `str_split_once()` Part of https://github.com/rust-lang/rust/issues/74773. Beyond increased clarity, this fixes some instances of a common confusion with how `splitn(2)` behaves: the first element will always be `Some()`, regardless of the delimiter, and even if the value is empty. Given this code: ```rust fn main() { let val = "..."; let mut iter = val.splitn(2, '='); println!("Input: {:?}, first: {:?}, second: {:?}", val, iter.next(), iter.next()); } ``` We get: ``` Input: "no_delimiter", first: Some("no_delimiter"), second: None Input: "k=v", first: Some("k"), second: Some("v") Input: "=", first: Some(""), second: Some("") ``` Using `str_split_once()` makes more clear what happens when the delimiter is not found.
2020-12-08Use Pin for the 'don't move' requirement of ReentrantMutex.Mara Bos-48/+39
The code in io::stdio before this change misused the ReentrantMutexes, by calling init() on them and moving them afterwards. Now that ReentrantMutex requires Pin for init(), this mistake is no longer easy to make.
2020-12-08Remove unnecessary import of `crate::marker` in std::sys_common::remutex.Mara Bos-2/+1
It was used for marker::Send, but Send is already in scope.
2020-12-07Fix net.rs - rsplitn() returns a reverse iteratorEric Arellano-2/+1
2020-12-07Dogfood 'str_split_once()` in the std libEric Arellano-3/+1
2020-11-22Drop support for cloudabi targetsLzu Tao-2/+1
2020-10-16Rollup merge of #77648 - fusion-engineering-forks:static-mutex, r=dtolnayDylan DPC-12/+6
Static mutex is static StaticMutex is only ever used with as a static (as the name already suggests). So it doesn't have to be generic over a lifetime, but can simply assume 'static. This 'static lifetime guarantees the object is never moved, so this is no longer a manually checked requirement for unsafe calls to lock(). @rustbot modify labels: +T-libs +A-concurrency +C-cleanup
2020-10-16Rollup merge of #77646 - fusion-engineering-forks:use-static-mutex, r=dtolnayDylan DPC-17/+5
For backtrace, use StaticMutex instead of a raw sys Mutex. The code used the very unsafe `sys::mutex::Mutex` directly, and built its own unlock-on-drop wrapper around it. The StaticMutex wrapper already provides that and is easier to use safely. @rustbot modify labels: +T-libs +C-cleanup
2020-10-16Rollup merge of #77619 - fusion-engineering-forks:wasm-parker, r=dtolnayDylan DPC-1/+5
Use futex-based thread-parker for Wasm32. This uses the existing `sys_common/thread_parker/futex.rs` futex-based thread parker (that was already used for Linux) for wasm32 as well (if the wasm32 atomics target feature is enabled, which is not the case by default). Wasm32 provides the basic futex operations as instructions: https://webassembly.github.io/threads/syntax/instructions.html These are now exposed from `sys::futex::{futex_wait, futex_wake}`, just like on Linux. So, `thread_parker/futex.rs` stays completely unmodified.
2020-10-14Remove lifetime from StaticMutex and assume 'static.Mara Bos-12/+5
StaticMutex is only ever used with as a static (as the name already suggests). So it doesn't have to be generic over a lifetime, but can simply assume 'static. This 'static lifetime guarantees the object is never moved, so this is no longer a manually checked requirement for unsafe calls to lock().
2020-10-14Fix comment about non-reentrant StaticMutex::lock().Mara Bos-2/+3
The comment said it's UB to call lock() while it is locked. That'd be quite a useless Mutex. :) It was supposed to say 'locked by the same thread', not just 'locked'.
2020-10-08Remove unnecessary rustc_const_stable attributes.Mara Bos-1/+0
2020-10-07For backtrace, use StaticMutex instead of a raw sys Mutex.Mara Bos-17/+5
2020-10-06Use futex-based thread-parker for Wasm32.Mara Bos-1/+5
2020-10-06Add fast futex-based thread parker for Windows.Mara Bos-0/+2
2020-10-04Auto merge of #77380 - fusion-engineering-forks:unbox-the-mutex, r=dtolnaybors-42/+83
Unbox mutexes and condvars on some platforms Both mutexes and condition variables contained a Box containing the actual os-specific object. This was done because moving these objects may cause undefined behaviour on some platforms. However, this is not needed on Windows[1], Wasm[2], cloudabi[2], and 'unsupported'[3], were the box was only needlessly making them less efficient. This change gets rid of the box on those platforms. On those platforms, `Condvar` can no longer verify it is only used with one `Mutex`, as mutexes no longer have a stable address. This was addressed and considered acceptable in #76932. [1]\: https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-initializesrwlock [2]\: These are just a single atomic integer together with futex wait/wake calls/instructions. [3]\: The `unsupported` platform doesn't support multiple threads at all.
2020-10-02Auto merge of #77029 - ehuss:command-access, r=dtolnaybors-0/+37
Add accessors to Command. This adds some accessor methods to `Command` to provide a way to access the values set when building the `Command`. An example where this can be useful is to display the command to be executed. This is roughly based on the [`ProcessBuilder`](https://github.com/rust-lang/cargo/blob/13b73cdaf76b2d9182515c9cf26a8f68342d08ef/src/cargo/util/process_builder.rs#L105-L134) in Cargo. Possible concerns about the API: - Values with NULs on Unix will be returned as `"<string-with-nul>"`. I don't think it is practical to avoid this, since otherwise a whole separate copy of all the values would need to be kept in `Command`. - Does not handle `arg0` on Unix. This can be awkward to support in `get_args` and is rarely used. I figure if someone really wants it, it can be added to `CommandExt` as a separate method. - Does not offer a way to detect `env_clear`. I'm uncertain if it would be useful for anyone. - Does not offer a way to get an environment variable by name (`get_env`). I figure this can be added later if anyone really wants it. I think the motivation for this is weak, though. Also, the API could be a little awkward (return a `Option<Option<&OsStr>>`?). - `get_envs` could skip "cleared" entries and just return `&OsStr` values instead of `Option<&OsStr>`. I'm on the fence here. My use case is to display a shell command, and I only intend it to be roughly equivalent to the actual execution, and I probably won't display `None` entries. I erred on the side of providing extra information, but I suspect many situations will just filter out the `None`s. - Could implement more iterator stuff (like `DoubleEndedIterator`). I have not implemented new std items before, so I'm uncertain if the existing issue should be reused, or if a new tracking issue is needed. cc #44434
2020-10-02Make it possible to have unboxed condvars on specific platforms.Mara Bos-2/+2
This commit keeps all condvars boxed on all platforms, but makes it trivial to remove the box on some platforms later.