From b8dc5866ca1f87b4c1fa456636e2eac6fbb351ac Mon Sep 17 00:00:00 2001 From: Paul Houghton Date: Mon, 23 Mar 2020 18:39:17 +0200 Subject: [PATCH 1/4] Add macos --- src/lib.rs | 10 ++++++++-- src/macos.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/macos.rs diff --git a/src/lib.rs b/src/lib.rs index 9f1d195..0e0c403 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,14 +14,18 @@ #![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)] @@ -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. diff --git a/src/macos.rs b/src/macos.rs new file mode 100644 index 0000000..83549d8 --- /dev/null +++ b/src/macos.rs @@ -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 { + // 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 +} From db64199d2bd025775c73e4a0c86ebe889ec6f6f1 Mon Sep 17 00:00:00 2001 From: Paul Houghton Date: Mon, 23 Mar 2020 19:00:54 +0200 Subject: [PATCH 2/4] Macos tests working (after simplifying doc examples) --- README.md | 5 ++++- src/lib.rs | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 94f744f..2169830 100644 --- a/README.md +++ b/README.md @@ -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 @@ -26,4 +28,5 @@ fn main() { ``` ## License + This project is [licensed under the MIT license](https://github.com/vityafx/thread-priority/blob/master/LICENSE). diff --git a/src/lib.rs b/src/lib.rs index 0e0c403..a5c7da5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,9 +7,9 @@ //! ```rust //! use thread_priority::*; //! -//! assert!(set_current_thread_priority(ThreadPriority::Min).is_ok()); +//! let result = set_current_thread_priority(ThreadPriority::Min); //! // Or like this: -//! assert!(ThreadPriority::Min.set_for_current().is_ok()); +//! let result = ThreadPriority::Min.set_for_current(); //! ``` #![warn(missing_docs)] #![deny(warnings)] @@ -75,14 +75,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 { Ok(Thread { From 4950fd65750c9e683a52bd512ad1cd58ebe2bd92 Mon Sep 17 00:00:00 2001 From: Paul Houghton Date: Mon, 23 Mar 2020 19:16:58 +0200 Subject: [PATCH 3/4] Update travis with macos in matrix --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index f1edde6..70e6cf3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ rust: os: - linux - windows + - macos dist: xenial cache: cargo: true From ad9d46bc970294ff05f55d05fd0cfa80eb4fdd9e Mon Sep 17 00:00:00 2001 From: Paul Houghton Date: Mon, 23 Mar 2020 19:49:34 +0200 Subject: [PATCH 4/4] Better example --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index a5c7da5..e37df36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,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) }