|
167 | 167 | #![stable(feature = "rust1", since = "1.0.0")] |
168 | 168 |
|
169 | 169 | use any::Any; |
| 170 | +use boxed::FnBox; |
170 | 171 | use cell::UnsafeCell; |
171 | 172 | use ffi::{CStr, CString}; |
172 | 173 | use fmt; |
173 | 174 | use io; |
| 175 | +use mem; |
174 | 176 | use panic; |
175 | 177 | use panicking; |
176 | 178 | use str; |
@@ -452,8 +454,8 @@ impl Builder { |
452 | 454 | /// [`io::Result`]: ../../std/io/type.Result.html |
453 | 455 | /// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html |
454 | 456 | #[unstable(feature = "thread_spawn_unchecked", issue = "55132")] |
455 | | - pub unsafe fn spawn_unchecked<F, T>(self, f: F) -> io::Result<JoinHandle<T>> where |
456 | | - F: FnOnce() -> T, F: Send, T: Send |
| 457 | + pub unsafe fn spawn_unchecked<'a, F, T>(self, f: F) -> io::Result<JoinHandle<T>> where |
| 458 | + F: FnOnce() -> T, F: Send + 'a, T: Send + 'a |
457 | 459 | { |
458 | 460 | let Builder { name, stack_size } = self; |
459 | 461 |
|
@@ -482,7 +484,21 @@ impl Builder { |
482 | 484 | }; |
483 | 485 |
|
484 | 486 | Ok(JoinHandle(JoinInner { |
485 | | - native: Some(imp::Thread::new(stack_size, Box::new(main))?), |
| 487 | + // `imp::Thread::new` takes a closure with a `'static` lifetime, since it's passed |
| 488 | + // through FFI or otherwise used with low-level threading primitives that have no |
| 489 | + // notion of or way to enforce lifetimes. |
| 490 | + // |
| 491 | + // As mentioned in the `Safety` section of this function's documentation, the caller of |
| 492 | + // this function needs to guarantee that the passed-in lifetime is sufficiently long |
| 493 | + // for the lifetime of the thread. |
| 494 | + // |
| 495 | + // Similarly, the `sys` implementation must guarantee that no references to the closure |
| 496 | + // exist after the thread has terminated, which is signaled by `Thread::join` |
| 497 | + // returning. |
| 498 | + native: Some(imp::Thread::new( |
| 499 | + stack_size, |
| 500 | + mem::transmute::<Box<dyn FnBox() + 'a>, Box<dyn FnBox() + 'static>>(Box::new(main)) |
| 501 | + )?), |
486 | 502 | thread: my_thread, |
487 | 503 | packet: Packet(my_packet), |
488 | 504 | })) |
|
0 commit comments