From fc5cab5c2180b4ba8efcf74b937c2b7e26dad781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Fri, 17 Mar 2023 19:50:39 +0100 Subject: [PATCH] Everywhere: Use MonotonicTime instead of Duration This is easily identifiable by anyone who uses Duration::now_monotonic, and any downstream users of that data. --- AK/Format.cpp | 14 ++++++------- Kernel/Devices/Audio/IntelHDA/Controller.cpp | 6 +++--- Kernel/Storage/SD/SDHostController.cpp | 6 +++--- Kernel/Time/TimeManagement.cpp | 16 +++++++-------- Kernel/Time/TimeManagement.h | 5 +++-- .../Applications/HexEditor/HexDocument.cpp | 2 +- Userland/Applications/HexEditor/HexDocument.h | 4 ++-- .../Applications/ThemeEditor/MainWidget.cpp | 4 ++-- .../Applications/ThemeEditor/MainWidget.h | 2 +- Userland/Libraries/LibCore/ElapsedTimer.cpp | 5 ++--- Userland/Libraries/LibCore/ElapsedTimer.h | 4 ++-- .../LibCore/EventLoopImplementationUnix.cpp | 20 +++++++++---------- .../LibCore/EventLoopImplementationUnix.h | 3 ++- Userland/Libraries/LibDesktop/Screensaver.cpp | 2 +- Userland/Libraries/LibDesktop/Screensaver.h | 4 ++-- Userland/Libraries/LibGUI/MessageBox.cpp | 8 ++++---- Userland/Libraries/LibGUI/MessageBox.h | 4 ++-- Userland/Libraries/LibGUI/TextDocument.cpp | 4 ++-- Userland/Libraries/LibGUI/TextDocument.h | 4 ++-- Userland/Libraries/LibGUI/UndoStack.cpp | 2 +- Userland/Libraries/LibGUI/UndoStack.h | 4 ++-- .../LibJS/Contrib/Test262/AgentObject.cpp | 4 ++-- .../Libraries/LibVideo/PlaybackManager.cpp | 14 ++++++------- .../LibWeb/HTML/HTMLMediaElement.cpp | 4 ++-- .../Libraries/LibWeb/HTML/HTMLMediaElement.h | 2 +- .../LibWeb/HighResolutionTime/Performance.cpp | 2 +- .../LibWeb/HighResolutionTime/TimeOrigin.cpp | 2 +- .../LibWeb/WebDriver/ExecuteScript.cpp | 4 ++-- .../WebContent/WebDriverConnection.cpp | 4 ++-- 29 files changed, 79 insertions(+), 80 deletions(-) diff --git a/AK/Format.cpp b/AK/Format.cpp index a67b46a518..12daeb6815 100644 --- a/AK/Format.cpp +++ b/AK/Format.cpp @@ -916,14 +916,14 @@ void vdbg(StringView fmtstr, TypeErasedFormatParams& params, bool newline) #ifdef AK_OS_SERENITY # ifdef KERNEL if (Kernel::Processor::is_initialized() && TimeManagement::is_initialized()) { - struct timespec ts = TimeManagement::the().monotonic_time(TimePrecision::Coarse).to_timespec(); + auto time = TimeManagement::the().monotonic_time(TimePrecision::Coarse); if (Kernel::Thread::current()) { auto& thread = *Kernel::Thread::current(); thread.process().name().with([&](auto& process_name) { - builder.appendff("{}.{:03} \033[34;1m[#{} {}({}:{})]\033[0m: ", ts.tv_sec, ts.tv_nsec / 1000000, Kernel::Processor::current_id(), process_name->view(), thread.pid().value(), thread.tid().value()); + builder.appendff("{}.{:03} \033[34;1m[#{} {}({}:{})]\033[0m: ", time.truncated_seconds(), time.nanoseconds_within_second() / 1000000, Kernel::Processor::current_id(), process_name->view(), thread.pid().value(), thread.tid().value()); }); } else { - builder.appendff("{}.{:03} \033[34;1m[#{} Kernel]\033[0m: ", ts.tv_sec, ts.tv_nsec / 1000000, Kernel::Processor::current_id()); + builder.appendff("{}.{:03} \033[34;1m[#{} Kernel]\033[0m: ", time.truncated_seconds(), time.nanoseconds_within_second() / 1000000, Kernel::Processor::current_id()); } } else { builder.appendff("\033[34;1m[Kernel]\033[0m: "); @@ -968,18 +968,16 @@ void vdmesgln(StringView fmtstr, TypeErasedFormatParams& params) StringBuilder builder; # ifdef AK_OS_SERENITY - struct timespec ts = {}; - if (TimeManagement::is_initialized()) { - ts = TimeManagement::the().monotonic_time(TimePrecision::Coarse).to_timespec(); + auto time = TimeManagement::the().monotonic_time(TimePrecision::Coarse); if (Kernel::Processor::is_initialized() && Kernel::Thread::current()) { auto& thread = *Kernel::Thread::current(); thread.process().name().with([&](auto& process_name) { - builder.appendff("{}.{:03} \033[34;1m[{}({}:{})]\033[0m: ", ts.tv_sec, ts.tv_nsec / 1000000, process_name->view(), thread.pid().value(), thread.tid().value()); + builder.appendff("{}.{:03} \033[34;1m[{}({}:{})]\033[0m: ", time.truncated_seconds(), time.nanoseconds_within_second() / 1000000, process_name->view(), thread.pid().value(), thread.tid().value()); }); } else { - builder.appendff("{}.{:03} \033[34;1m[Kernel]\033[0m: ", ts.tv_sec, ts.tv_nsec / 1000000); + builder.appendff("{}.{:03} \033[34;1m[Kernel]\033[0m: ", time.truncated_seconds(), time.nanoseconds_within_second() / 1000000); } } else { builder.appendff("\033[34;1m[Kernel]\033[0m: "); diff --git a/Kernel/Devices/Audio/IntelHDA/Controller.cpp b/Kernel/Devices/Audio/IntelHDA/Controller.cpp index cae6b6800b..74c8498f5a 100644 --- a/Kernel/Devices/Audio/IntelHDA/Controller.cpp +++ b/Kernel/Devices/Audio/IntelHDA/Controller.cpp @@ -328,12 +328,12 @@ ErrorOr Controller::get_pcm_output_sample_rate(size_t channel_index) ErrorOr wait_until(size_t delay_in_microseconds, size_t timeout_in_microseconds, Function()> condition) { + auto const timeout = Duration::from_microseconds(static_cast(timeout_in_microseconds)); auto const& time_management = TimeManagement::the(); - // FIXME: Use monotonic time instead. - u64 start_microseconds = time_management.now().offset_to_epoch().to_microseconds(); + auto start = time_management.monotonic_time(TimePrecision::Precise); while (!TRY(condition())) { microseconds_delay(delay_in_microseconds); - if ((time_management.now().offset_to_epoch().to_microseconds() - start_microseconds) >= timeout_in_microseconds) + if (time_management.monotonic_time(TimePrecision::Precise) - start >= timeout) return ETIMEDOUT; } return {}; diff --git a/Kernel/Storage/SD/SDHostController.cpp b/Kernel/Storage/SD/SDHostController.cpp index e65145d84b..b21acdabb1 100644 --- a/Kernel/Storage/SD/SDHostController.cpp +++ b/Kernel/Storage/SD/SDHostController.cpp @@ -25,9 +25,9 @@ namespace Kernel { static void delay(i64 nanoseconds) { - auto start = TimeManagement::the().monotonic_time().to_nanoseconds(); - auto end = start + nanoseconds; - while (TimeManagement::the().monotonic_time().to_nanoseconds() < end) + auto start = TimeManagement::the().monotonic_time(); + auto end = start + Duration::from_nanoseconds(nanoseconds); + while (TimeManagement::the().monotonic_time() < end) Processor::pause(); } diff --git a/Kernel/Time/TimeManagement.cpp b/Kernel/Time/TimeManagement.cpp index d7c3c18716..aea122a818 100644 --- a/Kernel/Time/TimeManagement.cpp +++ b/Kernel/Time/TimeManagement.cpp @@ -54,7 +54,7 @@ static u64 (*s_scheduler_current_time)(); static u64 current_time_monotonic() { // We always need a precise timestamp here, we cannot rely on a coarse timestamp - return (u64)TimeManagement::the().monotonic_time(TimePrecision::Precise).to_nanoseconds(); + return (u64)TimeManagement::the().monotonic_time(TimePrecision::Precise).nanoseconds(); } u64 TimeManagement::scheduler_current_time() @@ -81,11 +81,11 @@ Duration TimeManagement::current_time(clockid_t clock_id) const { switch (clock_id) { case CLOCK_MONOTONIC: - return monotonic_time(TimePrecision::Precise); + return monotonic_time(TimePrecision::Precise).time_since_start({}); case CLOCK_MONOTONIC_COARSE: - return monotonic_time(TimePrecision::Coarse); + return monotonic_time(TimePrecision::Coarse).time_since_start({}); case CLOCK_MONOTONIC_RAW: - return monotonic_time_raw(); + return monotonic_time_raw().time_since_start({}); case CLOCK_REALTIME: return epoch_time(TimePrecision::Precise).offset_to_epoch(); case CLOCK_REALTIME_COARSE: @@ -110,7 +110,7 @@ void TimeManagement::set_epoch_time(UnixDateTime ts) m_remaining_epoch_time_adjustment = {}; } -Duration TimeManagement::monotonic_time(TimePrecision precision) const +MonotonicTime TimeManagement::monotonic_time(TimePrecision precision) const { // This is the time when last updated by an interrupt. u64 seconds; @@ -145,7 +145,7 @@ Duration TimeManagement::monotonic_time(TimePrecision precision) const VERIFY(ticks < m_time_ticks_per_second); u64 ns = ((u64)ticks * 1000000000ull) / m_time_ticks_per_second; VERIFY(ns < 1000000000ull); - return Duration::from_timespec({ (i64)seconds, (i32)ns }); + return MonotonicTime::from_hardware_time({}, seconds, ns); } UnixDateTime TimeManagement::epoch_time(TimePrecision) const @@ -162,7 +162,7 @@ UnixDateTime TimeManagement::epoch_time(TimePrecision) const u64 TimeManagement::uptime_ms() const { - auto mtime = monotonic_time().to_timespec(); + auto mtime = monotonic_time().time_since_start({}).to_timespec(); // This overflows after 292 million years of uptime. // Since this is only used for performance timestamps and sys$times, that's probably enough. u64 ms = mtime.tv_sec * 1000ull; @@ -539,7 +539,7 @@ void TimeManagement::update_time_page() auto& page = time_page(); u32 update_iteration = AK::atomic_fetch_add(&page.update2, 1u, AK::MemoryOrder::memory_order_acquire); page.clocks[CLOCK_REALTIME_COARSE] = m_epoch_time.to_timespec(); - page.clocks[CLOCK_MONOTONIC_COARSE] = monotonic_time(TimePrecision::Coarse).to_timespec(); + page.clocks[CLOCK_MONOTONIC_COARSE] = monotonic_time(TimePrecision::Coarse).time_since_start({}).to_timespec(); AK::atomic_store(&page.update1, update_iteration + 1u, AK::MemoryOrder::memory_order_release); } diff --git a/Kernel/Time/TimeManagement.h b/Kernel/Time/TimeManagement.h index 278852c5be..642298e1b0 100644 --- a/Kernel/Time/TimeManagement.h +++ b/Kernel/Time/TimeManagement.h @@ -41,9 +41,10 @@ public: static u64 scheduler_current_time(); static ErrorOr validate_clock_id(clockid_t); + // This API cannot distinguish returned time types; prefer the clock-specific functions instead. Duration current_time(clockid_t) const; - Duration monotonic_time(TimePrecision = TimePrecision::Coarse) const; - Duration monotonic_time_raw() const + MonotonicTime monotonic_time(TimePrecision = TimePrecision::Coarse) const; + MonotonicTime monotonic_time_raw() const { // TODO: implement return monotonic_time(TimePrecision::Precise); diff --git a/Userland/Applications/HexEditor/HexDocument.cpp b/Userland/Applications/HexEditor/HexDocument.cpp index b8b5f7ce43..3c69f1b554 100644 --- a/Userland/Applications/HexEditor/HexDocument.cpp +++ b/Userland/Applications/HexEditor/HexDocument.cpp @@ -229,7 +229,7 @@ bool HexDocumentUndoCommand::merge_with(GUI::Command const& other) m_old[relative_start + i] = typed_other.m_old[i]; } - m_timestamp = Duration::now_monotonic(); + m_timestamp = MonotonicTime::now(); return true; } diff --git a/Userland/Applications/HexEditor/HexDocument.h b/Userland/Applications/HexEditor/HexDocument.h index c28a9aa885..402e9b8c14 100644 --- a/Userland/Applications/HexEditor/HexDocument.h +++ b/Userland/Applications/HexEditor/HexDocument.h @@ -103,9 +103,9 @@ public: ErrorOr try_add_changed_bytes(ByteBuffer old_values, ByteBuffer new_values); private: - bool commit_time_expired() const { return Duration::now_monotonic() - m_timestamp >= COMMAND_COMMIT_TIME; } + bool commit_time_expired() const { return MonotonicTime::now() - m_timestamp >= COMMAND_COMMIT_TIME; } - Duration m_timestamp = Duration::now_monotonic(); + MonotonicTime m_timestamp = MonotonicTime::now(); WeakPtr m_document; size_t m_position; ByteBuffer m_old; diff --git a/Userland/Applications/ThemeEditor/MainWidget.cpp b/Userland/Applications/ThemeEditor/MainWidget.cpp index f579b948b2..c1991bee9a 100644 --- a/Userland/Applications/ThemeEditor/MainWidget.cpp +++ b/Userland/Applications/ThemeEditor/MainWidget.cpp @@ -347,7 +347,7 @@ void MainWidget::save_to_file(String const& filename, NonnullOwnPtr if (sync_result.is_error()) { GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Failed to save theme file: {}", sync_result.error())); } else { - m_last_modified_time = Duration::now_monotonic(); + m_last_modified_time = MonotonicTime::now(); set_path(filename.to_deprecated_string()); window()->set_modified(false); } @@ -643,7 +643,7 @@ ErrorOr MainWidget::load_from_file(String const& filename, NonnullOwnPtrset_modified(false); return {}; } diff --git a/Userland/Applications/ThemeEditor/MainWidget.h b/Userland/Applications/ThemeEditor/MainWidget.h index 98cb9cc41d..8e7e10e690 100644 --- a/Userland/Applications/ThemeEditor/MainWidget.h +++ b/Userland/Applications/ThemeEditor/MainWidget.h @@ -124,7 +124,7 @@ private: Optional m_path; Gfx::Palette m_current_palette; - Duration m_last_modified_time { Duration::now_monotonic() }; + MonotonicTime m_last_modified_time { MonotonicTime::now() }; RefPtr m_alignment_model; diff --git a/Userland/Libraries/LibCore/ElapsedTimer.cpp b/Userland/Libraries/LibCore/ElapsedTimer.cpp index 93036780f2..f82451bd80 100644 --- a/Userland/Libraries/LibCore/ElapsedTimer.cpp +++ b/Userland/Libraries/LibCore/ElapsedTimer.cpp @@ -20,13 +20,12 @@ ElapsedTimer ElapsedTimer::start_new() void ElapsedTimer::start() { m_valid = true; - m_origin_time = m_precise ? Duration::now_monotonic() : Duration::now_monotonic_coarse(); + m_origin_time = m_precise ? MonotonicTime::now() : MonotonicTime::now_coarse(); } void ElapsedTimer::reset() { m_valid = false; - m_origin_time = {}; } i64 ElapsedTimer::elapsed_milliseconds() const @@ -37,7 +36,7 @@ i64 ElapsedTimer::elapsed_milliseconds() const Duration ElapsedTimer::elapsed_time() const { VERIFY(is_valid()); - auto now = m_precise ? Duration::now_monotonic() : Duration::now_monotonic_coarse(); + auto now = m_precise ? MonotonicTime::now() : MonotonicTime::now_coarse(); return now - m_origin_time; } diff --git a/Userland/Libraries/LibCore/ElapsedTimer.h b/Userland/Libraries/LibCore/ElapsedTimer.h index e324755fab..1ed5d0e88a 100644 --- a/Userland/Libraries/LibCore/ElapsedTimer.h +++ b/Userland/Libraries/LibCore/ElapsedTimer.h @@ -32,10 +32,10 @@ public: return elapsed_milliseconds(); } - Duration const& origin_time() const { return m_origin_time; } + MonotonicTime const& origin_time() const { return m_origin_time; } private: - Duration m_origin_time {}; + MonotonicTime m_origin_time { MonotonicTime::now() }; bool m_precise { false }; bool m_valid { false }; }; diff --git a/Userland/Libraries/LibCore/EventLoopImplementationUnix.cpp b/Userland/Libraries/LibCore/EventLoopImplementationUnix.cpp index 5ca382880d..08e6db5690 100644 --- a/Userland/Libraries/LibCore/EventLoopImplementationUnix.cpp +++ b/Userland/Libraries/LibCore/EventLoopImplementationUnix.cpp @@ -30,13 +30,13 @@ thread_local ThreadData* s_thread_data; struct EventLoopTimer { int timer_id { 0 }; Duration interval; - Duration fire_time; + MonotonicTime fire_time { MonotonicTime::now_coarse() }; bool should_reload { false }; TimerShouldFireWhenNotVisible fire_when_not_visible { TimerShouldFireWhenNotVisible::No }; WeakPtr owner; - void reload(Duration const& now) { fire_time = now + interval; } - bool has_expired(Duration const& now) const { return now > fire_time; } + void reload(MonotonicTime const& now) { fire_time = now + interval; } + bool has_expired(MonotonicTime const& now) const { return now > fire_time; } }; struct ThreadData { @@ -171,13 +171,13 @@ retry: // Figure out how long to wait at maximum. // This mainly depends on the PumpMode and whether we have pending events, but also the next expiring timer. - Duration now; + MonotonicTime now = MonotonicTime::now_coarse(); struct timeval timeout = { 0, 0 }; bool should_wait_forever = false; if (mode == EventLoopImplementation::PumpMode::WaitForEvents && !has_pending_events) { auto next_timer_expiration = get_next_timer_expiration(); if (next_timer_expiration.has_value()) { - now = Duration::now_monotonic_coarse(); + now = MonotonicTime::now(); auto computed_timeout = next_timer_expiration.value() - now; if (computed_timeout.is_negative()) computed_timeout = Duration::zero(); @@ -231,7 +231,7 @@ try_select_again: } if (!thread_data.timers.is_empty()) { - now = Duration::now_monotonic_coarse(); + now = MonotonicTime::now_coarse(); } // Handle expired timers. @@ -349,10 +349,10 @@ void EventLoopImplementationUnix::notify_forked_and_in_child() thread_data.pid = getpid(); } -Optional EventLoopManagerUnix::get_next_timer_expiration() +Optional EventLoopManagerUnix::get_next_timer_expiration() { - auto now = Duration::now_monotonic_coarse(); - Optional soonest {}; + auto now = MonotonicTime::now_coarse(); + Optional soonest {}; for (auto& it : ThreadData::the().timers) { auto& fire_time = it.value->fire_time; auto owner = it.value->owner.strong_ref(); @@ -490,7 +490,7 @@ int EventLoopManagerUnix::register_timer(Object& object, int milliseconds, bool auto timer = make(); timer->owner = object; timer->interval = Duration::from_milliseconds(milliseconds); - timer->reload(Duration::now_monotonic_coarse()); + timer->reload(MonotonicTime::now_coarse()); timer->should_reload = should_reload; timer->fire_when_not_visible = fire_when_not_visible; int timer_id = thread_data.id_allocator.allocate(); diff --git a/Userland/Libraries/LibCore/EventLoopImplementationUnix.h b/Userland/Libraries/LibCore/EventLoopImplementationUnix.h index 7cc7b191a6..ee93cefa4e 100644 --- a/Userland/Libraries/LibCore/EventLoopImplementationUnix.h +++ b/Userland/Libraries/LibCore/EventLoopImplementationUnix.h @@ -6,6 +6,7 @@ #pragma once +#include #include namespace Core { @@ -28,7 +29,7 @@ public: virtual void unregister_signal(int handler_id) override; void wait_for_events(EventLoopImplementation::PumpMode); - static Optional get_next_timer_expiration(); + static Optional get_next_timer_expiration(); private: void dispatch_signal(int signal_number); diff --git a/Userland/Libraries/LibDesktop/Screensaver.cpp b/Userland/Libraries/LibDesktop/Screensaver.cpp index 92dc32fb03..7190e6969d 100644 --- a/Userland/Libraries/LibDesktop/Screensaver.cpp +++ b/Userland/Libraries/LibDesktop/Screensaver.cpp @@ -40,7 +40,7 @@ void Screensaver::mousedown_event(GUI::MouseEvent&) void Screensaver::mousemove_event(GUI::MouseEvent& event) { - auto now = AK::Duration::now_monotonic(); + auto now = MonotonicTime::now(); if ((now - m_start_time).to_milliseconds() < mouse_tracking_delay_milliseconds) return; diff --git a/Userland/Libraries/LibDesktop/Screensaver.h b/Userland/Libraries/LibDesktop/Screensaver.h index 417a021951..8fc452e035 100644 --- a/Userland/Libraries/LibDesktop/Screensaver.h +++ b/Userland/Libraries/LibDesktop/Screensaver.h @@ -30,7 +30,7 @@ public: protected: Screensaver() - : m_start_time(AK::Duration::now_monotonic()) + : m_start_time(MonotonicTime::now()) { } @@ -38,7 +38,7 @@ private: void trigger_exit(); Optional m_mouse_origin; - AK::Duration m_start_time; + MonotonicTime m_start_time; }; } diff --git a/Userland/Libraries/LibGUI/MessageBox.cpp b/Userland/Libraries/LibGUI/MessageBox.cpp index aabec2b861..b761888aba 100644 --- a/Userland/Libraries/LibGUI/MessageBox.cpp +++ b/Userland/Libraries/LibGUI/MessageBox.cpp @@ -63,12 +63,12 @@ ErrorOr MessageBox::try_show_error(Window* parent_window, St return TRY(try_show(parent_window, text, "Error"sv, GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK)); } -Dialog::ExecResult MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp) +Dialog::ExecResult MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp) { - return MUST(try_ask_about_unsaved_changes(parent_window, path, last_unmodified_timestamp)); + return MUST(try_ask_about_unsaved_changes(parent_window, path, move(last_unmodified_timestamp))); } -ErrorOr MessageBox::try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp) +ErrorOr MessageBox::try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp) { StringBuilder builder; TRY(builder.try_append("Save changes to "sv)); @@ -79,7 +79,7 @@ ErrorOr MessageBox::try_ask_about_unsaved_changes(Window* pa TRY(builder.try_append(" before closing?"sv)); if (!path.is_empty() && last_unmodified_timestamp.has_value()) { - auto age = (Duration::now_monotonic() - *last_unmodified_timestamp).to_seconds(); + auto age = (MonotonicTime::now() - *last_unmodified_timestamp).to_seconds(); auto readable_time = human_readable_time(age); TRY(builder.try_appendff("\nLast saved {} ago.", readable_time)); } diff --git a/Userland/Libraries/LibGUI/MessageBox.h b/Userland/Libraries/LibGUI/MessageBox.h index b6b76c70ba..6031244066 100644 --- a/Userland/Libraries/LibGUI/MessageBox.h +++ b/Userland/Libraries/LibGUI/MessageBox.h @@ -40,12 +40,12 @@ public: static ExecResult show(Window* parent_window, StringView text, StringView title, Type type = Type::None, InputType input_type = InputType::OK); static ExecResult show_error(Window* parent_window, StringView text); - static ExecResult ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp = {}); + static ExecResult ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp = {}); static ErrorOr try_show(Badge, i32 window_server_client_id, i32 parent_window_id, StringView text, StringView title); static ErrorOr try_show(Window* parent_window, StringView text, StringView title, Type type = Type::None, InputType input_type = InputType::OK); static ErrorOr try_show_error(Window* parent_window, StringView text); - static ErrorOr try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp = {}); + static ErrorOr try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional last_unmodified_timestamp = {}); static ErrorOr> create(Window* parent_window, StringView text, StringView title, Type type = Type::None, InputType input_type = InputType::OK); diff --git a/Userland/Libraries/LibGUI/TextDocument.cpp b/Userland/Libraries/LibGUI/TextDocument.cpp index 8f68d156ed..17aa14605d 100644 --- a/Userland/Libraries/LibGUI/TextDocument.cpp +++ b/Userland/Libraries/LibGUI/TextDocument.cpp @@ -894,7 +894,7 @@ bool InsertTextCommand::merge_with(GUI::Command const& other) m_text = builder.to_deprecated_string(); m_range.set_end(typed_other.m_range.end()); - m_timestamp = Duration::now_monotonic(); + m_timestamp = MonotonicTime::now(); return true; } @@ -994,7 +994,7 @@ bool RemoveTextCommand::merge_with(GUI::Command const& other) m_text = builder.to_deprecated_string(); m_range.set_start(typed_other.m_range.start()); - m_timestamp = Duration::now_monotonic(); + m_timestamp = MonotonicTime::now(); return true; } diff --git a/Userland/Libraries/LibGUI/TextDocument.h b/Userland/Libraries/LibGUI/TextDocument.h index b6a7ce8b83..b005014f14 100644 --- a/Userland/Libraries/LibGUI/TextDocument.h +++ b/Userland/Libraries/LibGUI/TextDocument.h @@ -228,9 +228,9 @@ public: } protected: - bool commit_time_expired() const { return Duration::now_monotonic() - m_timestamp >= COMMAND_COMMIT_TIME; } + bool commit_time_expired() const { return MonotonicTime::now() - m_timestamp >= COMMAND_COMMIT_TIME; } - Duration m_timestamp = Duration::now_monotonic(); + MonotonicTime m_timestamp = MonotonicTime::now(); TextDocument& m_document; TextDocument::Client const* m_client { nullptr }; }; diff --git a/Userland/Libraries/LibGUI/UndoStack.cpp b/Userland/Libraries/LibGUI/UndoStack.cpp index 1642ca9e63..c607a94d8d 100644 --- a/Userland/Libraries/LibGUI/UndoStack.cpp +++ b/Userland/Libraries/LibGUI/UndoStack.cpp @@ -81,7 +81,7 @@ void UndoStack::set_current_unmodified() return; m_clean_index = m_stack_index; - m_last_unmodified_timestamp = Duration::now_monotonic(); + m_last_unmodified_timestamp = MonotonicTime::now(); if (on_state_change) on_state_change(); diff --git a/Userland/Libraries/LibGUI/UndoStack.h b/Userland/Libraries/LibGUI/UndoStack.h index 40f14c4c23..81a11df83d 100644 --- a/Userland/Libraries/LibGUI/UndoStack.h +++ b/Userland/Libraries/LibGUI/UndoStack.h @@ -31,7 +31,7 @@ public: void set_current_unmodified(); bool is_current_modified() const; - Optional last_unmodified_timestamp() const { return m_last_unmodified_timestamp; } + Optional last_unmodified_timestamp() const { return m_last_unmodified_timestamp; } void clear(); @@ -44,7 +44,7 @@ private: Vector> m_stack; size_t m_stack_index { 0 }; Optional m_clean_index; - Optional m_last_unmodified_timestamp; + Optional m_last_unmodified_timestamp; }; } diff --git a/Userland/Libraries/LibJS/Contrib/Test262/AgentObject.cpp b/Userland/Libraries/LibJS/Contrib/Test262/AgentObject.cpp index 6d267280ce..a5904babea 100644 --- a/Userland/Libraries/LibJS/Contrib/Test262/AgentObject.cpp +++ b/Userland/Libraries/LibJS/Contrib/Test262/AgentObject.cpp @@ -33,8 +33,8 @@ JS::ThrowCompletionOr AgentObject::initialize(JS::Realm& realm) JS_DEFINE_NATIVE_FUNCTION(AgentObject::monotonic_now) { - auto time = Duration::now_monotonic(); - auto milliseconds = time.to_milliseconds(); + auto time = MonotonicTime::now(); + auto milliseconds = time.milliseconds(); return Value(static_cast(milliseconds)); } diff --git a/Userland/Libraries/LibVideo/PlaybackManager.cpp b/Userland/Libraries/LibVideo/PlaybackManager.cpp index b6738461d5..a636b654b2 100644 --- a/Userland/Libraries/LibVideo/PlaybackManager.cpp +++ b/Userland/Libraries/LibVideo/PlaybackManager.cpp @@ -67,7 +67,7 @@ DecoderErrorOr> PlaybackManager::create(NonnullOw }, "Media Decoder"sv)); - playback_manager->m_playback_handler = make(*playback_manager, false, Time::zero(), SeekMode::Fast); + playback_manager->m_playback_handler = make(*playback_manager, false, Duration::zero(), SeekMode::Fast); DECODER_TRY_ALLOC(playback_manager->m_playback_handler->on_enter()); playback_manager->m_decode_thread->start(); @@ -217,7 +217,7 @@ void PlaybackManager::restart_playback() void PlaybackManager::decode_and_queue_one_sample() { #if PLAYBACK_MANAGER_DEBUG - auto start_time = Duration::now_monotonic(); + auto start_time = MonotonicTime::now(); #endif FrameQueueItem item_to_enqueue; @@ -292,7 +292,7 @@ void PlaybackManager::decode_and_queue_one_sample() VERIFY(!item_to_enqueue.is_empty()); #if PLAYBACK_MANAGER_DEBUG - dbgln("Media Decoder: Sample at {}ms took {}ms to decode, queue contains ~{} items", item_to_enqueue.timestamp().to_milliseconds(), (Time::now_monotonic() - start_time).to_milliseconds(), m_frame_queue.weak_used()); + dbgln("Media Decoder: Sample at {}ms took {}ms to decode, queue contains ~{} items", item_to_enqueue.timestamp().to_milliseconds(), (MonotonicTime::now() - start_time).to_milliseconds(), m_frame_queue.weak_used()); #endif auto wait = [&] { @@ -408,7 +408,7 @@ public: private: ErrorOr on_enter() override { - m_last_present_in_real_time = Duration::now_monotonic(); + m_last_present_in_real_time = MonotonicTime::now(); return do_timed_state_update(); } @@ -429,7 +429,7 @@ private: Duration current_time() const override { - return manager().m_last_present_in_media_time + (Duration::now_monotonic() - m_last_present_in_real_time); + return manager().m_last_present_in_media_time + (MonotonicTime::now() - m_last_present_in_real_time); } ErrorOr do_timed_state_update() override @@ -498,7 +498,7 @@ private: // If we have a frame, send it for presentation. if (should_present_frame) { - auto now = Duration::now_monotonic(); + auto now = MonotonicTime::now(); manager().m_last_present_in_media_time += now - m_last_present_in_real_time; m_last_present_in_real_time = now; @@ -520,7 +520,7 @@ private: return {}; } - Duration m_last_present_in_real_time = Duration::zero(); + MonotonicTime m_last_present_in_real_time = MonotonicTime::now_coarse(); }; class PlaybackManager::PausedStateHandler : public PlaybackManager::PlaybackStateHandler { diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp index e09afa573d..0c4fe4483c 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp @@ -1585,7 +1585,7 @@ WebIDL::ExceptionOr HTMLMediaElement::dispatch_time_update_event() ScopeGuard guard { [this] { m_running_time_update_event_handler = false; } }; m_running_time_update_event_handler = true; - m_last_time_update_event_time = Duration::now_monotonic(); + m_last_time_update_event_time = MonotonicTime::now(); dispatch_event(TRY(DOM::Event::create(realm(), HTML::EventNames::timeupdate))); return {}; @@ -1617,7 +1617,7 @@ void HTMLMediaElement::time_marches_on(TimeMarchesOnReason reason) auto dispatch_event = true; if (m_last_time_update_event_time.has_value()) { - auto time_since_last_event = Duration::now_monotonic() - *m_last_time_update_event_time; + auto time_since_last_event = MonotonicTime::now() - *m_last_time_update_event_time; dispatch_event = time_since_last_event.to_milliseconds() > 250; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h index da57cf2216..5484d484ec 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h @@ -211,7 +211,7 @@ private: Optional m_delaying_the_load_event; bool m_running_time_update_event_handler { false }; - Optional m_last_time_update_event_time; + Optional m_last_time_update_event_time; JS::GCPtr m_document_observer; diff --git a/Userland/Libraries/LibWeb/HighResolutionTime/Performance.cpp b/Userland/Libraries/LibWeb/HighResolutionTime/Performance.cpp index 7ad1f94762..d6768d00b8 100644 --- a/Userland/Libraries/LibWeb/HighResolutionTime/Performance.cpp +++ b/Userland/Libraries/LibWeb/HighResolutionTime/Performance.cpp @@ -51,7 +51,7 @@ JS::GCPtr Performance::timing() double Performance::time_origin() const { - return static_cast(m_timer.origin_time().to_milliseconds()); + return static_cast(m_timer.origin_time().milliseconds()); } // https://w3c.github.io/user-timing/#mark-method diff --git a/Userland/Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp b/Userland/Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp index fe32cde660..de1f1bcdea 100644 --- a/Userland/Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp +++ b/Userland/Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp @@ -55,7 +55,7 @@ DOMHighResTimeStamp coarsened_shared_current_time(bool cross_origin_isolated_cap DOMHighResTimeStamp unsafe_shared_current_time() { // The unsafe shared current time must return the current value of the shared monotonic clock. - return Duration::now_monotonic().to_nanoseconds() / 1e6; + return MonotonicTime::now().truncated_seconds(); } } diff --git a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp index 9abb42dc59..cafdded844 100644 --- a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp @@ -329,7 +329,7 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt auto* window = page.top_level_browsing_context().active_window(); auto& realm = window->realm(); auto& vm = window->vm(); - auto start = Duration::now_monotonic(); + auto start = MonotonicTime::now(); // 4. Let promise be a new Promise. auto promise = JS::Promise::create(realm); @@ -383,7 +383,7 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt vm.custom_data()->spin_event_loop_until([&] { if (script_promise.state() != JS::Promise::State::Pending) return true; - if (timeout.has_value() && (Duration::now_monotonic() - start) > Duration::from_seconds(static_cast(*timeout))) + if (timeout.has_value() && (MonotonicTime::now() - start) > Duration::from_seconds(static_cast(*timeout))) return true; return false; }); diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index 0121ac5f96..ec7db5c925 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -1958,7 +1958,7 @@ Gfx::IntRect WebDriverConnection::iconify_the_window() ErrorOr WebDriverConnection::find(StartNodeGetter&& start_node_getter, Web::WebDriver::LocationStrategy using_, StringView value) { // 1. Let end time be the current time plus the session implicit wait timeout. - auto end_time = Duration::now_monotonic() + Duration::from_milliseconds(static_cast(m_timeouts_configuration.implicit_wait_timeout)); + auto end_time = MonotonicTime::now() + Duration::from_milliseconds(static_cast(m_timeouts_configuration.implicit_wait_timeout)); // 2. Let location strategy be equal to using. auto location_strategy = using_; @@ -1985,7 +1985,7 @@ ErrorOr WebDriverConnection::find(StartNodeGet return true; // 6. If elements returned is empty and the current time is less than end time return to step 4. Otherwise, continue to the next step. - return maybe_elements.value()->length() != 0 || Duration::now_monotonic() >= end_time; + return maybe_elements.value()->length() != 0 || MonotonicTime::now() >= end_time; }); auto elements = TRY(maybe_elements);