about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorPeter Atashian <retep998@gmail.com>2015-12-03 21:58:00 -0500
committerPeter Atashian <retep998@gmail.com>2015-12-04 20:09:32 -0500
commit9749a193d629dd723562004879bca3e2d47e25a1 (patch)
treeaa8f19d13f1c3379eafc4332436db308e84b6f84 /src/libstd/sys
parent95cdada99a02c5abea4898f46e04f7a271419dac (diff)
downloadrust-9749a193d629dd723562004879bca3e2d47e25a1.tar.gz
rust-9749a193d629dd723562004879bca3e2d47e25a1.zip
Add JoinHandleExt to get the pthread_t on unix platforms
Signed-off-by: Peter Atashian <retep998@gmail.com>
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/unix/ext/mod.rs1
-rw-r--r--src/libstd/sys/unix/ext/raw.rs3
-rw-r--r--src/libstd/sys/unix/ext/thread.rs43
-rw-r--r--src/libstd/sys/unix/thread.rs8
4 files changed, 55 insertions, 0 deletions
diff --git a/src/libstd/sys/unix/ext/mod.rs b/src/libstd/sys/unix/ext/mod.rs
index b57e325089d..276ef25a03a 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 8377e53f417..d1cc6cba51d 100644
--- a/src/libstd/sys/unix/ext/raw.rs
+++ b/src/libstd/sys/unix/ext/raw.rs
@@ -17,6 +17,9 @@
 #[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};
 #[doc(inline)]
diff --git a/src/libstd/sys/unix/ext/thread.rs b/src/libstd/sys/unix/ext/thread.rs
new file mode 100644
index 00000000000..c1c1609632a
--- /dev/null
+++ b/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
+    }
+}
diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs
index f111f97be60..4d715b579c6 100644
--- a/src/libstd/sys/unix/thread.rs
+++ b/src/libstd/sys/unix/thread.rs
@@ -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 {