@@ -18,7 +18,10 @@ use crate::{
1818 task:: Task ,
1919 } ,
2020 sched:: Scheduler ,
21- uapi,
21+ uapi:: {
22+ self ,
23+ resource:: { PRIO_MAX , PRIO_MIN } ,
24+ } ,
2225 util:: {
2326 event:: Event ,
2427 mutex:: { Mutex , spin:: SpinMutex } ,
@@ -40,7 +43,7 @@ use alloc::{
4043} ;
4144use core:: {
4245 mem,
43- sync:: atomic:: { AtomicBool , AtomicU32 , AtomicUsize , Ordering } ,
46+ sync:: atomic:: { AtomicBool , AtomicI8 , AtomicU32 , AtomicUsize , Ordering } ,
4447 time:: Duration ,
4548} ;
4649
@@ -59,6 +62,8 @@ pub struct Process {
5962 name : SpinMutex < String > ,
6063 /// The parent of this process, or [`None`], if this is the init process.
6164 parent : SpinMutex < Option < Weak < Process > > > ,
65+ /// A value between -20 and 19, where -20 is the highest priority and 0 is a neutral priority.
66+ nice : AtomicI8 ,
6267 /// A list of [`Task`]s associated with this process.
6368 pub threads : SpinMutex < Vec < Arc < Task > > > ,
6469 /// The address space for this process.
@@ -137,6 +142,18 @@ impl Process {
137142 self . parent . lock ( ) . as_ref ( ) . and_then ( Weak :: upgrade)
138143 }
139144
145+ pub const fn clamp_nice ( nice : i32 ) -> i8 {
146+ nice. clamp ( PRIO_MIN as i32 , PRIO_MAX as i32 - 1 ) as i8
147+ }
148+
149+ pub fn set_nice ( & self , nice : i32 ) {
150+ self . nice . store ( Self :: clamp_nice ( nice) , Ordering :: Relaxed ) ;
151+ }
152+
153+ pub fn get_nice ( & self ) -> i8 {
154+ self . nice . load ( Ordering :: Relaxed )
155+ }
156+
140157 pub fn new ( name : String , parent : Option < Arc < Self > > ) -> EResult < Self > {
141158 Self :: new_with_space ( name, parent, AddressSpace :: new ( ) )
142159 }
@@ -188,6 +205,7 @@ impl Process {
188205 stop_unwaited : AtomicBool :: new ( false ) ,
189206 continue_unwaited : AtomicBool :: new ( false ) ,
190207 umask : AtomicU32 :: new ( self . umask . load ( Ordering :: Relaxed ) ) ,
208+ nice : AtomicI8 :: new ( self . nice . load ( Ordering :: Relaxed ) ) ,
191209 } ) ;
192210
193211 // Create a heap allocated context that we can pass to the entry point.
@@ -273,6 +291,7 @@ impl Process {
273291 stop_unwaited : AtomicBool :: new ( false ) ,
274292 continue_unwaited : AtomicBool :: new ( false ) ,
275293 umask : AtomicU32 :: new ( 0o022 ) ,
294+ nice : AtomicI8 :: new ( 0 ) ,
276295 } )
277296 }
278297
0 commit comments