~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/kernel/task.rs

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /rust/kernel/task.rs (Version linux-6.12-rc7) and /rust/kernel/task.rs (Version linux-4.17.19)


  1 // SPDX-License-Identifier: GPL-2.0               
  2                                                   
  3 //! Tasks (threads and processes).                
  4 //!                                               
  5 //! C header: [`include/linux/sched.h`](srctre    
  6                                                   
  7 use crate::types::Opaque;                         
  8 use core::{                                       
  9     ffi::{c_int, c_long, c_uint},                 
 10     marker::PhantomData,                          
 11     ops::Deref,                                   
 12     ptr,                                          
 13 };                                                
 14                                                   
 15 /// A sentinel value used for infinite timeout    
 16 pub const MAX_SCHEDULE_TIMEOUT: c_long = c_lon    
 17                                                   
 18 /// Bitmask for tasks that are sleeping in an     
 19 pub const TASK_INTERRUPTIBLE: c_int = bindings    
 20 /// Bitmask for tasks that are sleeping in an     
 21 pub const TASK_UNINTERRUPTIBLE: c_int = bindin    
 22 /// Convenience constant for waking up tasks r    
 23 /// uninterruptible sleep.                        
 24 pub const TASK_NORMAL: c_uint = bindings::TASK    
 25                                                   
 26 /// Returns the currently running task.           
 27 #[macro_export]                                   
 28 macro_rules! current {                            
 29     () => {                                       
 30         // SAFETY: Deref + addr-of below creat    
 31         // caller.                                
 32         unsafe { &*$crate::task::Task::current    
 33     };                                            
 34 }                                                 
 35                                                   
 36 /// Wraps the kernel's `struct task_struct`.      
 37 ///                                               
 38 /// # Invariants                                  
 39 ///                                               
 40 /// All instances are valid tasks created by t    
 41 ///                                               
 42 /// Instances of this type are always refcount    
 43 /// that the allocation remains valid at least    
 44 ///                                               
 45 /// # Examples                                    
 46 ///                                               
 47 /// The following is an example of getting the    
 48 /// when compared to the C version:               
 49 ///                                               
 50 /// ```                                           
 51 /// let pid = current!().pid();                   
 52 /// ```                                           
 53 ///                                               
 54 /// Getting the PID of the current process, al    
 55 ///                                               
 56 /// ```                                           
 57 /// let pid = current!().group_leader().pid();    
 58 /// ```                                           
 59 ///                                               
 60 /// Getting the current task and storing it in    
 61 /// incremented when creating `State` and decr    
 62 ///                                               
 63 /// ```                                           
 64 /// use kernel::{task::Task, types::ARef};        
 65 ///                                               
 66 /// struct State {                                
 67 ///     creator: ARef<Task>,                      
 68 ///     index: u32,                               
 69 /// }                                             
 70 ///                                               
 71 /// impl State {                                  
 72 ///     fn new() -> Self {                        
 73 ///         Self {                                
 74 ///             creator: current!().into(),       
 75 ///             index: 0,                         
 76 ///         }                                     
 77 ///     }                                         
 78 /// }                                             
 79 /// ```                                           
 80 #[repr(transparent)]                              
 81 pub struct Task(pub(crate) Opaque<bindings::ta    
 82                                                   
 83 // SAFETY: By design, the only way to access a    
 84 // `ARef<Task>` obtained through the `AlwaysRe    
 85 // which a `Task` can be accessed mutably is w    
 86 // runs. It is safe for that to happen on any     
 87 unsafe impl Send for Task {}                      
 88                                                   
 89 // SAFETY: It's OK to access `Task` through sh    
 90 // either accessing properties that don't chan    
 91 // synchronised by C code (e.g., `signal_pendi    
 92 unsafe impl Sync for Task {}                      
 93                                                   
 94 /// The type of process identifiers (PIDs).       
 95 type Pid = bindings::pid_t;                       
 96                                                   
 97 impl Task {                                       
 98     /// Returns a task reference for the curre    
 99     ///                                           
100     /// The recommended way to get the current    
101     /// [`current`] macro because it is safe.     
102     ///                                           
103     /// # Safety                                  
104     ///                                           
105     /// Callers must ensure that the returned     
106     pub unsafe fn current() -> impl Deref<Targ    
107         struct TaskRef<'a> {                      
108             task: &'a Task,                       
109             _not_send: PhantomData<*mut ()>,      
110         }                                         
111                                                   
112         impl Deref for TaskRef<'_> {              
113             type Target = Task;                   
114                                                   
115             fn deref(&self) -> &Self::Target {    
116                 self.task                         
117             }                                     
118         }                                         
119                                                   
120         // SAFETY: Just an FFI call with no ad    
121         let ptr = unsafe { bindings::get_curre    
122                                                   
123         TaskRef {                                 
124             // SAFETY: If the current thread i    
125             // that `TaskRef` is not `Send`, w    
126             // (where it could potentially out    
127             task: unsafe { &*ptr.cast() },        
128             _not_send: PhantomData,               
129         }                                         
130     }                                             
131                                                   
132     /// Returns the group leader of the given     
133     pub fn group_leader(&self) -> &Task {         
134         // SAFETY: By the type invariant, we k    
135         // have a valid `group_leader`.           
136         let ptr = unsafe { *ptr::addr_of!((*se    
137                                                   
138         // SAFETY: The lifetime of the returne    
139         // and given that a task has a referen    
140         // the lifetime of the returned task r    
141         unsafe { &*ptr.cast() }                   
142     }                                             
143                                                   
144     /// Returns the PID of the given task.        
145     pub fn pid(&self) -> Pid {                    
146         // SAFETY: By the type invariant, we k    
147         // have a valid pid.                      
148         unsafe { *ptr::addr_of!((*self.0.get()    
149     }                                             
150                                                   
151     /// Determines whether the given task has     
152     pub fn signal_pending(&self) -> bool {        
153         // SAFETY: By the type invariant, we k    
154         unsafe { bindings::signal_pending(self    
155     }                                             
156                                                   
157     /// Wakes up the task.                        
158     pub fn wake_up(&self) {                       
159         // SAFETY: By the type invariant, we k    
160         // And `wake_up_process` is safe to be    
161         // running.                               
162         unsafe { bindings::wake_up_process(sel    
163     }                                             
164 }                                                 
165                                                   
166 // SAFETY: The type invariants guarantee that     
167 unsafe impl crate::types::AlwaysRefCounted for    
168     fn inc_ref(&self) {                           
169         // SAFETY: The existence of a shared r    
170         unsafe { bindings::get_task_struct(sel    
171     }                                             
172                                                   
173     unsafe fn dec_ref(obj: ptr::NonNull<Self>)    
174         // SAFETY: The safety requirements gua    
175         unsafe { bindings::put_task_struct(obj    
176     }                                             
177 }                                                 
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php