Skip to content

Add platform specific casts #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: rust
os: linux
os:
- linux
- osx
rust:
- stable
- nightly
Expand Down
95 changes: 82 additions & 13 deletions src/resource.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Set and get program scheduling priority
use errno::{Errno, errno, set_errno};
use libc::{PRIO_PROCESS,PRIO_PGRP,PRIO_USER,setpriority,getpriority, id_t};
use libc::{PRIO_PROCESS,PRIO_PGRP,PRIO_USER};

///! Set and get program scheduling priority
/// Which identifier type to use (`pid`, `gid`, or `uid`)
#[allow(missing_docs)]
pub enum Which {
Expand All @@ -26,11 +25,7 @@ pub fn set_priority(which: Which, who: i32, priority: i32) -> Result<(), ()> {
Which::Group => PRIO_PGRP,
Which::User => PRIO_USER,
};

match unsafe { setpriority(c_which as u32, who as id_t, priority) } {
0 => Ok(()),
_ => Err(()),
}
platform::set_priority(c_which, who, priority)
}

/// Get the scheduling priority for the `Which` of the calling process
Expand All @@ -45,11 +40,85 @@ pub fn get_priority(which: Which, who: i32) -> Result<i32, ()> {
Which::Group => PRIO_PGRP,
Which::User => PRIO_USER,
};
platform::get_priority(c_which, who)
}

mod platform {
use errno::{Errno, errno, set_errno};
use libc::{setpriority,getpriority};

// glibc
#[cfg(target_env="gnu")]
pub fn get_priority(which: i32, who: i32) -> Result<i32, ()> {
set_errno(Errno(0));
let priority = unsafe { getpriority(which as u32, who as u32) };
match errno().0 {
0 => Ok(priority),
_ => Err(()),
}
}

#[cfg(target_env="gnu")]
pub fn set_priority(which: i32, who: i32, priority: i32) -> Result<(), ()> {
match unsafe { setpriority(which as u32, who as u32, priority) } {
0 => Ok(()),
_ => Err(()),
}
}

#[cfg(target_env="musl")]
pub fn get_priority(which: i32, who: i32) -> Result<i32, ()> {
set_errno(Errno(0));
let priority = unsafe { getpriority(which, who as u32) };
match errno().0 {
0 => Ok(priority),
_ => Err(()),
}
}

#[cfg(target_env="musl")]
pub fn set_priority(which: i32, who: i32, priority: i32) -> Result<(), ()> {
match unsafe { setpriority(which, who as u32, priority) } {
0 => Ok(()),
_ => Err(()),
}
}

// FreeBSD
#[cfg(target_os="freebsd")]
pub fn get_priority(which: i32, who: i32) -> Result<i32, ()> {
set_errno(Errno(0));
let priority = unsafe { getpriority(which, who) };
match errno().0 {
0 => Ok(priority),
_ => Err(()),
}
}

#[cfg(target_os="freebsd")]
pub fn set_priority(which: i32, who: i32, priority: i32) -> Result<(), ()> {
match unsafe { setpriority(which, who, priority) } {
0 => Ok(()),
_ => Err(()),
}
}

// OS X
#[cfg(target_os="macos")]
pub fn get_priority(which: i32, who: i32) -> Result<i32, ()> {
set_errno(Errno(0));
let priority = unsafe { getpriority(which, who as u32) };
match errno().0 {
0 => Ok(priority),
_ => Err(()),
}
}

set_errno(Errno(0));
let priority = unsafe { getpriority(c_which as u32, who as id_t) };
match errno().0 {
0 => Ok(priority),
_ => Err(()),
#[cfg(target_os="macos")]
pub fn set_priority(which: i32, who: i32, priority: i32) -> Result<(), ()> {
match unsafe { setpriority(which, who as u32, priority) } {
0 => Ok(()),
_ => Err(()),
}
}
}
4 changes: 3 additions & 1 deletion src/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn set_self_policy(policy: Policy, priority: i32) -> Result<(), ()> {
/// Set the scheduling policy for a process
#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
pub fn set_policy(pid: i32, policy: Policy, priority: i32) -> Result<(), ()> {
use std::mem;
let c_policy = match policy {
Policy::Other => SCHED_OTHER,
Policy::Fifo => SCHED_FIFO,
Expand All @@ -42,7 +43,8 @@ pub fn set_policy(pid: i32, policy: Policy, priority: i32) -> Result<(), ()> {
Policy::Idle => SCHED_IDLE,
Policy::Deadline => SCHED_DEADLINE,
};
let params = sched_param { sched_priority: priority };
let mut params: sched_param = unsafe { mem::zeroed() };
params.sched_priority = priority;
let params_ptr: *const sched_param = &params;

match unsafe { sched_setscheduler(pid, c_policy, params_ptr) } {
Expand Down