]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/util.rs
Add externfn macro and correctly label fixed_stack_segments
[rust.git] / src / libstd / rt / util.rs
1 // Copyright 2013 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 use container::Container;
12 use from_str::FromStr;
13 use libc;
14 use option::{Some, None};
15 use os;
16 use str::StrSlice;
17
18 /// Get the number of cores available
19 pub fn num_cpus() -> uint {
20     #[fixed_stack_segment]; #[inline(never)];
21
22     unsafe {
23         return rust_get_num_cpus();
24     }
25
26     extern {
27         fn rust_get_num_cpus() -> libc::uintptr_t;
28     }
29 }
30
31 /// Get's the number of scheduler threads requested by the environment
32 /// either `RUST_THREADS` or `num_cpus`.
33 pub fn default_sched_threads() -> uint {
34     match os::getenv("RUST_THREADS") {
35         Some(nstr) => FromStr::from_str(nstr).unwrap(),
36         None => num_cpus()
37     }
38 }
39
40 pub fn dumb_println(s: &str) {
41     use io::WriterUtil;
42     let dbg = ::libc::STDERR_FILENO as ::io::fd_t;
43     dbg.write_str(s);
44     dbg.write_str("\n");
45 }
46
47 pub fn abort(msg: &str) -> ! {
48     let msg = if !msg.is_empty() { msg } else { "aborted" };
49     let hash = msg.iter().fold(0, |accum, val| accum + (val as uint) );
50     let quote = match hash % 10 {
51         0 => "
52 It was from the artists and poets that the pertinent answers came, and I
53 know that panic would have broken loose had they been able to compare notes.
54 As it was, lacking their original letters, I half suspected the compiler of
55 having asked leading questions, or of having edited the correspondence in
56 corroboration of what he had latently resolved to see.",
57         1 => "
58 There are not many persons who know what wonders are opened to them in the
59 stories and visions of their youth; for when as children we listen and dream,
60 we think but half-formed thoughts, and when as men we try to remember, we are
61 dulled and prosaic with the poison of life. But some of us awake in the night
62 with strange phantasms of enchanted hills and gardens, of fountains that sing
63 in the sun, of golden cliffs overhanging murmuring seas, of plains that stretch
64 down to sleeping cities of bronze and stone, and of shadowy companies of heroes
65 that ride caparisoned white horses along the edges of thick forests; and then
66 we know that we have looked back through the ivory gates into that world of
67 wonder which was ours before we were wise and unhappy.",
68         2 => "
69 Instead of the poems I had hoped for, there came only a shuddering blackness
70 and ineffable loneliness; and I saw at last a fearful truth which no one had
71 ever dared to breathe before — the unwhisperable secret of secrets — The fact
72 that this city of stone and stridor is not a sentient perpetuation of Old New
73 York as London is of Old London and Paris of Old Paris, but that it is in fact
74 quite dead, its sprawling body imperfectly embalmed and infested with queer
75 animate things which have nothing to do with it as it was in life.",
76         3 => "
77 The ocean ate the last of the land and poured into the smoking gulf, thereby
78 giving up all it had ever conquered. From the new-flooded lands it flowed
79 again, uncovering death and decay; and from its ancient and immemorial bed it
80 trickled loathsomely, uncovering nighted secrets of the years when Time was
81 young and the gods unborn. Above the waves rose weedy remembered spires. The
82 moon laid pale lilies of light on dead London, and Paris stood up from its damp
83 grave to be sanctified with star-dust. Then rose spires and monoliths that were
84 weedy but not remembered; terrible spires and monoliths of lands that men never
85 knew were lands...",
86         4 => "
87 There was a night when winds from unknown spaces whirled us irresistibly into
88 limitless vacum beyond all thought and entity. Perceptions of the most
89 maddeningly untransmissible sort thronged upon us; perceptions of infinity
90 which at the time convulsed us with joy, yet which are now partly lost to my
91 memory and partly incapable of presentation to others.",
92         _ => "You've met with a terrible fate, haven't you?"
93     };
94     rterrln!("%s", "");
95     rterrln!("%s", quote);
96     rterrln!("%s", "");
97     rterrln!("fatal runtime error: %s", msg);
98
99     abort();
100
101     fn abort() -> ! {
102         #[fixed_stack_segment]; #[inline(never)];
103         unsafe { libc::abort() }
104     }
105 }
106
107 pub fn set_exit_status(code: int) {
108     #[fixed_stack_segment]; #[inline(never)];
109     unsafe {
110         return rust_set_exit_status_newrt(code as libc::uintptr_t);
111     }
112
113     extern {
114         fn rust_set_exit_status_newrt(code: libc::uintptr_t);
115     }
116 }
117
118 pub fn get_exit_status() -> int {
119     #[fixed_stack_segment]; #[inline(never)];
120     unsafe {
121         return rust_get_exit_status_newrt() as int;
122     }
123
124     extern {
125         fn rust_get_exit_status_newrt() -> libc::uintptr_t;
126     }
127 }