Skip to main content

strat9_kernel/process/sched_classes/
nice.rs

1// SPDX-License-Identifier: MPL-2.0
2
3use core::sync::atomic::{AtomicI32, Ordering};
4
5/// A strongly typed wrapper over nice values.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
7pub struct NiceValue(i8);
8
9impl Default for NiceValue {
10    /// Builds a default instance.
11    fn default() -> Self {
12        Self(0)
13    }
14}
15
16impl NiceValue {
17    pub const MIN: Self = Self(-20);
18    pub const MAX: Self = Self(19);
19
20    /// Constructs a `NiceValue` from an integer.
21    /// Values outside the valid range [-20, 19] are saturated.
22    pub fn new(value: i8) -> Self {
23        Self(value.clamp(Self::MIN.0, Self::MAX.0))
24    }
25
26    /// Gets the inner primitive value.
27    pub const fn get(self) -> i8 {
28        self.0
29    }
30}
31
32/// Dynamic scheduling priority (nice value) for the FAIR class.
33#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
34pub struct Nice(NiceValue);
35
36impl Nice {
37    /// Creates a new instance.
38    pub fn new(value: i8) -> Self {
39        Self(NiceValue::new(value))
40    }
41
42    /// Performs the value operation.
43    pub const fn value(self) -> NiceValue {
44        self.0
45    }
46}
47
48pub struct AtomicNice(AtomicI32);
49
50impl AtomicNice {
51    /// Creates a new instance.
52    pub const fn new(nice: Nice) -> Self {
53        Self(AtomicI32::new(nice.0 .0 as i32))
54    }
55
56    /// Performs the load operation.
57    pub fn load(&self, order: Ordering) -> Nice {
58        Nice::new(self.0.load(order) as i8)
59    }
60
61    /// Performs the store operation.
62    pub fn store(&self, nice: Nice, order: Ordering) {
63        self.0.store(nice.0 .0 as i32, order)
64    }
65}