#[repr(transparent)]pub struct Task(_);
Expand description
Wraps the kernel’s struct task_struct
.
Invariants
Instances of this type are always ref-counted, that is, a call to get_task_struct
ensures
that the allocation remains valid at least until the matching call to put_task_struct
.
Examples
The following is an example of getting the PID of the current thread with zero additional cost when compared to the C version:
use kernel::task::Task;
let pid = Task::current().pid();
Getting the PID of the current process, also zero additional cost:
use kernel::task::Task;
let pid = Task::current().group_leader().pid();
Getting the current task and storing it in some struct. The reference count is automatically
incremented when creating State
and decremented when it is dropped:
use kernel::{task::Task, ARef};
struct State {
creator: ARef<Task>,
index: u32,
}
impl State {
fn new() -> Self {
Self {
creator: Task::current().into(),
index: 0,
}
}
}
Implementations
sourceimpl Task
impl Task
sourcepub fn current<'a>() -> TaskRef<'a>
pub fn current<'a>() -> TaskRef<'a>
Returns a task reference for the currently executing task/thread.
sourcepub fn group_leader(&self) -> &Task
pub fn group_leader(&self) -> &Task
Returns the group leader of the given task.
sourcepub fn signal_pending(&self) -> bool
pub fn signal_pending(&self) -> bool
Determines whether the given task has pending signals.
sourcepub fn spawn<T: FnOnce() + Send + 'static>(
name: Arguments<'_>,
func: T
) -> Result<ARef<Task>>
pub fn spawn<T: FnOnce() + Send + 'static>(
name: Arguments<'_>,
func: T
) -> Result<ARef<Task>>
Starts a new kernel thread and runs it.
Examples
Launches 10 threads and waits for them to complete.
use core::sync::atomic::{AtomicU32, Ordering};
use kernel::sync::{CondVar, Mutex};
use kernel::task::Task;
kernel::init_static_sync! {
static COUNT: Mutex<u32> = 0;
static COUNT_IS_ZERO: CondVar;
}
fn threadfn() {
pr_info!("Running from thread {}\n", Task::current().pid());
let mut guard = COUNT.lock();
*guard -= 1;
if *guard == 0 {
COUNT_IS_ZERO.notify_all();
}
}
// Set count to 10 and spawn 10 threads.
*COUNT.lock() = 10;
for i in 0..10 {
Task::spawn(fmt!("test{i}"), threadfn).unwrap();
}
// Wait for count to drop to zero.
let mut guard = COUNT.lock();
while (*guard != 0) {
COUNT_IS_ZERO.wait(&mut guard);
}
Trait Implementations
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more