From 2b6546c40a7ea7738a3bda81f57e396ab3a305ea Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 27 Feb 2021 23:56:16 +0100 Subject: [PATCH] Kernel: Make Thread use AK::Time internally This commit is very invasive, because Thread likes to take a pointer and write to it. This means that translating between timespec/timeval/Time would have been more difficult than just changing everything that hands a raw pointer to Thread, in bulk. --- Kernel/Devices/AsyncDeviceRequest.cpp | 2 +- Kernel/Devices/AsyncDeviceRequest.h | 2 +- Kernel/Devices/USB/UHCIController.cpp | 6 ++-- Kernel/Syscalls/alarm.cpp | 10 +++--- Kernel/Syscalls/beep.cpp | 2 +- Kernel/Syscalls/clock.cpp | 11 +++--- Kernel/Syscalls/futex.cpp | 4 +-- Kernel/Syscalls/select.cpp | 8 ++--- Kernel/Tasks/SyncTask.cpp | 2 +- Kernel/Thread.cpp | 4 +-- Kernel/Thread.h | 49 ++++++--------------------- Kernel/ThreadBlockers.cpp | 31 ++++++++++++++--- Kernel/TimerQueue.cpp | 12 +++---- Kernel/TimerQueue.h | 5 ++- 14 files changed, 66 insertions(+), 82 deletions(-) diff --git a/Kernel/Devices/AsyncDeviceRequest.cpp b/Kernel/Devices/AsyncDeviceRequest.cpp index 28e6bea121dcf0..1a4430de3e1a25 100644 --- a/Kernel/Devices/AsyncDeviceRequest.cpp +++ b/Kernel/Devices/AsyncDeviceRequest.cpp @@ -68,7 +68,7 @@ void AsyncDeviceRequest::request_finished() m_queue.wake_all(); } -auto AsyncDeviceRequest::wait(timeval* timeout) -> RequestWaitResult +auto AsyncDeviceRequest::wait(Time* timeout) -> RequestWaitResult { VERIFY(!m_parent_request); auto request_result = get_request_result(); diff --git a/Kernel/Devices/AsyncDeviceRequest.h b/Kernel/Devices/AsyncDeviceRequest.h index 0577a3aa17fe22..ec3d7c4e92bb9b 100644 --- a/Kernel/Devices/AsyncDeviceRequest.h +++ b/Kernel/Devices/AsyncDeviceRequest.h @@ -76,7 +76,7 @@ class AsyncDeviceRequest : public RefCounted { void add_sub_request(NonnullRefPtr); - [[nodiscard]] RequestWaitResult wait(timeval* = nullptr); + [[nodiscard]] RequestWaitResult wait(Time* = nullptr); void do_start(Badge) { diff --git a/Kernel/Devices/USB/UHCIController.cpp b/Kernel/Devices/USB/UHCIController.cpp index 51430d35b1458c..852f389aa96ab7 100644 --- a/Kernel/Devices/USB/UHCIController.cpp +++ b/Kernel/Devices/USB/UHCIController.cpp @@ -398,10 +398,8 @@ void UHCIController::do_debug_transfer() void UHCIController::spawn_port_proc() { RefPtr usb_hotplug_thread; - timespec sleep_time {}; - sleep_time.tv_sec = 1; - Process::create_kernel_process(usb_hotplug_thread, "UHCIHotplug", [&, sleep_time] { + Process::create_kernel_process(usb_hotplug_thread, "UHCIHotplug", [&] { for (;;) { for (int port = 0; port < UHCI_ROOT_PORT_COUNT; port++) { u16 port_data = 0; @@ -448,7 +446,7 @@ void UHCIController::spawn_port_proc() } } } - (void)Thread::current()->sleep(sleep_time); + (void)Thread::current()->sleep(Time::from_seconds(1)); } }); } diff --git a/Kernel/Syscalls/alarm.cpp b/Kernel/Syscalls/alarm.cpp index e195de65fdef23..ff3de1350c0a7e 100644 --- a/Kernel/Syscalls/alarm.cpp +++ b/Kernel/Syscalls/alarm.cpp @@ -36,9 +36,8 @@ KResultOr Process::sys$alarm(unsigned seconds) if (auto alarm_timer = move(m_alarm_timer)) { if (TimerQueue::the().cancel_timer(*alarm_timer)) { // The timer hasn't fired. Round up the remaining time (if any) - timespec remaining; - timespec_add(alarm_timer->remaining(), { 0, 1000000000 - 1 }, remaining); - previous_alarm_remaining = remaining.tv_sec; + Time remaining = alarm_timer->remaining() + Time::from_nanoseconds(999'999'999); + previous_alarm_remaining = remaining.to_truncated_seconds(); } // We had an existing alarm, must return a non-zero value here! if (previous_alarm_remaining == 0) @@ -46,8 +45,9 @@ KResultOr Process::sys$alarm(unsigned seconds) } if (seconds > 0) { - auto deadline = TimeManagement::the().current_time(CLOCK_REALTIME_COARSE).value(); - timespec_add(deadline, { seconds, 0 }, deadline); + // FIXME: Should use AK::Time internally + auto deadline = Time::from_timespec(TimeManagement::the().current_time(CLOCK_REALTIME_COARSE).value()); + deadline = deadline + Time::from_seconds(seconds); m_alarm_timer = TimerQueue::the().add_timer_without_id(CLOCK_REALTIME_COARSE, deadline, [this]() { [[maybe_unused]] auto rc = send_signal(SIGALRM, nullptr); }); diff --git a/Kernel/Syscalls/beep.cpp b/Kernel/Syscalls/beep.cpp index a94df54079e07b..c614a0e47f1e9f 100644 --- a/Kernel/Syscalls/beep.cpp +++ b/Kernel/Syscalls/beep.cpp @@ -32,7 +32,7 @@ namespace Kernel { KResultOr Process::sys$beep() { PCSpeaker::tone_on(440); - auto result = Thread::current()->sleep({ 0, 200 }); + auto result = Thread::current()->sleep(Time::from_nanoseconds(200'000'000)); PCSpeaker::tone_off(); if (result.was_interrupted()) return EINTR; diff --git a/Kernel/Syscalls/clock.cpp b/Kernel/Syscalls/clock.cpp index 4e780706c4c5b7..d883e6ac04c0bc 100644 --- a/Kernel/Syscalls/clock.cpp +++ b/Kernel/Syscalls/clock.cpp @@ -94,13 +94,12 @@ KResultOr Process::sys$clock_nanosleep(Userspacesleep_until(params.clock_id, requested_sleep->to_timespec()).was_interrupted(); + was_interrupted = Thread::current()->sleep_until(params.clock_id, requested_sleep.value()).was_interrupted(); } else { - timespec remaining_sleep; - // FIXME: Should use AK::Time internally - was_interrupted = Thread::current()->sleep(params.clock_id, requested_sleep->to_timespec(), &remaining_sleep).was_interrupted(); - if (was_interrupted && params.remaining_sleep && !copy_to_user(params.remaining_sleep, &remaining_sleep)) + Time remaining_sleep; + was_interrupted = Thread::current()->sleep(params.clock_id, requested_sleep.value(), &remaining_sleep).was_interrupted(); + timespec remaining_sleep_ts = remaining_sleep.to_timespec(); + if (was_interrupted && params.remaining_sleep && !copy_to_user(params.remaining_sleep, &remaining_sleep_ts)) return EFAULT; } if (was_interrupted) diff --git a/Kernel/Syscalls/futex.cpp b/Kernel/Syscalls/futex.cpp index 08ec27d49cdbb7..c030a2c59bb2c3 100644 --- a/Kernel/Syscalls/futex.cpp +++ b/Kernel/Syscalls/futex.cpp @@ -123,9 +123,7 @@ KResultOr Process::sys$futex(Userspace use return EFAULT; clockid_t clock_id = (params.futex_op & FUTEX_CLOCK_REALTIME) ? CLOCK_REALTIME_COARSE : CLOCK_MONOTONIC_COARSE; bool is_absolute = cmd != FUTEX_WAIT; - // FIXME: Should use AK::Time internally - timespec timeout_copy = timeout_time->to_timespec(); - timeout = Thread::BlockTimeout(is_absolute, &timeout_copy, nullptr, clock_id); + timeout = Thread::BlockTimeout(is_absolute, &timeout_time.value(), nullptr, clock_id); } if (cmd == FUTEX_WAIT_BITSET && params.val3 == FUTEX_BITSET_MATCH_ANY) cmd = FUTEX_WAIT; diff --git a/Kernel/Syscalls/select.cpp b/Kernel/Syscalls/select.cpp index 3259e51f077394..0b7014eb8bebe6 100644 --- a/Kernel/Syscalls/select.cpp +++ b/Kernel/Syscalls/select.cpp @@ -48,9 +48,7 @@ KResultOr Process::sys$select(Userspace u Optional