Skip to content

Commit f9069ba

Browse files
committed
core::rt: Add LocalServices for thread-local language services
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.
1 parent 4eff313 commit f9069ba

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/libcore/rt/local_services.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Language-level runtime services that should reasonably expected
12+
//! to be available 'everywhere'. Local heaps, GC, unwinding,
13+
//! local storage, and logging. Even a 'freestanding' Rust would likely want
14+
//! to implement this.
15+
16+
//! Local services may exist in at least three different contexts:
17+
//! when running as a task, when running in the scheduler's context,
18+
//! or when running outside of a scheduler but with local services
19+
//! (freestanding rust with local services?).
20+
21+
use prelude::*;
22+
use super::sched::{Task, local_sched};
23+
24+
pub struct LocalServices {
25+
heap: LocalHeap,
26+
gc: GarbageCollector,
27+
storage: LocalStorage,
28+
logger: Logger,
29+
unwinder: Unwinder
30+
}
31+
32+
pub struct LocalHeap;
33+
pub struct GarbageCollector;
34+
pub struct LocalStorage;
35+
pub struct Logger;
36+
pub struct Unwinder;
37+
38+
impl LocalServices {
39+
pub fn new() -> LocalServices {
40+
LocalServices {
41+
heap: LocalHeap,
42+
gc: GarbageCollector,
43+
storage: LocalStorage,
44+
logger: Logger,
45+
unwinder: Unwinder
46+
}
47+
}
48+
}
49+
50+
/// Borrow a pointer to the installed local services.
51+
/// Fails (likely aborting the process) if local services are not available.
52+
pub fn borrow_local_services(f: &fn(&mut LocalServices)) {
53+
do local_sched::borrow |sched| {
54+
match sched.current_task {
55+
Some(~ref mut task) => {
56+
f(&mut task.local_services)
57+
}
58+
None => {
59+
fail!(~"no local services for schedulers yet")
60+
}
61+
}
62+
}
63+
}

src/libcore/rt/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ mod stack;
4848
mod context;
4949
mod thread;
5050
pub mod env;
51+
pub mod local_services;
5152

5253
/// Tools for testing the runtime
5354
#[cfg(test)]
@@ -97,7 +98,7 @@ pub fn start(main: *u8, _argc: int, _argv: **c_char, _crate_map: *u8) -> int {
9798
/// Different runtime services are available depending on context.
9899
#[deriving(Eq)]
99100
pub enum RuntimeContext {
100-
// Only default services, e.g. exchange heap
101+
// Only the exchange heap is available
101102
GlobalContext,
102103
// The scheduler may be accessed
103104
SchedulerContext,

src/libcore/rt/sched/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use super::work_queue::WorkQueue;
1616
use super::stack::{StackPool, StackSegment};
1717
use super::rtio::{EventLoop, EventLoopObject};
1818
use super::context::Context;
19+
use super::local_services::LocalServices;
1920
use cell::Cell;
2021

2122
#[cfg(test)] use super::uvio::UvEventLoop;
@@ -38,7 +39,7 @@ pub struct Scheduler {
3839
/// Always valid when a task is executing, otherwise not
3940
priv saved_context: Context,
4041
/// The currently executing task
41-
priv current_task: Option<~Task>,
42+
current_task: Option<~Task>,
4243
/// An action performed after a context switch on behalf of the
4344
/// code running before the context switch
4445
priv cleanup_job: Option<CleanupJob>
@@ -326,6 +327,8 @@ pub struct Task {
326327
/// These are always valid when the task is not running, unless
327328
/// the task is dead
328329
priv saved_context: Context,
330+
/// The heap, GC, unwinding, local storage, logging
331+
local_services: LocalServices
329332
}
330333

331334
pub impl Task {
@@ -337,6 +340,7 @@ pub impl Task {
337340
return Task {
338341
current_stack_segment: stack,
339342
saved_context: initial_context,
343+
local_services: LocalServices::new()
340344
};
341345
}
342346

0 commit comments

Comments
 (0)