2018-08-29 08:21:01 -05:00
|
|
|
#![allow(missing_docs, nonstandard_style)]
|
2014-09-30 17:03:56 -07:00
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::ffi::{OsStr, OsString};
|
|
|
|
|
use crate::io::ErrorKind;
|
|
|
|
|
use crate::os::windows::ffi::{OsStrExt, OsStringExt};
|
|
|
|
|
use crate::path::PathBuf;
|
|
|
|
|
use crate::time::Duration;
|
2014-09-30 17:03:56 -07:00
|
|
|
|
2017-11-01 12:32:13 -07:00
|
|
|
pub use self::rand::hashmap_random_keys;
|
2019-12-22 17:42:04 -05:00
|
|
|
pub use libc::strlen;
|
2017-11-01 12:32:13 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
#[macro_use]
|
|
|
|
|
pub mod compat;
|
2015-06-26 09:30:35 -07:00
|
|
|
|
2018-11-03 11:15:48 -07:00
|
|
|
pub mod alloc;
|
2016-09-29 22:00:44 +00:00
|
|
|
pub mod args;
|
2014-10-10 10:11:49 -07:00
|
|
|
pub mod c;
|
2017-11-01 12:59:40 -07:00
|
|
|
pub mod cmath;
|
std: Rewrite the `sync` module
This commit is a reimplementation of `std::sync` to be based on the
system-provided primitives wherever possible. The previous implementation was
fundamentally built on top of channels, and as part of the runtime reform it has
become clear that this is not the level of abstraction that the standard level
should be providing. This rewrite aims to provide as thin of a shim as possible
on top of the system primitives in order to make them safe.
The overall interface of the `std::sync` module has in general not changed, but
there are a few important distinctions, highlighted below:
* The condition variable type, `Condvar`, has been separated out of a `Mutex`.
A condition variable is now an entirely separate type. This separation
benefits users who only use one mutex, and provides a clearer distinction of
who's responsible for managing condition variables (the application).
* All of `Condvar`, `Mutex`, and `RWLock` are now directly built on top of
system primitives rather than using a custom implementation. The `Once`,
`Barrier`, and `Semaphore` types are still built upon these abstractions of
the system primitives.
* The `Condvar`, `Mutex`, and `RWLock` types all have a new static type and
constant initializer corresponding to them. These are provided primarily for C
FFI interoperation, but are often useful to otherwise simply have a global
lock. The types, however, will leak memory unless `destroy()` is called on
them, which is clearly documented.
* The `Condvar` implementation for an `RWLock` write lock has been removed. This
may be added back in the future with a userspace implementation, but this
commit is focused on exposing the system primitives first.
* The fundamental architecture of this design is to provide two separate layers.
The first layer is that exposed by `sys_common` which is a cross-platform
bare-metal abstraction of the system synchronization primitives. No attempt is
made at making this layer safe, and it is quite unsafe to use! It is currently
not exported as part of the API of the standard library, but the stabilization
of the `sys` module will ensure that these will be exposed in time. The
purpose of this layer is to provide the core cross-platform abstractions if
necessary to implementors.
The second layer is the layer provided by `std::sync` which is intended to be
the thinnest possible layer on top of `sys_common` which is entirely safe to
use. There are a few concerns which need to be addressed when making these
system primitives safe:
* Once used, the OS primitives can never be **moved**. This means that they
essentially need to have a stable address. The static primitives use
`&'static self` to enforce this, and the non-static primitives all use a
`Box` to provide this guarantee.
* Poisoning is leveraged to ensure that invalid data is not accessible from
other tasks after one has panicked.
In addition to these overall blanket safety limitations, each primitive has a
few restrictions of its own:
* Mutexes and rwlocks can only be unlocked from the same thread that they
were locked by. This is achieved through RAII lock guards which cannot be
sent across threads.
* Mutexes and rwlocks can only be unlocked if they were previously locked.
This is achieved by not exposing an unlocking method.
* A condition variable can only be waited on with a locked mutex. This is
achieved by requiring a `MutexGuard` in the `wait()` method.
* A condition variable cannot be used concurrently with more than one mutex.
This is guaranteed by dynamically binding a condition variable to
precisely one mutex for its entire lifecycle. This restriction may be able
to be relaxed in the future (a mutex is unbound when no threads are
waiting on the condvar), but for now it is sufficient to guarantee safety.
* Condvars now support timeouts for their blocking operations. The
implementation for these operations is provided by the system.
Due to the modification of the `Condvar` API, removal of the `std::sync::mutex`
API, and reimplementation, this is a breaking change. Most code should be fairly
easy to port using the examples in the documentation of these primitives.
[breaking-change]
Closes #17094
Closes #18003
2014-11-24 11:16:40 -08:00
|
|
|
pub mod condvar;
|
2016-09-21 19:50:30 +00:00
|
|
|
pub mod env;
|
2015-05-05 16:35:15 -07:00
|
|
|
pub mod fs;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod handle;
|
2019-02-08 20:42:34 +01:00
|
|
|
pub mod io;
|
2016-09-22 00:10:37 +00:00
|
|
|
pub mod memchr;
|
std: Rewrite the `sync` module
This commit is a reimplementation of `std::sync` to be based on the
system-provided primitives wherever possible. The previous implementation was
fundamentally built on top of channels, and as part of the runtime reform it has
become clear that this is not the level of abstraction that the standard level
should be providing. This rewrite aims to provide as thin of a shim as possible
on top of the system primitives in order to make them safe.
The overall interface of the `std::sync` module has in general not changed, but
there are a few important distinctions, highlighted below:
* The condition variable type, `Condvar`, has been separated out of a `Mutex`.
A condition variable is now an entirely separate type. This separation
benefits users who only use one mutex, and provides a clearer distinction of
who's responsible for managing condition variables (the application).
* All of `Condvar`, `Mutex`, and `RWLock` are now directly built on top of
system primitives rather than using a custom implementation. The `Once`,
`Barrier`, and `Semaphore` types are still built upon these abstractions of
the system primitives.
* The `Condvar`, `Mutex`, and `RWLock` types all have a new static type and
constant initializer corresponding to them. These are provided primarily for C
FFI interoperation, but are often useful to otherwise simply have a global
lock. The types, however, will leak memory unless `destroy()` is called on
them, which is clearly documented.
* The `Condvar` implementation for an `RWLock` write lock has been removed. This
may be added back in the future with a userspace implementation, but this
commit is focused on exposing the system primitives first.
* The fundamental architecture of this design is to provide two separate layers.
The first layer is that exposed by `sys_common` which is a cross-platform
bare-metal abstraction of the system synchronization primitives. No attempt is
made at making this layer safe, and it is quite unsafe to use! It is currently
not exported as part of the API of the standard library, but the stabilization
of the `sys` module will ensure that these will be exposed in time. The
purpose of this layer is to provide the core cross-platform abstractions if
necessary to implementors.
The second layer is the layer provided by `std::sync` which is intended to be
the thinnest possible layer on top of `sys_common` which is entirely safe to
use. There are a few concerns which need to be addressed when making these
system primitives safe:
* Once used, the OS primitives can never be **moved**. This means that they
essentially need to have a stable address. The static primitives use
`&'static self` to enforce this, and the non-static primitives all use a
`Box` to provide this guarantee.
* Poisoning is leveraged to ensure that invalid data is not accessible from
other tasks after one has panicked.
In addition to these overall blanket safety limitations, each primitive has a
few restrictions of its own:
* Mutexes and rwlocks can only be unlocked from the same thread that they
were locked by. This is achieved through RAII lock guards which cannot be
sent across threads.
* Mutexes and rwlocks can only be unlocked if they were previously locked.
This is achieved by not exposing an unlocking method.
* A condition variable can only be waited on with a locked mutex. This is
achieved by requiring a `MutexGuard` in the `wait()` method.
* A condition variable cannot be used concurrently with more than one mutex.
This is guaranteed by dynamically binding a condition variable to
precisely one mutex for its entire lifecycle. This restriction may be able
to be relaxed in the future (a mutex is unbound when no threads are
waiting on the condvar), but for now it is sufficient to guarantee safety.
* Condvars now support timeouts for their blocking operations. The
implementation for these operations is provided by the system.
Due to the modification of the `Condvar` API, removal of the `std::sync::mutex`
API, and reimplementation, this is a breaking change. Most code should be fairly
easy to port using the examples in the documentation of these primitives.
[breaking-change]
Closes #17094
Closes #18003
2014-11-24 11:16:40 -08:00
|
|
|
pub mod mutex;
|
2015-02-05 16:50:11 -08:00
|
|
|
pub mod net;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub mod os;
|
2015-01-21 15:55:31 -08:00
|
|
|
pub mod os_str;
|
2016-09-21 19:11:39 +00:00
|
|
|
pub mod path;
|
2015-05-05 16:35:15 -07:00
|
|
|
pub mod pipe;
|
|
|
|
|
pub mod process;
|
2016-02-17 13:56:16 -08:00
|
|
|
pub mod rand;
|
std: Rewrite the `sync` module
This commit is a reimplementation of `std::sync` to be based on the
system-provided primitives wherever possible. The previous implementation was
fundamentally built on top of channels, and as part of the runtime reform it has
become clear that this is not the level of abstraction that the standard level
should be providing. This rewrite aims to provide as thin of a shim as possible
on top of the system primitives in order to make them safe.
The overall interface of the `std::sync` module has in general not changed, but
there are a few important distinctions, highlighted below:
* The condition variable type, `Condvar`, has been separated out of a `Mutex`.
A condition variable is now an entirely separate type. This separation
benefits users who only use one mutex, and provides a clearer distinction of
who's responsible for managing condition variables (the application).
* All of `Condvar`, `Mutex`, and `RWLock` are now directly built on top of
system primitives rather than using a custom implementation. The `Once`,
`Barrier`, and `Semaphore` types are still built upon these abstractions of
the system primitives.
* The `Condvar`, `Mutex`, and `RWLock` types all have a new static type and
constant initializer corresponding to them. These are provided primarily for C
FFI interoperation, but are often useful to otherwise simply have a global
lock. The types, however, will leak memory unless `destroy()` is called on
them, which is clearly documented.
* The `Condvar` implementation for an `RWLock` write lock has been removed. This
may be added back in the future with a userspace implementation, but this
commit is focused on exposing the system primitives first.
* The fundamental architecture of this design is to provide two separate layers.
The first layer is that exposed by `sys_common` which is a cross-platform
bare-metal abstraction of the system synchronization primitives. No attempt is
made at making this layer safe, and it is quite unsafe to use! It is currently
not exported as part of the API of the standard library, but the stabilization
of the `sys` module will ensure that these will be exposed in time. The
purpose of this layer is to provide the core cross-platform abstractions if
necessary to implementors.
The second layer is the layer provided by `std::sync` which is intended to be
the thinnest possible layer on top of `sys_common` which is entirely safe to
use. There are a few concerns which need to be addressed when making these
system primitives safe:
* Once used, the OS primitives can never be **moved**. This means that they
essentially need to have a stable address. The static primitives use
`&'static self` to enforce this, and the non-static primitives all use a
`Box` to provide this guarantee.
* Poisoning is leveraged to ensure that invalid data is not accessible from
other tasks after one has panicked.
In addition to these overall blanket safety limitations, each primitive has a
few restrictions of its own:
* Mutexes and rwlocks can only be unlocked from the same thread that they
were locked by. This is achieved through RAII lock guards which cannot be
sent across threads.
* Mutexes and rwlocks can only be unlocked if they were previously locked.
This is achieved by not exposing an unlocking method.
* A condition variable can only be waited on with a locked mutex. This is
achieved by requiring a `MutexGuard` in the `wait()` method.
* A condition variable cannot be used concurrently with more than one mutex.
This is guaranteed by dynamically binding a condition variable to
precisely one mutex for its entire lifecycle. This restriction may be able
to be relaxed in the future (a mutex is unbound when no threads are
waiting on the condvar), but for now it is sufficient to guarantee safety.
* Condvars now support timeouts for their blocking operations. The
implementation for these operations is provided by the system.
Due to the modification of the `Condvar` API, removal of the `std::sync::mutex`
API, and reimplementation, this is a breaking change. Most code should be fairly
easy to port using the examples in the documentation of these primitives.
[breaking-change]
Closes #17094
Closes #18003
2014-11-24 11:16:40 -08:00
|
|
|
pub mod rwlock;
|
2014-11-23 19:21:17 -08:00
|
|
|
pub mod thread;
|
2020-07-12 11:37:11 +02:00
|
|
|
pub mod thread_local_dtor;
|
2020-07-12 11:45:04 +02:00
|
|
|
pub mod thread_local_key;
|
2020-10-06 00:34:59 +02:00
|
|
|
pub mod thread_parker;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod time;
|
2019-05-27 17:15:27 +02:00
|
|
|
cfg_if::cfg_if! {
|
|
|
|
|
if #[cfg(not(target_vendor = "uwp"))] {
|
|
|
|
|
pub mod stdio;
|
2019-05-27 17:16:20 +02:00
|
|
|
pub mod stack_overflow;
|
2019-05-27 17:15:27 +02:00
|
|
|
} else {
|
|
|
|
|
pub mod stdio_uwp;
|
2019-05-27 17:16:20 +02:00
|
|
|
pub mod stack_overflow_uwp;
|
2019-05-27 17:15:27 +02:00
|
|
|
pub use self::stdio_uwp as stdio;
|
2019-05-27 17:16:20 +02:00
|
|
|
pub use self::stack_overflow_uwp as stack_overflow;
|
2019-05-27 17:15:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
2014-10-10 10:11:49 -07:00
|
|
|
|
2021-04-11 07:05:39 +02:00
|
|
|
// SAFETY: must be called only once during runtime initialization.
|
2021-04-18 07:19:39 +02:00
|
|
|
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
|
2021-04-11 23:48:10 +02:00
|
|
|
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
|
|
|
|
|
stack_overflow::init();
|
|
|
|
|
}
|
2021-04-11 07:05:39 +02:00
|
|
|
|
|
|
|
|
// SAFETY: must be called only once during runtime cleanup.
|
2021-04-18 07:19:39 +02:00
|
|
|
// NOTE: this is not guaranteed to run, for example when the program aborts.
|
2021-04-11 07:05:39 +02:00
|
|
|
pub unsafe fn cleanup() {
|
|
|
|
|
net::cleanup();
|
|
|
|
|
}
|
2015-09-08 15:53:46 -07:00
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
pub fn decode_error_kind(errno: i32) -> ErrorKind {
|
2021-06-18 17:51:50 +01:00
|
|
|
use ErrorKind::*;
|
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
match errno as c::DWORD {
|
2021-06-18 17:51:50 +01:00
|
|
|
c::ERROR_ACCESS_DENIED => return PermissionDenied,
|
|
|
|
|
c::ERROR_ALREADY_EXISTS => return AlreadyExists,
|
|
|
|
|
c::ERROR_FILE_EXISTS => return AlreadyExists,
|
|
|
|
|
c::ERROR_BROKEN_PIPE => return BrokenPipe,
|
|
|
|
|
c::ERROR_FILE_NOT_FOUND => return NotFound,
|
|
|
|
|
c::ERROR_PATH_NOT_FOUND => return NotFound,
|
|
|
|
|
c::ERROR_NO_DATA => return BrokenPipe,
|
2022-02-10 23:49:27 +09:00
|
|
|
c::ERROR_INVALID_NAME => return InvalidFilename,
|
2022-01-31 17:28:05 +09:00
|
|
|
c::ERROR_INVALID_PARAMETER => return InvalidInput,
|
2021-06-18 17:51:50 +01:00
|
|
|
c::ERROR_NOT_ENOUGH_MEMORY | c::ERROR_OUTOFMEMORY => return OutOfMemory,
|
2020-05-02 14:05:56 +02:00
|
|
|
c::ERROR_SEM_TIMEOUT
|
|
|
|
|
| c::WAIT_TIMEOUT
|
|
|
|
|
| c::ERROR_DRIVER_CANCEL_TIMEOUT
|
|
|
|
|
| c::ERROR_OPERATION_ABORTED
|
|
|
|
|
| c::ERROR_SERVICE_REQUEST_TIMEOUT
|
|
|
|
|
| c::ERROR_COUNTER_TIMEOUT
|
|
|
|
|
| c::ERROR_TIMEOUT
|
|
|
|
|
| c::ERROR_RESOURCE_CALL_TIMED_OUT
|
|
|
|
|
| c::ERROR_CTX_MODEM_RESPONSE_TIMEOUT
|
|
|
|
|
| c::ERROR_CTX_CLIENT_QUERY_TIMEOUT
|
|
|
|
|
| c::FRS_ERR_SYSVOL_POPULATE_TIMEOUT
|
|
|
|
|
| c::ERROR_DS_TIMELIMIT_EXCEEDED
|
|
|
|
|
| c::DNS_ERROR_RECORD_TIMED_OUT
|
|
|
|
|
| c::ERROR_IPSEC_IKE_TIMED_OUT
|
|
|
|
|
| c::ERROR_RUNLEVEL_SWITCH_TIMEOUT
|
2021-06-18 17:51:50 +01:00
|
|
|
| c::ERROR_RUNLEVEL_SWITCH_AGENT_TIMEOUT => return TimedOut,
|
|
|
|
|
c::ERROR_CALL_NOT_IMPLEMENTED => return Unsupported,
|
2021-04-30 17:05:01 +01:00
|
|
|
c::ERROR_HOST_UNREACHABLE => return HostUnreachable,
|
|
|
|
|
c::ERROR_NETWORK_UNREACHABLE => return NetworkUnreachable,
|
|
|
|
|
c::ERROR_DIRECTORY => return NotADirectory,
|
|
|
|
|
c::ERROR_DIRECTORY_NOT_SUPPORTED => return IsADirectory,
|
|
|
|
|
c::ERROR_DIR_NOT_EMPTY => return DirectoryNotEmpty,
|
|
|
|
|
c::ERROR_WRITE_PROTECT => return ReadOnlyFilesystem,
|
2021-06-18 19:30:55 +01:00
|
|
|
c::ERROR_DISK_FULL | c::ERROR_HANDLE_DISK_FULL => return StorageFull,
|
2021-04-30 17:05:01 +01:00
|
|
|
c::ERROR_SEEK_ON_DEVICE => return NotSeekable,
|
|
|
|
|
c::ERROR_DISK_QUOTA_EXCEEDED => return FilesystemQuotaExceeded,
|
|
|
|
|
c::ERROR_FILE_TOO_LARGE => return FileTooLarge,
|
|
|
|
|
c::ERROR_BUSY => return ResourceBusy,
|
|
|
|
|
c::ERROR_POSSIBLE_DEADLOCK => return Deadlock,
|
|
|
|
|
c::ERROR_NOT_SAME_DEVICE => return CrossesDevices,
|
|
|
|
|
c::ERROR_TOO_MANY_LINKS => return TooManyLinks,
|
2022-02-10 23:49:27 +09:00
|
|
|
c::ERROR_FILENAME_EXCED_RANGE => return InvalidFilename,
|
2015-11-02 16:23:22 -08:00
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match errno {
|
2021-06-18 17:51:50 +01:00
|
|
|
c::WSAEACCES => PermissionDenied,
|
|
|
|
|
c::WSAEADDRINUSE => AddrInUse,
|
|
|
|
|
c::WSAEADDRNOTAVAIL => AddrNotAvailable,
|
|
|
|
|
c::WSAECONNABORTED => ConnectionAborted,
|
|
|
|
|
c::WSAECONNREFUSED => ConnectionRefused,
|
|
|
|
|
c::WSAECONNRESET => ConnectionReset,
|
|
|
|
|
c::WSAEINVAL => InvalidInput,
|
|
|
|
|
c::WSAENOTCONN => NotConnected,
|
|
|
|
|
c::WSAEWOULDBLOCK => WouldBlock,
|
|
|
|
|
c::WSAETIMEDOUT => TimedOut,
|
2021-04-30 17:05:01 +01:00
|
|
|
c::WSAEHOSTUNREACH => HostUnreachable,
|
|
|
|
|
c::WSAENETDOWN => NetworkDown,
|
|
|
|
|
c::WSAENETUNREACH => NetworkUnreachable,
|
2015-01-31 20:24:36 -08:00
|
|
|
|
2021-06-18 17:51:50 +01:00
|
|
|
_ => Uncategorized,
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 02:43:23 +00:00
|
|
|
pub fn unrolled_find_u16s(needle: u16, haystack: &[u16]) -> Option<usize> {
|
2019-12-29 15:47:13 +00:00
|
|
|
let ptr = haystack.as_ptr();
|
2020-04-02 02:43:23 +00:00
|
|
|
let mut start = &haystack[..];
|
|
|
|
|
|
|
|
|
|
// For performance reasons unfold the loop eight times.
|
2020-07-13 09:34:34 +00:00
|
|
|
while start.len() >= 8 {
|
2020-07-13 09:35:10 +00:00
|
|
|
macro_rules! if_return {
|
|
|
|
|
($($n:literal,)+) => {
|
|
|
|
|
$(
|
|
|
|
|
if start[$n] == needle {
|
|
|
|
|
return Some((&start[$n] as *const u16 as usize - ptr as usize) / 2);
|
|
|
|
|
}
|
|
|
|
|
)+
|
|
|
|
|
}
|
2020-04-02 02:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 09:35:10 +00:00
|
|
|
if_return!(0, 1, 2, 3, 4, 5, 6, 7,);
|
|
|
|
|
|
2020-04-02 02:43:23 +00:00
|
|
|
start = &start[8..];
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 09:35:51 +00:00
|
|
|
for c in start {
|
2020-04-02 02:43:23 +00:00
|
|
|
if *c == needle {
|
2020-07-13 09:35:51 +00:00
|
|
|
return Some((c as *const u16 as usize - ptr as usize) / 2);
|
2020-04-02 02:43:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
2019-12-29 15:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> crate::io::Result<Vec<u16>> {
|
|
|
|
|
fn inner(s: &OsStr) -> crate::io::Result<Vec<u16>> {
|
2015-11-19 18:01:11 +00:00
|
|
|
let mut maybe_result: Vec<u16> = s.encode_wide().collect();
|
2020-04-02 02:43:23 +00:00
|
|
|
if unrolled_find_u16s(0, &maybe_result).is_some() {
|
2021-08-08 01:04:33 -07:00
|
|
|
return Err(crate::io::const_io_error!(
|
2019-12-22 17:42:04 -05:00
|
|
|
ErrorKind::InvalidInput,
|
2021-08-08 01:04:33 -07:00
|
|
|
"strings passed to WinAPI cannot contain NULs",
|
2019-12-22 17:42:04 -05:00
|
|
|
));
|
2015-11-19 18:01:11 +00:00
|
|
|
}
|
|
|
|
|
maybe_result.push(0);
|
|
|
|
|
Ok(maybe_result)
|
|
|
|
|
}
|
|
|
|
|
inner(s.as_ref())
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
2015-10-07 23:11:25 +01:00
|
|
|
// Many Windows APIs follow a pattern of where we hand a buffer and then they
|
|
|
|
|
// will report back to us how large the buffer should be or how many bytes
|
2015-01-27 12:20:58 -08:00
|
|
|
// currently reside in the buffer. This function is an abstraction over these
|
|
|
|
|
// functions by making them easier to call.
|
|
|
|
|
//
|
|
|
|
|
// The first callback, `f1`, is yielded a (pointer, len) pair which can be
|
|
|
|
|
// passed to a syscall. The `ptr` is valid for `len` items (u16 in this case).
|
|
|
|
|
// The closure is expected to return what the syscall returns which will be
|
|
|
|
|
// interpreted by this function to determine if the syscall needs to be invoked
|
|
|
|
|
// again (with more buffer space).
|
|
|
|
|
//
|
|
|
|
|
// Once the syscall has completed (errors bail out early) the second closure is
|
|
|
|
|
// yielded the data which has been read from the syscall. The return value
|
|
|
|
|
// from this closure is then the return value of the function.
|
2019-02-11 04:23:21 +09:00
|
|
|
fn fill_utf16_buf<F1, F2, T>(mut f1: F1, f2: F2) -> crate::io::Result<T>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
F1: FnMut(*mut u16, c::DWORD) -> c::DWORD,
|
|
|
|
|
F2: FnOnce(&[u16]) -> T,
|
2015-01-27 12:20:58 -08:00
|
|
|
{
|
|
|
|
|
// Start off with a stack buf but then spill over to the heap if we end up
|
|
|
|
|
// needing more space.
|
|
|
|
|
let mut stack_buf = [0u16; 512];
|
|
|
|
|
let mut heap_buf = Vec::new();
|
|
|
|
|
unsafe {
|
|
|
|
|
let mut n = stack_buf.len();
|
|
|
|
|
loop {
|
|
|
|
|
let buf = if n <= stack_buf.len() {
|
2015-02-18 14:48:57 -05:00
|
|
|
&mut stack_buf[..]
|
2015-01-27 12:20:58 -08:00
|
|
|
} else {
|
|
|
|
|
let extra = n - heap_buf.len();
|
|
|
|
|
heap_buf.reserve(extra);
|
|
|
|
|
heap_buf.set_len(n);
|
2015-02-18 14:48:57 -05:00
|
|
|
&mut heap_buf[..]
|
2015-01-27 12:20:58 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// This function is typically called on windows API functions which
|
|
|
|
|
// will return the correct length of the string, but these functions
|
|
|
|
|
// also return the `0` on error. In some cases, however, the
|
|
|
|
|
// returned "correct length" may actually be 0!
|
|
|
|
|
//
|
|
|
|
|
// To handle this case we call `SetLastError` to reset it to 0 and
|
|
|
|
|
// then check it again if we get the "0 error value". If the "last
|
|
|
|
|
// error" is still 0 then we interpret it as a 0 length buffer and
|
|
|
|
|
// not an actual error.
|
|
|
|
|
c::SetLastError(0);
|
2015-11-02 16:23:22 -08:00
|
|
|
let k = match f1(buf.as_mut_ptr(), n as c::DWORD) {
|
|
|
|
|
0 if c::GetLastError() == 0 => 0,
|
2019-02-11 04:23:21 +09:00
|
|
|
0 => return Err(crate::io::Error::last_os_error()),
|
2015-01-27 12:20:58 -08:00
|
|
|
n => n,
|
|
|
|
|
} as usize;
|
2015-11-02 16:23:22 -08:00
|
|
|
if k == n && c::GetLastError() == c::ERROR_INSUFFICIENT_BUFFER {
|
2015-01-27 12:20:58 -08:00
|
|
|
n *= 2;
|
|
|
|
|
} else if k >= n {
|
|
|
|
|
n = k;
|
|
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
return Ok(f2(&buf[..k]));
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-23 10:59:17 -08:00
|
|
|
fn os2path(s: &[u16]) -> PathBuf {
|
2015-03-23 15:54:39 -07:00
|
|
|
PathBuf::from(OsString::from_wide(s))
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
2019-06-25 19:43:18 +02:00
|
|
|
pub fn truncate_utf16_at_nul(v: &[u16]) -> &[u16] {
|
2020-04-02 02:43:23 +00:00
|
|
|
match unrolled_find_u16s(0, v) {
|
2015-01-27 12:20:58 -08:00
|
|
|
// don't include the 0
|
|
|
|
|
Some(i) => &v[..i],
|
2019-12-22 17:42:04 -05:00
|
|
|
None => v,
|
2014-09-30 17:03:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-02-02 21:39:14 -08:00
|
|
|
|
2016-09-30 22:11:46 +00:00
|
|
|
pub trait IsZero {
|
2016-06-28 08:56:56 -07:00
|
|
|
fn is_zero(&self) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_is_zero {
|
|
|
|
|
($($t:ident)*) => ($(impl IsZero for $t {
|
|
|
|
|
fn is_zero(&self) -> bool {
|
|
|
|
|
*self == 0
|
|
|
|
|
}
|
|
|
|
|
})*)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl_is_zero! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
|
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
pub fn cvt<I: IsZero>(i: I) -> crate::io::Result<I> {
|
2020-08-10 18:04:44 +02:00
|
|
|
if i.is_zero() { Err(crate::io::Error::last_os_error()) } else { Ok(i) }
|
2015-02-02 21:39:14 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 22:11:46 +00:00
|
|
|
pub fn dur2timeout(dur: Duration) -> c::DWORD {
|
2015-04-28 11:40:04 -07:00
|
|
|
// Note that a duration is a (u64, u32) (seconds, nanoseconds) pair, and the
|
|
|
|
|
// timeouts in windows APIs are typically u32 milliseconds. To translate, we
|
|
|
|
|
// have two pieces to take care of:
|
|
|
|
|
//
|
|
|
|
|
// * Nanosecond precision is rounded up
|
|
|
|
|
// * Greater than u32::MAX milliseconds (50 days) is rounded up to INFINITE
|
|
|
|
|
// (never time out).
|
2019-12-22 17:42:04 -05:00
|
|
|
dur.as_secs()
|
|
|
|
|
.checked_mul(1000)
|
|
|
|
|
.and_then(|ms| ms.checked_add((dur.subsec_nanos() as u64) / 1_000_000))
|
|
|
|
|
.and_then(|ms| ms.checked_add(if dur.subsec_nanos() % 1_000_000 > 0 { 1 } else { 0 }))
|
2020-06-02 07:59:11 +00:00
|
|
|
.map(|ms| if ms > <c::DWORD>::MAX as u64 { c::INFINITE } else { ms as c::DWORD })
|
2019-12-22 17:42:04 -05:00
|
|
|
.unwrap_or(c::INFINITE)
|
2015-04-28 11:40:04 -07:00
|
|
|
}
|
2016-09-30 23:07:04 +00:00
|
|
|
|
2020-08-10 17:39:06 +02:00
|
|
|
/// Use `__fastfail` to abort the process
|
|
|
|
|
///
|
|
|
|
|
/// This is the same implementation as in libpanic_abort's `__rust_start_panic`. See
|
|
|
|
|
/// that function for more information on `__fastfail`
|
2018-07-16 16:38:56 -07:00
|
|
|
#[allow(unreachable_code)]
|
2020-05-17 19:37:44 +02:00
|
|
|
pub fn abort_internal() -> ! {
|
2020-08-27 19:11:48 +02:00
|
|
|
const FAST_FAIL_FATAL_APP_EXIT: usize = 7;
|
2022-01-25 12:41:26 -05:00
|
|
|
#[cfg(not(miri))] // inline assembly does not work in Miri
|
2020-05-17 19:37:44 +02:00
|
|
|
unsafe {
|
2020-08-27 19:11:48 +02:00
|
|
|
cfg_if::cfg_if! {
|
|
|
|
|
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
|
2021-12-10 00:15:33 +00:00
|
|
|
core::arch::asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT);
|
2020-08-27 19:11:48 +02:00
|
|
|
crate::intrinsics::unreachable();
|
2020-08-29 12:30:49 +02:00
|
|
|
} else if #[cfg(all(target_arch = "arm", target_feature = "thumb-mode"))] {
|
2021-12-10 00:15:33 +00:00
|
|
|
core::arch::asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT);
|
2020-08-27 19:11:48 +02:00
|
|
|
crate::intrinsics::unreachable();
|
|
|
|
|
} else if #[cfg(target_arch = "aarch64")] {
|
2021-12-10 00:15:33 +00:00
|
|
|
core::arch::asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT);
|
2020-08-27 19:11:48 +02:00
|
|
|
crate::intrinsics::unreachable();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-16 16:38:56 -07:00
|
|
|
}
|
2020-06-03 15:15:53 -04:00
|
|
|
crate::intrinsics::abort();
|
2016-09-30 23:07:04 +00:00
|
|
|
}
|