2013-02-03 20:15:43 -06: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.
|
|
|
|
|
2013-04-24 22:20:03 -05:00
|
|
|
/*! The Rust runtime, including the scheduler and I/O interface
|
|
|
|
|
|
|
|
# XXX
|
|
|
|
|
|
|
|
* Unsafe uses of borrowed pointers should just use unsafe pointers
|
|
|
|
* Unwinding is not wired up correctly
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-04-23 21:21:37 -05:00
|
|
|
|
2013-03-24 20:59:04 -05:00
|
|
|
#[doc(hidden)];
|
|
|
|
|
2013-03-15 20:06:19 -05:00
|
|
|
use libc::c_char;
|
2013-03-12 15:05:45 -05:00
|
|
|
|
2013-04-15 15:40:36 -05:00
|
|
|
#[path = "sched/mod.rs"]
|
2013-02-03 20:15:43 -06:00
|
|
|
mod sched;
|
2013-04-24 22:20:03 -05:00
|
|
|
pub mod rtio;
|
2013-03-12 16:06:19 -05:00
|
|
|
pub mod uvll;
|
2013-02-03 20:15:43 -06:00
|
|
|
mod uvio;
|
2013-03-15 20:07:36 -05:00
|
|
|
#[path = "uv/mod.rs"]
|
2013-02-03 20:15:43 -06:00
|
|
|
mod uv;
|
2013-03-13 22:02:48 -05:00
|
|
|
#[path = "io/mod.rs"]
|
|
|
|
mod io;
|
2013-02-03 20:15:43 -06:00
|
|
|
// FIXME #5248: The import in `sched` doesn't resolve unless this is pub!
|
|
|
|
pub mod thread_local_storage;
|
|
|
|
mod work_queue;
|
|
|
|
mod stack;
|
|
|
|
mod context;
|
|
|
|
mod thread;
|
2013-03-12 14:23:24 -05:00
|
|
|
pub mod env;
|
2013-04-21 18:28:17 -05:00
|
|
|
pub mod local_services;
|
2013-04-21 21:03:52 -05:00
|
|
|
mod local_heap;
|
2013-03-15 20:06:19 -05:00
|
|
|
|
2013-04-20 02:33:49 -05:00
|
|
|
/// Tools for testing the runtime
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod test;
|
|
|
|
|
2013-03-30 21:59:21 -05:00
|
|
|
pub fn start(main: *u8, _argc: int, _argv: **c_char, _crate_map: *u8) -> int {
|
2013-05-08 01:01:02 -05:00
|
|
|
|
2013-03-30 21:59:21 -05:00
|
|
|
use self::sched::{Scheduler, Task};
|
|
|
|
use self::uvio::UvEventLoop;
|
2013-05-08 01:01:02 -05:00
|
|
|
use sys::Closure;
|
|
|
|
use ptr;
|
|
|
|
use cast;
|
2013-03-30 21:59:21 -05:00
|
|
|
|
|
|
|
let loop_ = ~UvEventLoop::new();
|
|
|
|
let mut sched = ~Scheduler::new(loop_);
|
2013-05-08 01:01:02 -05:00
|
|
|
|
2013-03-30 21:59:21 -05:00
|
|
|
let main_task = ~do Task::new(&mut sched.stack_pool) {
|
2013-05-08 01:01:02 -05:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// `main` is an `fn() -> ()` that doesn't take an environment
|
|
|
|
// XXX: Could also call this as an `extern "Rust" fn` once they work
|
|
|
|
let main = Closure {
|
|
|
|
code: main as *(),
|
|
|
|
env: ptr::null(),
|
|
|
|
};
|
|
|
|
let mainfn: &fn() = cast::transmute(main);
|
|
|
|
|
|
|
|
mainfn();
|
|
|
|
}
|
2013-03-30 21:59:21 -05:00
|
|
|
};
|
2013-05-08 01:01:02 -05:00
|
|
|
|
2013-03-30 21:59:21 -05:00
|
|
|
sched.task_queue.push_back(main_task);
|
|
|
|
sched.run();
|
|
|
|
|
2013-05-08 01:01:02 -05:00
|
|
|
return 0;
|
2013-03-30 21:59:21 -05:00
|
|
|
}
|
2013-03-27 17:24:50 -05:00
|
|
|
|
|
|
|
/// Possible contexts in which Rust code may be executing.
|
|
|
|
/// Different runtime services are available depending on context.
|
|
|
|
#[deriving(Eq)]
|
|
|
|
pub enum RuntimeContext {
|
2013-04-21 18:28:17 -05:00
|
|
|
// Only the exchange heap is available
|
2013-03-27 17:24:50 -05:00
|
|
|
GlobalContext,
|
|
|
|
// The scheduler may be accessed
|
|
|
|
SchedulerContext,
|
|
|
|
// Full task services, e.g. local heap, unwinding
|
|
|
|
TaskContext,
|
|
|
|
// Running in an old-style task
|
|
|
|
OldTaskContext
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn context() -> RuntimeContext {
|
|
|
|
|
|
|
|
use task::rt::rust_task;
|
2013-04-18 21:32:32 -05:00
|
|
|
use self::sched::local_sched;
|
2013-03-27 17:24:50 -05:00
|
|
|
|
|
|
|
// XXX: Hitting TLS twice to check if the scheduler exists
|
|
|
|
// then to check for the task is not good for perf
|
|
|
|
if unsafe { rust_try_get_task().is_not_null() } {
|
|
|
|
return OldTaskContext;
|
|
|
|
} else {
|
2013-04-18 21:32:32 -05:00
|
|
|
if local_sched::exists() {
|
2013-03-27 17:24:50 -05:00
|
|
|
let context = ::cell::empty_cell();
|
2013-04-18 21:32:32 -05:00
|
|
|
do local_sched::borrow |sched| {
|
2013-03-27 17:24:50 -05:00
|
|
|
if sched.in_task_context() {
|
|
|
|
context.put_back(TaskContext);
|
|
|
|
} else {
|
|
|
|
context.put_back(SchedulerContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return context.take();
|
|
|
|
} else {
|
|
|
|
return GlobalContext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub extern {
|
|
|
|
#[rust_stack]
|
|
|
|
fn rust_try_get_task() -> *rust_task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_context() {
|
|
|
|
use unstable::run_in_bare_thread;
|
2013-04-18 21:32:32 -05:00
|
|
|
use self::sched::{local_sched, Task};
|
2013-03-27 17:24:50 -05:00
|
|
|
use self::uvio::UvEventLoop;
|
|
|
|
use cell::Cell;
|
|
|
|
|
|
|
|
assert!(context() == OldTaskContext);
|
|
|
|
do run_in_bare_thread {
|
|
|
|
assert!(context() == GlobalContext);
|
|
|
|
let mut sched = ~UvEventLoop::new_scheduler();
|
|
|
|
let task = ~do Task::new(&mut sched.stack_pool) {
|
|
|
|
assert!(context() == TaskContext);
|
2013-04-18 21:32:32 -05:00
|
|
|
let sched = local_sched::take();
|
2013-03-27 17:24:50 -05:00
|
|
|
do sched.deschedule_running_task_and_then() |task| {
|
|
|
|
assert!(context() == SchedulerContext);
|
|
|
|
let task = Cell(task);
|
2013-04-18 21:32:32 -05:00
|
|
|
do local_sched::borrow |sched| {
|
2013-03-27 17:24:50 -05:00
|
|
|
sched.task_queue.push_back(task.take());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sched.task_queue.push_back(task);
|
|
|
|
sched.run();
|
|
|
|
}
|
|
|
|
}
|