| 13 | namespace Gjs { |
| 14 | |
| 15 | bool MainLoop::spin(GjsContextPrivate* gjs) { |
| 16 | if (m_exiting) |
| 17 | return false; |
| 18 | |
| 19 | // Check if System.exit() has been called. |
| 20 | 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 | debug("Not spinning loop because System.exit called"); |
| 24 | exit(); |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | Gjs::AutoPointer<GMainContext, GMainContext, g_main_context_unref> |
| 29 | main_context{g_main_context_ref_thread_default()}; |
| 30 | |
| 31 | debug("Spinning loop until released or hook cleared"); |
| 32 | do { |
| 33 | bool blocking = can_block(); |
| 34 | |
| 35 | // Only run the loop if there are pending jobs. |
| 36 | if (g_main_context_pending(main_context)) |
| 37 | g_main_context_iteration(main_context, blocking); |
| 38 | |
| 39 | // If System.exit() has not been called |
| 40 | if (gjs->should_exit(nullptr)) { |
| 41 | debug("Stopped spinning loop because System.exit called"); |
| 42 | exit(); |
| 43 | return false; |
| 44 | } |
| 45 | } while ( |
| 46 | // and there is not a pending main loop hook |
| 47 | !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 | (can_block() || !gjs->empty())); |
| 51 | |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | }; // namespace Gjs |
no test coverage detected