Add thread support and thread spawn syscall
This commit is contained in:
parent
98f665298c
commit
5f1274dd39
@ -248,7 +248,11 @@ pub fn send_ipc_to(
|
|||||||
clippy::unwrap_used,
|
clippy::unwrap_used,
|
||||||
reason = "The PID is known valid due to using it in process_mut above"
|
reason = "The PID is known valid due to using it in process_mut above"
|
||||||
)]
|
)]
|
||||||
TASKING.wake(pid, SleepReason::WaitingForIPC).unwrap();
|
TASKING.process(pid, |process| {
|
||||||
|
for (tid, _) in &*process.threads().read() {
|
||||||
|
TASKING.wake(pid, tid, SleepReason::WaitingForIPC).unwrap();
|
||||||
|
}
|
||||||
|
}).unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -759,7 +763,7 @@ extern "C" fn syscall_handler() {
|
|||||||
}
|
}
|
||||||
24 => {
|
24 => {
|
||||||
let pid = usize(regs.rcx);
|
let pid = usize(regs.rcx);
|
||||||
if TASKING.wake(pid, SleepReason::NewProcess).is_err() {
|
if TASKING.wake(pid, 0,SleepReason::NewProcess).is_err() {
|
||||||
retval = 1;
|
retval = 1;
|
||||||
} else {
|
} else {
|
||||||
retval = 0;
|
retval = 0;
|
||||||
@ -773,6 +777,15 @@ extern "C" fn syscall_handler() {
|
|||||||
retval = 0;
|
retval = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26 => {
|
||||||
|
let res = TASKING.new_thread(ptr::with_exposed_provenance(usize(regs.rcx)), usize(regs.rdx));
|
||||||
|
if let Ok(tid) = res {
|
||||||
|
retval = 0;
|
||||||
|
retval2 = u64(tid);
|
||||||
|
} else {
|
||||||
|
retval = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
unsafe { SYSCALL_REGS = regs };
|
unsafe { SYSCALL_REGS = regs };
|
||||||
|
@ -290,5 +290,5 @@ pub fn main() {
|
|||||||
let init_pid = TASKING
|
let init_pid = TASKING
|
||||||
.new_process(ptr::with_exposed_provenance(usize(init.ehdr.e_entry)), init_addr_space)
|
.new_process(ptr::with_exposed_provenance(usize(init.ehdr.e_entry)), init_addr_space)
|
||||||
.expect("Failed to create init process");
|
.expect("Failed to create init process");
|
||||||
TASKING.wake(init_pid, SleepReason::NewProcess).expect("Failed to wake new init process");
|
TASKING.wake(init_pid, 0,SleepReason::NewProcess).expect("Failed to wake new init process");
|
||||||
}
|
}
|
||||||
|
249
src/tasking.rs
249
src/tasking.rs
@ -64,6 +64,7 @@ extern "C" fn task_init() {
|
|||||||
naked_asm!(
|
naked_asm!(
|
||||||
"pop rcx", // Get the user stack pointer
|
"pop rcx", // Get the user stack pointer
|
||||||
"pop rbx", // Get the entry point
|
"pop rbx", // Get the entry point
|
||||||
|
"pop rdi", // Get argument to the entry point
|
||||||
"push 43", // Push the stack segment selector - same as data
|
"push 43", // Push the stack segment selector - same as data
|
||||||
"push rcx", // Push the stack pointer
|
"push rcx", // Push the stack pointer
|
||||||
"pushfq", // Get the flags into RAX
|
"pushfq", // Get the flags into RAX
|
||||||
@ -87,13 +88,103 @@ pub enum SleepReason {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Process {
|
pub struct Process {
|
||||||
address_space: Option<AddressSpace>,
|
address_space: Option<AddressSpace>,
|
||||||
kernel_stack: Box<[usize], &'static ASpaceMutex>,
|
|
||||||
kernel_esp: *mut usize,
|
|
||||||
kernel_esp_top: VirtAddr,
|
|
||||||
address_spaces: Mutex<Slab<AddressSpace>>,
|
address_spaces: Mutex<Slab<AddressSpace>>,
|
||||||
data_buffers: Mutex<Slab<*mut [u8]>>,
|
data_buffers: Mutex<Slab<*mut [u8]>>,
|
||||||
message_queue: Mutex<SegQueue<IpcMessage>>,
|
message_queue: Mutex<SegQueue<IpcMessage>>,
|
||||||
|
threads: RwLock<Slab<Thread>>,
|
||||||
|
main_thread: usize,
|
||||||
|
num_threads: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Process {
|
||||||
|
fn new(
|
||||||
|
address_space: AddressSpace,
|
||||||
|
entry_point: *const extern "C" fn(usize) -> !,
|
||||||
|
) -> Result<Self, PagingError> {
|
||||||
|
let mut proc = Self {
|
||||||
|
address_space: Some(address_space),
|
||||||
|
address_spaces: Mutex::new(Slab::new()),
|
||||||
|
data_buffers: Mutex::new(Slab::new()),
|
||||||
|
message_queue: Mutex::new(SegQueue::new()),
|
||||||
|
threads: RwLock::new(Slab::new()),
|
||||||
|
main_thread: 0,
|
||||||
|
num_threads: 0,
|
||||||
|
};
|
||||||
|
let main_thread = proc.new_thread(entry_point, 0)?;
|
||||||
|
proc.main_thread = main_thread;
|
||||||
|
Ok(proc)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_thread(
|
||||||
|
&mut self,
|
||||||
|
entry_point: *const extern "C" fn(usize) -> !,
|
||||||
|
argument: usize,
|
||||||
|
) -> Result<usize, PagingError> {
|
||||||
|
let stack_start = 0xFFF_FF80_0000 - (u64(self.num_threads) * (16*4096));
|
||||||
|
let mut kernel_stack = Vec::with_capacity_in(KSTACK_SIZE, &*KERNEL_SPACE);
|
||||||
|
|
||||||
|
kernel_stack.resize(KSTACK_SIZE - 0x4, 0);
|
||||||
|
#[expect(clippy::as_conversions, reason = "Needed to get address of function")]
|
||||||
|
kernel_stack.push(task_init as usize);
|
||||||
|
kernel_stack.push(stack_start as usize + (16 * 4096));
|
||||||
|
kernel_stack.push(entry_point.expose_provenance());
|
||||||
|
kernel_stack.push(argument);
|
||||||
|
let mut kernel_stack = kernel_stack.into_boxed_slice();
|
||||||
|
if let Some(address_space) = self.address_space.as_mut() {
|
||||||
|
address_space.map_assert_unused(
|
||||||
|
#[expect(
|
||||||
|
clippy::unwrap_used,
|
||||||
|
reason = "from_start_address requires the address to be page aligned, which it is."
|
||||||
|
)]
|
||||||
|
Page::from_start_address(VirtAddr::new(stack_start)).unwrap(),
|
||||||
|
16,
|
||||||
|
PageTableFlags::USER_ACCESSIBLE,
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
ACTIVE_SPACE.lock().map_assert_unused(
|
||||||
|
#[expect(
|
||||||
|
clippy::unwrap_used,
|
||||||
|
reason = "from_start_address requires the address to be page aligned, which it is."
|
||||||
|
)]
|
||||||
|
Page::from_start_address(VirtAddr::new(stack_start)).unwrap(),
|
||||||
|
16,
|
||||||
|
PageTableFlags::USER_ACCESSIBLE,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
let sleeping =
|
||||||
|
if self.threads.read().is_empty() { Some(SleepReason::NewProcess) } else { None };
|
||||||
|
let thread = Thread {
|
||||||
|
#[expect(
|
||||||
|
clippy::indexing_slicing,
|
||||||
|
reason = "Stack length is 0x1_0000, this cannot panic"
|
||||||
|
)]
|
||||||
|
kernel_esp: &mut kernel_stack[KSTACK_SIZE - 10],
|
||||||
|
#[expect(
|
||||||
|
clippy::indexing_slicing,
|
||||||
|
reason = "Stack length is 0x1_0000, this cannot panic"
|
||||||
|
)]
|
||||||
|
kernel_esp_top: VirtAddr::from_ptr(
|
||||||
|
addr_of!(kernel_stack[KSTACK_SIZE - 1]).wrapping_add(1),
|
||||||
|
),
|
||||||
|
kernel_stack,
|
||||||
|
sleeping: RwLock::new(sleeping),
|
||||||
|
};
|
||||||
|
let idx = self.threads.write().insert(thread);
|
||||||
|
self.num_threads += 1;
|
||||||
|
Ok(idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn threads(&self) -> &RwLock<Slab<Thread>> {
|
||||||
|
&self.threads
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Thread {
|
||||||
sleeping: RwLock<Option<SleepReason>>,
|
sleeping: RwLock<Option<SleepReason>>,
|
||||||
|
kernel_stack: Box<[usize], &'static ASpaceMutex>,
|
||||||
|
kernel_esp: *mut usize,
|
||||||
|
kernel_esp_top: VirtAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct IpcMessage {
|
pub struct IpcMessage {
|
||||||
@ -130,6 +221,7 @@ pub static TASKING: Lazy<Tasking> = Lazy::new(|| Tasking {
|
|||||||
processes: RwLock::new(Slab::new()),
|
processes: RwLock::new(Slab::new()),
|
||||||
ready_to_run: Mutex::new(VecDeque::new()),
|
ready_to_run: Mutex::new(VecDeque::new()),
|
||||||
current_pid: RwLock::new(None),
|
current_pid: RwLock::new(None),
|
||||||
|
current_tid: RwLock::new(None),
|
||||||
freeable_kstacks: Mutex::new(Vec::new()),
|
freeable_kstacks: Mutex::new(Vec::new()),
|
||||||
wfi_loop: AtomicBool::new(false),
|
wfi_loop: AtomicBool::new(false),
|
||||||
});
|
});
|
||||||
@ -137,8 +229,9 @@ pub static TASKING: Lazy<Tasking> = Lazy::new(|| Tasking {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Tasking {
|
pub struct Tasking {
|
||||||
processes: RwLock<Slab<Process>>,
|
processes: RwLock<Slab<Process>>,
|
||||||
ready_to_run: Mutex<VecDeque<usize>>,
|
ready_to_run: Mutex<VecDeque<(usize, usize)>>,
|
||||||
current_pid: RwLock<Option<usize>>,
|
current_pid: RwLock<Option<usize>>,
|
||||||
|
current_tid: RwLock<Option<usize>>,
|
||||||
freeable_kstacks: Mutex<Vec<Box<[usize], &'static ASpaceMutex>>>,
|
freeable_kstacks: Mutex<Vec<Box<[usize], &'static ASpaceMutex>>>,
|
||||||
wfi_loop: AtomicBool,
|
wfi_loop: AtomicBool,
|
||||||
}
|
}
|
||||||
@ -148,45 +241,11 @@ pub const KSTACK_SIZE: usize = (4 * 4096) / 8;
|
|||||||
impl Tasking {
|
impl Tasking {
|
||||||
pub fn new_process(
|
pub fn new_process(
|
||||||
&self,
|
&self,
|
||||||
entry_point: *const extern "C" fn() -> !,
|
entry_point: *const extern "C" fn(usize) -> !,
|
||||||
mut address_space: AddressSpace,
|
address_space: AddressSpace,
|
||||||
) -> Result<usize, PagingError> {
|
) -> Result<usize, PagingError> {
|
||||||
let mut kernel_stack = Vec::with_capacity_in(KSTACK_SIZE, &*KERNEL_SPACE);
|
let process = Process::new(address_space, entry_point)?;
|
||||||
kernel_stack.resize(KSTACK_SIZE - 0x3, 0);
|
let pid = self.processes.write().insert(process);
|
||||||
#[expect(clippy::as_conversions, reason = "Needed to get address of function")]
|
|
||||||
kernel_stack.push(task_init as usize);
|
|
||||||
kernel_stack.push(0xFFF_FF80_0000 + (16 * 4096));
|
|
||||||
kernel_stack.push(entry_point.expose_provenance());
|
|
||||||
let mut kernel_stack = kernel_stack.into_boxed_slice();
|
|
||||||
address_space.map_assert_unused(
|
|
||||||
#[expect(
|
|
||||||
clippy::unwrap_used,
|
|
||||||
reason = "from_start_address requires the address to be page aligned, which it is."
|
|
||||||
)]
|
|
||||||
Page::from_start_address(VirtAddr::new(0xFFF_FF80_0000)).unwrap(),
|
|
||||||
16,
|
|
||||||
PageTableFlags::USER_ACCESSIBLE,
|
|
||||||
)?;
|
|
||||||
let pid = self.processes.write().insert(Process {
|
|
||||||
#[expect(
|
|
||||||
clippy::indexing_slicing,
|
|
||||||
reason = "Stack length is 0x1_0000, this cannot panic"
|
|
||||||
)]
|
|
||||||
kernel_esp: &mut kernel_stack[KSTACK_SIZE - 9],
|
|
||||||
#[expect(
|
|
||||||
clippy::indexing_slicing,
|
|
||||||
reason = "Stack length is 0x1_0000, this cannot panic"
|
|
||||||
)]
|
|
||||||
kernel_esp_top: VirtAddr::from_ptr(
|
|
||||||
addr_of!(kernel_stack[KSTACK_SIZE - 1]).wrapping_add(1),
|
|
||||||
),
|
|
||||||
kernel_stack,
|
|
||||||
address_space: Some(address_space),
|
|
||||||
address_spaces: Mutex::new(Slab::new()),
|
|
||||||
data_buffers: Mutex::new(Slab::new()),
|
|
||||||
message_queue: Mutex::new(SegQueue::new()),
|
|
||||||
sleeping: RwLock::new(Some(SleepReason::NewProcess)),
|
|
||||||
});
|
|
||||||
if let Some(&proc_man_pid) = REGISTERD_PIDS.read().get(&3) {
|
if let Some(&proc_man_pid) = REGISTERD_PIDS.read().get(&3) {
|
||||||
let mut varint_buf = unsigned_varint::encode::u64_buffer();
|
let mut varint_buf = unsigned_varint::encode::u64_buffer();
|
||||||
let mut buffer = Vec::new_in(&*ACTIVE_SPACE);
|
let mut buffer = Vec::new_in(&*ACTIVE_SPACE);
|
||||||
@ -220,10 +279,21 @@ impl Tasking {
|
|||||||
Ok(pid)
|
Ok(pid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_thread(&self, entry_point: *const extern "C" fn(usize) -> !, argument: usize) -> Result<usize, PagingError> {
|
||||||
|
let current_pid = self.current_pid().unwrap();
|
||||||
|
let new_tid = self.current_process_mut(|process| {
|
||||||
|
process.new_thread(entry_point, argument)
|
||||||
|
})?;
|
||||||
|
self.ready_to_run.lock().push_back((current_pid, new_tid));
|
||||||
|
Ok(new_tid)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ok_to_yield(&self) -> bool {
|
pub fn ok_to_yield(&self) -> bool {
|
||||||
!(self.freeable_kstacks.is_locked()
|
!(self.freeable_kstacks.is_locked()
|
||||||
|| (self.current_pid.reader_count() > 0)
|
|| (self.current_pid.reader_count() > 0)
|
||||||
|| (self.current_pid.writer_count() > 0)
|
|| (self.current_pid.writer_count() > 0)
|
||||||
|
|| (self.current_tid.reader_count() > 0)
|
||||||
|
|| (self.current_tid.writer_count() > 0)
|
||||||
|| self.ready_to_run.is_locked()
|
|| self.ready_to_run.is_locked()
|
||||||
|| (self.processes.reader_count() > 0)
|
|| (self.processes.reader_count() > 0)
|
||||||
|| (self.processes.writer_count() > 0)
|
|| (self.processes.writer_count() > 0)
|
||||||
@ -238,14 +308,17 @@ impl Tasking {
|
|||||||
self.wfi_loop.store(false, Ordering::Relaxed);
|
self.wfi_loop.store(false, Ordering::Relaxed);
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
let next_process_pid = self.ready_to_run.lock().pop_front();
|
let rtr_head = self.ready_to_run.lock().pop_front();
|
||||||
if let Some(next_process_pid) = next_process_pid {
|
if let Some((next_process_pid, next_process_tid)) = rtr_head {
|
||||||
self.wfi_loop.store(false, Ordering::Relaxed);
|
self.wfi_loop.store(false, Ordering::Relaxed);
|
||||||
if Some(next_process_pid) == self.current_pid() {
|
if Some(next_process_pid) == self.current_pid()
|
||||||
println!("Yielding to current process! Returning");
|
&& Some(next_process_tid) == self.current_tid()
|
||||||
|
{
|
||||||
|
println!("Yielding to current thread! Returning");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#[warn(clippy::unwrap_used, reason = "FIXME(?)")]
|
#[warn(clippy::unwrap_used, reason = "FIXME(?)")]
|
||||||
|
if current_pid != next_process_pid {
|
||||||
let current_address_space = self
|
let current_address_space = self
|
||||||
.process_mut(next_process_pid, |process| {
|
.process_mut(next_process_pid, |process| {
|
||||||
#[expect(
|
#[expect(
|
||||||
@ -262,19 +335,20 @@ impl Tasking {
|
|||||||
self.current_process_mut(|process| {
|
self.current_process_mut(|process| {
|
||||||
process.address_space = Some(current_address_space);
|
process.address_space = Some(current_address_space);
|
||||||
});
|
});
|
||||||
if self.current_process(|process| process.sleeping.read().is_none()) {
|
|
||||||
self.ready_to_run.lock().push_back(current_pid);
|
|
||||||
}
|
}
|
||||||
let curr_stack =
|
if self.current_thread(|thread| thread.sleeping.read().is_none()) {
|
||||||
self.current_process_mut(|process| addr_of_mut!(process.kernel_esp));
|
self.ready_to_run.lock().push_back((current_pid, self.current_tid().unwrap()));
|
||||||
|
}
|
||||||
|
let curr_stack = self.current_thread_mut(|thread| addr_of_mut!(thread.kernel_esp));
|
||||||
*self.current_pid.write() = Some(next_process_pid);
|
*self.current_pid.write() = Some(next_process_pid);
|
||||||
let kernel_esp = self.current_process(|process| {
|
*self.current_tid.write() = Some(next_process_tid);
|
||||||
gdt::set_tss_stack(process.kernel_esp_top);
|
let kernel_esp = self.current_thread(|thread| {
|
||||||
process.kernel_esp
|
gdt::set_tss_stack(thread.kernel_esp_top);
|
||||||
|
thread.kernel_esp
|
||||||
});
|
});
|
||||||
switch_to_asm(curr_stack, kernel_esp);
|
switch_to_asm(curr_stack, kernel_esp);
|
||||||
break;
|
break;
|
||||||
} else if self.current_process(|process| process.sleeping.read().is_some()) {
|
} else if self.current_thread(|thread| thread.sleeping.read().is_some()) {
|
||||||
self.wfi_loop.store(true, Ordering::Relaxed);
|
self.wfi_loop.store(true, Ordering::Relaxed);
|
||||||
x86_64::instructions::interrupts::enable_and_hlt();
|
x86_64::instructions::interrupts::enable_and_hlt();
|
||||||
x86_64::instructions::interrupts::disable();
|
x86_64::instructions::interrupts::disable();
|
||||||
@ -289,8 +363,13 @@ impl Tasking {
|
|||||||
*self.current_pid.read()
|
*self.current_pid.read()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn current_tid(&self) -> Option<usize> {
|
||||||
|
*self.current_tid.read()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn exit(&self, code: u8) -> ! {
|
pub fn exit(&self, code: u8) -> ! {
|
||||||
if let Some(current_pid) = self.current_pid() {
|
if let Some(current_pid) = self.current_pid() {
|
||||||
|
self.ready_to_run.lock().retain(|&(ent_pid, _ent_tid)| ent_pid != current_pid);
|
||||||
if let Some(&proc_man_pid) = REGISTERD_PIDS.read().get(&3) {
|
if let Some(&proc_man_pid) = REGISTERD_PIDS.read().get(&3) {
|
||||||
let mut varint_buf = unsigned_varint::encode::u64_buffer();
|
let mut varint_buf = unsigned_varint::encode::u64_buffer();
|
||||||
let mut buffer = Vec::new_in(&*ACTIVE_SPACE);
|
let mut buffer = Vec::new_in(&*ACTIVE_SPACE);
|
||||||
@ -321,17 +400,18 @@ impl Tasking {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
loop {
|
loop {
|
||||||
let next_process_pid = self.ready_to_run.lock().pop_front();
|
let rtr_head = self.ready_to_run.lock().pop_front();
|
||||||
if let Some(next_process_pid) = next_process_pid {
|
if let Some((next_process_pid, next_process_tid)) = rtr_head {
|
||||||
self.wfi_loop.store(false, Ordering::Relaxed);
|
self.wfi_loop.store(false, Ordering::Relaxed);
|
||||||
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
|
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
|
||||||
if self.current_pid.read().is_some() {
|
if self.current_pid.read().is_some() {
|
||||||
self.current_process(|process| {
|
self.current_thread(|process| {
|
||||||
*process.sleeping.write() = Some(SleepReason::Exited);
|
*process.sleeping.write() = Some(SleepReason::Exited);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
*self.current_pid.write() = Some(next_process_pid);
|
*self.current_pid.write() = Some(next_process_pid);
|
||||||
let kernel_esp = self
|
*self.current_tid.write() = Some(next_process_tid);
|
||||||
|
self
|
||||||
.current_process_mut(|process| {
|
.current_process_mut(|process| {
|
||||||
#[expect(
|
#[expect(
|
||||||
clippy::expect_used,
|
clippy::expect_used,
|
||||||
@ -342,8 +422,10 @@ impl Tasking {
|
|||||||
.take()
|
.take()
|
||||||
.expect("Non-current process has active page table")
|
.expect("Non-current process has active page table")
|
||||||
.activate();
|
.activate();
|
||||||
gdt::set_tss_stack(process.kernel_esp_top);
|
});
|
||||||
process.kernel_esp
|
let kernel_esp = self.current_thread_mut(|thread| {
|
||||||
|
gdt::set_tss_stack(thread.kernel_esp_top);
|
||||||
|
thread.kernel_esp
|
||||||
});
|
});
|
||||||
switch_to_asm_exit(kernel_esp);
|
switch_to_asm_exit(kernel_esp);
|
||||||
unreachable!()
|
unreachable!()
|
||||||
@ -360,14 +442,29 @@ impl Tasking {
|
|||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
let mut processes = self.processes.write();
|
let mut processes = self.processes.write();
|
||||||
let process = processes.get(pid).ok_or(())?;
|
let process = processes.remove(pid);
|
||||||
if process.sleeping.read().is_none() {
|
self.ready_to_run.lock().retain(|&(ent_pid, _ent_tid)| ent_pid != pid);
|
||||||
self.ready_to_run.lock().retain(|e| *e != pid);
|
let threads = process.threads.into_inner();
|
||||||
|
for (_, thread) in threads {
|
||||||
|
self.freeable_kstacks.lock().push(thread.kernel_stack);
|
||||||
}
|
}
|
||||||
self.freeable_kstacks.lock().push(processes.remove(pid).kernel_stack);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn current_thread<F: FnOnce(&Thread) -> T, T>(&self, func: F) -> T {
|
||||||
|
self.current_process(|process| {
|
||||||
|
let threads = process.threads.read();
|
||||||
|
func(&threads[self.current_tid().unwrap()])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn current_thread_mut<F: FnOnce(&mut Thread) -> T, T>(&self, func: F) -> T {
|
||||||
|
self.current_process_mut(|process| {
|
||||||
|
let mut threads = process.threads.write();
|
||||||
|
func(&mut threads[self.current_tid().unwrap()])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn current_process<F: FnOnce(&Process) -> T, T>(&self, func: F) -> T {
|
pub fn current_process<F: FnOnce(&Process) -> T, T>(&self, func: F) -> T {
|
||||||
let processes = self.processes.read();
|
let processes = self.processes.read();
|
||||||
#[warn(clippy::unwrap_used, reason = "FIXME")]
|
#[warn(clippy::unwrap_used, reason = "FIXME")]
|
||||||
@ -382,6 +479,16 @@ impl Tasking {
|
|||||||
func(&mut processes[self.current_pid().unwrap()])
|
func(&mut processes[self.current_pid().unwrap()])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn process<F: FnOnce(&Process) -> T, T>(
|
||||||
|
&self,
|
||||||
|
pid: usize,
|
||||||
|
func: F,
|
||||||
|
) -> Result<T, InvalidPid> {
|
||||||
|
let processes = self.processes.read();
|
||||||
|
#[warn(clippy::unwrap_used, reason = "FIXME")]
|
||||||
|
Ok(func(processes.get(pid).ok_or(InvalidPid)?))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn process_mut<F: FnOnce(&mut Process) -> T, T>(
|
pub fn process_mut<F: FnOnce(&mut Process) -> T, T>(
|
||||||
&self,
|
&self,
|
||||||
pid: usize,
|
pid: usize,
|
||||||
@ -395,19 +502,21 @@ impl Tasking {
|
|||||||
pub fn sleep(&self, reason: SleepReason) {
|
pub fn sleep(&self, reason: SleepReason) {
|
||||||
#[warn(clippy::unwrap_used, reason = "FIXME")]
|
#[warn(clippy::unwrap_used, reason = "FIXME")]
|
||||||
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
|
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
|
||||||
self.current_process(|process| {
|
self.current_thread(|thread| {
|
||||||
*process.sleeping.write() = Some(reason);
|
*thread.sleeping.write() = Some(reason);
|
||||||
});
|
});
|
||||||
self.task_yield();
|
self.task_yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wake(&self, pid: usize, reason: SleepReason) -> Result<(), InvalidPid> {
|
pub fn wake(&self, pid: usize, tid: usize, reason: SleepReason) -> Result<(), InvalidPid> {
|
||||||
let processes = self.processes.read();
|
let processes = self.processes.read();
|
||||||
let process = processes.get(pid).ok_or(InvalidPid)?;
|
let process = processes.get(pid).ok_or(InvalidPid)?;
|
||||||
let mut sleeping = process.sleeping.write();
|
let threads = process.threads.read();
|
||||||
|
let thread = threads.get(tid).ok_or(InvalidPid)?;
|
||||||
|
let mut sleeping = thread.sleeping.write();
|
||||||
if *sleeping == Some(reason) {
|
if *sleeping == Some(reason) {
|
||||||
if Some(pid) != self.current_pid() {
|
if Some(pid) != self.current_pid() || Some(tid) != self.current_tid() {
|
||||||
self.ready_to_run.lock().push_back(pid);
|
self.ready_to_run.lock().push_back((pid, tid));
|
||||||
}
|
}
|
||||||
*sleeping = None;
|
*sleeping = None;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user