aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/shutdownguard.cpp
blob: 4fbef5834f073cefcdf183094efe837f9feb261c (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
70
71
72
73
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <shutdownguard.h>

#include <threadutils.h>
#include <qtcassert.h>

namespace Utils {

class ShutdownGuardHolder final
{
public:
    ~ShutdownGuardHolder()
    {
        if (!m_alreadyGone)
            triggerShutdownGuard();
    }

    QObject *shutdownGuard()
    {
        QTC_CHECK(!m_alreadyGone);
        if (!m_shutdownGuard) {
            QTC_CHECK(Utils::isMainThread());
            m_shutdownGuard = new QObject;
        }
        return m_shutdownGuard;
    }

    void triggerShutdownGuard()
    {
        m_alreadyGone = true;
        delete m_shutdownGuard;
        m_shutdownGuard = nullptr;
    }

private:
    QObject *m_shutdownGuard = nullptr;
    bool m_alreadyGone = false;
};

static ShutdownGuardHolder theShutdownGuardHolder;

/*!
    Destroys the shutdown guard object and consequently all
    objects guarded by it.

    In a normal run of the application this function is called
    automatically at the appropriate time.
*/

void triggerShutdownGuard()
{
    return theShutdownGuardHolder.triggerShutdownGuard();
}

/*!
    Returns an object that can be used as the parent for objects that should be
    destroyed just at the end of the applications lifetime.
    The object is destroyed after all plugins' aboutToShutdown methods
    have finished, just before the plugins are deleted.

    Only use this from the application's main thread.

    \sa ExtensionSystem::IPlugin::aboutToShutdown()
*/

QObject *shutdownGuard()
{
    return theShutdownGuardHolder.shutdownGuard();
}

} // Utils