]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/mod.rs
6260c3b77ff81227bf2c52b750f1e21801eca202
[rust.git] / src / libstd / sys_common / mod.rs
1 //! Platform-independent platform abstraction
2 //!
3 //! This is the platform-independent portion of the standard library's
4 //! platform abstraction layer, whereas `std::sys` is the
5 //! platform-specific portion.
6 //!
7 //! The relationship between `std::sys_common`, `std::sys` and the
8 //! rest of `std` is complex, with dependencies going in all
9 //! directions: `std` depending on `sys_common`, `sys_common`
10 //! depending on `sys`, and `sys` depending on `sys_common` and `std`.
11 //! Ideally `sys_common` would be split into two and the dependencies
12 //! between them all would form a dag, facilitating the extraction of
13 //! `std::sys` from the standard library.
14
15 #![allow(missing_docs)]
16 #![allow(missing_debug_implementations)]
17
18 use crate::sync::Once;
19 use crate::sys;
20
21 macro_rules! rtabort {
22     ($($t:tt)*) => (crate::sys_common::util::abort(format_args!($($t)*)))
23 }
24
25 macro_rules! rtassert {
26     ($e:expr) => (if !$e {
27         rtabort!(concat!("assertion failed: ", stringify!($e)));
28     })
29 }
30
31 #[allow(unused_macros)] // not used on all platforms
32 macro_rules! rtunwrap {
33     ($ok:ident, $e:expr) => (if let $ok(v) = $e {
34         v
35     } else {
36         rtabort!(concat!("unwrap failed: ", stringify!($e)));
37     })
38 }
39
40 pub mod alloc;
41 pub mod at_exit_imp;
42 #[cfg(feature = "backtrace")]
43 pub mod backtrace;
44 pub mod condvar;
45 pub mod io;
46 pub mod mutex;
47 #[cfg(any(rustdoc, // see `mod os`, docs are generated for multiple platforms
48           unix,
49           target_os = "redox",
50           target_os = "cloudabi",
51           target_arch = "wasm32",
52           all(target_vendor = "fortanix", target_env = "sgx")))]
53 pub mod os_str_bytes;
54 pub mod poison;
55 pub mod remutex;
56 pub mod rwlock;
57 pub mod thread;
58 pub mod thread_info;
59 pub mod thread_local;
60 pub mod util;
61 pub mod wtf8;
62 pub mod bytestring;
63 pub mod process;
64 pub mod fs;
65
66 cfg_if! {
67     if #[cfg(any(target_os = "cloudabi",
68                  target_os = "l4re",
69                  target_os = "redox",
70                  all(target_arch = "wasm32", not(target_os = "emscripten")),
71                  all(target_vendor = "fortanix", target_env = "sgx")))] {
72         pub use crate::sys::net;
73     } else {
74         pub mod net;
75     }
76 }
77
78 #[cfg(feature = "backtrace")]
79 #[cfg(any(all(unix, not(target_os = "emscripten")),
80           all(windows, target_env = "gnu"),
81           target_os = "redox"))]
82 pub mod gnu;
83
84 // common error constructors
85
86 /// A trait for viewing representations from std types
87 #[doc(hidden)]
88 pub trait AsInner<Inner: ?Sized> {
89     fn as_inner(&self) -> &Inner;
90 }
91
92 /// A trait for viewing representations from std types
93 #[doc(hidden)]
94 pub trait AsInnerMut<Inner: ?Sized> {
95     fn as_inner_mut(&mut self) -> &mut Inner;
96 }
97
98 /// A trait for extracting representations from std types
99 #[doc(hidden)]
100 pub trait IntoInner<Inner> {
101     fn into_inner(self) -> Inner;
102 }
103
104 /// A trait for creating std types from internal representations
105 #[doc(hidden)]
106 pub trait FromInner<Inner> {
107     fn from_inner(inner: Inner) -> Self;
108 }
109
110 /// Enqueues a procedure to run when the main thread exits.
111 ///
112 /// Currently these closures are only run once the main *Rust* thread exits.
113 /// Once the `at_exit` handlers begin running, more may be enqueued, but not
114 /// infinitely so. Eventually a handler registration will be forced to fail.
115 ///
116 /// Returns `Ok` if the handler was successfully registered, meaning that the
117 /// closure will be run once the main thread exits. Returns `Err` to indicate
118 /// that the closure could not be registered, meaning that it is not scheduled
119 /// to be run.
120 pub fn at_exit<F: FnOnce() + Send + 'static>(f: F) -> Result<(), ()> {
121     if at_exit_imp::push(Box::new(f)) {Ok(())} else {Err(())}
122 }
123
124 /// One-time runtime cleanup.
125 pub fn cleanup() {
126     static CLEANUP: Once = Once::new();
127     CLEANUP.call_once(|| unsafe {
128         sys::args::cleanup();
129         sys::stack_overflow::cleanup();
130         at_exit_imp::cleanup();
131     });
132 }
133
134 // Computes (value*numer)/denom without overflow, as long as both
135 // (numer*denom) and the overall result fit into i64 (which is the case
136 // for our time conversions).
137 #[allow(dead_code)] // not used on all platforms
138 pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
139     let q = value / denom;
140     let r = value % denom;
141     // Decompose value as (value/denom*denom + value%denom),
142     // substitute into (value*numer)/denom and simplify.
143     // r < denom, so (denom*numer) is the upper bound of (r*numer)
144     q * numer + r * numer / denom
145 }
146
147 #[test]
148 fn test_muldiv() {
149     assert_eq!(mul_div_u64( 1_000_000_000_001, 1_000_000_000, 1_000_000),
150                1_000_000_000_001_000);
151 }