1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use crate::{Error, ThreadPriority};
pub type ThreadId = libc::pthread_t;
pub struct ScheduleParams {
pub sched_priority: libc::c_int,
}
impl ScheduleParams {
#[cfg(not(target_env = "musl"))]
fn into_posix(self) -> libc::sched_param {
libc::sched_param {
sched_priority: self.sched_priority,
}
}
#[cfg(target_env = "musl")]
fn into_posix(self) -> libc::sched_param {
use libc::timespec as TimeSpec;
libc::sched_param {
sched_priority: self.sched_priority,
sched_ss_low_priority: 0,
sched_ss_repl_period: TimeSpec {
tv_sec: 0,
tv_nsec: 0,
},
sched_ss_init_budget: TimeSpec {
tv_sec: 0,
tv_nsec: 0,
},
sched_ss_max_repl: 0,
}
}
fn from_posix(sched_param: libc::sched_param) -> Self {
ScheduleParams {
sched_priority: sched_param.sched_priority,
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum RealtimeThreadSchedulePolicy {
Fifo,
RoundRobin,
}
impl RealtimeThreadSchedulePolicy {
fn to_posix(self) -> libc::c_int {
match self {
RealtimeThreadSchedulePolicy::Fifo => 1,
RealtimeThreadSchedulePolicy::RoundRobin => 2,
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum NormalThreadSchedulePolicy {
Idle,
Batch,
Other,
Normal,
}
impl NormalThreadSchedulePolicy {
fn to_posix(self) -> libc::c_int {
match self {
NormalThreadSchedulePolicy::Idle => 5,
NormalThreadSchedulePolicy::Batch => 3,
NormalThreadSchedulePolicy::Other | NormalThreadSchedulePolicy::Normal => 0,
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum ThreadSchedulePolicy {
Normal(NormalThreadSchedulePolicy),
Realtime(RealtimeThreadSchedulePolicy),
}
impl ThreadSchedulePolicy {
fn to_posix(self) -> libc::c_int {
match self {
ThreadSchedulePolicy::Normal(p) => p.to_posix(),
ThreadSchedulePolicy::Realtime(p) => p.to_posix(),
}
}
fn from_posix(policy: libc::c_int) -> Result<ThreadSchedulePolicy, Error> {
match policy {
0 => Ok(ThreadSchedulePolicy::Normal(
NormalThreadSchedulePolicy::Normal,
)),
3 => Ok(ThreadSchedulePolicy::Normal(
NormalThreadSchedulePolicy::Batch,
)),
5 => Ok(ThreadSchedulePolicy::Normal(
NormalThreadSchedulePolicy::Idle,
)),
1 => Ok(ThreadSchedulePolicy::Realtime(
RealtimeThreadSchedulePolicy::Fifo,
)),
2 => Ok(ThreadSchedulePolicy::Realtime(
RealtimeThreadSchedulePolicy::RoundRobin,
)),
_ => Err(Error::Ffi("Can't parse schedule policy from posix")),
}
}
}
impl ThreadPriority {
pub fn to_posix(self, policy: ThreadSchedulePolicy) -> Result<libc::c_int, Error> {
let ret = match self {
ThreadPriority::Min => match policy {
ThreadSchedulePolicy::Realtime(_) => Ok(1),
_ => Ok(0),
},
ThreadPriority::Specific(p) => match policy {
ThreadSchedulePolicy::Realtime(_) if (p == 0 || p > 99) => {
Err(Error::Priority("The value is out of range [0; 99]"))
}
ThreadSchedulePolicy::Normal(_) if p != 0 => Err(Error::Priority(
"The value can be only 0 for normal scheduling policy",
)),
_ => Ok(p),
},
ThreadPriority::Max => match policy {
ThreadSchedulePolicy::Realtime(_) => Ok(99),
_ => Ok(0),
},
};
ret.map(|p| p as libc::c_int)
}
pub fn from_posix(params: ScheduleParams) -> ThreadPriority {
ThreadPriority::Specific(params.sched_priority as u32)
}
}
pub fn set_thread_priority_and_policy(
native: ThreadId,
priority: ThreadPriority,
policy: ThreadSchedulePolicy,
) -> Result<(), Error> {
let params = ScheduleParams {
sched_priority: priority.to_posix(policy)?,
};
set_thread_schedule_policy(native, policy, params)
}
pub fn set_current_thread_priority(priority: ThreadPriority) -> Result<(), Error> {
let thread_id = thread_native_id();
let policy = ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Normal);
set_thread_priority_and_policy(thread_id, priority, policy)
}
pub fn thread_schedule_policy() -> Result<ThreadSchedulePolicy, Error> {
unsafe { ThreadSchedulePolicy::from_posix(libc::sched_getscheduler(libc::getpid())) }
}
pub fn set_thread_schedule_policy(
native: ThreadId,
policy: ThreadSchedulePolicy,
params: ScheduleParams,
) -> Result<(), Error> {
let params = params.into_posix();
unsafe {
let ret = libc::pthread_setschedparam(
native,
policy.to_posix(),
¶ms as *const libc::sched_param,
);
match ret {
0 => Ok(()),
e => Err(Error::OS(e)),
}
}
}
pub fn thread_schedule_policy_param(
native: ThreadId,
) -> Result<(ThreadSchedulePolicy, ScheduleParams), Error> {
unsafe {
let mut policy = 0i32;
let mut params = ScheduleParams { sched_priority: 0 }.into_posix();
let ret = libc::pthread_getschedparam(
native,
&mut policy as *mut libc::c_int,
&mut params as *mut libc::sched_param,
);
match ret {
0 => Ok((
ThreadSchedulePolicy::from_posix(policy)?,
ScheduleParams::from_posix(params),
)),
e => Err(Error::OS(e)),
}
}
}
pub fn thread_priority() -> Result<ThreadPriority, Error> {
Ok(ThreadPriority::from_posix(
thread_schedule_policy_param(thread_native_id())?.1,
))
}
pub fn thread_native_id() -> ThreadId {
unsafe { libc::pthread_self() }
}
#[cfg(test)]
mod tests {
use crate::unix::*;
#[test]
fn thread_schedule_policy_param_test() {
let thread_id = thread_native_id();
assert!(thread_schedule_policy_param(thread_id).is_ok());
}
#[test]
fn set_thread_priority_test() {
let thread_id = thread_native_id();
assert!(set_thread_priority_and_policy(
thread_id,
ThreadPriority::Min,
ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Normal)
)
.is_ok());
assert!(set_thread_priority_and_policy(
thread_id,
ThreadPriority::Max,
ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Normal)
)
.is_ok());
assert!(set_thread_priority_and_policy(
thread_id,
ThreadPriority::Specific(0),
ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Normal)
)
.is_ok());
}
}