Skip to content

Commit 95cdada

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 f5150dd commit 95cdada

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

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

+1
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

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#![unstable(feature = "thread_extensions", issue = "29791")]
14+
15+
use os::windows::io::{RawHandle, AsRawHandle, IntoRawHandle};
16+
use thread;
17+
use sys_common::{AsInner, IntoInner};
18+
19+
impl<T> AsRawHandle for thread::JoinHandle<T> {
20+
fn as_raw_handle(&self) -> RawHandle {
21+
self.as_inner().handle().raw() as *mut _
22+
}
23+
}
24+
25+
impl<T> IntoRawHandle for thread::JoinHandle<T> {
26+
fn into_raw_handle(self) -> RawHandle {
27+
self.into_inner().into_handle().into_raw() as *mut _
28+
}
29+
}

src/libstd/sys/windows/thread.rs

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ impl Thread {
7777
c::Sleep(super::dur2timeout(dur))
7878
}
7979
}
80+
81+
pub fn handle(&self) -> &Handle { &self.handle }
82+
83+
pub fn into_handle(self) -> Handle { self.handle }
8084
}
8185

8286
pub mod guard {

src/libstd/thread/mod.rs

+9
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
////////////////////////////////////////////////////////////////////////////////
@@ -621,6 +622,14 @@ impl<T> JoinHandle<T> {
621622
}
622623
}
623624

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

0 commit comments

Comments
 (0)