Skip to content

Commit aad4e56

Browse files
committed
Auto merge of #30177 - retep998:handling-threads, r=alexcrichton
Allows a `HANDLE` to be extracted from a `JoinHandle` on Windows. Allows a `pthread_t` to be extracted from a `JoinHandle` everywhere else. Because #29461 was closed. r? @alexcrichton
2 parents 9f715e9 + 9749a19 commit aad4e56

File tree

18 files changed

+122
-0
lines changed

18 files changed

+122
-0
lines changed

src/libstd/os/android/raw.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
#![stable(feature = "raw_ext", since = "1.1.0")]
1414

15+
use os::raw::c_long;
16+
17+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = c_long;
18+
1519
#[doc(inline)]
1620
#[stable(feature = "raw_ext", since = "1.1.0")]
1721
pub use self::arch::{dev_t, mode_t, blkcnt_t, blksize_t, ino_t, nlink_t, off_t, stat, time_t};

src/libstd/os/bitrig/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use os::unix::raw::{uid_t, gid_t};
2525
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
2626
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64;
2727

28+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;
29+
2830
#[repr(C)]
2931
#[derive(Clone)]
3032
#[stable(feature = "raw_ext", since = "1.1.0")]

src/libstd/os/dragonfly/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use os::unix::raw::{uid_t, gid_t};
2525
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
2626
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64;
2727

28+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;
29+
2830
#[repr(C)]
2931
#[derive(Clone)]
3032
#[stable(feature = "raw_ext", since = "1.1.0")]

src/libstd/os/freebsd/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#[stable(feature = "raw_ext", since = "1.1.0")] pub type blkcnt_t = i64;
2222
#[stable(feature = "raw_ext", since = "1.1.0")] pub type fflags_t = u32;
2323

24+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;
25+
2426
#[doc(inline)]
2527
#[stable(feature = "raw_ext", since = "1.1.0")]
2628
pub use self::arch::{stat, time_t};

src/libstd/os/ios/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use os::unix::raw::{uid_t, gid_t};
2424
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
2525
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = c_long;
2626

27+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;
28+
2729
#[repr(C)]
2830
#[derive(Clone)]
2931
#[stable(feature = "raw_ext", since = "1.1.0")]

src/libstd/os/linux/raw.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
1313
#![stable(feature = "raw_ext", since = "1.1.0")]
1414

15+
use os::raw::c_ulong;
16+
1517
#[stable(feature = "raw_ext", since = "1.1.0")] pub type dev_t = u64;
1618
#[stable(feature = "raw_ext", since = "1.1.0")] pub type mode_t = u32;
1719

20+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = c_ulong;
21+
1822
#[doc(inline)]
1923
#[stable(feature = "raw_ext", since = "1.1.0")]
2024
pub use self::arch::{off_t, ino_t, nlink_t, blksize_t, blkcnt_t, stat, time_t};

src/libstd/os/macos/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use os::unix::raw::{uid_t, gid_t};
2424
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
2525
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = c_long;
2626

27+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;
28+
2729
#[repr(C)]
2830
#[derive(Clone)]
2931
#[stable(feature = "raw_ext", since = "1.1.0")]

src/libstd/os/nacl/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#[stable(feature = "raw_ext", since = "1.1.0")] pub type blksize_t = i32;
2525
#[stable(feature = "raw_ext", since = "1.1.0")] pub type blkcnt_t = i32;
2626

27+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;
28+
2729
#[repr(C)]
2830
#[derive(Copy, Clone)]
2931
#[stable(feature = "raw_ext", since = "1.1.0")]

src/libstd/os/netbsd/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use os::unix::raw::{uid_t, gid_t};
2525
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
2626
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64;
2727

28+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;
29+
2830
#[repr(C)]
2931
#[derive(Clone)]
3032
#[stable(feature = "raw_ext", since = "1.1.0")]

src/libstd/os/openbsd/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use os::unix::raw::{uid_t, gid_t};
2525
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
2626
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64;
2727

28+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;
29+
2830
#[repr(C)]
2931
#[derive(Clone)]
3032
#[stable(feature = "raw_ext", since = "1.1.0")]

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

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub mod ffi;
3434
pub mod fs;
3535
pub mod process;
3636
pub mod raw;
37+
pub mod thread;
3738

3839
/// A prelude for conveniently writing platform-specific code.
3940
///

src/libstd/sys/unix/ext/raw.rs

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#[stable(feature = "raw_ext", since = "1.1.0")] pub type gid_t = u32;
1717
#[stable(feature = "raw_ext", since = "1.1.0")] pub type pid_t = i32;
1818

19+
#[doc(inline)]
20+
#[unstable(feature = "pthread_t", issue = "29791")]
21+
pub use sys::platform::raw::pthread_t;
1922
#[doc(inline)]
2023
#[stable(feature = "raw_ext", since = "1.1.0")]
2124
pub use sys::platform::raw::{dev_t, ino_t, mode_t, nlink_t, off_t, blksize_t};

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

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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::process` module.
12+
13+
#![unstable(feature = "thread_extensions", issue = "29791")]
14+
15+
use os::unix::raw::{pthread_t};
16+
use sys_common::{AsInner, IntoInner};
17+
use thread::{JoinHandle};
18+
19+
#[unstable(feature = "thread_extensions", issue = "29791")]
20+
pub type RawPthread = pthread_t;
21+
22+
/// Unix-specific extensions to `std::thread::JoinHandle`
23+
#[unstable(feature = "thread_extensions", issue = "29791")]
24+
pub trait JoinHandleExt {
25+
/// Extracts the raw pthread_t without taking ownership
26+
fn as_pthread_t(&self) -> RawPthread;
27+
/// Consumes the thread, returning the raw pthread_t
28+
///
29+
/// This function **transfers ownership** of the underlying pthread_t to
30+
/// the caller. Callers are then the unique owners of the pthread_t and
31+
/// must either detech or join the pthread_t once it's no longer needed.
32+
fn into_pthread_t(self) -> RawPthread;
33+
}
34+
35+
#[unstable(feature = "thread_extensions", issue = "29791")]
36+
impl<T> JoinHandleExt for JoinHandle<T> {
37+
fn as_pthread_t(&self) -> RawPthread {
38+
self.as_inner().id() as RawPthread
39+
}
40+
fn into_pthread_t(self) -> RawPthread {
41+
self.into_inner().into_id() as RawPthread
42+
}
43+
}

src/libstd/sys/unix/thread.rs

+8
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ impl Thread {
152152
debug_assert_eq!(ret, 0);
153153
}
154154
}
155+
156+
pub fn id(&self) -> libc::pthread_t { self.id }
157+
158+
pub fn into_id(self) -> libc::pthread_t {
159+
let id = self.id;
160+
mem::forget(self);
161+
id
162+
}
155163
}
156164

157165
impl Drop for Thread {

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)