2014-10-01 00:03:56 +00:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-09-30 23:56:28 +00:00
|
|
|
//! Platform-independent platform abstraction
|
|
|
|
//!
|
2016-11-10 03:15:56 +00:00
|
|
|
//! This is the platform-independent portion of the standard library's
|
2016-09-30 23:56:28 +00:00
|
|
|
//! platform abstraction layer, whereas `std::sys` is the
|
|
|
|
//! platform-specific portion.
|
|
|
|
//!
|
|
|
|
//! The relationship between `std::sys_common`, `std::sys` and the
|
|
|
|
//! rest of `std` is complex, with dependencies going in all
|
|
|
|
//! directions: `std` depending on `sys_common`, `sys_common`
|
|
|
|
//! depending on `sys`, and `sys` depending on `sys_common` and `std`.
|
|
|
|
//! Ideally `sys_common` would be split into two and the dependencies
|
|
|
|
//! between them all would form a dag, facilitating the extraction of
|
|
|
|
//! `std::sys` from the standard library.
|
|
|
|
|
2014-11-09 12:59:23 +00:00
|
|
|
#![allow(missing_docs)]
|
2016-12-20 20:18:55 +00:00
|
|
|
#![allow(missing_debug_implementations)]
|
2014-10-01 00:03:56 +00:00
|
|
|
|
2015-09-08 22:53:46 +00:00
|
|
|
use sync::Once;
|
|
|
|
use sys;
|
|
|
|
|
|
|
|
pub mod at_exit_imp;
|
2017-01-23 23:55:35 +00:00
|
|
|
#[cfg(feature = "backtrace")]
|
2014-11-24 03:21:17 +00:00
|
|
|
pub mod backtrace;
|
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 19:16:40 +00:00
|
|
|
pub mod condvar;
|
2015-09-08 22:53:46 +00: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 19:16:40 +00:00
|
|
|
pub mod mutex;
|
2015-04-03 21:46:54 +00:00
|
|
|
pub mod poison;
|
|
|
|
pub mod remutex;
|
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 19:16:40 +00:00
|
|
|
pub mod rwlock;
|
2014-11-24 03:21:17 +00:00
|
|
|
pub mod thread;
|
2014-12-07 02:34:37 +00:00
|
|
|
pub mod thread_info;
|
2014-11-14 22:20:57 +00:00
|
|
|
pub mod thread_local;
|
2015-09-08 22:53:46 +00:00
|
|
|
pub mod util;
|
2015-01-21 23:55:31 +00:00
|
|
|
pub mod wtf8;
|
2017-12-18 00:13:10 +00:00
|
|
|
pub mod bytestring;
|
2014-10-01 00:03:56 +00:00
|
|
|
|
2017-10-23 03:01:00 +00:00
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(any(target_os = "redox", target_os = "l4re"))] {
|
|
|
|
pub use sys::net;
|
|
|
|
} else if #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] {
|
|
|
|
pub use sys::net;
|
|
|
|
} else {
|
|
|
|
pub mod net;
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 02:57:49 +00:00
|
|
|
|
2017-01-23 23:55:35 +00:00
|
|
|
#[cfg(feature = "backtrace")]
|
2017-07-23 06:05:47 +00:00
|
|
|
#[cfg(any(all(unix, not(target_os = "emscripten")),
|
2017-07-14 02:07:37 +00:00
|
|
|
all(windows, target_env = "gnu"),
|
|
|
|
target_os = "redox"))]
|
2015-08-26 00:44:55 +00:00
|
|
|
pub mod gnu;
|
|
|
|
|
2014-10-01 00:03:56 +00:00
|
|
|
// common error constructors
|
|
|
|
|
2015-01-21 23:55:31 +00:00
|
|
|
/// A trait for viewing representations from std types
|
2015-02-03 05:39:14 +00:00
|
|
|
#[doc(hidden)]
|
2015-01-21 23:55:31 +00:00
|
|
|
pub trait AsInner<Inner: ?Sized> {
|
2014-11-21 02:26:47 +00:00
|
|
|
fn as_inner(&self) -> &Inner;
|
2014-10-01 00:03:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
/// A trait for viewing representations from std types
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub trait AsInnerMut<Inner: ?Sized> {
|
|
|
|
fn as_inner_mut(&mut self) -> &mut Inner;
|
|
|
|
}
|
|
|
|
|
2015-01-21 23:55:31 +00:00
|
|
|
/// A trait for extracting representations from std types
|
2015-02-03 05:39:14 +00:00
|
|
|
#[doc(hidden)]
|
2015-01-21 23:55:31 +00:00
|
|
|
pub trait IntoInner<Inner> {
|
|
|
|
fn into_inner(self) -> Inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A trait for creating std types from internal representations
|
2015-02-03 05:39:14 +00:00
|
|
|
#[doc(hidden)]
|
2015-01-21 23:55:31 +00:00
|
|
|
pub trait FromInner<Inner> {
|
|
|
|
fn from_inner(inner: Inner) -> Self;
|
|
|
|
}
|
2015-09-08 22:53:46 +00:00
|
|
|
|
|
|
|
/// Enqueues a procedure to run when the main thread exits.
|
|
|
|
///
|
|
|
|
/// Currently these closures are only run once the main *Rust* thread exits.
|
|
|
|
/// Once the `at_exit` handlers begin running, more may be enqueued, but not
|
|
|
|
/// infinitely so. Eventually a handler registration will be forced to fail.
|
|
|
|
///
|
|
|
|
/// Returns `Ok` if the handler was successfully registered, meaning that the
|
|
|
|
/// closure will be run once the main thread exits. Returns `Err` to indicate
|
|
|
|
/// that the closure could not be registered, meaning that it is not scheduled
|
|
|
|
/// to be run.
|
|
|
|
pub fn at_exit<F: FnOnce() + Send + 'static>(f: F) -> Result<(), ()> {
|
|
|
|
if at_exit_imp::push(Box::new(f)) {Ok(())} else {Err(())}
|
|
|
|
}
|
|
|
|
|
2016-10-01 01:38:29 +00:00
|
|
|
macro_rules! rtabort {
|
|
|
|
($($t:tt)*) => (::sys_common::util::abort(format_args!($($t)*)))
|
|
|
|
}
|
|
|
|
|
2015-09-08 22:53:46 +00:00
|
|
|
/// One-time runtime cleanup.
|
|
|
|
pub fn cleanup() {
|
|
|
|
static CLEANUP: Once = Once::new();
|
|
|
|
CLEANUP.call_once(|| unsafe {
|
2016-09-29 22:00:44 +00:00
|
|
|
sys::args::cleanup();
|
2015-09-08 22:53:46 +00:00
|
|
|
sys::stack_overflow::cleanup();
|
|
|
|
at_exit_imp::cleanup();
|
|
|
|
});
|
|
|
|
}
|
2015-11-17 01:36:14 +00:00
|
|
|
|
|
|
|
// Computes (value*numer)/denom without overflow, as long as both
|
|
|
|
// (numer*denom) and the overall result fit into i64 (which is the case
|
|
|
|
// for our time conversions).
|
|
|
|
#[allow(dead_code)] // not used on all platforms
|
|
|
|
pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
|
|
|
|
let q = value / denom;
|
|
|
|
let r = value % denom;
|
|
|
|
// Decompose value as (value/denom*denom + value%denom),
|
|
|
|
// substitute into (value*numer)/denom and simplify.
|
|
|
|
// r < denom, so (denom*numer) is the upper bound of (r*numer)
|
|
|
|
q * numer + r * numer / denom
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_muldiv() {
|
|
|
|
assert_eq!(mul_div_u64( 1_000_000_000_001, 1_000_000_000, 1_000_000),
|
|
|
|
1_000_000_000_001_000);
|
|
|
|
}
|