kernel/scheduler/priority.rs
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15/// Represents the priority of a thread.
16///
17/// Note: An enum is used to provide guarantees to the compiler about the
18/// range of values that can be used. An example of where this is useful is that
19/// it can allow the compiler to elide bounds checks for an array indexed by
20/// Priority. Another example is that it allows for eliding valid range checks
21/// in bitmask operations. Downsides include that the Priority declaration is
22/// quite verbose and normal integer operations are not straightforward.
23///
24/// This is designed such that:
25/// - The priority can be used as an index in an array.
26/// - An array indexed by all priorities will be compact with no gaps.
27///
28/// A choice was made to explicitly support 32 priorities initially with the
29/// possibility of customizing the Priority type being supported in the future.
30#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
31#[repr(u8)]
32pub enum Priority {
33 Level0 = 0,
34 Level1 = 1,
35 Level2 = 2,
36 Level3 = 3,
37 Level4 = 4,
38 Level5 = 5,
39 Level6 = 6,
40 Level7 = 7,
41 Level8 = 8,
42 Level9 = 9,
43 Level10 = 10,
44 Level11 = 11,
45 Level12 = 12,
46 Level13 = 13,
47 Level14 = 14,
48 Level15 = 15,
49 Level16 = 16,
50 Level17 = 17,
51 Level18 = 18,
52 Level19 = 19,
53 Level20 = 20,
54 Level21 = 21,
55 Level22 = 22,
56 Level23 = 23,
57 Level24 = 24,
58 Level25 = 25,
59 Level26 = 26,
60 Level27 = 27,
61 Level28 = 28,
62 Level29 = 29,
63 Level30 = 30,
64 Level31 = 31,
65}
66
67impl Priority {
68 pub const IDLE_PRIORITY: Priority = Priority::Level0;
69 pub const DEFAULT_PRIORITY: Priority = Priority::Level15;
70 /// The total number of priorities supported.
71 pub const NUM_PRIORITIES: usize = Priority::Level31 as usize + 1;
72}