Skip to content

Commit f3bc6f9

Browse files
rust: task: add as_raw() to Task
Added new function `Task::as_raw()` which returns the raw pointer for the underlying task struct. I also refactored `Task` to instead use the newly created function instead of `self.0.get()` as I feel like `self.as_raw()` is more intuitive. Signed-off-by: Antonio Hickey <[email protected]>
1 parent 8f7e376 commit f3bc6f9

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

rust/kernel/task.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,16 @@ impl Task {
124124
}
125125
}
126126

127+
/// Returns a raw pointer to the underlying C task struct.
128+
pub fn as_raw(&self) -> *mut bindings::task_struct {
129+
self.0.get()
130+
}
131+
127132
/// Returns the group leader of the given task.
128133
pub fn group_leader(&self) -> &Task {
129-
// SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
134+
// SAFETY: By the type invariant, we know that `self.as_raw()` is a valid task. Valid tasks always
130135
// have a valid group_leader.
131-
let ptr = unsafe { *ptr::addr_of!((*self.0.get()).group_leader) };
136+
let ptr = unsafe { *ptr::addr_of!((*self.as_raw()).group_leader) };
132137

133138
// SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
134139
// and given that a task has a reference to its group leader, we know it must be valid for
@@ -138,43 +143,43 @@ impl Task {
138143

139144
/// Returns the PID of the given task.
140145
pub fn pid(&self) -> Pid {
141-
// SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
146+
// SAFETY: By the type invariant, we know that `self.as_raw()` is a valid task. Valid tasks always
142147
// have a valid pid.
143-
unsafe { *ptr::addr_of!((*self.0.get()).pid) }
148+
unsafe { *ptr::addr_of!((*self.as_raw()).pid) }
144149
}
145150

146151
/// Returns the UID of the given task.
147152
pub fn uid(&self) -> Kuid {
148-
// SAFETY: By the type invariant, we know that `self.0` is valid.
149-
Kuid::from_raw(unsafe { bindings::task_uid(self.0.get()) })
153+
// SAFETY: By the type invariant, we know that `self.as_raw()` is valid.
154+
Kuid::from_raw(unsafe { bindings::task_uid(self.as_raw()) })
150155
}
151156

152157
/// Returns the effective UID of the given task.
153158
pub fn euid(&self) -> Kuid {
154-
// SAFETY: By the type invariant, we know that `self.0` is valid.
155-
Kuid::from_raw(unsafe { bindings::task_euid(self.0.get()) })
159+
// SAFETY: By the type invariant, we know that `self.as_raw()` is valid.
160+
Kuid::from_raw(unsafe { bindings::task_euid(self.as_raw()) })
156161
}
157162

158163
/// Determines whether the given task has pending signals.
159164
pub fn signal_pending(&self) -> bool {
160-
// SAFETY: By the type invariant, we know that `self.0` is valid.
161-
unsafe { bindings::signal_pending(self.0.get()) != 0 }
165+
// SAFETY: By the type invariant, we know that `self.as_raw()` is valid.
166+
unsafe { bindings::signal_pending(self.as_raw()) != 0 }
162167
}
163168

164169
/// Returns the given task's pid in the current pid namespace.
165170
pub fn pid_in_current_ns(&self) -> Pid {
166171
// SAFETY: Calling `task_active_pid_ns` with the current task is always safe.
167172
let namespace = unsafe { bindings::task_active_pid_ns(bindings::get_current()) };
168-
// SAFETY: We know that `self.0.get()` is valid by the type invariant.
169-
unsafe { bindings::task_tgid_nr_ns(self.0.get(), namespace) }
173+
// SAFETY: We know that `self.raw()` is valid by the type invariant.
174+
unsafe { bindings::task_tgid_nr_ns(self.as_raw(), namespace) }
170175
}
171176

172177
/// Wakes up the task.
173178
pub fn wake_up(&self) {
174-
// SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
179+
// SAFETY: By the type invariant, we know that `self.raw()` is non-null and valid.
175180
// And `wake_up_process` is safe to be called for any valid task, even if the task is
176181
// running.
177-
unsafe { bindings::wake_up_process(self.0.get()) };
182+
unsafe { bindings::wake_up_process(self.as_raw()) };
178183
}
179184
}
180185

0 commit comments

Comments
 (0)