Skip to content

Commit

Permalink
core::rt: Add LocalServices for thread-local language services
Browse files Browse the repository at this point in the history
Things like the GC heap and unwinding are desirable everywhere the language
might be used, not just in tasks. All Rust code should have access to
LocalServices.
  • Loading branch information
brson committed Apr 21, 2013
1 parent 4eff313 commit f9069ba
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
63 changes: 63 additions & 0 deletions src/libcore/rt/local_services.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.

//! Language-level runtime services that should reasonably expected
//! to be available 'everywhere'. Local heaps, GC, unwinding,
//! 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 super::sched::{Task, local_sched};

pub struct LocalServices {
heap: LocalHeap,
gc: GarbageCollector,
storage: LocalStorage,
logger: Logger,
unwinder: Unwinder
}

pub struct LocalHeap;
pub struct GarbageCollector;
pub struct LocalStorage;
pub struct Logger;
pub struct Unwinder;

impl LocalServices {
pub fn new() -> LocalServices {
LocalServices {
heap: LocalHeap,
gc: GarbageCollector,
storage: LocalStorage,
logger: Logger,
unwinder: 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)) {
do local_sched::borrow |sched| {
match sched.current_task {
Some(~ref mut task) => {
f(&mut task.local_services)
}
None => {
fail!(~"no local services for schedulers yet")
}
}
}
}
3 changes: 2 additions & 1 deletion src/libcore/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ mod stack;
mod context;
mod thread;
pub mod env;
pub mod local_services;

/// Tools for testing the runtime
#[cfg(test)]
Expand Down Expand Up @@ -97,7 +98,7 @@ pub fn start(main: *u8, _argc: int, _argv: **c_char, _crate_map: *u8) -> int {
/// Different runtime services are available depending on context.
#[deriving(Eq)]
pub enum RuntimeContext {
// Only default services, e.g. exchange heap
// Only the exchange heap is available
GlobalContext,
// The scheduler may be accessed
SchedulerContext,
Expand Down
6 changes: 5 additions & 1 deletion src/libcore/rt/sched/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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 cell::Cell;

#[cfg(test)] use super::uvio::UvEventLoop;
Expand All @@ -38,7 +39,7 @@ pub struct Scheduler {
/// Always valid when a task is executing, otherwise not
priv saved_context: Context,
/// The currently executing task
priv current_task: Option<~Task>,
current_task: Option<~Task>,
/// An action performed after a context switch on behalf of the
/// code running before the context switch
priv cleanup_job: Option<CleanupJob>
Expand Down Expand Up @@ -326,6 +327,8 @@ pub struct Task {
/// These are always valid when the task is not running, unless
/// the task is dead
priv saved_context: Context,
/// The heap, GC, unwinding, local storage, logging
local_services: LocalServices
}

pub impl Task {
Expand All @@ -337,6 +340,7 @@ pub impl Task {
return Task {
current_stack_segment: stack,
saved_context: initial_context,
local_services: LocalServices::new()
};
}

Expand Down

0 comments on commit f9069ba

Please sign in to comment.