1use core::sync::atomic::Ordering;
6
7mod cap;
8mod cred;
9mod futex;
10mod ktask;
11mod process;
12mod task_manager;
13mod task_state;
14mod task_struct;
15mod tid_allocator;
16mod work_queue;
17
18pub use cap::*;
19pub use cred::*;
20pub use futex::*;
21pub use ktask::*;
22pub use process::*;
23pub use task_manager::{TASK_MANAGER, TaskManagerTrait};
24pub use task_state::TaskState;
25pub use task_struct::FsStruct;
26pub use task_struct::SharedTask;
27pub use task_struct::Task as TaskStruct;
28pub use work_queue::*;
29
30use alloc::sync::Arc;
31
32use crate::mm::memory_space::MemorySpace;
33use crate::sync::SpinLock;
34use crate::uapi::signal::NUM_SIGCHLD;
35use crate::{
36 arch::trap::{TrapFrame, restore},
37 kernel::{cpu::current_cpu, schedule},
38 vfs::{FDTable, File, FsError},
39};
40
41pub(crate) fn forkret() {
44 let fp: *mut TrapFrame;
45 {
46 let cpu = current_cpu().lock();
47 let task = cpu.current_task.as_ref().unwrap();
48 fp = task.lock().trap_frame_ptr.load(Ordering::SeqCst);
49 }
50 unsafe { restore(&*fp) };
52}
53
54pub(crate) fn terminate_task(code: usize) -> ! {
60 let task = {
61 let cpu = current_cpu().lock();
62 cpu.current_task.as_ref().unwrap().clone()
63 };
64
65 {
66 let mut t = task.lock();
67 t.state = TaskState::Zombie;
69 t.exit_code = Some(code as i32);
70 }
71 drop(task);
72 schedule();
73 unreachable!("terminate_task: should not return after scheduled out terminated task");
74}
75
76pub fn current_task() -> SharedTask {
79 current_cpu()
80 .lock()
81 .current_task
82 .as_ref()
83 .expect("current_task: CPU has no current task")
84 .clone()
85}
86
87pub fn current_memory_space() -> Arc<SpinLock<MemorySpace>> {
90 current_cpu()
91 .lock()
92 .current_memory_space
93 .as_ref()
94 .expect("current_memory_space: current task has no memory space")
95 .clone()
96}
97
98pub fn notify_parent(task: SharedTask) {
102 let ppid = {
103 let t = task.lock();
104 t.ppid
105 };
106
107 let t = TASK_MANAGER.lock();
108 if let Some(p) = t.get_task(ppid) {
109 t.send_signal(p.clone(), NUM_SIGCHLD);
112
113 let wait_child = p.lock().wait_child.clone();
117 wait_child.lock().wake_up_one();
127 } else {
128 let pid = task.lock().pid;
130 crate::pr_warn!(
131 "notify_parent: parent task {} not found for child {}",
132 ppid,
133 pid
134 );
135 }
136}
137
138pub fn current_fd_table() -> Arc<FDTable> {
140 current_task().lock().fd_table.clone()
141}
142
143pub fn get_file(fd: usize) -> Result<Arc<dyn File>, FsError> {
145 current_fd_table().get(fd)
146}