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