about summary refs log tree commit diff
path: root/src/libstd/rt
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-04-18 22:16:35 -0700
committerbors <bors@rust-lang.org>2014-04-18 22:16:35 -0700
commit9d5082e88a11f1daf66f062eb061efcee54718a0 (patch)
tree3067cc60a445df6e72947bc13ca36b5baee28188 /src/libstd/rt
parentaf24045ff0e17764524a9eaf243479a3260c2d8b (diff)
parentc318d72b8651e40559c40455475f730cf5f07a1f (diff)
downloadrust-9d5082e88a11f1daf66f062eb061efcee54718a0.tar.gz
rust-9d5082e88a11f1daf66f062eb061efcee54718a0.zip
auto merge of #13606 : alexcrichton/rust/better-thread-errors, r=brson
On windows, correctly check for errors when spawning threads, and on both
windows and unix handle the error more gracefully rather than printing an opaque
assertion failure.

Closes #13589
Diffstat (limited to 'src/libstd/rt')
-rw-r--r--src/libstd/rt/thread.rs21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/libstd/rt/thread.rs b/src/libstd/rt/thread.rs
index 4aa85bc4c72..2d952b2a9db 100644
--- a/src/libstd/rt/thread.rs
+++ b/src/libstd/rt/thread.rs
@@ -150,6 +150,7 @@ mod imp {
     use libc;
     use libc::types::os::arch::extra::{LPSECURITY_ATTRIBUTES, SIZE_T, BOOL,
                                        LPVOID, DWORD, LPDWORD, HANDLE};
+    use os;
     use ptr;
     use rt::stack::RED_ZONE;
 
@@ -168,8 +169,15 @@ mod imp {
         // kernel does, might as well make it explicit.  With the current
         // 20 kB red zone, that makes for a 64 kB minimum stack.
         let stack_size = (cmp::max(stack, RED_ZONE) + 0xfffe) & (-0xfffe - 1);
-        CreateThread(ptr::mut_null(), stack_size as libc::size_t,
-                     super::thread_start, arg, 0, ptr::mut_null())
+        let ret = CreateThread(ptr::mut_null(), stack_size as libc::size_t,
+                               super::thread_start, arg, 0, ptr::mut_null());
+
+        if ret as uint == 0 {
+            // be sure to not leak the closure
+            let _p: ~proc():Send = cast::transmute(arg);
+            fail!("failed to spawn native thread: {}", os::last_os_error());
+        }
+        return ret;
     }
 
     pub unsafe fn join(native: rust_thread) {
@@ -243,9 +251,14 @@ mod imp {
         };
 
         let arg: *libc::c_void = cast::transmute(p);
-        assert_eq!(pthread_create(&mut native, &attr,
-                                  super::thread_start, arg), 0);
+        let ret = pthread_create(&mut native, &attr, super::thread_start, arg);
         assert_eq!(pthread_attr_destroy(&mut attr), 0);
+
+        if ret != 0 {
+            // be sure to not leak the closure
+            let _p: ~proc():Send = cast::transmute(arg);
+            fail!("failed to spawn native thread: {}", os::last_os_error());
+        }
         native
     }