summaryrefslogtreecommitdiff
path: root/gjs/mainloop.h
blob: f374060c8322f78721269cd9d840924184586c49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2021 Evan Welsh <contact@evanwelsh.com>

#pragma once

#include <config.h>

#include <glib.h>

class GjsContextPrivate;

namespace Gjs {

class MainLoop {
    // grefcounts start at one and become invalidated when they are decremented
    // to zero. So the actual hold count is equal to the "ref" count minus 1.
    // We nonetheless use grefcount here because it takes care of dealing with
    // integer overflow for us.
    grefcount m_hold_count;
    bool m_exiting;

    [[nodiscard]] bool can_block() {
        // Don't block if exiting
        if (m_exiting)
            return false;

        g_assert(!g_ref_count_compare(&m_hold_count, 0) &&
                 "main loop released too many times");

        // If the reference count is not zero or one, the loop is being held.
        return !g_ref_count_compare(&m_hold_count, 1);
    }

    void exit() {
        m_exiting = true;

        // Reset the reference count to 1 to exit
        g_ref_count_init(&m_hold_count);
    }

 public:
    MainLoop() : m_exiting(false) { g_ref_count_init(&m_hold_count); }
    ~MainLoop() {
        g_assert(g_ref_count_compare(&m_hold_count, 1) &&
                 "mismatched hold/release on main loop");
    }

    void hold() {
        // Don't allow new holds after exit() is called
        if (m_exiting)
            return;

        g_ref_count_inc(&m_hold_count);
    }

    void release() {
        // Ignore releases after exit(), exit() resets the refcount
        if (m_exiting)
            return;

        bool zero [[maybe_unused]] = g_ref_count_dec(&m_hold_count);
        g_assert(!zero && "main loop released too many times");
    }

    [[nodiscard]] bool spin(GjsContextPrivate*);
};

};  // namespace Gjs