Skip to content

Commit 7aec917

Browse files
committed
Auto merge of #29153 - arcnmx:thread-spawn, r=alexcrichton
Fixes #29128 Most of the weird lifetime things and `inner` stuff seems like leftover cruft from `thread::scoped`. Should `JoinInner` just be removed/merged with `JoinHandle`? Also is it okay to remove the `FnBox`? I'm not really sure why there were two allocations there...
2 parents 3e268f2 + 1303687 commit 7aec917

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/libstd/thread/mod.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@
162162

163163
use prelude::v1::*;
164164

165-
use alloc::boxed::FnBox;
166165
use any::Any;
167166
use cell::UnsafeCell;
168167
use fmt;
@@ -249,16 +248,6 @@ impl Builder {
249248
pub fn spawn<F, T>(self, f: F) -> io::Result<JoinHandle<T>> where
250249
F: FnOnce() -> T, F: Send + 'static, T: Send + 'static
251250
{
252-
unsafe {
253-
self.spawn_inner(Box::new(f)).map(JoinHandle)
254-
}
255-
}
256-
257-
// NB: this function is unsafe as the lifetime parameter of the code to run
258-
// in the new thread is not tied into the return value, and the return
259-
// value must not outlast that lifetime.
260-
unsafe fn spawn_inner<'a, T: Send>(self, f: Box<FnBox() -> T + Send + 'a>)
261-
-> io::Result<JoinInner<T>> {
262251
let Builder { name, stack_size } = self;
263252

264253
let stack_size = stack_size.unwrap_or(util::min_stack());
@@ -274,22 +263,26 @@ impl Builder {
274263
if let Some(name) = their_thread.name() {
275264
imp::Thread::set_name(name);
276265
}
277-
thread_info::set(imp::guard::current(), their_thread);
278-
let mut output = None;
279-
let try_result = {
280-
let ptr = &mut output;
281-
unwind::try(move || *ptr = Some(f()))
282-
};
283-
*their_packet.get() = Some(try_result.map(|()| {
284-
output.unwrap()
285-
}));
266+
unsafe {
267+
thread_info::set(imp::guard::current(), their_thread);
268+
let mut output = None;
269+
let try_result = {
270+
let ptr = &mut output;
271+
unwind::try(move || *ptr = Some(f()))
272+
};
273+
*their_packet.get() = Some(try_result.map(|()| {
274+
output.unwrap()
275+
}));
276+
}
286277
};
287278

288-
Ok(JoinInner {
289-
native: Some(try!(imp::Thread::new(stack_size, Box::new(main)))),
279+
Ok(JoinHandle(JoinInner {
280+
native: unsafe {
281+
Some(try!(imp::Thread::new(stack_size, Box::new(main))))
282+
},
290283
thread: my_thread,
291284
packet: Packet(my_packet),
292-
})
285+
}))
293286
}
294287
}
295288

0 commit comments

Comments
 (0)