2013-02-03 18:15:43 -08:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
2014-12-14 22:52:55 -08:00
|
|
|
//! Runtime services
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
2014-12-14 22:52:55 -08:00
|
|
|
//! The `rt` module provides a narrow set of runtime services,
|
|
|
|
//! including the global heap (exported in `heap`) and unwinding and
|
|
|
|
//! backtrace support. The APIs in this module are highly unstable,
|
|
|
|
//! and should be considered as private implementation details for the
|
|
|
|
//! time being.
|
2013-04-23 19:21:37 -07:00
|
|
|
|
2014-06-30 17:22:40 -07:00
|
|
|
#![experimental]
|
|
|
|
|
2014-01-26 03:43:42 -05:00
|
|
|
// FIXME: this should not be here.
|
2014-10-27 15:37:07 -07:00
|
|
|
#![allow(missing_docs)]
|
2013-03-24 18:59:04 -07:00
|
|
|
|
2014-11-14 13:40:34 -08:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2014-11-23 19:21:17 -08:00
|
|
|
use kinds::Send;
|
2014-12-14 00:05:32 -08:00
|
|
|
use ops::FnOnce;
|
2014-12-06 18:34:37 -08:00
|
|
|
use sys;
|
2014-12-22 09:04:23 -08:00
|
|
|
use thunk::Thunk;
|
2013-03-12 13:05:45 -07:00
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
// Reexport some of our utilities which are expected by other crates.
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
pub use self::util::{default_sched_threads, min_stack, running_on_valgrind};
|
2014-11-23 19:21:17 -08:00
|
|
|
pub use self::unwind::{begin_unwind, begin_unwind_fmt};
|
2014-03-19 00:42:02 -07:00
|
|
|
|
2014-11-23 19:21:17 -08:00
|
|
|
// Reexport some functionality from liballoc.
|
2014-10-24 17:34:57 -04:00
|
|
|
pub use alloc::heap;
|
2013-05-19 01:13:53 -07:00
|
|
|
|
2014-10-09 15:17:22 -04:00
|
|
|
// Simple backtrace functionality (to print on panic)
|
2014-02-05 15:19:40 -08:00
|
|
|
pub mod backtrace;
|
|
|
|
|
2014-11-23 19:21:17 -08:00
|
|
|
// Internals
|
|
|
|
mod macros;
|
|
|
|
|
|
|
|
// These should be refactored/moved/made private over time
|
|
|
|
pub mod util;
|
|
|
|
pub mod unwind;
|
2014-12-10 07:37:56 -08:00
|
|
|
pub mod args;
|
2014-11-23 19:21:17 -08:00
|
|
|
|
|
|
|
mod at_exit_imp;
|
|
|
|
mod libunwind;
|
|
|
|
|
2014-12-26 16:04:27 -05:00
|
|
|
/// The default error code of the rust runtime if the main thread panics instead
|
2014-11-23 19:21:17 -08:00
|
|
|
/// of exiting cleanly.
|
|
|
|
pub const DEFAULT_ERROR_CODE: int = 101;
|
2013-05-19 14:39:46 -07:00
|
|
|
|
2014-11-14 13:55:57 -08:00
|
|
|
#[cfg(any(windows, android))]
|
2014-12-10 07:37:56 -08:00
|
|
|
const OS_DEFAULT_STACK_ESTIMATE: uint = 1 << 20;
|
2014-11-14 13:55:57 -08:00
|
|
|
#[cfg(all(unix, not(android)))]
|
2014-12-10 07:37:56 -08:00
|
|
|
const OS_DEFAULT_STACK_ESTIMATE: uint = 2 * (1 << 20);
|
2014-11-14 13:55:57 -08:00
|
|
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
#[lang = "start"]
|
|
|
|
fn lang_start(main: *const u8, argc: int, argv: *const *const u8) -> int {
|
2014-12-22 09:04:23 -08:00
|
|
|
use prelude::v1::*;
|
|
|
|
|
2014-11-14 14:38:41 -08:00
|
|
|
use mem;
|
2014-12-22 09:04:23 -08:00
|
|
|
use os;
|
2014-11-14 13:55:57 -08:00
|
|
|
use rt;
|
2014-12-22 09:04:23 -08:00
|
|
|
use sys_common::thread_info::{mod, NewThread};
|
|
|
|
use sys_common;
|
|
|
|
use thread::Thread;
|
2014-11-14 13:55:57 -08:00
|
|
|
|
|
|
|
let something_around_the_top_of_the_stack = 1;
|
|
|
|
let addr = &something_around_the_top_of_the_stack as *const int;
|
|
|
|
let my_stack_top = addr as uint;
|
|
|
|
|
|
|
|
// FIXME #11359 we just assume that this thread has a stack of a
|
|
|
|
// certain size, and estimate that there's at most 20KB of stack
|
|
|
|
// frames above our current position.
|
|
|
|
let my_stack_bottom = my_stack_top + 20000 - OS_DEFAULT_STACK_ESTIMATE;
|
|
|
|
|
2014-12-10 07:37:56 -08:00
|
|
|
let failed = unsafe {
|
|
|
|
// First, make sure we don't trigger any __morestack overflow checks,
|
|
|
|
// and next set up our stack to have a guard page and run through our
|
|
|
|
// own fault handlers if we hit it.
|
|
|
|
sys_common::stack::record_os_managed_stack_bounds(my_stack_bottom,
|
|
|
|
my_stack_top);
|
|
|
|
sys::thread::guard::init();
|
|
|
|
sys::stack_overflow::init();
|
|
|
|
|
|
|
|
// Next, set up the current Thread with the guard information we just
|
|
|
|
// created. Note that this isn't necessary in general for new threads,
|
|
|
|
// but we just do this to name the main thread and to give it correct
|
|
|
|
// info about the stack bounds.
|
2014-12-10 19:46:38 -08:00
|
|
|
let thread: Thread = NewThread::new(Some("<main>".to_string()));
|
2014-12-10 07:37:56 -08:00
|
|
|
thread_info::set((my_stack_bottom, my_stack_top),
|
|
|
|
sys::thread::guard::main(),
|
|
|
|
thread);
|
|
|
|
|
|
|
|
// By default, some platforms will send a *signal* when a EPIPE error
|
|
|
|
// would otherwise be delivered. This runtime doesn't install a SIGPIPE
|
|
|
|
// handler, causing it to kill the program, which isn't exactly what we
|
|
|
|
// want!
|
|
|
|
//
|
|
|
|
// Hence, we set SIGPIPE to ignore when the program starts up in order
|
|
|
|
// to prevent this problem.
|
|
|
|
#[cfg(windows)] fn ignore_sigpipe() {}
|
|
|
|
#[cfg(unix)] fn ignore_sigpipe() {
|
|
|
|
use libc;
|
|
|
|
use libc::funcs::posix01::signal::signal;
|
|
|
|
unsafe {
|
|
|
|
assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != -1);
|
|
|
|
}
|
2014-11-14 13:55:57 -08:00
|
|
|
}
|
2014-12-10 07:37:56 -08:00
|
|
|
ignore_sigpipe();
|
|
|
|
|
|
|
|
// Store our args if necessary in a squirreled away location
|
|
|
|
args::init(argc, argv);
|
|
|
|
|
|
|
|
// And finally, let's run some code!
|
|
|
|
let res = unwind::try(|| {
|
|
|
|
let main: fn() = mem::transmute(main);
|
|
|
|
main();
|
2014-12-06 18:34:37 -08:00
|
|
|
});
|
|
|
|
cleanup();
|
2014-12-10 07:37:56 -08:00
|
|
|
res.is_err()
|
|
|
|
};
|
|
|
|
|
|
|
|
// If the exit code wasn't set, then the try block must have panicked.
|
|
|
|
if failed {
|
|
|
|
rt::DEFAULT_ERROR_CODE
|
|
|
|
} else {
|
|
|
|
os::get_exit_status()
|
2014-12-06 18:34:37 -08:00
|
|
|
}
|
2014-11-23 19:21:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
2014-12-26 16:04:27 -05:00
|
|
|
/// after all other threads have exited.
|
2014-11-23 19:21:17 -08:00
|
|
|
///
|
2014-12-26 16:04:27 -05:00
|
|
|
/// The procedure is *not* executed with a local `Thread` available to it, so
|
2014-11-23 19:21:17 -08:00
|
|
|
/// 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.
|
2014-12-14 00:05:32 -08:00
|
|
|
pub fn at_exit<F:FnOnce()+Send>(f: F) {
|
|
|
|
at_exit_imp::push(Thunk::new(f));
|
2014-11-14 13:55:57 -08:00
|
|
|
}
|
|
|
|
|
2013-06-19 01:08:47 -07:00
|
|
|
/// One-time runtime cleanup.
|
2013-11-25 18:27:27 -08:00
|
|
|
///
|
|
|
|
/// 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() {
|
2014-11-23 19:21:17 -08:00
|
|
|
args::cleanup();
|
2014-12-06 18:34:37 -08:00
|
|
|
sys::stack_overflow::cleanup();
|
2014-12-18 23:29:30 -08:00
|
|
|
// FIXME: (#20012): the resources being cleaned up by at_exit
|
|
|
|
// currently are not prepared for cleanup to happen asynchronously
|
|
|
|
// with detached threads using the resources; for now, we leak.
|
|
|
|
// at_exit_imp::cleanup();
|
2013-06-17 23:18:20 -07:00
|
|
|
}
|