1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 03:37:35 +00:00

Everywhere: Use MonotonicTime instead of Duration

This is easily identifiable by anyone who uses Duration::now_monotonic,
and any downstream users of that data.
This commit is contained in:
kleines Filmröllchen 2023-03-17 19:50:39 +01:00 committed by Jelle Raaijmakers
parent b2e7b8cdff
commit fc5cab5c21
29 changed files with 79 additions and 80 deletions

View file

@ -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;
}

View file

@ -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 };
};

View file

@ -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<Object> 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<Duration> EventLoopManagerUnix::get_next_timer_expiration()
Optional<MonotonicTime> EventLoopManagerUnix::get_next_timer_expiration()
{
auto now = Duration::now_monotonic_coarse();
Optional<Duration> soonest {};
auto now = MonotonicTime::now_coarse();
Optional<MonotonicTime> 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<EventLoopTimer>();
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();

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Time.h>
#include <LibCore/EventLoopImplementation.h>
namespace Core {
@ -28,7 +29,7 @@ public:
virtual void unregister_signal(int handler_id) override;
void wait_for_events(EventLoopImplementation::PumpMode);
static Optional<Duration> get_next_timer_expiration();
static Optional<MonotonicTime> get_next_timer_expiration();
private:
void dispatch_signal(int signal_number);

View file

@ -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;

View file

@ -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<Gfx::IntPoint> m_mouse_origin;
AK::Duration m_start_time;
MonotonicTime m_start_time;
};
}

View file

@ -63,12 +63,12 @@ ErrorOr<Dialog::ExecResult> 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<Duration> last_unmodified_timestamp)
Dialog::ExecResult MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<MonotonicTime> 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<Dialog::ExecResult> MessageBox::try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Duration> last_unmodified_timestamp)
ErrorOr<Dialog::ExecResult> MessageBox::try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<MonotonicTime> last_unmodified_timestamp)
{
StringBuilder builder;
TRY(builder.try_append("Save changes to "sv));
@ -79,7 +79,7 @@ ErrorOr<Dialog::ExecResult> 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));
}

View file

@ -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<Duration> last_unmodified_timestamp = {});
static ExecResult ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<MonotonicTime> last_unmodified_timestamp = {});
static ErrorOr<ExecResult> try_show(Badge<FileSystemAccessServer::ConnectionFromClient>, i32 window_server_client_id, i32 parent_window_id, StringView text, StringView title);
static ErrorOr<ExecResult> try_show(Window* parent_window, StringView text, StringView title, Type type = Type::None, InputType input_type = InputType::OK);
static ErrorOr<ExecResult> try_show_error(Window* parent_window, StringView text);
static ErrorOr<ExecResult> try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Duration> last_unmodified_timestamp = {});
static ErrorOr<ExecResult> try_ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<MonotonicTime> last_unmodified_timestamp = {});
static ErrorOr<NonnullRefPtr<MessageBox>> create(Window* parent_window, StringView text, StringView title, Type type = Type::None, InputType input_type = InputType::OK);

View file

@ -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;
}

View file

@ -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 };
};

View file

@ -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();

View file

@ -31,7 +31,7 @@ public:
void set_current_unmodified();
bool is_current_modified() const;
Optional<Duration> last_unmodified_timestamp() const { return m_last_unmodified_timestamp; }
Optional<MonotonicTime> last_unmodified_timestamp() const { return m_last_unmodified_timestamp; }
void clear();
@ -44,7 +44,7 @@ private:
Vector<NonnullOwnPtr<Command>> m_stack;
size_t m_stack_index { 0 };
Optional<size_t> m_clean_index;
Optional<Duration> m_last_unmodified_timestamp;
Optional<MonotonicTime> m_last_unmodified_timestamp;
};
}

View file

@ -33,8 +33,8 @@ JS::ThrowCompletionOr<void> 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<double>(milliseconds));
}

View file

@ -67,7 +67,7 @@ DecoderErrorOr<NonnullOwnPtr<PlaybackManager>> PlaybackManager::create(NonnullOw
},
"Media Decoder"sv));
playback_manager->m_playback_handler = make<SeekingStateHandler>(*playback_manager, false, Time::zero(), SeekMode::Fast);
playback_manager->m_playback_handler = make<SeekingStateHandler>(*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<void> 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<void> 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 {

View file

@ -1585,7 +1585,7 @@ WebIDL::ExceptionOr<void> 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;
}

View file

@ -211,7 +211,7 @@ private:
Optional<DOM::DocumentLoadEventDelayer> m_delaying_the_load_event;
bool m_running_time_update_event_handler { false };
Optional<Duration> m_last_time_update_event_time;
Optional<MonotonicTime> m_last_time_update_event_time;
JS::GCPtr<DOM::DocumentObserver> m_document_observer;

View file

@ -51,7 +51,7 @@ JS::GCPtr<NavigationTiming::PerformanceTiming> Performance::timing()
double Performance::time_origin() const
{
return static_cast<double>(m_timer.origin_time().to_milliseconds());
return static_cast<double>(m_timer.origin_time().milliseconds());
}
// https://w3c.github.io/user-timing/#mark-method

View file

@ -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();
}
}

View file

@ -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<i64>(*timeout)))
if (timeout.has_value() && (MonotonicTime::now() - start) > Duration::from_seconds(static_cast<i64>(*timeout)))
return true;
return false;
});