]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/mod.rs
883ab34f07c5832513d5a5c5753baac903ee0247
[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 pub mod alloc;
32 pub mod at_exit_imp;
33 #[cfg(feature = "backtrace")]
34 pub mod backtrace;
35 pub mod condvar;
36 pub mod io;
37 pub mod mutex;
38 #[cfg(any(rustdoc, // see `mod os`, docs are generated for multiple platforms
39           unix,
40           target_os = "redox",
41           target_os = "cloudabi",
42           target_arch = "wasm32",
43           all(target_vendor = "fortanix", target_env = "sgx")))]
44 pub mod os_str_bytes;
45 pub mod poison;
46 pub mod remutex;
47 pub mod rwlock;
48 pub mod thread;
49 pub mod thread_info;
50 pub mod thread_local;
51 pub mod util;
52 pub mod wtf8;
53 pub mod bytestring;
54 pub mod process;
55
56 cfg_if! {
57     if #[cfg(any(target_os = "cloudabi",
58                  target_os = "l4re",
59                  target_os = "redox",
60                  all(target_arch = "wasm32", not(target_os = "emscripten")),
61                  all(target_vendor = "fortanix", target_env = "sgx")))] {
62         pub use crate::sys::net;
63     } else {
64         pub mod net;
65     }
66 }
67
68 #[cfg(feature = "backtrace")]
69 #[cfg(any(all(unix, not(target_os = "emscripten")),
70           all(windows, target_env = "gnu"),
71           target_os = "redox"))]
72 pub mod gnu;
73
74 // common error constructors
75
76 /// A trait for viewing representations from std types
77 #[doc(hidden)]
78 pub trait AsInner<Inner: ?Sized> {
79     fn as_inner(&self) -> &Inner;
80 }
81
82 /// A trait for viewing representations from std types
83 #[doc(hidden)]
84 pub trait AsInnerMut<Inner: ?Sized> {
85     fn as_inner_mut(&mut self) -> &mut Inner;
86 }
87
88 /// A trait for extracting representations from std types
89 #[doc(hidden)]
90 pub trait IntoInner<Inner> {
91     fn into_inner(self) -> Inner;
92 }
93
94 /// A trait for creating std types from internal representations
95 #[doc(hidden)]
96 pub trait FromInner<Inner> {
97     fn from_inner(inner: Inner) -> Self;
98 }
99
100 /// Enqueues a procedure to run when the main thread exits.
101 ///
102 /// Currently these closures are only run once the main *Rust* thread exits.
103 /// Once the `at_exit` handlers begin running, more may be enqueued, but not
104 /// infinitely so. Eventually a handler registration will be forced to fail.
105 ///
106 /// Returns `Ok` if the handler was successfully registered, meaning that the
107 /// closure will be run once the main thread exits. Returns `Err` to indicate
108 /// that the closure could not be registered, meaning that it is not scheduled
109 /// to be run.
110 pub fn at_exit<F: FnOnce() + Send + 'static>(f: F) -> Result<(), ()> {
111     if at_exit_imp::push(Box::new(f)) {Ok(())} else {Err(())}
112 }
113
114 /// One-time runtime cleanup.
115 pub fn cleanup() {
116     static CLEANUP: Once = Once::new();
117     CLEANUP.call_once(|| unsafe {
118         sys::args::cleanup();
119         sys::stack_overflow::cleanup();
120         at_exit_imp::cleanup();
121     });
122 }
123
124 // Computes (value*numer)/denom without overflow, as long as both
125 // (numer*denom) and the overall result fit into i64 (which is the case
126 // for our time conversions).
127 #[allow(dead_code)] // not used on all platforms
128 pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
129     let q = value / denom;
130     let r = value % denom;
131     // Decompose value as (value/denom*denom + value%denom),
132     // substitute into (value*numer)/denom and simplify.
133     // r < denom, so (denom*numer) is the upper bound of (r*numer)
134     q * numer + r * numer / denom
135 }
136
137 #[test]
138 fn test_muldiv() {
139     assert_eq!(mul_div_u64( 1_000_000_000_001, 1_000_000_000, 1_000_000),
140                1_000_000_000_001_000);
141 }