about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2017-10-26 03:02:48 +0800
committerGitHub <noreply@github.com>2017-10-26 03:02:48 +0800
commitbea6136b4a11d442d66256b6a27804348765fb22 (patch)
tree2c6aa3049ac098a53b3719e92ffef2d3f4505155 /src/libstd
parent2f5ae25a34077aad82a5dc41c2684cd21279df58 (diff)
parent29b319b6b20bb8e18b7e3c14528f9933de2f5e92 (diff)
downloadrust-bea6136b4a11d442d66256b6a27804348765fb22.tar.gz
rust-bea6136b4a11d442d66256b6a27804348765fb22.zip
Rollup merge of #45059 - tmccombs:pid, r=alexcrichton
Add current_pid function

Fixes #44971
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/process.rs19
-rw-r--r--src/libstd/sys/redox/os.rs4
-rw-r--r--src/libstd/sys/unix/os.rs4
-rw-r--r--src/libstd/sys/windows/os.rs4
4 files changed, 31 insertions, 0 deletions
diff --git a/src/libstd/process.rs b/src/libstd/process.rs
index 7c107177c64..c19ece6a314 100644
--- a/src/libstd/process.rs
+++ b/src/libstd/process.rs
@@ -1310,6 +1310,25 @@ pub fn abort() -> ! {
     unsafe { ::sys::abort_internal() };
 }
 
+/// Returns the OS-assigned process identifier associated with this process.
+///
+/// # Examples
+///
+/// Basic usage:
+///
+/// ```no_run
+/// #![feature(getpid)]
+/// use std::process;
+///
+/// println!("My pid is {}", process::id());
+/// ```
+///
+///
+#[unstable(feature = "getpid", issue = "44971", reason = "recently added")]
+pub fn id() -> u32 {
+    ::sys::os::getpid()
+}
+
 #[cfg(all(test, not(target_os = "emscripten")))]
 mod tests {
     use io::prelude::*;
diff --git a/src/libstd/sys/redox/os.rs b/src/libstd/sys/redox/os.rs
index efddd5f0294..c27e2ee172c 100644
--- a/src/libstd/sys/redox/os.rs
+++ b/src/libstd/sys/redox/os.rs
@@ -209,3 +209,7 @@ pub fn exit(code: i32) -> ! {
     let _ = syscall::exit(code as usize);
     unreachable!();
 }
+
+pub fn getpid() -> u32 {
+    syscall::getpid().unwrap() as u32
+}
diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs
index d8c30534eed..40b73f1b307 100644
--- a/src/libstd/sys/unix/os.rs
+++ b/src/libstd/sys/unix/os.rs
@@ -511,3 +511,7 @@ pub fn home_dir() -> Option<PathBuf> {
 pub fn exit(code: i32) -> ! {
     unsafe { libc::exit(code as c_int) }
 }
+
+pub fn getpid() -> u32 {
+    unsafe { libc::getpid() as u32 }
+}
diff --git a/src/libstd/sys/windows/os.rs b/src/libstd/sys/windows/os.rs
index a51b458451e..b9448243559 100644
--- a/src/libstd/sys/windows/os.rs
+++ b/src/libstd/sys/windows/os.rs
@@ -318,6 +318,10 @@ pub fn exit(code: i32) -> ! {
     unsafe { c::ExitProcess(code as c::UINT) }
 }
 
+pub fn getpid() -> u32 {
+    unsafe { c::GetCurrentProcessId() as u32 }
+}
+
 #[cfg(test)]
 mod tests {
     use io::Error;