summaryrefslogtreecommitdiff
path: root/src/core/jobs/qaspectjobmanager.cpp
blob: a4bd751772c82584eb03bcee339617d8a734206c (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qaspectjobmanager_p.h"

#include <QtCore/QAtomicInt>
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QThread>
#include <QtCore/QFuture>
#include <Qt3DCore/private/qaspectmanager_p.h>
#include <Qt3DCore/private/qthreadpooler_p.h>
#include <Qt3DCore/private/task_p.h>

QT_BEGIN_NAMESPACE

namespace Qt3DCore {

QAspectJobManager::QAspectJobManager(QAspectManager *parent)
    : QAbstractAspectJobManager(parent)
    , m_threadPooler(new QThreadPooler(this))
    , m_aspectManager(parent)
{
}

QAspectJobManager::~QAspectJobManager()
{
}

void QAspectJobManager::initialize()
{
}

// Adds all Aspect Jobs to be processed for a frame
void QAspectJobManager::enqueueJobs(const std::vector<QAspectJobPtr> &jobQueue)
{
    auto systemService = m_aspectManager ? m_aspectManager->serviceLocator()->systemInformation() : nullptr;
    if (systemService)
        systemService->writePreviousFrameTraces();

    // Convert QJobs to Tasks
    QHash<QAspectJob *, AspectTaskRunnable *> tasksMap;
    QList<RunnableInterface *> taskList;
    taskList.reserve(jobQueue.size());
    for (const QAspectJobPtr &job : jobQueue) {
        AspectTaskRunnable *task = new AspectTaskRunnable(systemService);
        task->m_job = job;
        tasksMap.insert(job.data(), task);

        taskList << task;
    }

    for (const QAspectJobPtr &job : jobQueue) {
        const std::vector<QWeakPointer<QAspectJob> > &deps = job->dependencies();
        AspectTaskRunnable *taskDepender = tasksMap.value(job.data());

        int dependerCount = 0;
        for (const QWeakPointer<QAspectJob> &dep : deps) {
            AspectTaskRunnable *taskDependee = tasksMap.value(dep.toStrongRef().data());
            // The dependencies here are not hard requirements, i.e., the dependencies
            // not in the jobQueue should already have their data ready.
            if (taskDependee) {
                taskDependee->m_dependers.append(taskDepender);
                ++dependerCount;
            }
        }

        taskDepender->m_dependerCount += dependerCount;
    }

    m_threadPooler->mapDependables(taskList);
}

// Wait for all aspects jobs to be completed
int QAspectJobManager::waitForAllJobs()
{
    return m_threadPooler->waitForAllJobs();
}

void QAspectJobManager::waitForPerThreadFunction(JobFunction func, void *arg)
{
    const int threadCount = QAspectJobManager::idealThreadCount();
    QAtomicInt atomicCount(threadCount);

    QList<RunnableInterface *> taskList;
    for (int i = 0; i < threadCount; ++i) {
        SyncTaskRunnable *syncTask = new SyncTaskRunnable(func, arg, &atomicCount);
        taskList << syncTask;
    }

    QFuture<void> future = m_threadPooler->mapDependables(taskList);
    future.waitForFinished();
}


int QAspectJobManager::idealThreadCount()
{
    static int jobCount = 0;
    if (jobCount)
        return jobCount;

    const QByteArray maxThreadCount = qgetenv("QT3D_MAX_THREAD_COUNT");
    if (!maxThreadCount.isEmpty()) {
        bool conversionOK = false;
        const int maxThreadCountValue = maxThreadCount.toInt(&conversionOK);
        if (conversionOK) {
            jobCount = maxThreadCountValue;
            return jobCount;
        }
    }

    jobCount = QThread::idealThreadCount();
    return jobCount;
}

} // namespace Qt3DCore

QT_END_NAMESPACE

#include "moc_qaspectjobmanager_p.cpp"