Skip to content

Thread Group Based Multithreading; Deferred Task Disposal; Misc; Bugs Fixed.#79

Merged
doruche merged 3 commits intomainfrom
dev/drc/chaos
May 4, 2026
Merged

Thread Group Based Multithreading; Deferred Task Disposal; Misc; Bugs Fixed.#79
doruche merged 3 commits intomainfrom
dev/drc/chaos

Conversation

@doruche
Copy link
Copy Markdown
Contributor

@doruche doruche commented May 2, 2026

AI Usage

  • Concurrency races detection.

Close #78

@doruche doruche changed the title Thread Group Based Multithreading; Misc; Bugs Fixed. Thread Group Based Multithreading; Deferred Task Disposal; Misc; Bugs Fixed. May 2, 2026
@doruche doruche requested a review from Copilot May 2, 2026 16:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR restructures the kernel's task model around thread groups instead of per-task parent/child links, then threads that new topology through clone/exit/wait/accounting paths and scheduler/trap cleanup hooks. It fits into the kernel's core process-management layer by changing how processes, threads, exit state, and deferred destruction are represented and observed.

Changes:

  • Split task topology into dedicated thread-group and parent/child modules, plus deferred task disposal support.
  • Updated task syscalls and lifecycle flows (clone, execve, exit, exit_group, wait4, getpid, getppid, gettid, times) to use thread-group semantics.
  • Added supporting ABI/kernel plumbing, scheduler/trap cleanup hooks, and a few platform/config updates.

Reviewed changes

Copilot reviewed 46 out of 47 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
conf/platforms/qemu-virt-rv64.toml Adjusts example QEMU rootfs drive path for RV64.
conf/platforms/qemu-virt-la64.toml Adjusts example QEMU rootfs drive path for LA64.
anemone-kernel/src/time/api/times.rs Switches times(2) accounting to thread-group CPU usage.
anemone-kernel/src/task/topology/thread_group.rs Adds thread-group/member APIs and dethreading logic.
anemone-kernel/src/task/topology/parent_child.rs Adds inter-thread-group parent/child traversal and reaping helpers.
anemone-kernel/src/task/topology/mod.rs Replaces old topology core with thread-group-aware publishing/lookup.
anemone-kernel/src/task/topology/deferred.rs Introduces deferred task disposal queue and helpers.
anemone-kernel/src/task/topology.rs Removes old monolithic topology implementation.
anemone-kernel/src/task/tid.rs Adds typed accessor for TidHandle.
anemone-kernel/src/task/task.rs Removes old standalone task definition file.
anemone-kernel/src/task/sig/mod.rs Adds placeholder signal module.
anemone-kernel/src/task/mod.rs Inlines/refactors task and thread-group core types and constructors.
anemone-kernel/src/task/fs.rs Renames FS-state copy helper to fork.
anemone-kernel/src/task/files.rs Renames file-table copy helper to fork.
anemone-kernel/src/task/cpu_usage.rs Splits task vs thread-group CPU accounting.
anemone-kernel/src/task/api/wait4.rs Reworks wait/reap semantics around child thread groups.
anemone-kernel/src/task/api/mod.rs Registers new gettid syscall module.
anemone-kernel/src/task/api/gettid.rs Adds gettid(2) syscall handler.
anemone-kernel/src/task/api/getppid.rs Returns parent thread-group ID instead of old task parent.
anemone-kernel/src/task/api/getpid.rs Returns TGID for getpid(2).
anemone-kernel/src/task/api/exit/mod.rs Reworks thread/task exit, exit-group, reparenting, and cleanup.
anemone-kernel/src/task/api/exit/exit_group.rs Wires syscall to new kernel_exit_group.
anemone-kernel/src/task/api/exit/exit.rs Wires syscall to new ExitCode-based exit path.
anemone-kernel/src/task/api/execve/kernel.rs Dethreads before exec and reorders address-space activation.
anemone-kernel/src/task/api/clone/mod.rs Adds thread-group-aware clone validation, setup, and publish flow.
anemone-kernel/src/task/api/clone/clone.rs Inlines syscall preparse tracing and logging tweaks.
anemone-kernel/src/syserror.rs Adds Interrupted / EINTR.
anemone-kernel/src/sync/spinlock.rs Adds SpinLock::into_inner.
anemone-kernel/src/sync/rwlock.rs Adds RwLock::into_inner.
anemone-kernel/src/sched/mod.rs Defers disposal from scheduler loop and adds notify.
anemone-kernel/src/sched/event.rs Makes waits stop on killed tasks and documents lock expectations.
anemone-kernel/src/mm/uspace/fault.rs Uses thread-group exit on fatal user page faults.
anemone-kernel/src/main.rs Changes KUnit startup behavior in BSP init path.
anemone-kernel/src/exception/ipi.rs Cleans now-unused task import.
anemone-kernel/src/arch/riscv64/sched.rs Adapts kernel-thread exit call to ExitCode.
anemone-kernel/src/arch/riscv64/exception/trap/utrap.rs Adds killed-task exit and deferred disposal on user trap return.
anemone-kernel/src/arch/riscv64/exception/trap/ktrap.rs Adds killed-task exit and deferred disposal on kernel trap return.
anemone-kernel/src/arch/riscv64/bootstrap.rs Publishes init/AP tasks with new thread-group-aware API.
anemone-kernel/src/arch/loongarch64/sched.rs Adapts kernel-thread exit call to ExitCode.
anemone-kernel/src/arch/loongarch64/exception/trap/utrap.rs Adds killed-task exit and deferred disposal on user trap return.
anemone-kernel/src/arch/loongarch64/exception/trap/ktrap.rs Adds killed-task exit and deferred disposal on kernel trap return.
anemone-kernel/src/arch/loongarch64/bootstrap.rs Publishes init/AP tasks with new thread-group-aware API.
anemone-abi/src/syscall/riscv.rs Adds RISC-V syscall number for gettid.
anemone-abi/src/syscall/loongarch.rs Adds LoongArch syscall number for gettid.
anemone-abi/src/process.rs Adds newer clone flag constants to ABI.
.vscode/settings.json Switches default rust-analyzer target selection.
.gitignore Broadens ignored rootfs config file pattern.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +174 to +182
pub fn cpu_usage_snapshot(&self) -> ThreadGroupCpuUsage {
let mut snapshot = self.inner.read_irqsave().cpu_usage;

pub fn on_reap_child(&self, child: &Task) {
self.for_each_member(|member| {
let member_cpu_usage = member.cpu_usage_snapshot();
snapshot.accumulate_member_usage(&member_cpu_usage);
});

snapshot
#[cfg(feature = "kunit")]
{
crate::debug::kunit::kunit_runner();
// crate::debug::kunit::kunit_runner();
# "-no-reboot",
# "-drive",
# "file=build/rootfs/minimal/rootfs.img,format=raw,if=none,id=x1",
# "file=build/rootfs/minimal-rv/rootfs.img,format=raw,if=none,id=x1",

# "-drive",
# "file=build/rootfs/minimal/rootfs.img,format=raw,if=none,id=x1",
# "file=build/rootfs/minimal-la/rootfs.img,format=raw,if=none,id=x1",
Comment thread anemone-kernel/src/task/mod.rs Outdated
Comment on lines +291 to +296
/// This function is unsafe because:
/// * The parent task of the created task is set to [None] by default.
/// However, according to the task tree structure, there could be only
/// one root task whose `parent` is [None]. Casually creating multiple
/// root tasks will break the task hierarchy and cause undefined
/// behavior.
Comment on lines +315 to +333
if flags.contains(CloneFlags::PARENT_SETTID) {
if let Some(parent_tid) = parent_tid {
// again, map_err cannot be used here.
if let Err(e) = parent_tid.safe_write(new_tid) {
let _ = unsafe { Box::from_raw(frame_ptr) };
unsafe { guard.forget() };
defer_to_dispose(Arc::new(new_task));
return Err(e);
}
} else {
// this is valid. it's user's responsibility to provide a valid pointer if they
// set the flag.
kdebugln!(
"clone: PARENT_SETTID flag is set, but parent_tid pointer is null. ignoring..."
);
}
}

match guard.publish(new_task, binding) {
Comment thread anemone-kernel/src/task/api/wait4.rs Outdated
Comment on lines +205 to +214
if tg.ntasks() == 0 {
// this may happen, even though above exists a check for matched_any.
// consider following scenario:
// - thread A and B are waiting for the only child C.
// - C exits, A and B are both woken up.
// - A gets the lock first, reaps C successfully and returns.
// - B gets the lock later, fails to find C. now there are no children, but B
// did match C. -ECHILD should be returned in this case, since 0 is for "no
// child reaped but there are still matching children".
return Err(SysError::ChildNotFound);
Comment on lines +233 to +246
let mut topology = TOPOLOGY.inner.write_irqsave();

let child_tg = topology.thread_groups.remove(&child_tgid)?;

assert!(
matches!(
child_tg.status().life_cycle(),
ThreadGroupLifeCycle::Exited(_)
),
"task topology: child thread group {} is not exited yet when reaping",
child_tgid
);
assert!(
child_tg.parent_tgid() == Some(self.tgid()),
@doruche doruche merged commit 02cb68a into main May 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Thread Group

2 participants