1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "task_p.h" |
5 | |
6 | #include <QtCore/QDebug> |
7 | #include <QtCore/QElapsedTimer> |
8 | #include <QtCore/QMutexLocker> |
9 | |
10 | #include <Qt3DCore/private/qthreadpooler_p.h> |
11 | #include <Qt3DCore/private/qsysteminformationservice_p_p.h> |
12 | |
13 | QT_BEGIN_NAMESPACE |
14 | |
15 | namespace Qt3DCore { |
16 | |
17 | RunnableInterface::~RunnableInterface() |
18 | { |
19 | } |
20 | |
21 | // Aspect task |
22 | |
23 | AspectTaskRunnable::AspectTaskRunnable(QSystemInformationService *service) |
24 | : m_service(service) |
25 | , m_pooler(nullptr) |
26 | , m_id(0) |
27 | , m_reserved(false) |
28 | { |
29 | } |
30 | |
31 | AspectTaskRunnable::~AspectTaskRunnable() |
32 | { |
33 | } |
34 | |
35 | bool AspectTaskRunnable::isRequired() const |
36 | { |
37 | return m_job ? QAspectJobPrivate::get(job: m_job.data())->isRequired() : false; |
38 | } |
39 | |
40 | void AspectTaskRunnable::run() |
41 | { |
42 | if (m_job) { |
43 | QAspectJobPrivate *jobD = QAspectJobPrivate::get(job: m_job.data()); |
44 | QTaskLogger logger(m_pooler ? m_service : nullptr, jobD->m_jobId, QTaskLogger::AspectJob); |
45 | m_job->run(); |
46 | } |
47 | |
48 | // We could have an append sub task or something in here |
49 | // So that a job can post sub jobs ? |
50 | |
51 | if (m_pooler) |
52 | m_pooler->taskFinished(task: this); |
53 | } |
54 | |
55 | // Synchronized task |
56 | |
57 | SyncTaskRunnable::SyncTaskRunnable(QAbstractAspectJobManager::JobFunction func, |
58 | void *arg, QAtomicInt *atomicCount) |
59 | : m_func(func) |
60 | , m_arg(arg) |
61 | , m_atomicCount(atomicCount) |
62 | , m_pooler(nullptr) |
63 | , m_reserved(false) |
64 | , m_id(0) |
65 | { |
66 | } |
67 | |
68 | SyncTaskRunnable::~SyncTaskRunnable() |
69 | { |
70 | } |
71 | |
72 | bool SyncTaskRunnable::isRequired() const |
73 | { |
74 | return true; |
75 | } |
76 | |
77 | void SyncTaskRunnable::run() |
78 | { |
79 | // Call the function |
80 | m_func(m_arg); |
81 | |
82 | // Decrement the atomic counter to let others know we've done our bit |
83 | m_atomicCount->deref(); |
84 | |
85 | // Wait for the other worker threads to be done |
86 | while (m_atomicCount->loadRelaxed() > 0) |
87 | QThread::currentThread()->yieldCurrentThread(); |
88 | |
89 | if (m_pooler) |
90 | m_pooler->taskFinished(task: this); |
91 | } |
92 | |
93 | } // namespace Qt3DCore { |
94 | |
95 | QT_END_NAMESPACE |
96 | |