]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/common/mod.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / libstd / sys / common / mod.rs
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
11 #![allow(missing_docs)]
12
13 use boxed::Box;
14 use sync::Once;
15 use sys;
16
17 macro_rules! rtabort {
18 ($($t:tt)*) => (::sys_common::util::abort(format_args!($($t)*)))
19 }
20
21 macro_rules! rtassert {
22 ($e:expr) => ({
23 if !$e {
24 rtabort!(concat!("assertion failed: ", stringify!($e)))
25 }
26 })
27 }
28
29 pub mod args;
30 pub mod at_exit_imp;
31 pub mod backtrace;
32 pub mod condvar;
33 pub mod dwarf;
34 pub mod io;
35 pub mod libunwind;
36 pub mod mutex;
37 pub mod net;
38 pub mod poison;
39 pub mod remutex;
40 pub mod rwlock;
41 pub mod thread;
42 pub mod thread_info;
43 pub mod thread_local;
44 pub mod unwind;
45 pub mod util;
46 pub mod wtf8;
47
48 #[cfg(any(all(unix, not(any(target_os = "macos", target_os = "ios"))),
49 all(windows, target_env = "gnu")))]
50 pub mod gnu;
51
52 // common error constructors
53
54 /// A trait for viewing representations from std types
55 #[doc(hidden)]
56 pub trait AsInner<Inner: ?Sized> {
57 fn as_inner(&self) -> &Inner;
58 }
59
60 /// A trait for viewing representations from std types
61 #[doc(hidden)]
62 pub trait AsInnerMut<Inner: ?Sized> {
63 fn as_inner_mut(&mut self) -> &mut Inner;
64 }
65
66 /// A trait for extracting representations from std types
67 #[doc(hidden)]
68 pub trait IntoInner<Inner> {
69 fn into_inner(self) -> Inner;
70 }
71
72 /// A trait for creating std types from internal representations
73 #[doc(hidden)]
74 pub trait FromInner<Inner> {
75 fn from_inner(inner: Inner) -> Self;
76 }
77
78 /// Enqueues a procedure to run when the main thread exits.
79 ///
80 /// Currently these closures are only run once the main *Rust* thread exits.
81 /// Once the `at_exit` handlers begin running, more may be enqueued, but not
82 /// infinitely so. Eventually a handler registration will be forced to fail.
83 ///
84 /// Returns `Ok` if the handler was successfully registered, meaning that the
85 /// closure will be run once the main thread exits. Returns `Err` to indicate
86 /// that the closure could not be registered, meaning that it is not scheduled
87 /// to be run.
88 pub fn at_exit<F: FnOnce() + Send + 'static>(f: F) -> Result<(), ()> {
89 if at_exit_imp::push(Box::new(f)) {Ok(())} else {Err(())}
90 }
91
92 /// One-time runtime cleanup.
93 pub fn cleanup() {
94 static CLEANUP: Once = Once::new();
95 CLEANUP.call_once(|| unsafe {
96 args::cleanup();
97 sys::stack_overflow::cleanup();
98 at_exit_imp::cleanup();
99 });
100 }
101
102 // Computes (value*numer)/denom without overflow, as long as both
103 // (numer*denom) and the overall result fit into i64 (which is the case
104 // for our time conversions).
105 #[allow(dead_code)] // not used on all platforms
106 pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
107 let q = value / denom;
108 let r = value % denom;
109 // Decompose value as (value/denom*denom + value%denom),
110 // substitute into (value*numer)/denom and simplify.
111 // r < denom, so (denom*numer) is the upper bound of (r*numer)
112 q * numer + r * numer / denom
113 }
114
115 #[test]
116 fn test_muldiv() {
117 assert_eq!(mul_div_u64( 1_000_000_000_001, 1_000_000_000, 1_000_000),
118 1_000_000_000_001_000);
119 }