Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add JoinHandleExt to get the pthread_t on unix platforms
Signed-off-by: Peter Atashian <retep998@gmail.com>
  • Loading branch information
retep998 committed Dec 5, 2015
1 parent 95cdada commit 9749a19
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/libstd/os/android/raw.rs
Expand Up @@ -12,6 +12,10 @@

#![stable(feature = "raw_ext", since = "1.1.0")]

use os::raw::c_long;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = c_long;

#[doc(inline)]
#[stable(feature = "raw_ext", since = "1.1.0")]
pub use self::arch::{dev_t, mode_t, blkcnt_t, blksize_t, ino_t, nlink_t, off_t, stat, time_t};
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/os/bitrig/raw.rs
Expand Up @@ -25,6 +25,8 @@ use os::unix::raw::{uid_t, gid_t};
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;

#[repr(C)]
#[derive(Clone)]
#[stable(feature = "raw_ext", since = "1.1.0")]
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/os/dragonfly/raw.rs
Expand Up @@ -25,6 +25,8 @@ use os::unix::raw::{uid_t, gid_t};
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;

#[repr(C)]
#[derive(Clone)]
#[stable(feature = "raw_ext", since = "1.1.0")]
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/os/freebsd/raw.rs
Expand Up @@ -21,6 +21,8 @@
#[stable(feature = "raw_ext", since = "1.1.0")] pub type blkcnt_t = i64;
#[stable(feature = "raw_ext", since = "1.1.0")] pub type fflags_t = u32;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;

#[doc(inline)]
#[stable(feature = "raw_ext", since = "1.1.0")]
pub use self::arch::{stat, time_t};
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/os/ios/raw.rs
Expand Up @@ -24,6 +24,8 @@ use os::unix::raw::{uid_t, gid_t};
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = c_long;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;

#[repr(C)]
#[derive(Clone)]
#[stable(feature = "raw_ext", since = "1.1.0")]
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/os/linux/raw.rs
Expand Up @@ -12,9 +12,13 @@

#![stable(feature = "raw_ext", since = "1.1.0")]

use os::raw::c_ulong;

#[stable(feature = "raw_ext", since = "1.1.0")] pub type dev_t = u64;
#[stable(feature = "raw_ext", since = "1.1.0")] pub type mode_t = u32;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = c_ulong;

#[doc(inline)]
#[stable(feature = "raw_ext", since = "1.1.0")]
pub use self::arch::{off_t, ino_t, nlink_t, blksize_t, blkcnt_t, stat, time_t};
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/os/macos/raw.rs
Expand Up @@ -24,6 +24,8 @@ use os::unix::raw::{uid_t, gid_t};
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = c_long;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;

#[repr(C)]
#[derive(Clone)]
#[stable(feature = "raw_ext", since = "1.1.0")]
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/os/nacl/raw.rs
Expand Up @@ -24,6 +24,8 @@
#[stable(feature = "raw_ext", since = "1.1.0")] pub type blksize_t = i32;
#[stable(feature = "raw_ext", since = "1.1.0")] pub type blkcnt_t = i32;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;

#[repr(C)]
#[derive(Copy, Clone)]
#[stable(feature = "raw_ext", since = "1.1.0")]
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/os/netbsd/raw.rs
Expand Up @@ -25,6 +25,8 @@ use os::unix::raw::{uid_t, gid_t};
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;

#[repr(C)]
#[derive(Clone)]
#[stable(feature = "raw_ext", since = "1.1.0")]
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/os/openbsd/raw.rs
Expand Up @@ -25,6 +25,8 @@ use os::unix::raw::{uid_t, gid_t};
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64;

#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = usize;

#[repr(C)]
#[derive(Clone)]
#[stable(feature = "raw_ext", since = "1.1.0")]
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys/unix/ext/mod.rs
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
3 changes: 3 additions & 0 deletions src/libstd/sys/unix/ext/raw.rs
Expand Up @@ -16,6 +16,9 @@
#[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;

#[doc(inline)]
#[unstable(feature = "pthread_t", issue = "29791")]
pub use sys::platform::raw::pthread_t;
#[doc(inline)]
#[stable(feature = "raw_ext", since = "1.1.0")]
pub use sys::platform::raw::{dev_t, ino_t, mode_t, nlink_t, off_t, blksize_t};
Expand Down
43 changes: 43 additions & 0 deletions src/libstd/sys/unix/ext/thread.rs
@@ -0,0 +1,43 @@
// 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.

#![unstable(feature = "thread_extensions", issue = "29791")]

use os::unix::raw::{pthread_t};
use sys_common::{AsInner, IntoInner};
use thread::{JoinHandle};

#[unstable(feature = "thread_extensions", issue = "29791")]
pub type RawPthread = pthread_t;

/// Unix-specific extensions to `std::thread::JoinHandle`
#[unstable(feature = "thread_extensions", issue = "29791")]
pub trait JoinHandleExt {
/// Extracts the raw pthread_t without taking ownership
fn as_pthread_t(&self) -> RawPthread;
/// 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) -> RawPthread;
}

#[unstable(feature = "thread_extensions", issue = "29791")]
impl<T> JoinHandleExt for JoinHandle<T> {
fn as_pthread_t(&self) -> RawPthread {
self.as_inner().id() as RawPthread
}
fn into_pthread_t(self) -> RawPthread {
self.into_inner().into_id() as RawPthread
}
}
8 changes: 8 additions & 0 deletions src/libstd/sys/unix/thread.rs
Expand Up @@ -152,6 +152,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

0 comments on commit 9749a19

Please sign in to comment.