Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ rust:
os:
- linux
- windows
- macos
dist: xenial
cache:
cargo: true
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# thread-priority

[![Build status](https://travis-ci.org/vityafx/thread-priority.svg?branch=master)](https://travis-ci.org/vityafx/thread-priority)
[![Crates](https://img.shields.io/crates/v/thread-priority.svg)](https://crates.io/crates/thread-priority)
[![Docs](https://docs.rs/thread-priority/badge.svg)](https://docs.rs/thread-priority)
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)


A simple library to control thread schedule policies and thread priority.

This crate does not support all the plaforms yet but it is inteded to be developed so,
so feel free to contribute!

## Supported platforms

- Linux
- Windows

## Example

Setting current thread's priority to minimum:

```rust,no_run
Expand All @@ -26,4 +28,5 @@ fn main() {
```

## License

This project is [licensed under the MIT license](https://github.com/vityafx/thread-priority/blob/master/LICENSE).
28 changes: 22 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@
//! ```rust
//! use thread_priority::*;
//!
//! assert!(set_current_thread_priority(ThreadPriority::Min).is_ok());
//! let result = set_current_thread_priority(ThreadPriority::Min);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I ask why did you remove the assertion? This was an example and a doc-test which was running and which was testing the library...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On macOS it blows an error because the implementation there is a stub

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the pattern which works in my application. "optional" thread priority change.

// Ignore the response- we just keep going even if priorty can not be boosted on a given platform
    let _ = thread_priority::set_current_thread_priority(thread_priority::ThreadPriority::Max);

So you can pay attention to whether it is implemented for the current OS, but as an app developer you don't have to.

//! // Or like this:
//! assert!(ThreadPriority::Min.set_for_current().is_ok());
//! let result = ThreadPriority::Min.set_for_current();
//! ```
#![warn(missing_docs)]
#![deny(warnings)]

#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
pub mod unix;
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
pub use unix::*;
#[cfg(windows)]
pub mod windows;
#[cfg(windows)]
pub use windows::*;
#[cfg(target_os = "macos")]
pub mod macos;
#[cfg(target_os = "macos")]
pub use macos::*;

/// A error type
#[derive(Debug, Copy, Clone)]
Expand All @@ -33,6 +37,8 @@ pub enum Error {
OS(i32),
/// FFI failure
Ffi(&'static str),
/// Attempt to probe values on an platform which is not supported (macOS..)
UnsupportedPlatform(),
}

/// Thread priority enumeration.
Expand All @@ -54,6 +60,16 @@ pub enum ThreadPriority {

impl ThreadPriority {
/// Sets current thread's priority to this value.
///
///
/// # Usage
///
/// ```rust
/// use thread_priority::*;
///
/// // Ignore the response- we just keep going even if priority changes on a given platform are not supported (macOS)
/// let _ = set_current_thread_priority(ThreadPriority::Max);
/// ```
pub fn set_for_current(self) -> Result<(), Error> {
set_current_thread_priority(self)
}
Expand All @@ -69,14 +85,14 @@ pub struct Thread {
}

impl Thread {
/// Get current thread.
/// Get current thread as a platform-independent structure
///
/// # Usage
///
/// ```rust
/// use thread_priority::*;
///
/// assert!(Thread::current().is_ok());
/// let thread = Thread::current();
/// ```
pub fn current() -> Result<Thread, Error> {
Ok(Thread {
Expand Down
27 changes: 27 additions & 0 deletions src/macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! This module defines the unix thread control.
//!
//! The crate's prelude doesn't have much control over
//! the unix threads, and this module provides
//! better control over those.

use crate::{Error, ThreadPriority};

/// Return a placeholder type for the ThreadId. This is used in association with other types which are always errors on this unsupported platform (macOS) so this allows complilation and calling code has the option to handle the error gracefully at runtime
pub type ThreadId = i32;

/// Set current thread's priority.
pub fn set_current_thread_priority(_priority: ThreadPriority) -> Result<(), Error> {
// Silently do nothing- not a supported platform- priority will remain unchanged
Err(Error::UnsupportedPlatform())
}

/// Get current thread's priority value.
pub fn thread_priority() -> Result<ThreadPriority, Error> {
// Indicate that this is not a supported platform
Err(Error::UnsupportedPlatform())
}

/// Returns a placeholder for the current thread id. This is used in association with other types which are always errors on this unsupported platform (macOS) so this allows complilation and calling code has the option to handle the error gracefully at runtime
pub fn thread_native_id() -> ThreadId {
0
}