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

graphia-app / graphia / 42724698251

09 Nov 2026 10:47PM UTC coverage: 71.88% (+0.5%) from 71.427%
42724698251

push

github

timangus
Add Leiden clustering

11927 of 18208 branches covered (65.5%)

Branch coverage included in aggregate %.

486 of 497 new or added lines in 3 files covered. (97.79%)

699 existing lines in 55 files now uncovered.

30522 of 40847 relevant lines covered (74.72%)

740302.21 hits per line

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

65.32
/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"))
622 ✔
33
{}
622 ✔
34

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

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

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

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

51
    _tasks.emplace_back(task);
310 ✔
52

53
    return _tasks.size();
310 ✔
54
}
310 ✔
55

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

60
    if(_paused)
912 !
61
        return;
×
62

63
    if(!_tasks.empty() && _debug > 0)
912 !
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())
1,222 ✔
72
        executeOne();
310 ✔
73
}
912 ✔
74

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

79
    if(_paused)
310 !
80
        return;
×
81

82
    auto task = _tasks.front();
310 ✔
83
    _tasks.pop_front();
310 ✔
84

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

88
    task._function();
310 ✔
89

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

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

102
    _waitCondition.notify_all();
310 ✔
103
}
310 ✔
104

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

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

113
void DeferredExecutor::pause()
114
{
×
115
    const std::unique_lock<std::recursive_mutex> lock(_mutex);
×
116
    _paused = true;
×
117
}
×
118

119
void DeferredExecutor::resume()
120
{
×
121
    const std::unique_lock<std::recursive_mutex> lock(_mutex);
×
122
    _paused = false;
×
123
}
×
124

125
void DeferredExecutor::waitFor(size_t numTasks)
126
{
146 ✔
127
    std::unique_lock<std::recursive_mutex> lock(_mutex);
146 ✔
128

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

140
        return;
26 ✔
141
    }
26 ✔
142

143
    numTasks = std::min(_tasks.size(), numTasks);
120 ✔
144
    auto threadId = std::this_thread::get_id();
120 ✔
145
    _waitCount[threadId] = numTasks;
120 ✔
146

147
    if(_debug > 1)
120 !
UNCOV
148
    {
×
UNCOV
149
        qDebug() << "waitFor(" << numTasks << ") thread:" <<
×
UNCOV
150
            u::currentThreadName();
×
UNCOV
151
    }
×
152

153
    // Keep waiting until there are no remaining tasks
154
    _waitCondition.wait(lock,
120 ✔
155
    [this, threadId]
120 ✔
156
    {
240 ✔
157
        return !u::contains(_waitCount, threadId);
240 ✔
158
    });
240 ✔
159

160
    if(_debug > 1)
120 !
UNCOV
161
    {
×
162
        qDebug() << "waitFor complete thread:" <<
UNCOV
163
            u::currentThreadName();
×
UNCOV
164
    }
×
165
}
120 ✔
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