From 6987ad22e46f55b12d8749be7522f4578d227c62 Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Fri, 14 Nov 2014 16:30:16 -0800 Subject: [PATCH] Make most of std::rt private Previously, the entire runtime API surface was publicly exposed, but that is neither necessary nor desirable. This commit hides most of the module, using librustrt directly as needed. The arrangement will need to be revisited when rustrt is pulled into std. [breaking-change] --- src/libcollections/slice.rs | 8 ++++-- src/librustrt/local.rs | 4 ++- src/librustrt/mutex.rs | 10 ++++--- src/librustrt/task.rs | 4 ++- src/libstd/dynamic_lib.rs | 2 +- src/libstd/failure.rs | 2 +- src/libstd/io/stdio.rs | 8 +++--- src/libstd/lib.rs | 3 +- src/libstd/os.rs | 6 ++-- src/libstd/rt/backtrace.rs | 4 +-- src/libstd/rt/mod.rs | 14 ++++------ src/libstd/sys/common/helper_thread.rs | 6 ++-- src/libstd/sys/common/net.rs | 2 +- src/libstd/sys/unix/mod.rs | 2 +- src/libstd/sys/unix/pipe.rs | 2 +- src/libstd/sys/windows/mod.rs | 2 +- src/libstd/sys/windows/pipe.rs | 2 +- src/libstd/task.rs | 16 +++++------ src/libsync/comm/mod.rs | 8 ++++-- src/libsync/deque.rs | 2 +- src/test/run-pass/foreign-call-no-runtime.rs | 3 +- .../match-ref-binding-in-guard-3256.rs | 4 ++- src/test/run-pass/native-always-waits.rs | 28 ------------------- src/test/run-pass/writealias.rs | 3 +- 24 files changed, 64 insertions(+), 81 deletions(-) delete mode 100644 src/test/run-pass/native-always-waits.rs diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 4a54b361001ce..c3a248ce3185b 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -666,6 +666,8 @@ pub mod raw { #[cfg(test)] mod tests { + extern crate rustrt; + use std::cell::Cell; use std::default::Default; use std::mem; @@ -949,9 +951,9 @@ mod tests { #[test] fn test_swap_remove_noncopyable() { // Tests that we don't accidentally run destructors twice. - let mut v = vec![rt::exclusive::Exclusive::new(()), - rt::exclusive::Exclusive::new(()), - rt::exclusive::Exclusive::new(())]; + let mut v = vec![rustrt::exclusive::Exclusive::new(()), + rustrt::exclusive::Exclusive::new(()), + rustrt::exclusive::Exclusive::new(())]; let mut _e = v.swap_remove(0); assert_eq!(v.len(), 2); _e = v.swap_remove(1); diff --git a/src/librustrt/local.rs b/src/librustrt/local.rs index 8531f569a6b12..93c5508e0426f 100644 --- a/src/librustrt/local.rs +++ b/src/librustrt/local.rs @@ -52,8 +52,10 @@ impl Local> for Task { #[cfg(test)] mod test { + extern crate rustrt; + use std::prelude::*; - use std::rt::thread::Thread; + use rustrt::thread::Thread; use super::*; use task::Task; diff --git a/src/librustrt/mutex.rs b/src/librustrt/mutex.rs index 1c448736d3ec1..11f6015936329 100644 --- a/src/librustrt/mutex.rs +++ b/src/librustrt/mutex.rs @@ -33,7 +33,7 @@ //! # Example //! //! ```rust -//! use std::rt::mutex::{NativeMutex, StaticNativeMutex, NATIVE_MUTEX_INIT}; +//! use rustrt::mutex::{NativeMutex, StaticNativeMutex, NATIVE_MUTEX_INIT}; //! //! // Use a statically initialized mutex //! static LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT; @@ -108,7 +108,7 @@ impl StaticNativeMutex { /// # Example /// /// ```rust - /// use std::rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; + /// use rustrt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; /// static LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT; /// unsafe { /// let _guard = LOCK.lock(); @@ -225,7 +225,7 @@ impl NativeMutex { /// # Example /// /// ```rust - /// use std::rt::mutex::NativeMutex; + /// use rustrt::mutex::NativeMutex; /// unsafe { /// let mut lock = NativeMutex::new(); /// @@ -649,11 +649,13 @@ mod imp { #[cfg(test)] mod test { + extern crate rustrt; + use std::prelude::*; use std::mem::drop; use super::{StaticNativeMutex, NATIVE_MUTEX_INIT}; - use std::rt::thread::Thread; + use rustrt::thread::Thread; #[test] fn smoke_lock() { diff --git a/src/librustrt/task.rs b/src/librustrt/task.rs index 34c913c5bcb40..cec28a464f8b1 100644 --- a/src/librustrt/task.rs +++ b/src/librustrt/task.rs @@ -544,6 +544,8 @@ impl Death { #[cfg(test)] mod test { + extern crate rustrt; + use super::*; use std::prelude::*; use std::task; @@ -592,7 +594,7 @@ mod test { #[test] #[should_fail] fn test_begin_unwind() { - use std::rt::unwind::begin_unwind; + use rustrt::unwind::begin_unwind; begin_unwind("cause", &(file!(), line!())) } diff --git a/src/libstd/dynamic_lib.rs b/src/libstd/dynamic_lib.rs index 8bb82d5bc1e82..0f119d4448532 100644 --- a/src/libstd/dynamic_lib.rs +++ b/src/libstd/dynamic_lib.rs @@ -229,7 +229,7 @@ pub mod dl { } pub fn check_for_errors_in(f: || -> T) -> Result { - use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; + use rustrt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; static LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT; unsafe { // dlerror isn't thread safe, so we need to lock around this entire diff --git a/src/libstd/failure.rs b/src/libstd/failure.rs index 0775997435641..c23e043c17409 100644 --- a/src/libstd/failure.rs +++ b/src/libstd/failure.rs @@ -18,7 +18,7 @@ use kinds::Send; use option::{Some, None}; use result::Ok; use rt::backtrace; -use rt::{Stderr, Stdio}; +use rustrt::{Stderr, Stdio}; use rustrt::local::Local; use rustrt::task::Task; use str::Str; diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 362e80f9f12c3..7374668a69d83 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -40,9 +40,9 @@ use option::{Option, Some, None}; use boxed::Box; use sys::{fs, tty}; use result::{Ok, Err}; -use rt; -use rt::local::Local; -use rt::task::Task; +use rustrt; +use rustrt::local::Local; +use rustrt::task::Task; use slice::SlicePrelude; use str::StrPrelude; use uint; @@ -207,7 +207,7 @@ fn with_task_stdout(f: |&mut Writer| -> IoResult<()>) { local_stdout.replace(Some(my_stdout)); result } else { - let mut io = rt::Stdout; + let mut io = rustrt::Stdout; f(&mut io as &mut Writer) }; match result { diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index c27faea74bb8f..b35c49efdd80c 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -162,7 +162,6 @@ pub use core::result; pub use core::option; pub use alloc::boxed; - pub use alloc::rc; pub use core_collections::slice; @@ -247,7 +246,7 @@ pub mod fmt; #[path = "sys/common/mod.rs"] mod sys_common; -mod rt; +pub mod rt; mod failure; // A curious inner-module that's not exported that contains the binding diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 68ddabfd48f27..d7ba4877086ea 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -208,7 +208,7 @@ Accessing environment variables is not generally threadsafe. Serialize access through a global lock. */ fn with_env_lock(f: || -> T) -> T { - use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; + use rustrt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; static LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT; @@ -1039,9 +1039,9 @@ fn real_args_as_bytes() -> Vec> { target_os = "freebsd", target_os = "dragonfly"))] fn real_args_as_bytes() -> Vec> { - use rt; + use rustrt; - match rt::args::clone() { + match rustrt::args::clone() { Some(args) => args, None => panic!("process arguments not initialized") } diff --git a/src/libstd/rt/backtrace.rs b/src/libstd/rt/backtrace.rs index 11257d506b0e0..107518ef27c9d 100644 --- a/src/libstd/rt/backtrace.rs +++ b/src/libstd/rt/backtrace.rs @@ -238,7 +238,7 @@ mod imp { use mem; use option::{Some, None, Option}; use result::{Ok, Err}; - use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; + use rustrt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; /// As always - iOS on arm uses SjLj exceptions and /// _Unwind_Backtrace is even not available there. Still, @@ -667,7 +667,7 @@ mod imp { use option::{Some, None}; use path::Path; use result::{Ok, Err}; - use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; + use rustrt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; use slice::SlicePrelude; use str::StrPrelude; use dynamic_lib::DynamicLibrary; diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs index b6e57186afef1..21b4edb637581 100644 --- a/src/libstd/rt/mod.rs +++ b/src/libstd/rt/mod.rs @@ -66,9 +66,7 @@ pub use self::util::{default_sched_threads, min_stack, running_on_valgrind}; // Reexport functionality from librustrt and other crates underneath the // standard library which work together to create the entire runtime. pub use alloc::heap; -pub use rustrt::{task, local, mutex, exclusive, stack, args, thread}; -pub use rustrt::{Stdio, Stdout, Stderr, begin_unwind, begin_unwind_fmt}; -pub use rustrt::{at_exit, unwind, DEFAULT_ERROR_CODE}; +pub use rustrt::{begin_unwind, begin_unwind_fmt, at_exit}; // Simple backtrace functionality (to print on panic) pub mod backtrace; @@ -84,7 +82,7 @@ mod util; #[allow(experimental)] pub fn init(argc: int, argv: *const *const u8) { rustrt::init(argc, argv); - unsafe { unwind::register(failure::on_fail); } + unsafe { rustrt::unwind::register(failure::on_fail); } } #[cfg(any(windows, android))] @@ -147,19 +145,19 @@ pub fn start(argc: int, argv: *const *const u8, main: proc()) -> int { init(argc, argv); let mut exit_code = None; let mut main = Some(main); - let mut task = Task::new(Some((my_stack_bottom, my_stack_top)), - Some(rt::thread::main_guard_page())); + let mut task = box Task::new(Some((my_stack_bottom, my_stack_top)), + Some(rustrt::thread::main_guard_page())); task.name = Some(str::Slice("
")); drop(task.run(|| { unsafe { - rt::stack::record_os_managed_stack_bounds(my_stack_bottom, my_stack_top); + rustrt::stack::record_os_managed_stack_bounds(my_stack_bottom, my_stack_top); } (main.take().unwrap())(); exit_code = Some(os::get_exit_status()); }).destroy()); unsafe { rt::cleanup(); } // If the exit code wasn't set, then the task block must have panicked. - return exit_code.unwrap_or(rt::DEFAULT_ERROR_CODE); + return exit_code.unwrap_or(rustrt::DEFAULT_ERROR_CODE); } /// One-time runtime cleanup. diff --git a/src/libstd/sys/common/helper_thread.rs b/src/libstd/sys/common/helper_thread.rs index d7c286bf0b9f2..9508d8d92325b 100644 --- a/src/libstd/sys/common/helper_thread.rs +++ b/src/libstd/sys/common/helper_thread.rs @@ -22,8 +22,8 @@ use mem; use rustrt::bookkeeping; -use rt::mutex::StaticNativeMutex; -use rt; +use rustrt::mutex::StaticNativeMutex; +use rustrt; use cell::UnsafeCell; use sys::helper_signal; use prelude::*; @@ -83,7 +83,7 @@ impl Helper { self.lock.lock().signal() }); - rt::at_exit(proc() { self.shutdown() }); + rustrt::at_exit(proc() { self.shutdown() }); *self.initialized.get() = true; } } diff --git a/src/libstd/sys/common/net.rs b/src/libstd/sys/common/net.rs index 9b2b594a9c7cd..029fc85274261 100644 --- a/src/libstd/sys/common/net.rs +++ b/src/libstd/sys/common/net.rs @@ -16,7 +16,7 @@ use libc::{mod, c_char, c_int}; use mem; use num::Int; use ptr::{mod, null, null_mut}; -use rt::mutex; +use rustrt::mutex; use io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr}; use io::net::addrinfo; use io::{IoResult, IoError}; diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index 4db9e8a9df8b5..664a6a1e70c76 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -25,7 +25,7 @@ use sys_common::mkerr_libc; macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => ( static $name: Helper<$m> = Helper { - lock: ::rt::mutex::NATIVE_MUTEX_INIT, + lock: ::rustrt::mutex::NATIVE_MUTEX_INIT, chan: ::cell::UnsafeCell { value: 0 as *mut Sender<$m> }, signal: ::cell::UnsafeCell { value: 0 }, initialized: ::cell::UnsafeCell { value: false }, diff --git a/src/libstd/sys/unix/pipe.rs b/src/libstd/sys/unix/pipe.rs index 3fba06e0c7f1c..4d3469a9c24a8 100644 --- a/src/libstd/sys/unix/pipe.rs +++ b/src/libstd/sys/unix/pipe.rs @@ -12,7 +12,7 @@ use alloc::arc::Arc; use libc; use c_str::CString; use mem; -use rt::mutex; +use rustrt::mutex; use sync::atomic; use io::{mod, IoResult, IoError}; use prelude::*; diff --git a/src/libstd/sys/windows/mod.rs b/src/libstd/sys/windows/mod.rs index f316b2d8493f4..815ace21f879d 100644 --- a/src/libstd/sys/windows/mod.rs +++ b/src/libstd/sys/windows/mod.rs @@ -26,7 +26,7 @@ use sync::{Once, ONCE_INIT}; macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => ( static $name: Helper<$m> = Helper { - lock: ::rt::mutex::NATIVE_MUTEX_INIT, + lock: ::rustrt::mutex::NATIVE_MUTEX_INIT, chan: ::cell::UnsafeCell { value: 0 as *mut Sender<$m> }, signal: ::cell::UnsafeCell { value: 0 }, initialized: ::cell::UnsafeCell { value: false }, diff --git a/src/libstd/sys/windows/pipe.rs b/src/libstd/sys/windows/pipe.rs index e38202302fb0e..a623c2cd8e297 100644 --- a/src/libstd/sys/windows/pipe.rs +++ b/src/libstd/sys/windows/pipe.rs @@ -90,7 +90,7 @@ use c_str::CString; use mem; use ptr; use sync::atomic; -use rt::mutex; +use rustrt::mutex; use io::{mod, IoError, IoResult}; use prelude::*; diff --git a/src/libstd/task.rs b/src/libstd/task.rs index 8da32ba4b89cd..4f5f47e980c0d 100644 --- a/src/libstd/task.rs +++ b/src/libstd/task.rs @@ -50,9 +50,9 @@ use kinds::{Send, marker}; use option::{None, Some, Option}; use boxed::Box; use result::Result; -use rt::local::Local; -use rt::task; -use rt::task::Task; +use rustrt::local::Local; +use rustrt::task; +use rustrt::task::Task; use str::{Str, SendStr, IntoMaybeOwned}; use string::{String, ToString}; use sync::Future; @@ -142,13 +142,13 @@ impl TaskBuilder { stack_size: stack_size, }; if stdout.is_some() || stderr.is_some() { - spawner.spawn(opts, proc() { + Task::spawn(opts, proc() { let _ = stdout.map(stdio::set_stdout); let _ = stderr.map(stdio::set_stderr); f(); }) } else { - spawner.spawn(opts, f) + Task::spawn(opts, f) } } @@ -237,7 +237,7 @@ pub fn try_future(f: proc():Send -> T) -> Future Option { - use rt::task::Task; + use rustrt::task::Task; let task = Local::borrow(None::); match task.name { @@ -249,7 +249,7 @@ pub fn name() -> Option { /// Yield control to the task scheduler. #[unstable = "Name will change."] pub fn deschedule() { - use rt::task::Task; + use rustrt::task::Task; Task::yield_now(); } @@ -257,7 +257,7 @@ pub fn deschedule() { /// destructor that is run while unwinding the stack after a call to `panic!()`). #[unstable = "May move to a different module."] pub fn failing() -> bool { - use rt::task::Task; + use rustrt::task::Task; Local::borrow(None::).unwinder.unwinding() } diff --git a/src/libsync/comm/mod.rs b/src/libsync/comm/mod.rs index 02fdc69448ef7..3c7e46036d6fb 100644 --- a/src/libsync/comm/mod.rs +++ b/src/libsync/comm/mod.rs @@ -336,6 +336,8 @@ macro_rules! test ( mod $name { #![allow(unused_imports)] + extern crate rustrt; + use std::prelude::*; use comm::*; @@ -1512,7 +1514,7 @@ mod test { }) test!(fn sends_off_the_runtime() { - use std::rt::thread::Thread; + use rustrt::thread::Thread; let (tx, rx) = channel(); let t = Thread::start(proc() { @@ -1527,7 +1529,7 @@ mod test { }) test!(fn try_recvs_off_the_runtime() { - use std::rt::thread::Thread; + use rustrt::thread::Thread; let (tx, rx) = channel(); let (cdone, pdone) = channel(); @@ -1977,7 +1979,7 @@ mod sync_tests { }) test!(fn try_recvs_off_the_runtime() { - use std::rt::thread::Thread; + use rustrt::thread::Thread; let (tx, rx) = sync_channel::<()>(0); let (cdone, pdone) = channel(); diff --git a/src/libsync/deque.rs b/src/libsync/deque.rs index 2f5c455556c8f..1fece03b27364 100644 --- a/src/libsync/deque.rs +++ b/src/libsync/deque.rs @@ -414,7 +414,7 @@ mod tests { use super::{Data, BufferPool, Abort, Empty, Worker, Stealer}; use std::mem; - use std::rt::thread::Thread; + use rustrt::thread::Thread; use std::rand; use std::rand::Rng; use atomic::{AtomicBool, INIT_ATOMIC_BOOL, SeqCst, diff --git a/src/test/run-pass/foreign-call-no-runtime.rs b/src/test/run-pass/foreign-call-no-runtime.rs index 9dd52dfb6da0e..af36387f06c2c 100644 --- a/src/test/run-pass/foreign-call-no-runtime.rs +++ b/src/test/run-pass/foreign-call-no-runtime.rs @@ -9,9 +9,10 @@ // except according to those terms. extern crate libc; +extern crate rustrt; use std::mem; -use std::rt::thread::Thread; +use rustrt::thread::Thread; #[link(name = "rust_test_helpers")] extern { diff --git a/src/test/run-pass/match-ref-binding-in-guard-3256.rs b/src/test/run-pass/match-ref-binding-in-guard-3256.rs index 243c87c0eeb00..ac783961b508e 100644 --- a/src/test/run-pass/match-ref-binding-in-guard-3256.rs +++ b/src/test/run-pass/match-ref-binding-in-guard-3256.rs @@ -8,9 +8,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +extern crate rustrt; + pub fn main() { unsafe { - let x = Some(::std::rt::exclusive::Exclusive::new(true)); + let x = Some(::rustrt::exclusive::Exclusive::new(true)); match x { Some(ref z) if *z.lock() => { assert!(*z.lock()); diff --git a/src/test/run-pass/native-always-waits.rs b/src/test/run-pass/native-always-waits.rs deleted file mode 100644 index ea3eb29964883..0000000000000 --- a/src/test/run-pass/native-always-waits.rs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2014 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// ignore-android (FIXME #11419) - -extern crate native; - -static mut set: bool = false; - -#[start] -fn start(argc: int, argv: *const *const u8) -> int { - // make sure that native::start always waits for all children to finish - native::start(argc, argv, proc() { - spawn(proc() { - unsafe { set = true; } - }); - }); - - // if we didn't set the global, then return a nonzero code - if unsafe {set} {0} else {1} -} diff --git a/src/test/run-pass/writealias.rs b/src/test/run-pass/writealias.rs index ae49c07093b11..c8d281a791c90 100644 --- a/src/test/run-pass/writealias.rs +++ b/src/test/run-pass/writealias.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +extern crate rustrt; struct Point {x: int, y: int, z: int} @@ -15,7 +16,7 @@ fn f(p: &mut Point) { p.z = 13; } pub fn main() { unsafe { - let x = Some(::std::rt::exclusive::Exclusive::new(true)); + let x = Some(::rustrt::exclusive::Exclusive::new(true)); match x { Some(ref z) if *z.lock() => { assert!(*z.lock());