Skip to content

Commit 88d7df6

Browse files
committed
Add JoinHandleExt to get the pthread_t on unix platforms
Signed-off-by: Peter Atashian <[email protected]>
1 parent bd2c007 commit 88d7df6

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#[stable(feature = "raw_ext", since = "1.1.0")] pub type uid_t = u32;
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;
18+
#[unstable(feature = "pthread_t", issue = "0")]
19+
pub type pthread_t = usize;
1820

1921
#[doc(inline)]
2022
pub use sys::platform::raw::{dev_t, ino_t, mode_t, nlink_t, off_t, blksize_t};

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

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

src/libstd/sys/unix/thread.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ impl Thread {
167167
debug_assert_eq!(ret, 0);
168168
}
169169
}
170+
171+
pub fn id(&self) -> libc::pthread_t { self.id }
172+
173+
pub fn into_id(self) -> libc::pthread_t {
174+
let id = self.id;
175+
mem::forget(self);
176+
id
177+
}
170178
}
171179

172180
impl Drop for Thread {

0 commit comments

Comments
 (0)