LCOV - code coverage report
Current view: top level - gjs - mainloop.h (source / functions) Coverage Total Hit
Test: gjs- Code Coverage Lines: 92.6 % 27 25
Test Date: 2024-04-16 04:37:39 Functions: 100.0 % 7 7
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 66.7 % 6 4

             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                 :             : #pragma once
       6                 :             : 
       7                 :             : #include <config.h>
       8                 :             : 
       9                 :             : #include <glib.h>
      10                 :             : 
      11                 :             : #include "util/log.h"
      12                 :             : 
      13                 :             : class GjsContextPrivate;
      14                 :             : 
      15                 :             : namespace Gjs {
      16                 :             : 
      17                 :             : class MainLoop {
      18                 :             :     // grefcounts start at one and become invalidated when they are decremented
      19                 :             :     // to zero. So the actual hold count is equal to the "ref" count minus 1.
      20                 :             :     // We nonetheless use grefcount here because it takes care of dealing with
      21                 :             :     // integer overflow for us.
      22                 :             :     grefcount m_hold_count;
      23                 :             :     bool m_exiting;
      24                 :             : 
      25                 :        1379 :     void debug(const char* msg) {
      26                 :        1379 :         gjs_debug(GJS_DEBUG_MAINLOOP, "Main loop instance %p: %s", this, msg);
      27                 :        1379 :     }
      28                 :             : 
      29                 :        1457 :     [[nodiscard]] bool can_block() {
      30                 :             :         // Don't block if exiting
      31         [ -  + ]:        1457 :         if (m_exiting)
      32                 :           0 :             return false;
      33                 :             : 
      34                 :        1457 :         g_assert(!g_ref_count_compare(&m_hold_count, 0) &&
      35                 :             :                  "main loop released too many times");
      36                 :             : 
      37                 :             :         // If the reference count is not zero or one, the loop is being held.
      38                 :        1457 :         return !g_ref_count_compare(&m_hold_count, 1);
      39                 :             :     }
      40                 :             : 
      41                 :           4 :     void exit() {
      42                 :           4 :         m_exiting = true;
      43                 :             : 
      44                 :             :         // Reset the reference count to 1 to exit
      45                 :           4 :         g_ref_count_init(&m_hold_count);
      46                 :           4 :     }
      47                 :             : 
      48                 :             :  public:
      49                 :         242 :     MainLoop() : m_exiting(false) { g_ref_count_init(&m_hold_count); }
      50                 :         240 :     ~MainLoop() {
      51                 :         240 :         g_assert(g_ref_count_compare(&m_hold_count, 1) &&
      52                 :             :                  "mismatched hold/release on main loop");
      53                 :         240 :     }
      54                 :             : 
      55                 :         400 :     void hold() {
      56                 :             :         // Don't allow new holds after exit() is called
      57         [ +  + ]:         400 :         if (m_exiting)
      58                 :           1 :             return;
      59                 :             : 
      60                 :         399 :         debug("hold");
      61                 :         399 :         g_ref_count_inc(&m_hold_count);
      62                 :             :     }
      63                 :             : 
      64                 :         397 :     void release() {
      65                 :             :         // Ignore releases after exit(), exit() resets the refcount
      66         [ -  + ]:         397 :         if (m_exiting)
      67                 :           0 :             return;
      68                 :             : 
      69                 :         397 :         debug("release");
      70                 :         397 :         bool zero [[maybe_unused]] = g_ref_count_dec(&m_hold_count);
      71                 :         397 :         g_assert(!zero && "main loop released too many times");
      72                 :             :     }
      73                 :             : 
      74                 :             :     [[nodiscard]] bool spin(GjsContextPrivate*);
      75                 :             : };
      76                 :             : 
      77                 :             : };  // namespace Gjs
        

Generated by: LCOV version 2.0-1