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

graphia-app / graphia / 42521906589

08 Nov 2026 08:46PM UTC coverage: 71.457%. Remained the same
42521906589

push

github

timangus
CLAUDE: Suppress cppcheck's duplInheritedMember check and drop NodePositions' second mutex

11728 of 17976 branches covered (65.24%)

Branch coverage included in aggregate %.

29931 of 40323 relevant lines covered (74.23%)

519576.63 hits per line

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

94.74
/source/shared/utils/threadpool.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 "threadpool.h"
21

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

24
using namespace Qt::Literals::StringLiterals;
25

26
ThreadPool::ThreadPool(const QString& threadNamePrefix, unsigned int numThreads)
27
{
1,538 ✔
28
#ifdef Q_OS_WASM
29
    // On Emscripten thread support is implemented by providing a fixed pthread pool
30
    // which we size relative to hardware concurrency (via navigator.hardwareConcurrency)
31
    // Exhausting this pool potentially results in deadlock, so for /our/ thread pool,
32
    // we only occupy half of them, hopefully reducing the chance of this occuring
33
    numThreads = std::min(numThreads, std::thread::hardware_concurrency() / 2);
34
#endif
35

36
    for(unsigned int i = 0U; i < numThreads; i++)
7,682 ✔
37
    {
6,144 ✔
38
        auto threadName = u"%1%2"_s.arg(threadNamePrefix).arg(i + 1);
6,144 ✔
39

40
        _threads.emplace_back([threadName, this]
6,144 ✔
41
        {
6,144 ✔
42
            std::unique_lock<std::mutex> lock(_mutex);
6,137 ✔
43

44
            while(!_stop)
613,853 ✔
45
            {
612,705 ✔
46
                if(_tasks.empty())
612,705 ✔
47
                {
417,029 ✔
48
                    u::setCurrentThreadName(u"%1 (idle)"_s.arg(threadName));
417,029 ✔
49

50
                    // Block until a new task is queued
51
                    _waitForNewTask.wait(lock, [this] { return !_tasks.empty() || _stop; });
1,019,196 ✔
52
                }
417,029 ✔
53

54
                if(_stop)
612,705 ✔
55
                    break;
4,989 ✔
56

57
                auto task = std::move(_tasks.front());
607,716 ✔
58
                _tasks.pop();
607,716 ✔
59
                lock.unlock();
607,716 ✔
60

61
                u::setCurrentThreadName(u"%1 (busy)"_s.arg(threadName));
607,716 ✔
62
                task();
607,716 ✔
63

64
                lock.lock();
607,716 ✔
65
            }
607,716 ✔
66
        });
6,137 ✔
67
    }
6,144 ✔
68
}
1,538 ✔
69

70
ThreadPool::~ThreadPool()
71
{
1,538 ✔
72
    // Cancel all pending tasks
73
    std::unique_lock<std::mutex> lock(_mutex);
1,538 ✔
74
    _stop = true;
1,538 ✔
75
    while(!_tasks.empty())
1,538 !
76
        _tasks.pop();
×
77
    lock.unlock();
1,538 ✔
78

79
    // Tell all idle threads to unblock
80
    _waitForNewTask.notify_all();
1,538 ✔
81

82
    // Wait for all threads to finish
83
    for(auto& thread : _threads)
1,538 ✔
84
    {
6,144 ✔
85
        if(thread.joinable())
6,144 !
86
            thread.join();
6,144 ✔
87
    }
6,144 ✔
88
}
1,538 ✔
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