summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/runtimerender/Qt3DSRenderThreadPool.cpp
blob: 70499c62be30b2a2ce387764d954259cca7060ae (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/****************************************************************************
**
** Copyright (C) 2008-2012 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "Qt3DSRenderThreadPool.h"
#include "foundation/Qt3DSThread.h"
#include "EASTL/utility.h"
#include "EASTL/list.h"
#include "foundation/Qt3DSMutex.h"
#include "foundation/Qt3DSContainers.h"
#include "foundation/Qt3DSAtomic.h"
#include "foundation/Qt3DSBroadcastingAllocator.h"
#include "foundation/Qt3DSSync.h"
#include "foundation/Qt3DSMutex.h"
#include "foundation/Qt3DSPool.h"
#include "foundation/Qt3DSInvasiveLinkedList.h"

using namespace qt3ds::render;

namespace {
struct STask
{
    void *m_UserData;
    TTaskFunction m_Function;
    TTaskFunction m_CancelFunction;
    QT3DSU64 m_Id;
    TaskStates::Enum m_TaskState;
    STask *m_NextTask;
    STask *m_PreviousTask;

    STask(void *ud, TTaskFunction func, TTaskFunction cancelFunc, QT3DSU64 inId)
        : m_UserData(ud)
        , m_Function(func)
        , m_CancelFunction(cancelFunc)
        , m_Id(inId)
        , m_TaskState(TaskStates::Queued)
        , m_NextTask(NULL)
        , m_PreviousTask(NULL)
    {
    }
    STask()
        : m_UserData(NULL)
        , m_Function(NULL)
        , m_CancelFunction(NULL)
        , m_Id(0)
        , m_TaskState(TaskStates::UnknownTask)
        , m_NextTask(NULL)
        , m_PreviousTask(NULL)
    {
    }
    void CallFunction()
    {
        if (m_Function)
            m_Function(m_UserData);
    }
    void Cancel()
    {
        if (m_CancelFunction)
            m_CancelFunction(m_UserData);
    }
};

struct STaskHeadOp
{
    STask *get(STask &inTask) { return inTask.m_PreviousTask; }
    void set(STask &inTask, STask *inItem) { inTask.m_PreviousTask = inItem; }
};

struct STaskTailOp
{
    STask *get(STask &inTask) { return inTask.m_NextTask; }
    void set(STask &inTask, STask *inItem) { inTask.m_NextTask = inItem; }
};

typedef InvasiveLinkedList<STask, STaskHeadOp, STaskTailOp> TTaskList;

class IInternalTaskManager
{
protected:
    virtual ~IInternalTaskManager() {}
public:
    virtual STask GetNextTask() = 0;
    virtual void TaskFinished(QT3DSU64 inId) = 0;
};

struct SThreadPoolThread : public Thread
{
    IInternalTaskManager &m_Mgr;
    SThreadPoolThread(NVFoundationBase &foundation, IInternalTaskManager &inMgr)
        : Thread(foundation)
        , m_Mgr(inMgr)
    {
    }
    void execute(void) override
    {
        setName("Qt3DSRender Thread manager thread");
        while (!quitIsSignalled()) {
            STask task = m_Mgr.GetNextTask();
            if (task.m_Function) {
                task.CallFunction();
                m_Mgr.TaskFinished(task.m_Id);
            }
        }
        quit();
    }
};

struct SThreadPool : public IThreadPool, public IInternalTaskManager
{
    typedef nvhash_map<QT3DSU64, STask *> TIdTaskMap;
    typedef Mutex::ScopedLock TLockType;
    typedef Pool<STask, ForwardingAllocator> TTaskPool;

    NVFoundationBase &m_Foundation;
    volatile QT3DSI32 mRefCount;
    nvvector<SThreadPoolThread *> m_Threads;
    TIdTaskMap m_Tasks;
    Sync m_TaskListEvent;
    volatile bool m_Running;
    Mutex m_TaskListMutex;
    TTaskPool m_TaskPool;
    TTaskList m_TaskList;

    QT3DSU64 m_NextId;

    SThreadPool(NVFoundationBase &inBase, QT3DSU32 inMaxThreads)
        : m_Foundation(inBase)
        , mRefCount(0)
        , m_Threads(inBase.getAllocator(), "SThreadPool::m_Threads")
        , m_Tasks(inBase.getAllocator(), "SThreadPool::m_Tasks")
        , m_TaskListEvent(inBase.getAllocator())
        , m_Running(true)
        , m_TaskListMutex(m_Foundation.getAllocator())
        , m_TaskPool(ForwardingAllocator(m_Foundation.getAllocator(), "SThreadPool::m_TaskPool"))
        , m_NextId(1)
    {
        // Fire up our little pools of chaos.
        for (QT3DSU32 idx = 0; idx < inMaxThreads; ++idx) {
            m_Threads.push_back(
                QT3DS_NEW(m_Foundation.getAllocator(), SThreadPoolThread)(m_Foundation, *this));
            m_Threads.back()->start(Thread::DEFAULT_STACK_SIZE);
        }
    }

    void MutexHeldRemoveTaskFromList(STask *theTask)
    {
        if (theTask)
            m_TaskList.remove(*theTask);
        QT3DS_ASSERT(theTask->m_NextTask == NULL);
        QT3DS_ASSERT(theTask->m_PreviousTask == NULL);
    }

    STask *MutexHeldNextTask()
    {
        STask *theTask = m_TaskList.front_ptr();
        if (theTask) {
            MutexHeldRemoveTaskFromList(theTask);
        }
        if (theTask) {
            QT3DS_ASSERT(m_TaskList.m_Head != theTask);
            QT3DS_ASSERT(m_TaskList.m_Tail != theTask);
        }
        return theTask;
    }

    virtual ~SThreadPool()
    {
        m_Running = false;

        m_TaskListEvent.set();

        for (QT3DSU32 idx = 0, end = m_Threads.size(); idx < end; ++idx)
            m_Threads[idx]->signalQuit();

        for (QT3DSU32 idx = 0, end = m_Threads.size(); idx < end; ++idx) {
            m_Threads[idx]->waitForQuit();
            NVDelete(m_Foundation.getAllocator(), m_Threads[idx]);
        }

        m_Threads.clear();

        TLockType __listMutexLocker(m_TaskListMutex);

        for (STask *theTask = MutexHeldNextTask(); theTask; theTask = MutexHeldNextTask()) {
            theTask->Cancel();
        }

        m_Tasks.clear();
    }

    QT3DS_IMPLEMENT_REF_COUNT_ADDREF_RELEASE(m_Foundation.getAllocator())

    void VerifyTaskList()
    {
        STask *theLastTask = NULL;
        for (STask *theTask = m_TaskList.m_Head; theTask; theTask = theTask->m_NextTask) {
            QT3DS_ASSERT(theTask->m_PreviousTask == theLastTask);
            theLastTask = theTask;
        }
        theLastTask = NULL;
        for (STask *theTask = m_TaskList.m_Tail; theTask; theTask = theTask->m_PreviousTask) {
            QT3DS_ASSERT(theTask->m_NextTask == theLastTask);
            theLastTask = theTask;
        }
    }

    QT3DSU64 AddTask(void *inUserData, TTaskFunction inFunction,
                          TTaskFunction inCancelFunction) override
    {
        if (inFunction && m_Running) {
            TLockType __listMutexLocker(m_TaskListMutex);
            QT3DSU64 taskId = m_NextId;
            ++m_NextId;

            STask *theTask = (STask *)m_TaskPool.allocate(__FILE__, __LINE__);
            new (theTask) STask(inUserData, inFunction, inCancelFunction, taskId);
            TIdTaskMap::iterator theTaskIter =
                m_Tasks.insert(eastl::make_pair(taskId, theTask)).first;

            m_TaskList.push_back(*theTask);
            QT3DS_ASSERT(m_TaskList.m_Tail == theTask);

#ifdef _DEBUG
            VerifyTaskList();
#endif
            m_TaskListEvent.set();
            m_TaskListEvent.reset();
            return taskId;
        }
        QT3DS_ASSERT(false);
        return 0;
    }

    TaskStates::Enum GetTaskState(QT3DSU64 inTaskId) override
    {
        TLockType __listMutexLocker(m_TaskListMutex);
        TIdTaskMap::iterator theTaskIter = m_Tasks.find(inTaskId);
        if (theTaskIter != m_Tasks.end())
            return theTaskIter->second->m_TaskState;
        return TaskStates::UnknownTask;
    }

    CancelReturnValues::Enum CancelTask(QT3DSU64 inTaskId) override
    {
        TLockType __listMutexLocker(m_TaskListMutex);
        TIdTaskMap::iterator theTaskIter = m_Tasks.find(inTaskId);
        if (theTaskIter == m_Tasks.end())
            return CancelReturnValues::TaskCanceled;
        if (theTaskIter->second->m_TaskState == TaskStates::Running)
            return CancelReturnValues::TaskRunning;

        STask *theTask = theTaskIter->second;
        theTask->Cancel();
        MutexHeldRemoveTaskFromList(theTask);
        m_Tasks.erase(inTaskId);
        m_TaskPool.deallocate(theTask);

        return CancelReturnValues::TaskCanceled;
    }

    STask GetNextTask() override
    {

        if (m_Running) {
            {
                TLockType __listMutexLocker(m_TaskListMutex);
                STask *retval = MutexHeldNextTask();
                if (retval)
                    return *retval;
            }
            // If we couldn't get a task then wait.
            m_TaskListEvent.wait(1000);
        }
        return STask();
    }

    void TaskFinished(QT3DSU64 inId) override
    {
        TLockType __listMutexLocker(m_TaskListMutex);
        TIdTaskMap::iterator theTaskIter = m_Tasks.find(inId);
        if (theTaskIter == m_Tasks.end()) {
            QT3DS_ASSERT(false);
            return;
        }

        STask *theTask(theTaskIter->second);

#ifdef _DEBUG
        QT3DS_ASSERT(theTask->m_NextTask == NULL);
        QT3DS_ASSERT(theTask->m_PreviousTask == NULL);
#endif
        m_TaskPool.deallocate(theTask);
        m_Tasks.erase(inId);
        return;
    }
};
}

IThreadPool &IThreadPool::CreateThreadPool(NVFoundationBase &inFoundation, QT3DSU32 inNumThreads)
{
    return *QT3DS_NEW(inFoundation.getAllocator(), SThreadPool)(inFoundation, inNumThreads);
}