]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys_common/mod.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / libstd / sys_common / mod.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
c30ab7b3
SL
11//! Platform-independent platform abstraction
12//!
476ff2be 13//! This is the platform-independent portion of the standard library's
c30ab7b3
SL
14//! platform abstraction layer, whereas `std::sys` is the
15//! platform-specific portion.
16//!
17//! The relationship between `std::sys_common`, `std::sys` and the
18//! rest of `std` is complex, with dependencies going in all
19//! directions: `std` depending on `sys_common`, `sys_common`
20//! depending on `sys`, and `sys` depending on `sys_common` and `std`.
21//! Ideally `sys_common` would be split into two and the dependencies
22//! between them all would form a dag, facilitating the extraction of
23//! `std::sys` from the standard library.
24
1a4d82fc 25#![allow(missing_docs)]
32a655c1 26#![allow(missing_debug_implementations)]
1a4d82fc 27
e9174d1e
SL
28use sync::Once;
29use sys;
c34b1796 30
e9174d1e 31pub mod at_exit_imp;
8bb4bdeb 32#[cfg(feature = "backtrace")]
1a4d82fc
JJ
33pub mod backtrace;
34pub mod condvar;
e9174d1e 35pub mod io;
c30ab7b3 36pub mod memchr;
1a4d82fc 37pub mod mutex;
9346a6ac
AL
38pub mod poison;
39pub mod remutex;
1a4d82fc 40pub mod rwlock;
1a4d82fc
JJ
41pub mod thread;
42pub mod thread_info;
43pub mod thread_local;
e9174d1e 44pub mod util;
85aaf69f 45pub mod wtf8;
1a4d82fc 46
abe05a73
XL
47cfg_if! {
48 if #[cfg(any(target_os = "redox", target_os = "l4re"))] {
49 pub use sys::net;
50 } else if #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] {
51 pub use sys::net;
52 } else {
53 pub mod net;
54 }
55}
476ff2be 56
8bb4bdeb 57#[cfg(feature = "backtrace")]
ea8adc8c 58#[cfg(any(all(unix, not(target_os = "emscripten")),
041b39d2
XL
59 all(windows, target_env = "gnu"),
60 target_os = "redox"))]
e9174d1e
SL
61pub mod gnu;
62
1a4d82fc
JJ
63// common error constructors
64
85aaf69f
SL
65/// A trait for viewing representations from std types
66#[doc(hidden)]
67pub trait AsInner<Inner: ?Sized> {
1a4d82fc
JJ
68 fn as_inner(&self) -> &Inner;
69}
70
85aaf69f
SL
71/// A trait for viewing representations from std types
72#[doc(hidden)]
73pub trait AsInnerMut<Inner: ?Sized> {
74 fn as_inner_mut(&mut self) -> &mut Inner;
75}
76
77/// A trait for extracting representations from std types
78#[doc(hidden)]
79pub trait IntoInner<Inner> {
80 fn into_inner(self) -> Inner;
81}
82
83/// A trait for creating std types from internal representations
84#[doc(hidden)]
85pub trait FromInner<Inner> {
86 fn from_inner(inner: Inner) -> Self;
87}
e9174d1e
SL
88
89/// Enqueues a procedure to run when the main thread exits.
90///
91/// Currently these closures are only run once the main *Rust* thread exits.
92/// Once the `at_exit` handlers begin running, more may be enqueued, but not
93/// infinitely so. Eventually a handler registration will be forced to fail.
94///
95/// Returns `Ok` if the handler was successfully registered, meaning that the
96/// closure will be run once the main thread exits. Returns `Err` to indicate
97/// that the closure could not be registered, meaning that it is not scheduled
98/// to be run.
99pub fn at_exit<F: FnOnce() + Send + 'static>(f: F) -> Result<(), ()> {
100 if at_exit_imp::push(Box::new(f)) {Ok(())} else {Err(())}
101}
102
c30ab7b3
SL
103macro_rules! rtabort {
104 ($($t:tt)*) => (::sys_common::util::abort(format_args!($($t)*)))
105}
106
e9174d1e
SL
107/// One-time runtime cleanup.
108pub fn cleanup() {
109 static CLEANUP: Once = Once::new();
110 CLEANUP.call_once(|| unsafe {
c30ab7b3 111 sys::args::cleanup();
e9174d1e
SL
112 sys::stack_overflow::cleanup();
113 at_exit_imp::cleanup();
114 });
115}
92a42be0
SL
116
117// Computes (value*numer)/denom without overflow, as long as both
118// (numer*denom) and the overall result fit into i64 (which is the case
119// for our time conversions).
120#[allow(dead_code)] // not used on all platforms
121pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
122 let q = value / denom;
123 let r = value % denom;
124 // Decompose value as (value/denom*denom + value%denom),
125 // substitute into (value*numer)/denom and simplify.
126 // r < denom, so (denom*numer) is the upper bound of (r*numer)
127 q * numer + r * numer / denom
128}
129
130#[test]
131fn test_muldiv() {
132 assert_eq!(mul_div_u64( 1_000_000_000_001, 1_000_000_000, 1_000_000),
133 1_000_000_000_001_000);
134}