From 11df5a2ec3ac972656f99789da2b25631e7aba86 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 26 Jul 2023 21:32:00 +0200 Subject: [PATCH] kernel: Use pid_t internally for process IDs This removes a bunch of casts between pid_t and u64, and makes more sense since pid_t is literally the data type for process IDs. --- kernel/src/arch/x86_64/Thread.cpp | 2 +- kernel/src/fs/devices/ConsoleDevice.cpp | 2 +- kernel/src/sys/exec.cpp | 2 +- kernel/src/sys/id.cpp | 8 ++++---- kernel/src/sys/pstat.cpp | 4 ++-- kernel/src/thread/Scheduler.cpp | 12 ++++++------ kernel/src/thread/Scheduler.h | 2 +- kernel/src/thread/Thread.cpp | 10 +++++----- kernel/src/thread/Thread.h | 6 +++--- 9 files changed, 24 insertions(+), 24 deletions(-) diff --git a/kernel/src/arch/x86_64/Thread.cpp b/kernel/src/arch/x86_64/Thread.cpp index 311bf7b5..10f2e79d 100644 --- a/kernel/src/arch/x86_64/Thread.cpp +++ b/kernel/src/arch/x86_64/Thread.cpp @@ -106,7 +106,7 @@ bool Thread::deliver_signal(int signo, Registers* current_regs) memcpy(®s, current_regs, sizeof(regs)); - kinfoln("signal: delivering signal %d for thread %ld, ip=%p, sp=%p, handler=%p, sigreturn=%p", signo, id, + kinfoln("signal: delivering signal %d for thread %d, ip=%p, sp=%p, handler=%p, sigreturn=%p", signo, id, (void*)regs.rip, (void*)regs.rsp, (void*)handler.sa_handler, (void*)handler.__sa_sigreturn); regs.rsp -= 128; // Skip the red zone diff --git a/kernel/src/fs/devices/ConsoleDevice.cpp b/kernel/src/fs/devices/ConsoleDevice.cpp index 997b06b1..6f1218e9 100644 --- a/kernel/src/fs/devices/ConsoleDevice.cpp +++ b/kernel/src/fs/devices/ConsoleDevice.cpp @@ -31,7 +31,7 @@ Result ConsoleDevice::handle_background_process_group(bool can_succeed, in auto foreground_pgrp = m_foreground_process_group.value(); auto* current = Scheduler::current(); - if ((pid_t)current->pgid == foreground_pgrp) return {}; + if (current->pgid == foreground_pgrp) return {}; if ((current->signal_mask & (1 << (signo - 1))) || (current->signal_handlers[signo - 1].sa_handler == SIG_IGN)) { diff --git a/kernel/src/sys/exec.cpp b/kernel/src/sys/exec.cpp index 6fa4034c..2f42be68 100644 --- a/kernel/src/sys/exec.cpp +++ b/kernel/src/sys/exec.cpp @@ -119,7 +119,7 @@ Result sys_execve(Registers* regs, SyscallArgs args) current->has_called_exec = true; - kinfoln("exec: thread %lu was replaced with %s", current->id, path.chars()); + kinfoln("exec: thread %d was replaced with %s", current->id, path.chars()); return 0; } diff --git a/kernel/src/sys/id.cpp b/kernel/src/sys/id.cpp index 1ca81283..c8c923d1 100644 --- a/kernel/src/sys/id.cpp +++ b/kernel/src/sys/id.cpp @@ -101,8 +101,8 @@ Result sys_setpgid(Registers*, SyscallArgs args) pid_t pgid = (pid_t)args[1]; auto* current = Scheduler::current(); - if (pid == 0) pid = (pid_t)current->id; - if (pgid == 0) pgid = (pid_t)current->id; + if (pid == 0) pid = current->id; + if (pgid == 0) pgid = current->id; if (pgid < 0) return err(EINVAL); @@ -113,7 +113,7 @@ Result sys_setpgid(Registers*, SyscallArgs args) if (thread->has_called_exec) return err(EPERM); - if (pgid != (pid_t)current->id) + if (pgid != current->id) { bool pgid_exists = false; Scheduler::for_each_in_process_group(pgid, [&pgid_exists](Thread*) { @@ -133,7 +133,7 @@ Result sys_getpgid(Registers*, SyscallArgs args) pid_t pid = (pid_t)args[0]; auto* current = Scheduler::current(); - if (pid == 0) pid = (pid_t)current->id; + if (pid == 0) pid = current->id; if (pid < 0) return err(EINVAL); diff --git a/kernel/src/sys/pstat.cpp b/kernel/src/sys/pstat.cpp index 09d46115..b930f173 100644 --- a/kernel/src/sys/pstat.cpp +++ b/kernel/src/sys/pstat.cpp @@ -20,8 +20,8 @@ Result sys_pstat(Registers*, SyscallArgs args) auto* thread = TRY(Result::from_option(Scheduler::find_by_pid(pid), ESRCH)); struct process proc; - proc.ps_pid = (pid_t)thread->id; - proc.ps_ppid = thread->parent ? (pid_t)thread->parent->id : 0; + proc.ps_pid = thread->id; + proc.ps_ppid = thread->parent ? thread->parent->id : 0; proc.ps_uid = thread->auth.uid; proc.ps_gid = thread->auth.gid; proc.ps_euid = thread->auth.euid; diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 84686d23..f56bf161 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -40,7 +40,7 @@ namespace Scheduler g_idle.stack = idle_stack; - kinfoln("Created idle thread: id %lu with ip %#lx and sp %#lx", g_idle.id, g_idle.ip(), g_idle.sp()); + kinfoln("Created idle thread: id %d with ip %#lx and sp %#lx", g_idle.id, g_idle.ip(), g_idle.sp()); g_current = &g_idle; } @@ -95,7 +95,7 @@ namespace Scheduler thread->state = ThreadState::Runnable; - kinfoln("Created kernel thread: id %lu with ip %#lx and sp %#lx", thread->id, thread->ip(), thread->sp()); + kinfoln("Created kernel thread: id %d with ip %#lx and sp %#lx", thread->id, thread->ip(), thread->sp()); return thread; } @@ -161,7 +161,7 @@ namespace Scheduler thread->signal_handlers[i] = { .sa_handler = SIG_DFL, .sa_mask = 0, .sa_flags = 0 }; } - kinfoln("Created userspace thread: id %lu with ip %#.16lx and sp %#.16lx (ksp %#lx)", thread->id, thread->ip(), + kinfoln("Created userspace thread: id %d with ip %#.16lx and sp %#.16lx (ksp %#lx)", thread->id, thread->ip(), thread->sp(), thread->kernel_stack.top()); g_threads.append(thread); @@ -308,7 +308,7 @@ namespace Scheduler { for (auto* const thread : g_threads) { - if (thread->id == (u64)pid && thread->state != ThreadState::Dying) return thread; + if (thread->id == pid && thread->state != ThreadState::Dying) return thread; } return {}; @@ -347,11 +347,11 @@ namespace Scheduler CPU::disable_interrupts(); kdbgln("--- BEGIN SCHEDULER DUMP ---"); - kdbgln("current at %p, id = %zu", g_current, g_current->id); + kdbgln("current at %p, id = %d", g_current, g_current->id); for (const auto* thread : g_threads) { - kdbgln("%p %c [%-20s] %4zu, parent = (%-18p,%zu), state = %d, ticks: (k:%04zu,u:%04zu), status = " + kdbgln("%p %c [%-20s] %4d, parent = (%-18p,%d), state = %d, ticks: (k:%04zu,u:%04zu), status = " "%d, cwd = %s", thread, thread->is_kernel ? 'k' : 'u', thread->name.chars(), thread->id, thread->parent, thread->parent ? thread->parent->id : 0, (int)thread->state, thread->kernel_ticks_self, diff --git a/kernel/src/thread/Scheduler.h b/kernel/src/thread/Scheduler.h index 8ff2f939..7c846ee5 100644 --- a/kernel/src/thread/Scheduler.h +++ b/kernel/src/thread/Scheduler.h @@ -50,7 +50,7 @@ namespace Scheduler for (Thread* current = g_threads.first().value_or(nullptr); current; current = g_threads.next(current).value_or(nullptr)) { - if (current->pgid == (u64)group) + if (current->pgid == group) { bool should_continue = callback(current); if (!should_continue) return; diff --git a/kernel/src/thread/Thread.cpp b/kernel/src/thread/Thread.cpp index 4a7b80e1..3b5958f9 100644 --- a/kernel/src/thread/Thread.cpp +++ b/kernel/src/thread/Thread.cpp @@ -11,7 +11,7 @@ #include #include -static Atomic g_next_id; +static Atomic g_next_id; LinkedList g_threads; @@ -29,7 +29,7 @@ Result new_thread() return thread; } -u64 next_thread_id() +pid_t next_thread_id() { return g_next_id.load(); } @@ -98,9 +98,9 @@ Result> Thread::resolve_atfile(int dirfd, const String& pa if (parent->state == ThreadState::Waiting) { auto child = *parent->child_being_waited_for; - if (child == -1 || child == (pid_t)id) + if (child == -1 || child == id) { - parent->child_being_waited_for = (pid_t)id; + parent->child_being_waited_for = id; parent->wake_up(); } } @@ -151,7 +151,7 @@ void Thread::process_pending_signals(Registers* current_regs) if (pending_signals & (1 << i)) { pending_signals &= ~(1 << i); - kinfoln("signal: executing signal %d for thread %ld", signo, id); + kinfoln("signal: executing signal %d for thread %d", signo, id); auto handler = signal_handlers[i]; if (signo != SIGKILL && signo != SIGSTOP && handler.sa_handler == SIG_IGN) { diff --git a/kernel/src/thread/Thread.h b/kernel/src/thread/Thread.h index 0997f40e..c3e44958 100644 --- a/kernel/src/thread/Thread.h +++ b/kernel/src/thread/Thread.h @@ -56,8 +56,8 @@ struct Thread : public LinkedListNode { Registers regs; - u64 id; - u64 pgid { 0 }; + pid_t id; + pid_t pgid { 0 }; Credentials auth; @@ -159,6 +159,6 @@ bool is_in_kernel(Registers* regs); Result new_thread(); -u64 next_thread_id(); +pid_t next_thread_id(); extern LinkedList g_threads;