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) ]
76pub 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}
0 commit comments