[gjs/ewlsh/implicit-mainloop] Use main_context directly instead of mainloop.



commit 9a359489f14089571d534a2133f7b4ee5ee1ca73
Author: Evan Welsh <contact evanwelsh com>
Date:   Thu Apr 1 00:32:50 2021 -0700

    Use main_context directly instead of mainloop.

 gjs/context-private.h |  2 --
 gjs/context.cpp       |  5 ++---
 gjs/mainloop.cpp      | 27 +++++++++++++++++----------
 3 files changed, 19 insertions(+), 15 deletions(-)
---
diff --git a/gjs/context-private.h b/gjs/context-private.h
index d0ec91fb..e49c1ad2 100644
--- a/gjs/context-private.h
+++ b/gjs/context-private.h
@@ -89,7 +89,6 @@ class GjsContextPrivate : public JS::JobQueue {
 
     JobQueueStorage m_job_queue;
     GSource* m_promise_queue_source;
-    GMainLoop* m_event_loop;
 
     std::unordered_map<uint64_t, GjsAutoChar> m_unhandled_rejection_stacks;
 
@@ -174,7 +173,6 @@ class GjsContextPrivate : public JS::JobQueue {
     [[nodiscard]] GjsContext* public_context() const {
         return m_public_context;
     }
-    [[nodiscard]] GMainLoop* loop() { return m_event_loop; }
     [[nodiscard]] JSContext* context() const { return m_cx; }
     [[nodiscard]] JSObject* global() const { return m_global.get(); }
     [[nodiscard]] JSObject* internal_global() const {
diff --git a/gjs/context.cpp b/gjs/context.cpp
index e5e28f15..9890d2dc 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -431,7 +431,6 @@ void GjsContextPrivate::dispose(void) {
 }
 
 GjsContextPrivate::~GjsContextPrivate(void) {
-    g_clear_pointer(&m_event_loop, g_main_loop_unref);
     g_clear_pointer(&m_search_path, g_strfreev);
     g_clear_pointer(&m_program_path, g_free);
     g_clear_pointer(&m_program_name, g_free);
@@ -480,7 +479,6 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
       m_cx(cx),
       m_environment_preparer(cx) {
     m_owner_thread = g_thread_self();
-    m_event_loop = g_main_loop_new(nullptr, false);
 
     const char *env_profiler = g_getenv("GJS_ENABLE_PROFILER");
     if (env_profiler || m_should_listen_sigusr2)
@@ -761,7 +759,8 @@ void GjsContextPrivate::start_draining_job_queue(void) {
     if (!m_promise_queue_source) {
         gjs_debug(GJS_DEBUG_CONTEXT, "Starting promise job queue handler");
 
-        m_promise_queue_source = gjs_promise_queue_source_new(this, nullptr);
+        m_promise_queue_source =
+            gjs_promise_job_queue_source_new(this, nullptr);
         g_source_attach(m_promise_queue_source, nullptr);
     }
 
diff --git a/gjs/mainloop.cpp b/gjs/mainloop.cpp
index 50f59a9a..55cd22f2 100644
--- a/gjs/mainloop.cpp
+++ b/gjs/mainloop.cpp
@@ -11,9 +11,11 @@
 void gjs_spin_event_loop(JSContext* cx) {
     auto priv = GjsContextPrivate::from_cx(cx);
 
+    // Check if System.exit() has been called.
     if (priv->should_exit(nullptr))
         return;
 
+    // Whether there are still sources pending.
     bool has_pending;
 
     do {
@@ -22,18 +24,23 @@ void gjs_spin_event_loop(JSContext* cx) {
 
         has_pending = g_main_context_pending(nullptr);
 
-        if (!priv->empty())
-            g_main_loop_run(priv->loop());
+        // Only run the loop if there are pending jobs.
+        if (has_pending) {
+            // If a source was run, we'll iterate again regardless.
+            has_pending = g_main_context_iteration(nullptr, true);
+        }
 
+        // Check if System.exit() has been called.
         if (priv->should_exit(nullptr))
             break;
 
-        has_pending = g_main_context_pending(nullptr);
-
-        if (has_pending && !priv->should_exit(nullptr))
-            continue;
-
-        has_pending = g_main_context_pending(nullptr);
-    } while (has_pending == true && !priv->empty() &&
-             !priv->should_exit(nullptr));
+    } while (
+        // If there are pending sources
+        has_pending &&
+        // and the job queue is not empty
+        !priv->empty() &&
+        // and System.exit() has not been called
+        !priv->should_exit(nullptr)
+        // continue spinning the event loop.
+    );
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]