From bd2c0077cbf14864dbc848bccfea6d48f87e30cb Mon Sep 17 00:00:00 2001 From: Peter Atashian Date: Thu, 29 Oct 2015 16:54:09 -0400 Subject: [PATCH 1/2] AsRawHandle and IntoRawHandle for JoinHandle This allows users to get the HANDLE of a spawned thread on Windows Signed-off-by: Peter Atashian --- src/libstd/sys/windows/ext/mod.rs | 1 + src/libstd/sys/windows/ext/thread.rs | 27 +++++++++++++++++++++++++++ src/libstd/sys/windows/thread.rs | 4 ++++ src/libstd/thread/mod.rs | 9 +++++++++ 4 files changed, 41 insertions(+) create mode 100644 src/libstd/sys/windows/ext/thread.rs diff --git a/src/libstd/sys/windows/ext/mod.rs b/src/libstd/sys/windows/ext/mod.rs index f69c2d075e3ea..fe35c91728d4c 100644 --- a/src/libstd/sys/windows/ext/mod.rs +++ b/src/libstd/sys/windows/ext/mod.rs @@ -21,6 +21,7 @@ pub mod fs; pub mod io; pub mod raw; pub mod process; +pub mod thread; /// A prelude for conveniently writing platform-specific code. /// diff --git a/src/libstd/sys/windows/ext/thread.rs b/src/libstd/sys/windows/ext/thread.rs new file mode 100644 index 0000000000000..1454f81595ed7 --- /dev/null +++ b/src/libstd/sys/windows/ext/thread.rs @@ -0,0 +1,27 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Extensions to `std::thread` for Windows. + +use os::windows::io::{RawHandle, AsRawHandle, IntoRawHandle}; +use thread; +use sys_common::{AsInner, IntoInner}; + +impl AsRawHandle for thread::JoinHandle { + fn as_raw_handle(&self) -> RawHandle { + self.as_inner().handle().raw() as *mut _ + } +} + +impl IntoRawHandle for thread::JoinHandle { + fn into_raw_handle(self) -> RawHandle { + self.into_inner().into_handle().into_raw() as *mut _ + } +} diff --git a/src/libstd/sys/windows/thread.rs b/src/libstd/sys/windows/thread.rs index cf1b3ebddb97b..cebc064f86b41 100644 --- a/src/libstd/sys/windows/thread.rs +++ b/src/libstd/sys/windows/thread.rs @@ -78,6 +78,10 @@ impl Thread { c::Sleep(super::dur2timeout(dur)) } } + + pub fn handle(&self) -> &Handle { &self.handle } + + pub fn into_handle(self) -> Handle { self.handle } } pub mod guard { diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 75e3a52feea65..493e3123060b9 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -171,6 +171,7 @@ use sys::thread as imp; use sys_common::thread_info; use sys_common::unwind; use sys_common::util; +use sys_common::{AsInner, IntoInner}; use time::Duration; //////////////////////////////////////////////////////////////////////////////// @@ -619,6 +620,14 @@ impl JoinHandle { } } +impl AsInner for JoinHandle { + fn as_inner(&self) -> &imp::Thread { self.0.native.as_ref().unwrap() } +} + +impl IntoInner for JoinHandle { + fn into_inner(self) -> imp::Thread { self.0.native.unwrap() } +} + fn _assert_sync_and_send() { fn _assert_both() {} _assert_both::>(); From 88d7df6020d784d6fb3a5658530d378a3e1599a0 Mon Sep 17 00:00:00 2001 From: Peter Atashian Date: Tue, 10 Nov 2015 11:25:21 -0500 Subject: [PATCH 2/2] Add JoinHandleExt to get the pthread_t on unix platforms Signed-off-by: Peter Atashian --- src/libstd/sys/unix/ext/mod.rs | 1 + src/libstd/sys/unix/ext/raw.rs | 2 ++ src/libstd/sys/unix/ext/thread.rs | 40 +++++++++++++++++++++++++++++++ src/libstd/sys/unix/thread.rs | 8 +++++++ 4 files changed, 51 insertions(+) create mode 100644 src/libstd/sys/unix/ext/thread.rs diff --git a/src/libstd/sys/unix/ext/mod.rs b/src/libstd/sys/unix/ext/mod.rs index 5b9f36cbc373a..fd1eb752e63ac 100644 --- a/src/libstd/sys/unix/ext/mod.rs +++ b/src/libstd/sys/unix/ext/mod.rs @@ -34,6 +34,7 @@ pub mod ffi; pub mod fs; pub mod process; pub mod raw; +pub mod thread; /// A prelude for conveniently writing platform-specific code. /// diff --git a/src/libstd/sys/unix/ext/raw.rs b/src/libstd/sys/unix/ext/raw.rs index fa380abe6c54d..34db6ae8e9de8 100644 --- a/src/libstd/sys/unix/ext/raw.rs +++ b/src/libstd/sys/unix/ext/raw.rs @@ -15,6 +15,8 @@ #[stable(feature = "raw_ext", since = "1.1.0")] pub type uid_t = u32; #[stable(feature = "raw_ext", since = "1.1.0")] pub type gid_t = u32; #[stable(feature = "raw_ext", since = "1.1.0")] pub type pid_t = i32; +#[unstable(feature = "pthread_t", issue = "0")] +pub type pthread_t = usize; #[doc(inline)] pub use sys::platform::raw::{dev_t, ino_t, mode_t, nlink_t, off_t, blksize_t}; diff --git a/src/libstd/sys/unix/ext/thread.rs b/src/libstd/sys/unix/ext/thread.rs new file mode 100644 index 0000000000000..7fe3556ef3eab --- /dev/null +++ b/src/libstd/sys/unix/ext/thread.rs @@ -0,0 +1,40 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Unix-specific extensions to primitives in the `std::process` module. + +use os::unix::raw::{pthread_t}; +use sys_common::{AsInner, IntoInner}; +use thread::{JoinHandle}; + +#[unstable(feature = "thread_extensions", issue = "0")] +pub type RawId = pthread_t; + +/// Unix-specific extensions to `std::thread::JoinHandle` +#[unstable(feature = "thread_extensions", issue = "0")] +pub trait JoinHandleExt { + /// Extracts the raw pthread_t without taking ownership + fn as_pthread_t(&self) -> RawId; + /// Consumes the thread, returning the raw pthread_t + /// + /// This function **transfers ownership** of the underlying pthread_t to + /// the caller. Callers are then the unique owners of the pthread_t and + /// must either detech or join the pthread_t once it's no longer needed. + fn into_pthread_t(self) -> RawId; +} + +impl JoinHandleExt for JoinHandle { + fn as_pthread_t(&self) -> RawId { + self.as_inner().id() as RawId + } + fn into_pthread_t(self) -> RawId { + self.into_inner().into_id() as RawId + } +} diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 3eedb76c21b72..30fdb2a7dc477 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -167,6 +167,14 @@ impl Thread { debug_assert_eq!(ret, 0); } } + + pub fn id(&self) -> libc::pthread_t { self.id } + + pub fn into_id(self) -> libc::pthread_t { + let id = self.id; + mem::forget(self); + id + } } impl Drop for Thread {