summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorJelle Raaijmakers <jelle@gmta.nl>2022-04-24 01:48:11 +0200
committerLinus Groh <mail@linusgroh.de>2022-04-27 11:54:37 +0200
commitf25123df6633803e9a69b912b12aa302150a979b (patch)
treecacac39ad7b3d305c1f256664ca4f3329851801a /Userland
parent9e2a619fdcf7e232c1fd1d6ade9bbe88b810c0f8 (diff)
downloadserenity-f25123df6633803e9a69b912b12aa302150a979b.zip
LibCore: Remove main event loop
The main event loop functionality was used in just two places where the alternative is a bit simpler. Remove it in favor of referencing the event loop directly, or just invoking `EventLoop::current()`. Note that we don't need locking in the constructor since we're now only modifying a thread-local `Vector`. We also don't need locking in the old call sites to `::with_main_locked()` since we already lock the event loop in the subsequent `::post_event()` invocation.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Applications/CrashReporter/main.cpp10
-rw-r--r--Userland/Applications/SystemMonitor/ThreadStackWidget.cpp2
-rw-r--r--Userland/Libraries/LibCore/EventLoop.cpp47
-rw-r--r--Userland/Libraries/LibCore/EventLoop.h10
4 files changed, 22 insertions, 47 deletions
diff --git a/Userland/Applications/CrashReporter/main.cpp b/Userland/Applications/CrashReporter/main.cpp
index e7993e7874..9a956b3970 100644
--- a/Userland/Applications/CrashReporter/main.cpp
+++ b/Userland/Applications/CrashReporter/main.cpp
@@ -298,12 +298,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
size_t thread_index = 0;
coredump->for_each_thread_info([&](auto& thread_info) {
results.thread_backtraces.append(build_backtrace(*coredump, thread_info, thread_index, [&](size_t frame_index, size_t frame_count) {
- Core::EventLoop::with_main_locked([&](auto& main) {
- main->deferred_invoke([&, frame_index, frame_count] {
- window->set_progress(100.0f * (float)(frame_index + 1) / (float)frame_count);
- progressbar.set_value(frame_index + 1);
- progressbar.set_max(frame_count);
- });
+ app->event_loop().deferred_invoke([&, frame_index, frame_count] {
+ window->set_progress(100.0f * (float)(frame_index + 1) / (float)frame_count);
+ progressbar.set_value(frame_index + 1);
+ progressbar.set_max(frame_count);
});
}));
results.thread_cpu_registers.append(build_cpu_registers(thread_info, thread_index));
diff --git a/Userland/Applications/SystemMonitor/ThreadStackWidget.cpp b/Userland/Applications/SystemMonitor/ThreadStackWidget.cpp
index 998cb50868..3c025499aa 100644
--- a/Userland/Applications/SystemMonitor/ThreadStackWidget.cpp
+++ b/Userland/Applications/SystemMonitor/ThreadStackWidget.cpp
@@ -123,7 +123,7 @@ void ThreadStackWidget::refresh()
[weak_this = make_weak_ptr()](auto result) {
if (!weak_this)
return;
- Core::EventLoop::with_main_locked([&](auto* main_event_loop) { main_event_loop->post_event(const_cast<Core::Object&>(*weak_this), make<CompletionEvent>(move(result))); });
+ Core::EventLoop::current().post_event(const_cast<Core::Object&>(*weak_this), make<CompletionEvent>(move(result)));
});
}
diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp
index 99cfe1a144..e9e91cce97 100644
--- a/Userland/Libraries/LibCore/EventLoop.cpp
+++ b/Userland/Libraries/LibCore/EventLoop.cpp
@@ -61,8 +61,6 @@ struct EventLoop::Private {
Threading::Mutex lock;
};
-// The main event loop is global to the program, so it may be accessed from multiple threads.
-Threading::MutexProtected<EventLoop*> s_main_event_loop;
static Threading::MutexProtected<NeverDestroyed<IDAllocator>> s_id_allocator;
static Threading::MutexProtected<RefPtr<InspectorServerConnection>> s_inspector_server_connection;
@@ -323,27 +321,25 @@ EventLoop::EventLoop([[maybe_unused]] MakeInspectable make_inspectable)
s_timers = new HashMap<int, NonnullOwnPtr<EventLoopTimer>>;
s_notifiers = new HashTable<Notifier*>;
}
- s_main_event_loop.with_locked([&, this](auto*& main_event_loop) {
- if (main_event_loop == nullptr) {
- main_event_loop = this;
- s_pid = getpid();
- s_event_loop_stack->append(*this);
+
+ if (s_event_loop_stack->is_empty()) {
+ s_pid = getpid();
+ s_event_loop_stack->append(*this);
#ifdef __serenity__
- if (getuid() != 0) {
- if (getenv("MAKE_INSPECTABLE") == "1"sv)
- make_inspectable = Core::EventLoop::MakeInspectable::Yes;
-
- if (make_inspectable == MakeInspectable::Yes
- // FIXME: Deadlock potential; though the main loop and inspector server connection are rarely used in conjunction
- && !s_inspector_server_connection.with_locked([](auto inspector_server_connection) { return inspector_server_connection; })) {
- if (!connect_to_inspector_server())
- dbgln("Core::EventLoop: Failed to connect to InspectorServer");
- }
+ if (getuid() != 0) {
+ if (getenv("MAKE_INSPECTABLE") == "1"sv)
+ make_inspectable = Core::EventLoop::MakeInspectable::Yes;
+
+ if (make_inspectable == MakeInspectable::Yes
+ // FIXME: Deadlock potential; though the main loop and inspector server connection are rarely used in conjunction
+ && !s_inspector_server_connection.with_locked([](auto inspector_server_connection) { return inspector_server_connection; })) {
+ if (!connect_to_inspector_server())
+ dbgln("Core::EventLoop: Failed to connect to InspectorServer");
}
-#endif
}
- });
+#endif
+ }
initialize_wake_pipes();
@@ -352,13 +348,8 @@ EventLoop::EventLoop([[maybe_unused]] MakeInspectable make_inspectable)
EventLoop::~EventLoop()
{
- // NOTE: Pop the main event loop off of the stack when destroyed.
- s_main_event_loop.with_locked([this](auto*& main_event_loop) {
- if (this == main_event_loop) {
- s_event_loop_stack->take_last();
- main_event_loop = nullptr;
- }
- });
+ if (!s_event_loop_stack->is_empty() && &s_event_loop_stack->last() == this)
+ s_event_loop_stack->take_last();
}
bool connect_to_inspector_server()
@@ -639,7 +630,6 @@ void EventLoop::notify_forked(ForkEvent event)
VERIFY_EVENT_LOOP_INITIALIZED();
switch (event) {
case ForkEvent::Child:
- s_main_event_loop.with_locked([]([[maybe_unused]] auto*& main_event_loop) { main_event_loop = nullptr; });
s_event_loop_stack->clear();
s_timers->clear();
s_notifiers->clear();
@@ -650,9 +640,6 @@ void EventLoop::notify_forked(ForkEvent event)
info->next_signal_id = 0;
}
s_pid = 0;
-#ifdef __serenity__
- s_main_event_loop.with_locked([]([[maybe_unused]] auto*& main_event_loop) { main_event_loop = nullptr; });
-#endif
return;
}
diff --git a/Userland/Libraries/LibCore/EventLoop.h b/Userland/Libraries/LibCore/EventLoop.h
index 64350b99d8..0599944489 100644
--- a/Userland/Libraries/LibCore/EventLoop.h
+++ b/Userland/Libraries/LibCore/EventLoop.h
@@ -26,8 +26,6 @@
namespace Core {
-extern Threading::MutexProtected<EventLoop*> s_main_event_loop;
-
class EventLoop {
public:
enum class MakeInspectable {
@@ -59,14 +57,6 @@ public:
void post_event(Object& receiver, NonnullOwnPtr<Event>&&, ShouldWake = ShouldWake::No);
- template<typename Callback>
- static decltype(auto) with_main_locked(Callback callback)
- {
- return s_main_event_loop.with_locked([&callback](auto*& event_loop) {
- VERIFY(event_loop != nullptr);
- return callback(event_loop);
- });
- }
static EventLoop& current();
bool was_exit_requested() const { return m_exit_requested; }