lib.rs 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

11
#![crate_name = "rustrt"]
12 13 14 15 16
#![license = "MIT/ASL2"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
       html_favicon_url = "http://www.rust-lang.org/favicon.ico",
17
       html_root_url = "http://doc.rust-lang.org/master/")]
18 19

#![feature(macro_rules, phase, globs, thread_local, managed_boxes, asm)]
20
#![feature(linkage, lang_items, unsafe_destructor, default_type_params)]
21
#![feature(import_shadowing)]
22 23 24
#![no_std]
#![experimental]

A
Alex Crichton 已提交
25
#[phase(plugin, link)] extern crate core;
26 27 28 29
extern crate alloc;
extern crate libc;
extern crate collections;

W
wickerwaka 已提交
30
#[cfg(test)] extern crate "rustrt" as realrustrt;
31 32
#[cfg(test)] extern crate test;
#[cfg(test)] extern crate native;
33

A
Alex Crichton 已提交
34
#[cfg(test)] #[phase(plugin, link)] extern crate std;
35 36

pub use self::util::{Stdio, Stdout, Stderr};
37
pub use self::unwind::{begin_unwind, begin_unwind_fmt};
38 39 40

use core::prelude::*;

41
use alloc::boxed::Box;
42 43 44 45 46 47 48 49 50 51 52 53 54 55
use core::any::Any;

use task::{Task, BlockedTask, TaskOpts};

mod macros;

mod at_exit_imp;
mod local_ptr;
mod thread_local_storage;
mod util;
mod libunwind;

pub mod args;
pub mod bookkeeping;
A
Alex Crichton 已提交
56
pub mod c_str;
57 58 59 60 61 62 63 64
pub mod exclusive;
pub mod local;
pub mod local_data;
pub mod local_heap;
pub mod mutex;
pub mod rtio;
pub mod stack;
pub mod task;
A
Alex Crichton 已提交
65
pub mod thread;
66 67 68 69 70 71 72 73 74 75 76 77
pub mod unwind;

/// The interface to the current runtime.
///
/// This trait is used as the abstraction between 1:1 and M:N scheduling. The
/// two independent crates, libnative and libgreen, both have objects which
/// implement this trait. The goal of this trait is to encompass all the
/// fundamental differences in functionality between the 1:1 and M:N runtime
/// modes.
pub trait Runtime {
    // Necessary scheduling functions, used for channels and blocking I/O
    // (sometimes).
78 79 80 81 82
    fn yield_now(self: Box<Self>, cur_task: Box<Task>);
    fn maybe_yield(self: Box<Self>, cur_task: Box<Task>);
    fn deschedule(self: Box<Self>,
                  times: uint,
                  cur_task: Box<Task>,
83
                  f: |BlockedTask| -> Result<(), BlockedTask>);
84
    fn reawaken(self: Box<Self>, to_wake: Box<Task>);
85 86 87

    // Miscellaneous calls which are very different depending on what context
    // you're in.
88
    fn spawn_sibling(self: Box<Self>,
89 90 91 92 93 94 95 96 97
                     cur_task: Box<Task>,
                     opts: TaskOpts,
                     f: proc():Send);
    fn local_io<'a>(&'a mut self) -> Option<rtio::LocalIo<'a>>;
    /// The (low, high) edges of the current stack.
    fn stack_bounds(&self) -> (uint, uint); // (lo, hi)
    fn can_block(&self) -> bool;

    // FIXME: This is a serious code smell and this should not exist at all.
98
    fn wrap(self: Box<Self>) -> Box<Any+'static>;
99 100 101 102 103 104 105 106 107 108 109
}

/// The default error code of the rust runtime if the main task fails instead
/// of exiting cleanly.
pub static DEFAULT_ERROR_CODE: int = 101;

/// One-time runtime initialization.
///
/// Initializes global state, including frobbing
/// the crate's logging flags, registering GC
/// metadata, and storing the process arguments.
110
pub fn init(argc: int, argv: *const *const u8) {
111 112 113 114 115 116 117 118 119 120 121
    // FIXME: Derefing these pointers is not safe.
    // Need to propagate the unsafety to `start`.
    unsafe {
        args::init(argc, argv);
        local_ptr::init();
        at_exit_imp::init();
    }

    // FIXME(#14344) this shouldn't be necessary
    collections::fixme_14344_be_sure_to_link_to_collections();
    alloc::fixme_14344_be_sure_to_link_to_collections();
122
    libc::issue_14344_workaround();
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
}

/// Enqueues a procedure to run when the runtime is cleaned up
///
/// The procedure passed to this function will be executed as part of the
/// runtime cleanup phase. For normal rust programs, this means that it will run
/// after all other tasks have exited.
///
/// The procedure is *not* executed with a local `Task` available to it, so
/// primitives like logging, I/O, channels, spawning, etc, are *not* available.
/// This is meant for "bare bones" usage to clean up runtime details, this is
/// not meant as a general-purpose "let's clean everything up" function.
///
/// It is forbidden for procedures to register more `at_exit` handlers when they
/// are running, and doing so will lead to a process abort.
pub fn at_exit(f: proc():Send) {
    at_exit_imp::push(f);
}

/// One-time runtime cleanup.
///
/// This function is unsafe because it performs no checks to ensure that the
/// runtime has completely ceased running. It is the responsibility of the
/// caller to ensure that the runtime is entirely shut down and nothing will be
/// poking around at the internal components.
///
/// Invoking cleanup while portions of the runtime are still in use may cause
/// undefined behavior.
pub unsafe fn cleanup() {
    bookkeeping::wait_for_other_tasks();
    at_exit_imp::run();
    args::cleanup();
    local_ptr::cleanup();
}

// FIXME: these probably shouldn't be public...
#[doc(hidden)]
pub mod shouldnt_be_public {
    #[cfg(not(test))]
    pub use super::local_ptr::native::maybe_tls_key;
S
Steven Fackler 已提交
163
    #[cfg(all(not(windows), not(target_os = "android"), not(target_os = "ios")))]
164 165 166 167 168 169 170
    pub use super::local_ptr::compiled::RT_TLS_PTR;
}

#[cfg(not(test))]
mod std {
    pub use core::{fmt, option, cmp};
}