diff --git a/Cargo.toml b/Cargo.toml index cb3f470..53a3448 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "thread-priority" -version = "0.14.0" +version = "0.15.0" authors = ["Victor Polevoy "] description = "Library for managing threads priority and schedule policies" repository = "https://github.com/vityafx/thread-priority" diff --git a/src/unix.rs b/src/unix.rs index 15802b1..3a51b5d 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -741,14 +741,20 @@ pub trait ThreadExt { /// ```rust /// use thread_priority::*; /// - /// assert!(std::thread::current().get_native_id() > 0); - fn get_native_id(&self) -> ThreadId { - thread_native_id() - } + /// assert!(std::thread::current().get_native_id().unwrap() > 0); + fn get_native_id(&self) -> Result; } /// Auto-implementation of this trait for the [`std::thread::Thread`]. -impl ThreadExt for std::thread::Thread {} +impl ThreadExt for std::thread::Thread { + fn get_native_id(&self) -> Result { + if self.id() == std::thread::current().id() { + Ok(thread_native_id()) + } else { + Err(Error::Priority("The `ThreadExt::get_native_id()` is currently limited to be called on the current thread.")) + } + } +} /// Returns current thread id, which is the current OS's native handle. /// It may or may not be equal or even related to rust's thread id, diff --git a/src/windows.rs b/src/windows.rs index 372daad..2867484 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -405,9 +405,8 @@ pub trait ThreadExt { /// /// assert!(!std::thread::current().get_native_id().is_null()); /// ``` - fn get_native_id(&self) -> ThreadId { - thread_native_id() - } + fn get_native_id(&self) -> Result; + /// Sets current thread's ideal processor. /// For more info see [`set_current_thread_ideal_processor`]. /// @@ -437,4 +436,12 @@ pub trait ThreadExt { } /// Auto-implementation of this trait for the [`std::thread::Thread`]. -impl ThreadExt for std::thread::Thread {} +impl ThreadExt for std::thread::Thread { + fn get_native_id(&self) -> Result { + if self.id() == std::thread::current().id() { + Ok(thread_native_id()) + } else { + Err(Error::Priority("The `ThreadExt::get_native_id()` is currently limited to be called on the current thread.")) + } + } +} diff --git a/tests/common.rs b/tests/common.rs index 2e39cc5..95495d3 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -6,3 +6,34 @@ fn should_be_possible_to_reset_the_same_priority() { let set_result = thread_priority::set_current_thread_priority(current); assert_eq!(set_result, Ok(())); } + +#[rstest] +fn should_be_possible_to_get_current_thread_native_id_via_threadext() { + use thread_priority::ThreadExt; + + let current = std::thread::current(); + #[cfg(unix)] + assert_eq!( + current.get_native_id(), + Ok(thread_priority::unix::thread_native_id()) + ); + #[cfg(windows)] + assert_eq!( + current.get_native_id(), + Ok(thread_priority::windows::thread_native_id()) + ); +} + +#[rstest] +fn should_be_impossible_to_get_other_thread_native_id_via_threadext() { + use thread_priority::ThreadExt; + + let current = std::thread::current(); + let another_thread = std::thread::spawn(move || { + #[cfg(unix)] + assert!(current.get_native_id().is_err()); + #[cfg(windows)] + assert!(current.get_native_id().is_err()); + }); + another_thread.join().unwrap(); +}