|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Unix-specific extensions to primitives in the `std::thread` module. |
| 12 | +
|
| 13 | +#![stable(feature = "thread_extensions", since = "1.9.0")] |
| 14 | + |
| 15 | +use sys_common::{AsInner, IntoInner}; |
| 16 | +use thread::JoinHandle; |
| 17 | + |
| 18 | +#[stable(feature = "thread_extensions", since = "1.9.0")] |
| 19 | +#[allow(deprecated)] |
| 20 | +pub type RawPthread = usize; |
| 21 | + |
| 22 | +/// Unix-specific extensions to `std::thread::JoinHandle` |
| 23 | +#[stable(feature = "thread_extensions", since = "1.9.0")] |
| 24 | +pub trait JoinHandleExt { |
| 25 | + /// Extracts the raw pthread_t without taking ownership |
| 26 | + #[stable(feature = "thread_extensions", since = "1.9.0")] |
| 27 | + fn as_pthread_t(&self) -> RawPthread; |
| 28 | + |
| 29 | + /// Consumes the thread, returning the raw pthread_t |
| 30 | + /// |
| 31 | + /// This function **transfers ownership** of the underlying pthread_t to |
| 32 | + /// the caller. Callers are then the unique owners of the pthread_t and |
| 33 | + /// must either detach or join the pthread_t once it's no longer needed. |
| 34 | + #[stable(feature = "thread_extensions", since = "1.9.0")] |
| 35 | + fn into_pthread_t(self) -> RawPthread; |
| 36 | +} |
| 37 | + |
| 38 | +#[stable(feature = "thread_extensions", since = "1.9.0")] |
| 39 | +impl<T> JoinHandleExt for JoinHandle<T> { |
| 40 | + fn as_pthread_t(&self) -> RawPthread { |
| 41 | + self.as_inner().id() as RawPthread |
| 42 | + } |
| 43 | + |
| 44 | + fn into_pthread_t(self) -> RawPthread { |
| 45 | + self.into_inner().into_id() as RawPthread |
| 46 | + } |
| 47 | +} |
0 commit comments