Skip to content

AsRawHandle and IntoRawHandle for JoinHandle #29461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libstd/sys/unix/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/unix/ext/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a little more involved, taking a look at the definition in liblibc:

$ git grep 'type pthread_t'
src/unix/bsd/mod.rs:pub type pthread_t = ::uintptr_t;
src/unix/notbsd/android/mod.rs:pub type pthread_t = c_long;
src/unix/notbsd/linux/mod.rs:pub type pthread_t = c_ulong;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are c_long and c_ulong ever a different size than usize on Linux and Android?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of what actually happens they're all the same size (I believe), but I think it's best to ensure that they're the same definition so the libstd pthread_t is interoperable with the libc pthread_t.


#[doc(inline)]
pub use sys::platform::raw::{dev_t, ino_t, mode_t, nlink_t, off_t, blksize_t};
Expand Down
40 changes: 40 additions & 0 deletions src/libstd/sys/unix/ext/thread.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #29791 to connect these together, could you update the issue references here?

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<T> JoinHandleExt for JoinHandle<T> {
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
}
}
8 changes: 8 additions & 0 deletions src/libstd/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys/windows/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
27 changes: 27 additions & 0 deletions src/libstd/sys/windows/ext/thread.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<T> AsRawHandle for thread::JoinHandle<T> {
fn as_raw_handle(&self) -> RawHandle {
self.as_inner().handle().raw() as *mut _
}
}

impl<T> IntoRawHandle for thread::JoinHandle<T> {
fn into_raw_handle(self) -> RawHandle {
self.into_inner().into_handle().into_raw() as *mut _
}
}
4 changes: 4 additions & 0 deletions src/libstd/sys/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -619,6 +620,14 @@ impl<T> JoinHandle<T> {
}
}

impl<T> AsInner<imp::Thread> for JoinHandle<T> {
fn as_inner(&self) -> &imp::Thread { self.0.native.as_ref().unwrap() }
}

impl<T> IntoInner<imp::Thread> for JoinHandle<T> {
fn into_inner(self) -> imp::Thread { self.0.native.unwrap() }
}

fn _assert_sync_and_send() {
fn _assert_both<T: Send + Sync>() {}
_assert_both::<JoinHandle<()>>();
Expand Down