1
Fork 0

adds unsafe thread::Builder::spawn_unchecked function

moves code for `thread::Builder::spawn` into new public unsafe function `spawn_unchecked` and transforms `spawn` into a safe wrapper.
This commit is contained in:
oliver-giersch 2018-10-13 14:34:31 +02:00 committed by GitHub
parent 2d81989974
commit fbb95689d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -386,6 +386,13 @@ impl Builder {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn spawn<F, T>(self, f: F) -> io::Result<JoinHandle<T>> where
F: FnOnce() -> T, F: Send + 'static, T: Send + 'static
{
unsafe { self.spawn_unchecked(f) }
}
/// TODO: Doc
pub unsafe fn spawn_unchecked<'a, F, T>(self, f: F) -> io::Result<JoinHandle<T>> where
F: FnOnce() -> T, F: Send + 'a, T: Send + 'a
{
let Builder { name, stack_size } = self;
@ -402,16 +409,15 @@ impl Builder {
if let Some(name) = their_thread.cname() {
imp::Thread::set_name(name);
}
unsafe {
thread_info::set(imp::guard::current(), their_thread);
#[cfg(feature = "backtrace")]
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
::sys_common::backtrace::__rust_begin_short_backtrace(f)
}));
#[cfg(not(feature = "backtrace"))]
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(f));
*their_packet.get() = Some(try_result);
}
thread_info::set(imp::guard::current(), their_thread);
#[cfg(feature = "backtrace")]
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
::sys_common::backtrace::__rust_begin_short_backtrace(f)
}));
#[cfg(not(feature = "backtrace"))]
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(f));
*their_packet.get() = Some(try_result);
};
Ok(JoinHandle(JoinInner {
@ -420,7 +426,7 @@ impl Builder {
},
thread: my_thread,
packet: Packet(my_packet),
}))
}))
}
}