Skip to main content

dope_sys/thread/
handle.rs

1use std::{mem::ManuallyDrop, ptr};
2
3use o3::base::marker::ThreadBound;
4
5use crate::kernel;
6
7/// One joinable native thread, joined on drop by its creating thread.
8#[repr(transparent)]
9pub struct Handle(pub(super) libc::pthread_t, pub(super) ThreadBound);
10
11impl Handle {
12    /// Joins the uniquely owned thread from its creating thread.
13    /// Native failure terminates before worker storage can be reclaimed.
14    pub fn join(self) {
15        ManuallyDrop::new(self).wait();
16    }
17
18    fn wait(&self) {
19        // SAFETY: successful spawn created this unique joinable handle, retained
20        // by its creating thread. pthread_join returns zero or a positive error
21        // code, which is checked before the owner can reclaim worker storage.
22        if let Err(cause) = unsafe {
23            kernel::raw::Status::new(libc::pthread_join(self.0, ptr::null_mut())).try_pthread()
24        } {
25            kernel::Fatal::Join(cause).terminate()
26        }
27    }
28}
29
30impl Drop for Handle {
31    fn drop(&mut self) {
32        self.wait();
33    }
34}