about summary refs log tree commit diff
path: root/src/libstd/rt
diff options
context:
space:
mode:
authorAaron Turon <aturon@mozilla.com>2014-12-06 18:34:37 -0800
committerAaron Turon <aturon@mozilla.com>2014-12-18 23:31:51 -0800
commit43ae4b3301cc0605839778ecf59effb32b752e33 (patch)
treeaa111f5adc1eaa1e996847e1437d1b1b40821ce0 /src/libstd/rt
parent14c1a103bc3f78721df1dc860a75a477c8275e3a (diff)
downloadrust-43ae4b3301cc0605839778ecf59effb32b752e33.tar.gz
rust-43ae4b3301cc0605839778ecf59effb32b752e33.zip
Fallout from new thread API
Diffstat (limited to 'src/libstd/rt')
-rw-r--r--src/libstd/rt/at_exit_imp.rs33
-rw-r--r--src/libstd/rt/backtrace.rs4
-rw-r--r--src/libstd/rt/mod.rs38
-rw-r--r--src/libstd/rt/unwind.rs2
-rw-r--r--src/libstd/rt/util.rs12
5 files changed, 33 insertions, 56 deletions
diff --git a/src/libstd/rt/at_exit_imp.rs b/src/libstd/rt/at_exit_imp.rs
index b8012134c9e..1b97a01146c 100644
--- a/src/libstd/rt/at_exit_imp.rs
+++ b/src/libstd/rt/at_exit_imp.rs
@@ -14,7 +14,6 @@
 
 use core::prelude::*;
 
-use libc;
 use boxed::Box;
 use vec::Vec;
 use sync::{Mutex, atomic, Once, ONCE_INIT};
@@ -25,31 +24,30 @@ type Queue = Mutex<Vec<Thunk>>;
 
 static INIT: Once = ONCE_INIT;
 static QUEUE: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
-static RUNNING: atomic::AtomicBool = atomic::INIT_ATOMIC_BOOL;
 
 fn init() {
     let state: Box<Queue> = box Mutex::new(Vec::new());
     unsafe {
         QUEUE.store(mem::transmute(state), atomic::SeqCst);
-        libc::atexit(run);
+
+        // FIXME: switch this to use atexit as below. Currently this
+        // segfaults (the queue's memory is mysteriously gone), so
+        // instead the cleanup is tied to the `std::rt` entry point.
+        //
+        // ::libc::atexit(cleanup);
     }
 }
 
-// Note: this is private and so can only be called via atexit above,
-// which guarantees initialization.
-extern fn run() {
-    let cur = unsafe {
-        rtassert!(!RUNNING.load(atomic::SeqCst));
+pub fn cleanup() {
+    unsafe {
         let queue = QUEUE.swap(0, atomic::SeqCst);
-        rtassert!(queue != 0);
-
-        let queue: Box<Queue> = mem::transmute(queue);
-        let v = mem::replace(&mut *queue.lock(), Vec::new());
-        v
-    };
-
-    for to_run in cur.into_iter() {
-        to_run.invoke(());
+        if queue != 0 {
+            let queue: Box<Queue> = mem::transmute(queue);
+            let v = mem::replace(&mut *queue.lock(), Vec::new());
+            for to_run in v.into_iter() {
+                to_run.invoke();
+            }
+        }
     }
 }
 
@@ -60,7 +58,6 @@ pub fn push(f: Thunk) {
         // all with respect to `run`, meaning that this could theoretically be a
         // use-after-free. There's not much we can do to protect against that,
         // however. Let's just assume a well-behaved runtime and go from there!
-        rtassert!(!RUNNING.load(atomic::SeqCst));
         let queue = QUEUE.load(atomic::SeqCst);
         rtassert!(queue != 0);
         (*(queue as *const Queue)).lock().push(f);
diff --git a/src/libstd/rt/backtrace.rs b/src/libstd/rt/backtrace.rs
index 40885823a05..4a692bccf9e 100644
--- a/src/libstd/rt/backtrace.rs
+++ b/src/libstd/rt/backtrace.rs
@@ -12,12 +12,8 @@
 
 #![allow(non_camel_case_types)]
 
-use io::{IoResult, Writer};
-use iter::{Iterator, IteratorExt};
 use option::Option::{Some, None};
 use os;
-use result::Result::{Ok, Err};
-use str::{StrPrelude, from_str};
 use sync::atomic;
 
 pub use sys::backtrace::write;
diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs
index 44794d2b957..022e73121d7 100644
--- a/src/libstd/rt/mod.rs
+++ b/src/libstd/rt/mod.rs
@@ -48,14 +48,14 @@
 
 #![allow(dead_code)]
 
-use borrow::IntoCow;
 use failure;
 use os;
 use thunk::Thunk;
 use kinds::Send;
 use thread::Thread;
+use sys;
 use sys_common;
-use sys_common::thread::{mod, NewThread};
+use sys_common::thread_info::{mod, NewThread};
 
 // Reexport some of our utilities which are expected by other crates.
 pub use self::util::{default_sched_threads, min_stack, running_on_valgrind};
@@ -87,10 +87,9 @@ pub const DEFAULT_ERROR_CODE: int = 101;
 /// Initializes global state, including frobbing
 /// the crate's logging flags, registering GC
 /// metadata, and storing the process arguments.
+// FIXME: this should be unsafe
 #[allow(experimental)]
 pub fn init(argc: int, argv: *const *const u8) {
-    // FIXME: Derefing these pointers is not safe.
-    // Need to propagate the unsafety to `start`.
     unsafe {
         args::init(argc, argv);
         thread::init();
@@ -122,8 +121,6 @@ fn lang_start(main: *const u8, argc: int, argv: *const *const u8) -> int {
 pub fn start(argc: int, argv: *const *const u8, main: Thunk) -> int {
     use prelude::*;
     use rt;
-    use rt::task::Task;
-    use str;
 
     let something_around_the_top_of_the_stack = 1;
     let addr = &something_around_the_top_of_the_stack as *const int;
@@ -153,18 +150,19 @@ pub fn start(argc: int, argv: *const *const u8, main: Thunk) -> int {
     init(argc, argv);
     let mut exit_code = None;
 
-    let thread: std::Thread = NewThread::new(Some("<main>".into_string()));
+    let thread: Thread = NewThread::new(Some("<main>".into_string()));
     thread_info::set((my_stack_bottom, my_stack_top),
                      unsafe { sys::thread::guard::main() },
                      thread);
-    unwind::try(|| {
-        unsafe {
+    let mut main_opt = Some(main); // option dance
+    unsafe {
+        let _ = unwind::try(|| {
             sys_common::stack::record_os_managed_stack_bounds(my_stack_bottom, my_stack_top);
-        }
-        (main.take().unwrap()).invoke(());
-        exit_code = Some(os::get_exit_status());
-    });
-    unsafe { cleanup(); }
+            (main_opt.take().unwrap()).invoke();
+            exit_code = Some(os::get_exit_status());
+        });
+        cleanup();
+    }
     // If the exit code wasn't set, then the task block must have panicked.
     return exit_code.unwrap_or(rt::DEFAULT_ERROR_CODE);
 }
@@ -197,14 +195,6 @@ pub fn at_exit(f: proc():Send) {
 /// undefined behavior.
 pub unsafe fn cleanup() {
     args::cleanup();
-    thread::cleanup();
-}
-
-// FIXME: these probably shouldn't be public...
-#[doc(hidden)]
-pub mod shouldnt_be_public {
-    #[cfg(not(test))]
-    pub use super::local_ptr::native::maybe_tls_key;
-    #[cfg(all(not(windows), not(target_os = "android"), not(target_os = "ios")))]
-    pub use super::local_ptr::compiled::RT_TLS_PTR;
+    sys::stack_overflow::cleanup();
+    at_exit_imp::cleanup();
 }
diff --git a/src/libstd/rt/unwind.rs b/src/libstd/rt/unwind.rs
index decf7cfb60a..f9f76e35bd4 100644
--- a/src/libstd/rt/unwind.rs
+++ b/src/libstd/rt/unwind.rs
@@ -565,7 +565,7 @@ fn begin_unwind_inner(msg: Box<Any + Send>, file_line: &(&'static str, uint)) ->
 
     // Now that we've run all the necessary unwind callbacks, we actually
     // perform the unwinding.
-    if thread_info::unwinding() {
+    if thread_info::panicking() {
         // If a thread panics while it's already unwinding then we
         // have limited options. Currently our preference is to
         // just abort. In the future we may consider resuming
diff --git a/src/libstd/rt/util.rs b/src/libstd/rt/util.rs
index d3cfccab9d0..86dbb6066f3 100644
--- a/src/libstd/rt/util.rs
+++ b/src/libstd/rt/util.rs
@@ -196,8 +196,7 @@ memory and partly incapable of presentation to others.",
 }
 
 pub unsafe fn report_overflow() {
-    use rt::task::Task;
-    use rt::local::Local;
+    use thread::Thread;
 
     // See the message below for why this is not emitted to the
     // ^ Where did the message below go?
@@ -206,11 +205,6 @@ pub unsafe fn report_overflow() {
     // call would happen to initialized it (calling out to libuv),
     // and the FFI call needs 2MB of stack when we just ran out.
 
-    let task: Option<*mut Task> = Local::try_unsafe_borrow();
-
-    let name = task.and_then(|task| {
-        (*task).name.as_ref().map(|n| n.as_slice())
-    });
-
-    rterrln!("\ntask '{}' has overflowed its stack", name.unwrap_or("<unknown>"));
+    rterrln!("\nthread '{}' has overflowed its stack",
+             Thread::current().name().unwrap_or("<unknown>"));
 }