Files
moss-kernel/src/process/thread_group/pid.rs
Matthew Leach 35efecad76 process: split Task into Task (shared) and OwnedTask (local)
This commit refactors the core process representation to decouple
"Identity/Resources" from "Execution/Scheduling". Previously, a
monolithic `Task` struct wrapped in `Arc<SpinLock<>>` caused lock
contention during hot scheduling paths and conflated shared state with
CPU-local state.

The `Task` struct has been split into:

1. `Task` (Shared): Holds process-wide resources (VM, FileTable,
Credentials). Managed via `Arc` and internal fine-grained locking.

2. `OwnedTask` (Private): Holds execution state (Context, v_runtime,
signal mask). Strictly owned by a specific CPU (via the Scheduler) and
accessed lock-free.

Key changes:

* Scheduler:
  chedState` now owns tasks via `Box<OwnedTask>`.
  - Transitions between `run_queue` and `running_task` involve strictly
    moving ownership of the Box, ensuring pointer stability.
  - The EEVDF comparison logic now explicitly handles comparisons
    between the queued candidates and the currently running task (which is
    not in the queue).

* Current Task Access:
  - `current()` now returns a `CurrentTaskGuard` which:
    1. Disables preemption (preventing context switches while holding
       the reference).
    2. Performs a runtime borrow check (panic on double-mutable borrow).
    3. Dereferences a cached Per-CPU raw pointer for O(1) access.
2026-01-01 22:54:43 +00:00

49 lines
1.2 KiB
Rust

use libkernel::error::{KernelError, Result};
use crate::sched::current::current_task;
use core::convert::Infallible;
use super::{Pgid, Tgid, ThreadGroup};
/// Userspace `pid_t` type.
pub type PidT = i32;
pub fn sys_getpid() -> core::result::Result<usize, Infallible> {
Ok(current_task().process.tgid.value() as _)
}
pub fn sys_getppid() -> core::result::Result<usize, Infallible> {
Ok(current_task()
.process
.parent
.lock_save_irq()
.as_ref()
.and_then(|x| x.upgrade())
.map(|x| x.tgid.value())
.unwrap_or(0) as _)
}
pub fn sys_getpgid(pid: PidT) -> Result<usize> {
let pgid = if pid == 0 {
*current_task().process.pgid.lock_save_irq()
} else if let Some(tg) = ThreadGroup::get(Tgid::from_pid_t(pid)) {
*tg.pgid.lock_save_irq()
} else {
return Err(KernelError::NoProcess);
};
Ok(pgid.value() as _)
}
pub fn sys_setpgid(pid: PidT, pgid: Pgid) -> Result<usize> {
if pid == 0 {
*current_task().process.pgid.lock_save_irq() = pgid;
} else if let Some(tg) = ThreadGroup::get(Tgid::from_pid_t(pid)) {
*tg.pgid.lock_save_irq() = pgid;
} else {
return Err(KernelError::NoProcess);
};
Ok(0)
}