1+ //! User task management.
2+
13use core:: {
24 alloc:: Layout ,
35 cell:: RefCell ,
@@ -23,6 +25,7 @@ use weak_map::WeakMap;
2325
2426use crate :: time:: TimeStat ;
2527
28+ /// Create a new user task.
2629pub fn new_user_task (
2730 name : & str ,
2831 uctx : UspaceContext ,
@@ -58,6 +61,7 @@ pub struct TaskExt {
5861}
5962
6063impl TaskExt {
64+ /// Create a new [`TaskExt`].
6165 pub fn new ( thread : Arc < Thread > ) -> Self {
6266 Self {
6367 time : RefCell :: new ( TimeStat :: new ( ) ) ,
@@ -77,31 +81,36 @@ impl TaskExt {
7781 self . time . borrow ( ) . output ( )
7882 }
7983
84+ /// Get the [`ThreadData`] associated with this task.
8085 pub fn thread_data ( & self ) -> & ThreadData {
8186 self . thread . data ( ) . unwrap ( )
8287 }
8388
89+ /// Get the [`ProcessData`] associated with this task.
8490 pub fn process_data ( & self ) -> & ProcessData {
8591 self . thread . process ( ) . data ( ) . unwrap ( )
8692 }
8793}
8894
8995axtask:: def_task_ext!( TaskExt ) ;
9096
97+ /// Update the time statistics to reflect a switch from kernel mode to user mode.
9198pub fn time_stat_from_kernel_to_user ( ) {
9299 let curr_task = current ( ) ;
93100 curr_task
94101 . task_ext ( )
95102 . time_stat_from_kernel_to_user ( monotonic_time_nanos ( ) as usize ) ;
96103}
97104
105+ /// Update the time statistics to reflect a switch from user mode to kernel mode.
98106pub fn time_stat_from_user_to_kernel ( ) {
99107 let curr_task = current ( ) ;
100108 curr_task
101109 . task_ext ( )
102110 . time_stat_from_user_to_kernel ( monotonic_time_nanos ( ) as usize ) ;
103111}
104112
113+ /// Get the time statistics for the current task.
105114pub fn time_stat_output ( ) -> ( usize , usize , usize , usize ) {
106115 let curr_task = current ( ) ;
107116 let ( utime_ns, stime_ns) = curr_task. task_ext ( ) . time_stat_output ( ) ;
@@ -113,6 +122,7 @@ pub fn time_stat_output() -> (usize, usize, usize, usize) {
113122 )
114123}
115124
125+ /// Extended data for [`Thread`].
116126pub struct ThreadData {
117127 /// The clear thread tid field
118128 ///
@@ -123,23 +133,27 @@ pub struct ThreadData {
123133}
124134
125135impl ThreadData {
136+ /// Create a new [`ThreadData`].
126137 #[ allow( clippy:: new_without_default) ]
127138 pub fn new ( ) -> Self {
128139 Self {
129140 clear_child_tid : AtomicUsize :: new ( 0 ) ,
130141 }
131142 }
132143
144+ /// Get the clear child tid field.
133145 pub fn clear_child_tid ( & self ) -> usize {
134146 self . clear_child_tid . load ( Ordering :: Relaxed )
135147 }
136148
149+ /// Set the clear child tid field.
137150 pub fn set_clear_child_tid ( & self , clear_child_tid : usize ) {
138151 self . clear_child_tid
139152 . store ( clear_child_tid, Ordering :: Relaxed ) ;
140153 }
141154}
142155
156+ /// Extended data for [`Process`].
143157pub struct ProcessData {
144158 /// The executable path
145159 pub exe_path : RwLock < String > ,
@@ -154,6 +168,7 @@ pub struct ProcessData {
154168}
155169
156170impl ProcessData {
171+ /// Create a new [`ProcessData`].
157172 pub fn new ( exe_path : String , aspace : Arc < Mutex < AddrSpace > > ) -> Self {
158173 Self {
159174 exe_path : RwLock :: new ( exe_path) ,
@@ -164,18 +179,22 @@ impl ProcessData {
164179 }
165180 }
166181
182+ /// Get the bottom address of the user heap.
167183 pub fn get_heap_bottom ( & self ) -> usize {
168184 self . heap_bottom . load ( Ordering :: Acquire )
169185 }
170186
187+ /// Set the bottom address of the user heap.
171188 pub fn set_heap_bottom ( & self , bottom : usize ) {
172189 self . heap_bottom . store ( bottom, Ordering :: Release )
173190 }
174191
192+ /// Get the top address of the user heap.
175193 pub fn get_heap_top ( & self ) -> usize {
176194 self . heap_top . load ( Ordering :: Acquire )
177195 }
178196
197+ /// Set the top address of the user heap.
179198 pub fn set_heap_top ( & self , top : usize ) {
180199 self . heap_top . store ( top, Ordering :: Release )
181200 }
@@ -221,6 +240,8 @@ static PROCESS_TABLE: RwLock<WeakMap<Pid, Weak<Process>>> = RwLock::new(WeakMap:
221240static PROCESS_GROUP_TABLE : RwLock < WeakMap < Pid , Weak < ProcessGroup > > > = RwLock :: new ( WeakMap :: new ( ) ) ;
222241static SESSION_TABLE : RwLock < WeakMap < Pid , Weak < Session > > > = RwLock :: new ( WeakMap :: new ( ) ) ;
223242
243+ /// Add the thread and possibly its process, process group and session to the
244+ /// corresponding tables.
224245pub fn add_thread_to_table ( thread : & Arc < Thread > ) {
225246 let mut thread_table = THREAD_TABLE . write ( ) ;
226247 thread_table. insert ( thread. tid ( ) , thread) ;
0 commit comments