Skip to content

Commit

Permalink
Limit the use of "get_native_id()" to only current thread.
Browse files Browse the repository at this point in the history
This is a part of #30

We can't get another thread's ID if we didn't store it when it was
created. As we don't have control over that, we can't properly
implement the `ThreadExt::get_native_id()` for any thread which isn't
current.
  • Loading branch information
iddm committed Mar 16, 2023
1 parent a90c8e9 commit 3d450e5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "thread-priority"
version = "0.14.0"
version = "0.15.0"
authors = ["Victor Polevoy <[email protected]>"]
description = "Library for managing threads priority and schedule policies"
repository = "https://github.com/vityafx/thread-priority"
Expand Down
16 changes: 11 additions & 5 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ThreadId, Error>;
}

/// 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<ThreadId, Error> {
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,
Expand Down
15 changes: 11 additions & 4 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ThreadId, Error>;

/// Sets current thread's ideal processor.
/// For more info see [`set_current_thread_ideal_processor`].
///
Expand Down Expand Up @@ -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<ThreadId, Error> {
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."))
}
}
}
31 changes: 31 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 3d450e5

Please sign in to comment.