@@ -7,7 +7,8 @@ use crate::{
77
88#[ derive( Clone , Copy ) ]
99pub ( crate ) struct MaxPriority {
10- max : usize ,
10+ // Using Priority here tells the compiler about the maximum value, removing bounds checks.
11+ max : Priority ,
1112 mask : usize ,
1213}
1314
@@ -18,22 +19,26 @@ impl MaxPriority {
1819 } ;
1920
2021 const fn new ( ) -> Self {
21- Self { max : 0 , mask : 0 }
22+ Self {
23+ max : Priority :: ZERO ,
24+ mask : 0 ,
25+ }
2226 }
2327
24- fn mark_ready ( & mut self , level : usize ) {
25- self . max = self . max . max ( level ) ;
26- self . mask |= 1 << level;
28+ fn mark_ready ( & mut self , level : Priority ) {
29+ self . max = if level > self . max { level } else { self . max } ;
30+ self . mask |= 1 << level. get ( ) ;
2731 }
2832
2933 fn unmark ( & mut self , level : usize ) {
3034 self . mask &= !( 1 << level) ;
31- self . max = Self :: MAX_PRIORITY . saturating_sub ( self . mask . leading_zeros ( ) as usize ) ;
35+ self . max =
36+ Priority :: new ( Self :: MAX_PRIORITY . saturating_sub ( self . mask . leading_zeros ( ) as usize ) ) ;
3237 }
3338
3439 fn ready ( & self ) -> usize {
3540 // Priority 0 must always be ready
36- self . max
41+ self . max . get ( )
3742 }
3843}
3944
@@ -158,26 +163,27 @@ impl RunQueue {
158163 _state : & [ CpuSchedulerState ; Cpu :: COUNT ] ,
159164 mut ready_task : TaskPtr ,
160165 ) -> RunSchedulerOn {
161- let priority = ready_task. priority ( self ) . get ( ) ;
166+ let priority = ready_task. priority ( self ) ;
167+ let priority_n = priority. get ( ) ;
162168
163169 ready_task. set_state ( TaskState :: Ready ) ;
164170 if let Some ( mut containing_queue) = unsafe { ready_task. as_mut ( ) . current_queue . take ( ) } {
165171 unsafe {
166172 containing_queue. as_mut ( ) . remove ( ready_task) ;
167173 }
168174 }
169- self . ready_tasks [ priority ] . remove ( ready_task) ;
170- self . ready_tasks [ priority ] . push ( ready_task) ;
175+ self . ready_tasks [ priority_n ] . remove ( ready_task) ;
176+ self . ready_tasks [ priority_n ] . push ( ready_task) ;
171177
172178 cfg_if:: cfg_if! {
173179 if #[ cfg( multi_core) ] {
174180 let run_on = if _state[ 1 ] . initialized {
175181 self . select_scheduler_trigger_multi_core( _state, ready_task)
176182 } else {
177- self . select_scheduler_trigger_single_core( priority )
183+ self . select_scheduler_trigger_single_core( priority_n )
178184 } ;
179185 } else {
180- let run_on = self . select_scheduler_trigger_single_core( priority ) ;
186+ let run_on = self . select_scheduler_trigger_single_core( priority_n ) ;
181187 }
182188 }
183189
0 commit comments