| 1 |
// Licensed under the Apache License, Version 2.0 |
| 2 |
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 4 |
// All files in the project carrying such notice may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
use {Result, k32, last_error, w}; |
| 7 |
use handle::{Handle}; |
| 8 |
use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle}; |
| 9 |
use std::thread::{JoinHandle}; |
| 10 |
|
| 11 |
pub struct Thread(Handle); |
| 12 |
impl Thread { |
| 13 |
pub fn current() -> Result<Thread> { |
| 14 |
unsafe { Handle::duplicate_from(k32::GetCurrentThread()).map(Thread) } |
| 15 |
} |
| 16 |
/// Returns the old affinity mask on success |
| 17 |
pub fn set_affinity_mask(&self, mask: usize) -> Result<usize> { |
| 18 |
let res = unsafe { |
| 19 |
k32::SetThreadAffinityMask(*self.0, mask as w::ULONG_PTR) |
| 20 |
}; |
| 21 |
match res { |
| 22 |
0 => last_error(), |
| 23 |
prev => Ok(prev as usize), |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
impl<T> From<JoinHandle<T>> for Thread { |
| 28 |
fn from(o: JoinHandle<T>) -> Thread { |
| 29 |
unsafe { Thread::from_raw_handle(o.into_raw_handle()) } |
| 30 |
} |
| 31 |
} |
| 32 |
impl<'a, T> From<&'a JoinHandle<T>> for Thread { |
| 33 |
fn from(o: &'a JoinHandle<T>) -> Thread { |
| 34 |
unsafe { Thread::from_raw_handle(o.as_raw_handle()) } |
| 35 |
} |
| 36 |
} |
| 37 |
impl AsRawHandle for Thread { |
| 38 |
fn as_raw_handle(&self) -> w::HANDLE { |
| 39 |
self.0.as_raw_handle() |
| 40 |
} |
| 41 |
} |
| 42 |
impl IntoRawHandle for Thread { |
| 43 |
fn into_raw_handle(self) -> w::HANDLE { |
| 44 |
self.0.into_raw_handle() |
| 45 |
} |
| 46 |
} |
| 47 |
impl FromRawHandle for Thread { |
| 48 |
unsafe fn from_raw_handle(handle: w::HANDLE) -> Thread { |
| 49 |
Thread(Handle::from_raw_handle(handle)) |
| 50 |
} |
| 51 |
} |