core:rt:: Rename LocalServices to Task

This commit is contained in:
Brian Anderson 2013-05-19 01:04:01 -07:00
parent fa18a861fb
commit b0722c55f8
9 changed files with 67 additions and 78 deletions

View File

@ -67,7 +67,7 @@ pub fn log_type<T>(level: u32, object: &T) {
fn newsched_log_str(msg: ~str) {
unsafe {
match rt::local_services::unsafe_try_borrow_local_services() {
match rt::task::unsafe_try_borrow_local_task() {
Some(local) => {
// Use the available logger
(*local).logger.log(Left(msg));

View File

@ -31,14 +31,8 @@ access to the global heap. Unlike most of `rt` the global heap is
truly a global resource and generally operates independently of the
rest of the runtime.
All other runtime features are 'local', either thread-local or
task-local. Those critical to the functioning of the language are
defined in the module `local_services`. Local services are those which
are expected to be available to Rust code generally but rely on
thread- or task-local state. These currently include the local heap,
All other runtime features are task-local, including the local heap,
the garbage collector, local storage, logging and the stack unwinder.
Local services are primarily implemented for tasks, but may also
be implemented for use outside of tasks.
The relationship between `rt` and the rest of the core library is
not entirely clear yet and some modules will be moving into or
@ -67,7 +61,10 @@ use ptr::Ptr;
/// The global (exchange) heap.
pub mod global_heap;
/// The Scheduler and Coroutine types.
/// Implementations of language-critical runtime features like @.
pub mod task;
/// The coroutine task scheduler, built on the `io` event loop.
mod sched;
/// Thread-local access to the current Scheduler.
@ -77,9 +74,6 @@ pub mod local_sched;
#[path = "io/mod.rs"]
pub mod io;
/// Thread-local implementations of language-critical runtime features like @.
pub mod local_services;
/// The EventLoop and internal synchronous I/O interface.
mod rtio;

View File

@ -16,7 +16,7 @@ use super::work_queue::WorkQueue;
use super::stack::{StackPool, StackSegment};
use super::rtio::{EventLoop, EventLoopObject};
use super::context::Context;
use super::local_services::LocalServices;
use super::task::Task;
use cell::Cell;
// A more convenient name for external callers, e.g. `local_sched::take()`
@ -350,16 +350,16 @@ pub struct Coroutine {
/// the task is dead
priv saved_context: Context,
/// The heap, GC, unwinding, local storage, logging
local_services: LocalServices
task: Task
}
pub impl Coroutine {
fn new(stack_pool: &mut StackPool, start: ~fn()) -> Coroutine {
Coroutine::with_local(stack_pool, LocalServices::new(), start)
Coroutine::with_task(stack_pool, Task::new(), start)
}
fn with_local(stack_pool: &mut StackPool,
local_services: LocalServices,
fn with_task(stack_pool: &mut StackPool,
task: Task,
start: ~fn()) -> Coroutine {
let start = Coroutine::build_start_wrapper(start);
let mut stack = stack_pool.take_segment(MIN_STACK_SIZE);
@ -368,7 +368,7 @@ pub impl Coroutine {
return Coroutine {
current_stack_segment: stack,
saved_context: initial_context,
local_services: local_services
task: task
};
}
@ -385,7 +385,7 @@ pub impl Coroutine {
let sched = local_sched::unsafe_borrow();
let task = (*sched).current_task.get_mut_ref();
// FIXME #6141: shouldn't neet to put `start()` in another closure
task.local_services.run(||start());
task.task.run(||start());
}
let sched = local_sched::take();

View File

@ -13,11 +13,6 @@
//! local storage, and logging. Even a 'freestanding' Rust would likely want
//! to implement this.
//! Local services may exist in at least three different contexts:
//! when running as a task, when running in the scheduler's context,
//! or when running outside of a scheduler but with local services
//! (freestanding rust with local services?).
use prelude::*;
use libc::{c_void, uintptr_t};
use cast::transmute;
@ -25,7 +20,7 @@ use super::sched::local_sched;
use super::local_heap::LocalHeap;
use rt::logging::StdErrLogger;
pub struct LocalServices {
pub struct Task {
heap: LocalHeap,
gc: GarbageCollector,
storage: LocalStorage,
@ -41,9 +36,9 @@ pub struct Unwinder {
unwinding: bool,
}
impl LocalServices {
pub fn new() -> LocalServices {
LocalServices {
impl Task {
pub fn new() -> Task {
Task {
heap: LocalHeap::new(),
gc: GarbageCollector,
storage: LocalStorage(ptr::null(), None),
@ -53,8 +48,8 @@ impl LocalServices {
}
}
pub fn without_unwinding() -> LocalServices {
LocalServices {
pub fn without_unwinding() -> Task {
Task {
heap: LocalHeap::new(),
gc: GarbageCollector,
storage: LocalStorage(ptr::null(), None),
@ -66,9 +61,9 @@ impl LocalServices {
pub fn run(&mut self, f: &fn()) {
// This is just an assertion that `run` was called unsafely
// and this instance of LocalServices is still accessible.
do borrow_local_services |sched| {
assert!(ptr::ref_eq(sched, self));
// and this instance of Task is still accessible.
do borrow_local_task |task| {
assert!(ptr::ref_eq(task, self));
}
match self.unwinder {
@ -86,14 +81,14 @@ impl LocalServices {
/// Must be called manually before finalization to clean up
/// thread-local resources. Some of the routines here expect
/// LocalServices to be available recursively so this must be
/// called unsafely, without removing LocalServices from
/// Task to be available recursively so this must be
/// called unsafely, without removing Task from
/// thread-local-storage.
fn destroy(&mut self) {
// This is just an assertion that `destroy` was called unsafely
// and this instance of LocalServices is still accessible.
do borrow_local_services |sched| {
assert!(ptr::ref_eq(sched, self));
// and this instance of Task is still accessible.
do borrow_local_task |task| {
assert!(ptr::ref_eq(task, self));
}
match self.storage {
LocalStorage(ptr, Some(ref dtor)) => {
@ -105,7 +100,7 @@ impl LocalServices {
}
}
impl Drop for LocalServices {
impl Drop for Task {
fn finalize(&self) { assert!(self.destroyed) }
}
@ -156,11 +151,11 @@ impl Unwinder {
/// Borrow a pointer to the installed local services.
/// Fails (likely aborting the process) if local services are not available.
pub fn borrow_local_services(f: &fn(&mut LocalServices)) {
pub fn borrow_local_task(f: &fn(&mut Task)) {
do local_sched::borrow |sched| {
match sched.current_task {
Some(~ref mut task) => {
f(&mut task.local_services)
f(&mut task.task)
}
None => {
fail!("no local services for schedulers yet")
@ -169,10 +164,10 @@ pub fn borrow_local_services(f: &fn(&mut LocalServices)) {
}
}
pub unsafe fn unsafe_borrow_local_services() -> *mut LocalServices {
pub unsafe fn unsafe_borrow_local_task() -> *mut Task {
match (*local_sched::unsafe_borrow()).current_task {
Some(~ref mut task) => {
let s: *mut LocalServices = &mut task.local_services;
let s: *mut Task = &mut task.task;
return s;
}
None => {
@ -182,9 +177,9 @@ pub unsafe fn unsafe_borrow_local_services() -> *mut LocalServices {
}
}
pub unsafe fn unsafe_try_borrow_local_services() -> Option<*mut LocalServices> {
pub unsafe fn unsafe_try_borrow_local_task() -> Option<*mut Task> {
if local_sched::exists() {
Some(unsafe_borrow_local_services())
Some(unsafe_borrow_local_task())
} else {
None
}

View File

@ -13,7 +13,7 @@ use option::*;
use cell::Cell;
use result::{Result, Ok, Err};
use super::io::net::ip::{IpAddr, Ipv4};
use rt::local_services::LocalServices;
use rt::task::Task;
use rt::thread::Thread;
/// Creates a new scheduler in a new thread and runs a task in it,
@ -28,9 +28,9 @@ pub fn run_in_newsched_task(f: ~fn()) {
do run_in_bare_thread {
let mut sched = ~UvEventLoop::new_scheduler();
let task = ~Coroutine::with_local(&mut sched.stack_pool,
LocalServices::without_unwinding(),
f.take());
let task = ~Coroutine::with_task(&mut sched.stack_pool,
Task::without_unwinding(),
f.take());
sched.enqueue_task(task);
sched.run();
}
@ -41,9 +41,9 @@ pub fn spawntask(f: ~fn()) {
use super::sched::*;
let mut sched = local_sched::take();
let task = ~Coroutine::with_local(&mut sched.stack_pool,
LocalServices::without_unwinding(),
f);
let task = ~Coroutine::with_task(&mut sched.stack_pool,
Task::without_unwinding(),
f);
do sched.switch_running_tasks_and_then(task) |task| {
let task = Cell(task);
let sched = local_sched::take();
@ -56,9 +56,9 @@ pub fn spawntask_immediately(f: ~fn()) {
use super::sched::*;
let mut sched = local_sched::take();
let task = ~Coroutine::with_local(&mut sched.stack_pool,
LocalServices::without_unwinding(),
f);
let task = ~Coroutine::with_task(&mut sched.stack_pool,
Task::without_unwinding(),
f);
do sched.switch_running_tasks_and_then(task) |task| {
let task = Cell(task);
do local_sched::borrow |sched| {
@ -72,9 +72,9 @@ pub fn spawntask_later(f: ~fn()) {
use super::sched::*;
let mut sched = local_sched::take();
let task = ~Coroutine::with_local(&mut sched.stack_pool,
LocalServices::without_unwinding(),
f);
let task = ~Coroutine::with_task(&mut sched.stack_pool,
Task::without_unwinding(),
f);
sched.enqueue_task(task);
local_sched::put(sched);
@ -89,9 +89,9 @@ pub fn spawntask_random(f: ~fn()) {
let run_now: bool = Rand::rand(&mut rng);
let mut sched = local_sched::take();
let task = ~Coroutine::with_local(&mut sched.stack_pool,
LocalServices::without_unwinding(),
f);
let task = ~Coroutine::with_task(&mut sched.stack_pool,
Task::without_unwinding(),
f);
if run_now {
do sched.switch_running_tasks_and_then(task) |task| {
@ -155,9 +155,9 @@ pub fn spawntask_thread(f: ~fn()) -> Thread {
let f = Cell(f);
let thread = do Thread::start {
let mut sched = ~UvEventLoop::new_scheduler();
let task = ~Coroutine::with_local(&mut sched.stack_pool,
LocalServices::without_unwinding(),
f.take());
let task = ~Coroutine::with_task(&mut sched.stack_pool,
Task::without_unwinding(),
f.take());
sched.enqueue_task(task);
sched.run();
};

View File

@ -204,7 +204,7 @@ impl FailWithCause for &'static str {
pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
use option::Option;
use rt::{context, OldTaskContext, TaskContext};
use rt::local_services::{unsafe_borrow_local_services, Unwinder};
use rt::task::{unsafe_borrow_local_task, Unwinder};
let context = context();
match context {
@ -233,8 +233,8 @@ pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
gc::cleanup_stack_for_failure();
let local_services = unsafe_borrow_local_services();
let unwinder: &mut Option<Unwinder> = &mut (*local_services).unwinder;
let task = unsafe_borrow_local_task();
let unwinder: &mut Option<Unwinder> = &mut (*task).unwinder;
match *unwinder {
Some(ref mut unwinder) => unwinder.begin_unwind(),
None => abort!("failure without unwinder. aborting process")

View File

@ -18,7 +18,7 @@ use task::rt;
use local_data::LocalDataKey;
use super::rt::rust_task;
use rt::local_services::LocalStorage;
use rt::task::LocalStorage;
pub enum Handle {
OldHandle(*rust_task),
@ -28,15 +28,15 @@ pub enum Handle {
impl Handle {
pub fn new() -> Handle {
use rt::{context, OldTaskContext};
use rt::local_services::unsafe_borrow_local_services;
use rt::task::unsafe_borrow_local_task;
unsafe {
match context() {
OldTaskContext => {
OldHandle(rt::rust_get_task())
}
_ => {
let local_services = unsafe_borrow_local_services();
NewHandle(&mut (*local_services).storage)
let task = unsafe_borrow_local_task();
NewHandle(&mut (*task).storage)
}
}
}

View File

@ -504,7 +504,7 @@ pub fn failing() -> bool {
//! True if the running task has failed
use rt::{context, OldTaskContext};
use rt::local_services::borrow_local_services;
use rt::task::borrow_local_task;
match context() {
OldTaskContext => {
@ -514,7 +514,7 @@ pub fn failing() -> bool {
}
_ => {
let mut unwinding = false;
do borrow_local_services |local| {
do borrow_local_task |local| {
unwinding = match local.unwinder {
Some(unwinder) => {
unwinder.unwinding

View File

@ -17,7 +17,7 @@ use managed::raw::BoxRepr;
use str;
use sys;
use rt::{context, OldTaskContext};
use rt::local_services::borrow_local_services;
use rt::task::borrow_local_task;
use option::{Option, Some, None};
use io;
use rt::global_heap;
@ -243,8 +243,8 @@ pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
}
_ => {
let mut alloc = ::ptr::null();
do borrow_local_services |srv| {
alloc = srv.heap.alloc(td as *c_void, size as uint) as *c_char;
do borrow_local_task |task| {
alloc = task.heap.alloc(td as *c_void, size as uint) as *c_char;
}
return alloc;
}
@ -261,8 +261,8 @@ pub unsafe fn local_free(ptr: *c_char) {
rustrt::rust_upcall_free_noswitch(ptr);
}
_ => {
do borrow_local_services |srv| {
srv.heap.free(ptr as *c_void);
do borrow_local_task |task| {
task.heap.free(ptr as *c_void);
}
}
}