Branch data Line data Source code
1 : : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2021 Evan Welsh <contact@evanwelsh.com>
4 : :
5 : : #include <config.h>
6 : :
7 : : #include <glib.h>
8 : :
9 : : #include "gjs/auto.h"
10 : : #include "gjs/context-private.h"
11 : : #include "gjs/mainloop.h"
12 : :
13 : : namespace Gjs {
14 : :
15 : 603 : bool MainLoop::spin(GjsContextPrivate* gjs) {
16 [ + + ]: 603 : if (m_exiting)
17 : 1 : return false;
18 : :
19 : : // Check if System.exit() has been called.
20 [ + + ]: 602 : if (gjs->should_exit(nullptr)) {
21 : : // Return false to indicate the loop is exiting due to an exit call,
22 : : // the queue is likely not empty
23 : 3 : debug("Not spinning loop because System.exit called");
24 : 3 : exit();
25 : 3 : return false;
26 : : }
27 : :
28 : 599 : Gjs::AutoPointer<GMainContext, GMainContext, g_main_context_unref>
29 : 599 : main_context{g_main_context_ref_thread_default()};
30 : :
31 : 599 : debug("Spinning loop until released or hook cleared");
32 : : do {
33 : 852 : bool blocking = can_block();
34 : :
35 : : // Only run the loop if there are pending jobs.
36 [ + + ]: 852 : if (g_main_context_pending(main_context))
37 : 458 : g_main_context_iteration(main_context, blocking);
38 : :
39 : : // If System.exit() has not been called
40 [ + + ]: 852 : if (gjs->should_exit(nullptr)) {
41 : 3 : debug("Stopped spinning loop because System.exit called");
42 : 3 : exit();
43 : 3 : return false;
44 : : }
45 : : } while (
46 : : // and there is not a pending main loop hook
47 [ + + + + : 2290 : !gjs->has_main_loop_hook() &&
+ + ]
48 : : // and there are pending sources or the job queue is not empty
49 : : // continue spinning the event loop.
50 [ - + ]: 1441 : (can_block() || !gjs->empty()));
51 : :
52 : 596 : return true;
53 : 599 : }
54 : :
55 : : }; // namespace Gjs
|