@@ -145,11 +145,17 @@ impl Task {
145
145
}
146
146
}
147
147
148
+ /// Returns a raw pointer to the task.
149
+ #[ inline]
150
+ pub fn as_ptr ( & self ) -> * mut bindings:: task_struct {
151
+ self . 0 . get ( )
152
+ }
153
+
148
154
/// Returns the group leader of the given task.
149
155
pub fn group_leader ( & self ) -> & Task {
150
- // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
151
- // have a valid `group_leader` .
152
- let ptr = unsafe { * ptr:: addr_of!( ( * self . 0 . get ( ) ) . group_leader) } ;
156
+ // SAFETY: The group leader of a task never changes after initialization, so reading this
157
+ // field is not a data race .
158
+ let ptr = unsafe { * ptr:: addr_of!( ( * self . as_ptr ( ) ) . group_leader) } ;
153
159
154
160
// SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
155
161
// and given that a task has a reference to its group leader, we know it must be valid for
@@ -159,50 +165,49 @@ impl Task {
159
165
160
166
/// Returns the PID of the given task.
161
167
pub fn pid ( & self ) -> Pid {
162
- // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
163
- // have a valid pid .
164
- unsafe { * ptr:: addr_of!( ( * self . 0 . get ( ) ) . pid) }
168
+ // SAFETY: The pid of a task never changes after initialization, so reading this field is
169
+ // not a data race .
170
+ unsafe { * ptr:: addr_of!( ( * self . as_ptr ( ) ) . pid) }
165
171
}
166
172
167
173
/// Returns the UID of the given task.
168
174
pub fn uid ( & self ) -> Kuid {
169
- // SAFETY: By the type invariant, we know that `self.0` is valid.
170
- Kuid :: from_raw ( unsafe { bindings:: task_uid ( self . 0 . get ( ) ) } )
175
+ // SAFETY: It's always safe to call `task_uid` on a valid task .
176
+ Kuid :: from_raw ( unsafe { bindings:: task_uid ( self . as_ptr ( ) ) } )
171
177
}
172
178
173
179
/// Returns the effective UID of the given task.
174
180
pub fn euid ( & self ) -> Kuid {
175
- // SAFETY: By the type invariant, we know that `self.0` is valid.
176
- Kuid :: from_raw ( unsafe { bindings:: task_euid ( self . 0 . get ( ) ) } )
181
+ // SAFETY: It's always safe to call `task_euid` on a valid task .
182
+ Kuid :: from_raw ( unsafe { bindings:: task_euid ( self . as_ptr ( ) ) } )
177
183
}
178
184
179
185
/// Determines whether the given task has pending signals.
180
186
pub fn signal_pending ( & self ) -> bool {
181
- // SAFETY: By the type invariant, we know that `self.0` is valid.
182
- unsafe { bindings:: signal_pending ( self . 0 . get ( ) ) != 0 }
187
+ // SAFETY: It's always safe to call `signal_pending` on a valid task .
188
+ unsafe { bindings:: signal_pending ( self . as_ptr ( ) ) != 0 }
183
189
}
184
190
185
191
/// Returns the given task's pid in the current pid namespace.
186
192
pub fn pid_in_current_ns ( & self ) -> Pid {
187
- // SAFETY: We know that `self.0.get()` is valid by the type invariant, and passing a null
188
- // pointer as the namespace is correct for using the current namespace .
189
- unsafe { bindings:: task_tgid_nr_ns ( self . 0 . get ( ) , ptr:: null_mut ( ) ) }
193
+ // SAFETY: It's valid to pass a null pointer as the namespace (defaults to current
194
+ // namespace). The task pointer is also valid .
195
+ unsafe { bindings:: task_tgid_nr_ns ( self . as_ptr ( ) , ptr:: null_mut ( ) ) }
190
196
}
191
197
192
198
/// Wakes up the task.
193
199
pub fn wake_up ( & self ) {
194
- // SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
195
- // And `wake_up_process` is safe to be called for any valid task, even if the task is
200
+ // SAFETY: It's always safe to call `signal_pending` on a valid task, even if the task
196
201
// running.
197
- unsafe { bindings:: wake_up_process ( self . 0 . get ( ) ) } ;
202
+ unsafe { bindings:: wake_up_process ( self . as_ptr ( ) ) } ;
198
203
}
199
204
}
200
205
201
206
// SAFETY: The type invariants guarantee that `Task` is always refcounted.
202
207
unsafe impl crate :: types:: AlwaysRefCounted for Task {
203
208
fn inc_ref ( & self ) {
204
209
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
205
- unsafe { bindings:: get_task_struct ( self . 0 . get ( ) ) } ;
210
+ unsafe { bindings:: get_task_struct ( self . as_ptr ( ) ) } ;
206
211
}
207
212
208
213
unsafe fn dec_ref ( obj : ptr:: NonNull < Self > ) {
0 commit comments