Skip to content

Commit bd2c007

Browse files
committed
AsRawHandle and IntoRawHandle for JoinHandle
This allows users to get the HANDLE of a spawned thread on Windows Signed-off-by: Peter Atashian <[email protected]>
1 parent 2f59977 commit bd2c007

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

src/libstd/sys/windows/ext/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod fs;
2121
pub mod io;
2222
pub mod raw;
2323
pub mod process;
24+
pub mod thread;
2425

2526
/// A prelude for conveniently writing platform-specific code.
2627
///

src/libstd/sys/windows/ext/thread.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
//! Extensions to `std::thread` for Windows.
12+
13+
use os::windows::io::{RawHandle, AsRawHandle, IntoRawHandle};
14+
use thread;
15+
use sys_common::{AsInner, IntoInner};
16+
17+
impl<T> AsRawHandle for thread::JoinHandle<T> {
18+
fn as_raw_handle(&self) -> RawHandle {
19+
self.as_inner().handle().raw() as *mut _
20+
}
21+
}
22+
23+
impl<T> IntoRawHandle for thread::JoinHandle<T> {
24+
fn into_raw_handle(self) -> RawHandle {
25+
self.into_inner().into_handle().into_raw() as *mut _
26+
}
27+
}

src/libstd/sys/windows/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ impl Thread {
7878
c::Sleep(super::dur2timeout(dur))
7979
}
8080
}
81+
82+
pub fn handle(&self) -> &Handle { &self.handle }
83+
84+
pub fn into_handle(self) -> Handle { self.handle }
8185
}
8286

8387
pub mod guard {

src/libstd/thread/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ use sys::thread as imp;
171171
use sys_common::thread_info;
172172
use sys_common::unwind;
173173
use sys_common::util;
174+
use sys_common::{AsInner, IntoInner};
174175
use time::Duration;
175176

176177
////////////////////////////////////////////////////////////////////////////////
@@ -619,6 +620,14 @@ impl<T> JoinHandle<T> {
619620
}
620621
}
621622

623+
impl<T> AsInner<imp::Thread> for JoinHandle<T> {
624+
fn as_inner(&self) -> &imp::Thread { self.0.native.as_ref().unwrap() }
625+
}
626+
627+
impl<T> IntoInner<imp::Thread> for JoinHandle<T> {
628+
fn into_inner(self) -> imp::Thread { self.0.native.unwrap() }
629+
}
630+
622631
fn _assert_sync_and_send() {
623632
fn _assert_both<T: Send + Sync>() {}
624633
_assert_both::<JoinHandle<()>>();

0 commit comments

Comments
 (0)