• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

graphia-app / graphia / 43746057935

15 Nov 2026 06:15PM UTC coverage: 82.168% (+1.2%) from 80.934%
43746057935

push

github

timangus
CLAUDE: Add tests of mouse interaction with the graph display

13289 of 18562 branches covered (71.59%)

Branch coverage included in aggregate %.

36268 of 41750 relevant lines covered (86.87%)

408826.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.66
/source/shared/utils/deferredexecutor.cpp
1
/* Copyright © 2013-2025 Tim Angus
2
 * Copyright © 2013-2025 Tom Freeman
3
 *
4
 * This file is part of Graphia.
5
 *
6
 * Graphia is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Graphia is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with Graphia.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

20
#include "deferredexecutor.h"
21

22
#include "shared/utils/thread.h"
23
#include "shared/utils/container.h"
24

25
#include <QDebug>
26
#include <QtGlobal>
27

28
#include <algorithm>
29
#include <utility>
30

31
DeferredExecutor::DeferredExecutor() :
32
    _debug(qEnvironmentVariableIntValue("DEFERREDEXECUTOR_DEBUG"))
802 ✔
33
{}
802 ✔
34

35
DeferredExecutor::~DeferredExecutor()
36
{
802 ✔
37
    cancel();
802 ✔
38
}
802 ✔
39

40
size_t DeferredExecutor::enqueue(TaskFn&& function, const QString& description)
41
{
1,847 ✔
42
    const std::unique_lock<std::recursive_mutex> lock(_mutex);
1,847 ✔
43

44
    Task task;
1,847 ✔
45
    task._function = std::move(function);
1,847 ✔
46
    task._description = description;
1,847 ✔
47

48
    if(_debug > 1)
1,847 !
49
        qDebug() << "enqueue(...) thread:" << u::currentThreadName() << description;
1,847 ✔
50

51
    _tasks.emplace_back(task);
1,847 ✔
52

53
    return _tasks.size();
1,847 ✔
54
}
1,847 ✔
55

56
void DeferredExecutor::execute()
57
{
7,302 ✔
58
    const std::unique_lock<std::recursive_mutex> lock(_mutex);
7,302 ✔
59

60
    if(_paused)
7,302 !
61
        return;
×
62

63
    if(!_tasks.empty() && _debug > 0)
7,302 !
64
    {
×
65
        qDebug() << "execute() thread" << u::currentThreadName();
×
66

67
        for(const auto& task : _tasks)
×
68
            qDebug() << "\t" << task._description;
×
69
    }
×
70

71
    while(!_tasks.empty())
9,149 ✔
72
        executeOne();
1,847 ✔
73
}
7,302 ✔
74

75
void DeferredExecutor::executeOne()
76
{
1,847 ✔
77
    const std::unique_lock<std::recursive_mutex> lock(_mutex);
1,847 ✔
78

79
    if(_paused)
1,847 !
80
        return;
×
81

82
    auto task = _tasks.front();
1,847 ✔
83
    _tasks.pop_front();
1,847 ✔
84

85
    if(_debug > 2)
1,847 !
86
        qDebug() << "Executing" << task._description;
1,847 ✔
87

88
    task._function();
1,847 ✔
89

90
    // Decrement all the wait counts
91
    for(auto it = _waitCount.begin(); it != _waitCount.end();)
2,075 ✔
92
    {
228 ✔
93
        if(it->second > 0)
228 !
94
            it->second--;
228 ✔
95

96
        if(it->second == 0)
228 !
97
            it = _waitCount.erase(it);
228 ✔
98
        else
×
99
            ++it;
×
100
    }
228 ✔
101

102
    _waitCondition.notify_all();
1,847 ✔
103
}
1,847 ✔
104

105
void DeferredExecutor::cancel()
106
{
802 ✔
107
    const std::unique_lock<std::recursive_mutex> lock(_mutex);
802 ✔
108

109
    while(!_tasks.empty())
802 !
110
        _tasks.pop_front();
×
111
}
802 ✔
112

113
bool DeferredExecutor::hasTasks() const
114
{
186 ✔
115
    const std::unique_lock<std::recursive_mutex> lock(_mutex);
186 ✔
116
    return !_tasks.empty();
186 ✔
117
}
186 ✔
118

119
void DeferredExecutor::pause()
120
{
16 ✔
121
    const std::unique_lock<std::recursive_mutex> lock(_mutex);
16 ✔
122
    _paused = true;
16 ✔
123
}
16 ✔
124

125
void DeferredExecutor::resume()
126
{
212 ✔
127
    const std::unique_lock<std::recursive_mutex> lock(_mutex);
212 ✔
128
    _paused = false;
212 ✔
129
}
212 ✔
130

131
void DeferredExecutor::waitFor(size_t numTasks)
132
{
254 ✔
133
    std::unique_lock<std::recursive_mutex> lock(_mutex);
254 ✔
134

135
    if(_tasks.empty() || numTasks == 0)
254 !
136
    {
26 ✔
137
        // If there are no tasks to wait for, we can just return
138
        // This can happen when task(s) are executed and completed
139
        // before the call to waitFor occurs
140
        if(_debug > 2)
26 !
141
        {
×
142
            qDebug() << "waitFor(" << numTasks << ") called with" <<
×
143
                _tasks.size() << "tasks queued thread:" << u::currentThreadName();
×
144
        }
×
145

146
        return;
26 ✔
147
    }
26 ✔
148

149
    numTasks = std::min(_tasks.size(), numTasks);
228 ✔
150
    auto threadId = std::this_thread::get_id();
228 ✔
151
    _waitCount[threadId] = numTasks;
228 ✔
152

153
    if(_debug > 1)
228 !
154
    {
×
155
        qDebug() << "waitFor(" << numTasks << ") thread:" <<
×
156
            u::currentThreadName();
×
157
    }
×
158

159
    // Keep waiting until there are no remaining tasks
160
    _waitCondition.wait(lock,
228 ✔
161
    [this, threadId]
228 ✔
162
    {
456 ✔
163
        return !u::contains(_waitCount, threadId);
456 ✔
164
    });
456 ✔
165

166
    if(_debug > 1)
228 !
167
    {
×
168
        qDebug() << "waitFor complete thread:" <<
169
            u::currentThreadName();
×
170
    }
×
171
}
228 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc