|
| 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 | +} |
0 commit comments