#[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

Returns a task reference for the currently executing task/thread.

Returns the group leader of the given task.

Returns the PID of the given task.

Determines whether the given task has pending signals.

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);
}

Wakes up the task.

Trait Implementations

Increments the reference count on the object.
Decrements the reference count on the object. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
This documentation is an old archive. Please see https://rust.docs.kernel.org instead.