Skip to content

Commit 83a8287

Browse files
committed
Merge remote-tracking branch 'polachok/portability'
2 parents 5cf3468 + 2df379a commit 83a8287

3 files changed

Lines changed: 88 additions & 15 deletions

File tree

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
language: rust
2-
os: linux
2+
os:
3+
- linux
4+
- osx
35
rust:
46
- stable
57
- nightly

src/resource.rs

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
//! Set and get program scheduling priority
2-
use errno::{Errno, errno, set_errno};
3-
use libc::{PRIO_PROCESS,PRIO_PGRP,PRIO_USER,setpriority,getpriority, id_t};
1+
use libc::{PRIO_PROCESS,PRIO_PGRP,PRIO_USER};
42

3+
///! Set and get program scheduling priority
54
/// Which identifier type to use (`pid`, `gid`, or `uid`)
65
#[allow(missing_docs)]
76
pub enum Which {
@@ -26,11 +25,7 @@ pub fn set_priority(which: Which, who: i32, priority: i32) -> Result<(), ()> {
2625
Which::Group => PRIO_PGRP,
2726
Which::User => PRIO_USER,
2827
};
29-
30-
match unsafe { setpriority(c_which as u32, who as id_t, priority) } {
31-
0 => Ok(()),
32-
_ => Err(()),
33-
}
28+
platform::set_priority(c_which, who, priority)
3429
}
3530

3631
/// Get the scheduling priority for the `Which` of the calling process
@@ -45,11 +40,85 @@ pub fn get_priority(which: Which, who: i32) -> Result<i32, ()> {
4540
Which::Group => PRIO_PGRP,
4641
Which::User => PRIO_USER,
4742
};
43+
platform::get_priority(c_which, who)
44+
}
45+
46+
mod platform {
47+
use errno::{Errno, errno, set_errno};
48+
use libc::{setpriority,getpriority};
49+
50+
// glibc
51+
#[cfg(target_env="gnu")]
52+
pub fn get_priority(which: i32, who: i32) -> Result<i32, ()> {
53+
set_errno(Errno(0));
54+
let priority = unsafe { getpriority(which as u32, who as u32) };
55+
match errno().0 {
56+
0 => Ok(priority),
57+
_ => Err(()),
58+
}
59+
}
60+
61+
#[cfg(target_env="gnu")]
62+
pub fn set_priority(which: i32, who: i32, priority: i32) -> Result<(), ()> {
63+
match unsafe { setpriority(which as u32, who as u32, priority) } {
64+
0 => Ok(()),
65+
_ => Err(()),
66+
}
67+
}
68+
69+
#[cfg(target_env="musl")]
70+
pub fn get_priority(which: i32, who: i32) -> Result<i32, ()> {
71+
set_errno(Errno(0));
72+
let priority = unsafe { getpriority(which, who as u32) };
73+
match errno().0 {
74+
0 => Ok(priority),
75+
_ => Err(()),
76+
}
77+
}
78+
79+
#[cfg(target_env="musl")]
80+
pub fn set_priority(which: i32, who: i32, priority: i32) -> Result<(), ()> {
81+
match unsafe { setpriority(which, who as u32, priority) } {
82+
0 => Ok(()),
83+
_ => Err(()),
84+
}
85+
}
86+
87+
// FreeBSD
88+
#[cfg(target_os="freebsd")]
89+
pub fn get_priority(which: i32, who: i32) -> Result<i32, ()> {
90+
set_errno(Errno(0));
91+
let priority = unsafe { getpriority(which, who) };
92+
match errno().0 {
93+
0 => Ok(priority),
94+
_ => Err(()),
95+
}
96+
}
97+
98+
#[cfg(target_os="freebsd")]
99+
pub fn set_priority(which: i32, who: i32, priority: i32) -> Result<(), ()> {
100+
match unsafe { setpriority(which, who, priority) } {
101+
0 => Ok(()),
102+
_ => Err(()),
103+
}
104+
}
105+
106+
// OS X
107+
#[cfg(target_os="macos")]
108+
pub fn get_priority(which: i32, who: i32) -> Result<i32, ()> {
109+
set_errno(Errno(0));
110+
let priority = unsafe { getpriority(which, who as u32) };
111+
match errno().0 {
112+
0 => Ok(priority),
113+
_ => Err(()),
114+
}
115+
}
48116

49-
set_errno(Errno(0));
50-
let priority = unsafe { getpriority(c_which as u32, who as id_t) };
51-
match errno().0 {
52-
0 => Ok(priority),
53-
_ => Err(()),
117+
#[cfg(target_os="macos")]
118+
pub fn set_priority(which: i32, who: i32, priority: i32) -> Result<(), ()> {
119+
match unsafe { setpriority(which, who as u32, priority) } {
120+
0 => Ok(()),
121+
_ => Err(()),
122+
}
54123
}
55124
}

src/sched.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub fn set_self_policy(policy: Policy, priority: i32) -> Result<(), ()> {
3434
/// Set the scheduling policy for a process
3535
#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
3636
pub fn set_policy(pid: i32, policy: Policy, priority: i32) -> Result<(), ()> {
37+
use std::mem;
3738
let c_policy = match policy {
3839
Policy::Other => SCHED_OTHER,
3940
Policy::Fifo => SCHED_FIFO,
@@ -42,7 +43,8 @@ pub fn set_policy(pid: i32, policy: Policy, priority: i32) -> Result<(), ()> {
4243
Policy::Idle => SCHED_IDLE,
4344
Policy::Deadline => SCHED_DEADLINE,
4445
};
45-
let params = sched_param { sched_priority: priority };
46+
let mut params: sched_param = unsafe { mem::zeroed() };
47+
params.sched_priority = priority;
4648
let params_ptr: *const sched_param = &params;
4749

4850
match unsafe { sched_setscheduler(pid, c_policy, params_ptr) } {

0 commit comments

Comments
 (0)