Skip to content

Commit

Permalink
LibCore: Stop obsessing about tiny OOMs in Core::Timer
Browse files Browse the repository at this point in the history
Work towards SerenityOS#20405
  • Loading branch information
awesomekling committed Apr 17, 2024
1 parent bf1c827 commit 1cb5385
Show file tree
Hide file tree
Showing 52 changed files with 111 additions and 113 deletions.
4 changes: 2 additions & 2 deletions Ladybird/AppKit/UI/TaskManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ - (instancetype)init

__weak TaskManager* weak_self = self;

m_update_timer = MUST(Core::Timer::create_repeating(1000, [weak_self] {
m_update_timer = Core::Timer::create_repeating(1000, [weak_self] {
TaskManager* strong_self = weak_self;
if (strong_self == nil) {
return;
}

[strong_self updateStatistics];
}));
});

[self setContentView:scroll_view];
[self setTitle:@"Task Manager"];
Expand Down
4 changes: 2 additions & 2 deletions Tests/LibCore/TestLibCoreDeferredInvoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
TEST_CASE(deferred_invoke)
{
Core::EventLoop event_loop;
auto reaper = MUST(Core::Timer::create_single_shot(250, [] {
auto reaper = Core::Timer::create_single_shot(250, [] {
warnln("I waited for the deferred_invoke to happen, but it never did!");
VERIFY_NOT_REACHED();
}));
});

Core::deferred_invoke([&event_loop] {
event_loop.quit(0);
Expand Down
12 changes: 6 additions & 6 deletions Tests/LibCore/TestLibCoreFileWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ TEST_CASE(file_watcher_child_events)
event_count++;
};

auto timer1 = MUST(Core::Timer::create_single_shot(500, [&] {
auto timer1 = Core::Timer::create_single_shot(500, [&] {
int rc = creat("/tmp/testfile", 0777);
EXPECT_NE(rc, -1);
}));
});
timer1->start();

auto timer2 = MUST(Core::Timer::create_single_shot(1000, [&] {
auto timer2 = Core::Timer::create_single_shot(1000, [&] {
int rc = unlink("/tmp/testfile");
EXPECT_NE(rc, -1);
}));
});
timer2->start();

auto catchall_timer = MUST(Core::Timer::create_single_shot(2000, [&] {
auto catchall_timer = Core::Timer::create_single_shot(2000, [&] {
VERIFY_NOT_REACHED();
}));
});
catchall_timer->start();

event_loop.exec();
Expand Down
4 changes: 2 additions & 2 deletions Userland/Applications/Assistant/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
GUI::Application::the()->quit();
};

auto update_ui_timer = TRY(Core::Timer::create_single_shot(10, [&] {
auto update_ui_timer = Core::Timer::create_single_shot(10, [&] {
results_container.remove_all_children();
results_container.layout()->set_margins(app_state.visible_result_count ? GUI::Margins { 4, 0, 0, 0 } : GUI::Margins { 0 });

Expand All @@ -259,7 +259,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)

mark_selected_item();
Core::deferred_invoke([window] { window->resize(GUI::Desktop::the().rect().width() / 3, {}); });
}));
});

db.on_new_results = [&](auto results) {
if (results.is_empty()) {
Expand Down
4 changes: 2 additions & 2 deletions Userland/Applications/Browser/TaskManagerWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ TaskManagerWidget::~TaskManagerWidget() = default;

TaskManagerWidget::TaskManagerWidget()
{
m_update_timer = MUST(Core::Timer::create_repeating(1000, [this] {
m_update_timer = Core::Timer::create_repeating(1000, [this] {
this->update_statistics();
}));
});
m_update_timer->start();

m_web_view = add<WebView::OutOfProcessWebView>();
Expand Down
4 changes: 2 additions & 2 deletions Userland/Applications/ClockSettings/ClockSettingsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ ErrorOr<void> ClockSettingsWidget::setup()
set_modified(true);
};

m_clock_preview_update_timer = TRY(Core::Timer::create_repeating(1000, [&]() {
m_clock_preview_update_timer = Core::Timer::create_repeating(1000, [&]() {
update_clock_preview();
}));
});
m_clock_preview_update_timer->start();
update_clock_preview();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void MonitorSettingsWidget::apply_settings()
box->set_icon(window()->icon());

// If after 10 seconds the user doesn't close the message box, just close it.
auto revert_timer_or_error = Core::Timer::create_repeating(1000, [&] {
auto revert_timer = Core::Timer::create_repeating(1000, [&] {
seconds_until_revert -= 1;
current_box_text_or_error = box_text();
if (current_box_text_or_error.is_error()) {
Expand All @@ -285,11 +285,6 @@ void MonitorSettingsWidget::apply_settings()
box->close();
}
});
if (revert_timer_or_error.is_error()) {
GUI::MessageBox::show_error(window(), "Unable to apply changes"sv);
return;
}
auto revert_timer = revert_timer_or_error.release_value();
revert_timer->start();

// If the user selects "No", closes the window or the window gets closed by the 10 seconds timer, revert the changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ ErrorOr<void> HighlightPreviewWidget::reload_cursor()
m_cursor_params = Gfx::CursorParams::parse_from_filename(cursor_path, m_cursor_bitmap->rect().center()).constrained(*m_cursor_bitmap);
// Setup cursor animation:
if (m_cursor_params.frames() > 1 && m_cursor_params.frame_ms() > 0) {
m_frame_timer = TRY(Core::Timer::create_repeating(m_cursor_params.frame_ms(), [&] {
m_frame_timer = Core::Timer::create_repeating(m_cursor_params.frame_ms(), [&] {
m_cursor_frame = (m_cursor_frame + 1) % m_cursor_params.frames();
update();
}));
});
m_frame_timer->start();
} else {
m_frame_timer = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions Userland/Applications/Piano/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto wav_progress_window = ExportProgressWindow::construct(*window, wav_percent_written);
TRY(wav_progress_window->initialize());

auto main_widget_updater = TRY(Core::Timer::create_repeating(static_cast<int>((1 / 30.0) * 1000), [&] {
auto main_widget_updater = Core::Timer::create_repeating(static_cast<int>((1 / 30.0) * 1000), [&] {
if (window->is_active())
Core::EventLoop::current().post_event(main_widget, make<Core::CustomEvent>(0));
}));
});
main_widget_updater->start();

auto file_menu = window->add_menu("&File"_string);
Expand Down
2 changes: 1 addition & 1 deletion Userland/Applications/PixelPaint/Filters/Filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Filter::Filter(ImageEditor* editor)
, m_update_timer(Core::Timer::create_single_shot(100, [&] {
if (on_settings_change)
on_settings_change();
}).release_value_but_fixme_should_propagate_errors())
}))
{
m_update_timer->set_active(false);
}
Expand Down
2 changes: 1 addition & 1 deletion Userland/Applications/PixelPaint/ImageEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ImageEditor::ImageEditor(NonnullRefPtr<Image> image)
m_marching_ants_offset %= (marching_ant_length * 2);
if (!m_image->selection().is_empty() || m_image->selection().in_interactive_selection())
update();
}).release_value_but_fixme_should_propagate_errors();
});
m_marching_ants_timer->start();
}

Expand Down
2 changes: 1 addition & 1 deletion Userland/Applications/PixelPaint/Tools/SprayTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SprayTool::SprayTool()
{
m_timer = Core::Timer::create_repeating(200, [&]() {
paint_it();
}).release_value_but_fixme_should_propagate_errors();
});
}

static double nrand()
Expand Down
2 changes: 1 addition & 1 deletion Userland/Applications/PixelPaint/Tools/TextTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ TextTool::TextTool()
m_text_editor->set_font(m_selected_font);
m_cursor_blink_timer = Core::Timer::create_repeating(500, [&]() {
m_cursor_blink_state = !m_cursor_blink_state;
}).release_value_but_fixme_should_propagate_errors();
});
}

void TextTool::on_primary_color_change(Color color)
Expand Down
2 changes: 1 addition & 1 deletion Userland/Applications/SoundPlayer/PlaybackManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ PlaybackManager::PlaybackManager(NonnullRefPtr<Audio::ConnectionToServer> connec
if (!m_loader)
return;
next_buffer();
}).release_value_but_fixme_should_propagate_errors();
});
}

void PlaybackManager::set_loader(NonnullRefPtr<Audio::Loader>&& loader)
Expand Down
4 changes: 2 additions & 2 deletions Userland/Applications/Terminal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::unveil("/tmp/session/%sid/portal/config", "rw"));
TRY(Core::System::unveil(nullptr, nullptr));

auto modified_state_check_timer = TRY(Core::Timer::create_repeating(500, [&] {
auto modified_state_check_timer = Core::Timer::create_repeating(500, [&] {
window->set_modified(tty_has_foreground_process() || shell_child_process_count() > 0);
}));
});

listener.on_confirm_close_changed = [&](bool confirm_close) {
if (confirm_close) {
Expand Down
4 changes: 2 additions & 2 deletions Userland/Demos/CatDog/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto advice_widget = advice_window->set_main_widget<SpeechBubble>(catdog_widget);
advice_widget->set_layout<GUI::VerticalBoxLayout>(GUI::Margins {}, 0);

auto advice_timer = TRY(Core::Timer::create_single_shot(15'000, [&] {
auto advice_timer = Core::Timer::create_single_shot(15'000, [&] {
window->move_to_front();
advice_window->move_to_front();
catdog_widget->set_roaming(false);
advice_window->move_to(window->x() - advice_window->width() / 2, window->y() - advice_window->height());
advice_window->show();
advice_window->set_always_on_top();
}));
});
advice_timer->start();

advice_widget->on_dismiss = [&] {
Expand Down
3 changes: 1 addition & 2 deletions Userland/Demos/WidgetGallery/DemoWizardDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ DemoWizardDialog::DemoWizardDialog(GUI::Window* parent_window)
replace_page(*m_back_page);
}
},
this)
.release_value_but_fixme_should_propagate_errors();
this);
m_page_2->on_page_enter = [&]() {
m_page_2_progress_value = 0;
m_page_2_timer->restart();
Expand Down
2 changes: 1 addition & 1 deletion Userland/DevTools/HackStudio/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ void Editor::create_tokens_info_timer()
m_tokens_info_timer = Core::Timer::create_repeating((int)token_info_timer_interval_ms, [this] {
on_token_info_timer_tick();
m_tokens_info_timer->stop();
}).release_value_but_fixme_should_propagate_errors();
});
m_tokens_info_timer->start();
}

Expand Down
2 changes: 1 addition & 1 deletion Userland/DevTools/Profiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ static bool prompt_to_stop_profiling(pid_t pid, ByteString const& process_name)
clock.start();
auto update_timer = Core::Timer::create_repeating(100, [&] {
timer_label.set_text(String::formatted("{:.1} seconds", static_cast<float>(clock.elapsed()) / 1000.0f).release_value_but_fixme_should_propagate_errors());
}).release_value_but_fixme_should_propagate_errors();
});
update_timer->start();

auto& stop_button = widget->add<GUI::Button>("Stop"_string);
Expand Down
6 changes: 3 additions & 3 deletions Userland/Games/Hearts/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Game::Game()
m_delay_timer = Core::Timer::create_single_shot(0, [this] {
dbgln_if(HEARTS_DEBUG, "Continuing game after delay...");
advance_game();
}).release_value_but_fixme_should_propagate_errors();
});

constexpr int card_overlap = 20;
constexpr int outer_border_size = 15;
Expand Down Expand Up @@ -151,7 +151,7 @@ void Game::show_score_card(bool game_over)
if (!m_players[0].is_human) {
close_timer = Core::Timer::create_single_shot(2000, [&] {
score_dialog->close();
}).release_value_but_fixme_should_propagate_errors();
});
close_timer->start();
}

Expand Down Expand Up @@ -232,7 +232,7 @@ void Game::start_animation(Vector<NonnullRefPtr<Card>> cards, Gfx::IntPoint end,
m_animation_delay_timer = Core::Timer::create_single_shot(initial_delay_ms, [&] {
m_animation_playing = true;
start_timer(10);
}).release_value_but_fixme_should_propagate_errors();
});
m_animation_delay_timer->start();
}

Expand Down
2 changes: 1 addition & 1 deletion Userland/Games/MasterWord/WordGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace MasterWord {

WordGame::WordGame()
: m_clear_message_timer(Core::Timer::create_single_shot(5000, [this] { clear_message(); }).release_value_but_fixme_should_propagate_errors())
: m_clear_message_timer(Core::Timer::create_single_shot(5000, [this] { clear_message(); }))
{
read_words();
m_num_letters = Config::read_i32("MasterWord"sv, ""sv, "word_length"sv, 5);
Expand Down
3 changes: 1 addition & 2 deletions Userland/Games/Minesweeper/Field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ void Field::initialize()
++m_time_elapsed;
m_time_label.set_text(human_readable_digital_time(m_time_elapsed));
},
this)
.release_value_but_fixme_should_propagate_errors();
this);

// Square with mine will be filled with background color later, i.e. red
m_mine_palette.set_color(Gfx::ColorRole::Base, Color::from_rgb(0xff4040));
Expand Down
4 changes: 2 additions & 2 deletions Userland/Games/Solitaire/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)

uint64_t seconds_elapsed = 0;

auto timer = TRY(Core::Timer::create_repeating(1000, [&]() {
auto timer = Core::Timer::create_repeating(1000, [&]() {
++seconds_elapsed;
statusbar.set_text(2, String::formatted("Time: {}", human_readable_digital_time(seconds_elapsed)).release_value_but_fixme_should_propagate_errors());
}));
});

game.on_game_start = [&]() {
seconds_elapsed = 0;
Expand Down
4 changes: 2 additions & 2 deletions Userland/Games/Spider/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)

uint64_t seconds_elapsed = 0;

auto timer = TRY(Core::Timer::create_repeating(1000, [&]() {
auto timer = Core::Timer::create_repeating(1000, [&]() {
++seconds_elapsed;

statusbar.set_text(2, String::formatted("Time: {}", human_readable_digital_time(seconds_elapsed)).release_value_but_fixme_should_propagate_errors());
}));
});

game.on_game_start = [&]() {
seconds_elapsed = 0;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibAudio/PlaybackStreamSerenity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ErrorOr<NonnullRefPtr<PlaybackStream>> PlaybackStreamSerenity::create(OutputStat
if (auto result = connection->try_set_self_sample_rate(sample_rate); result.is_error())
return Error::from_string_literal("Failed to set sample rate");

auto polling_timer = TRY(Core::Timer::try_create());
auto polling_timer = Core::Timer::create();
auto implementation = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) PlaybackStreamSerenity(connection, move(polling_timer), move(data_request_callback))));
if (initial_state == OutputState::Playing)
connection->async_start_playback();
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibCore/Debounce.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ auto debounce(int timeout, TFunction function)
timer->stop();
timer->on_timeout = move(apply_function);
} else {
timer = Core::Timer::create_single_shot(timeout, move(apply_function)).release_value_but_fixme_should_propagate_errors();
timer = Core::Timer::create_single_shot(timeout, move(apply_function));
}
timer->start();
};
Expand Down
19 changes: 19 additions & 0 deletions Userland/Libraries/LibCore/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@

namespace Core {

NonnullRefPtr<Timer> Timer::create()
{
return adopt_ref(*new Timer);
}

NonnullRefPtr<Timer> Timer::create_repeating(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent)
{
return adopt_ref(*new Timer(interval_ms, move(timeout_handler), parent));
}

NonnullRefPtr<Timer> Timer::create_single_shot(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent)
{
auto timer = adopt_ref(*new Timer(interval_ms, move(timeout_handler), parent));
timer->set_single_shot(true);
return timer;
}

Timer::~Timer() = default;

Timer::Timer(EventReceiver* parent)
: EventReceiver(parent)
{
Expand Down
17 changes: 5 additions & 12 deletions Userland/Libraries/LibCore/Timer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2018-2024, Andreas Kling <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
Expand All @@ -16,18 +16,11 @@ class Timer final : public EventReceiver {
C_OBJECT(Timer);

public:
static ErrorOr<NonnullRefPtr<Timer>> create_repeating(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent = nullptr)
{
return adopt_nonnull_ref_or_enomem(new Timer(interval_ms, move(timeout_handler), parent));
}
static ErrorOr<NonnullRefPtr<Timer>> create_single_shot(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent = nullptr)
{
auto timer = TRY(adopt_nonnull_ref_or_enomem(new Timer(interval_ms, move(timeout_handler), parent)));
timer->set_single_shot(true);
return timer;
}
static NonnullRefPtr<Timer> create();
static NonnullRefPtr<Timer> create_repeating(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent = nullptr);
static NonnullRefPtr<Timer> create_single_shot(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent = nullptr);

virtual ~Timer() override = default;
virtual ~Timer() override;

void start();
void start(int interval_ms);
Expand Down
Loading

0 comments on commit 1cb5385

Please sign in to comment.