summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/foundation/linux/Qt3DSLinuxThread.cpp
blob: fa6e069e07a4c3f32d46036ad21eb48c0eb23732 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/****************************************************************************
**
** 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 "foundation/Qt3DS.h"
#include "foundation/Qt3DSFoundation.h"
#include "foundation/Qt3DSAtomic.h"
#include "foundation/Qt3DSThread.h"
#include "foundation/Qt3DSAssert.h"
#include "foundation/Qt3DSIntrinsics.h"
#include "foundation/Qt3DSBroadcastingAllocator.h"
#if !defined(QT3DS_APPLE) && !defined(ANDROID) && !defined(__CYGWIN__) && !defined(__QNX__) && !defined(__INTEGRITY)
#include <bits/local_lim.h> // PTHREAD_STACK_MIN
#endif
#include <stdio.h>
#include <pthread.h>
#if !defined(__QNX__) && !defined(__INTEGRITY)
#include <unistd.h>
#include <sys/syscall.h>
#if !defined(QT3DS_APPLE)
#include <asm/unistd.h>
#include <sys/resource.h>
#endif
#endif

#define NVSpinLockPause() asm("nop")

namespace qt3ds {
namespace foundation {
    using namespace intrinsics;

    typedef enum { _NVThreadNotStarted, _NVThreadStarted, _NVThreadStopped } NVThreadState;

    class ThreadImpl
    {
    public:
        ThreadImpl(NVFoundationBase &foundation)
            : mFoundation(foundation)
        {
        }
        NVFoundationBase &mFoundation;
        Thread::ExecuteFn fn;
        void *arg;
        volatile QT3DSI32 quitNow;
        volatile QT3DSI32 threadStarted;
        NVThreadState state;

        pthread_t thread;
        pid_t tid;
    };

    void *NVThreadStart(void *arg);

    Thread::Id Thread::getId() { return Id(pthread_self()); }

    Thread::Thread(NVFoundationBase &foundation)
    {
        mImpl = QT3DS_NEW(foundation.getAllocator(), ThreadImpl)(foundation);
        mImpl->thread = 0;
        mImpl->tid = 0;
        mImpl->state = _NVThreadNotStarted;
        mImpl->quitNow = 0;
        mImpl->threadStarted = 0;
        mImpl->fn = NULL;
        mImpl->arg = NULL;
    }

    Thread::Thread(NVFoundationBase &foundation, Thread::ExecuteFn fn, void *arg)
    {
        mImpl = (ThreadImpl *)QT3DS_NEW(foundation.getAllocator(), ThreadImpl)(foundation);
        mImpl->thread = 0;
        mImpl->tid = 0;
        mImpl->state = _NVThreadNotStarted;
        mImpl->quitNow = 0;
        mImpl->threadStarted = 0;
        mImpl->fn = fn;
        mImpl->arg = arg;

        start(0);
    }

    Thread::~Thread()
    {
        if (mImpl->state == _NVThreadStarted)
            kill();
        QT3DS_FREE(mImpl->mFoundation.getAllocator(), mImpl);
    }

    void Thread::start(QT3DSU32 stackSize)
    {
        if (mImpl->state != _NVThreadNotStarted)
            return;

        if (stackSize == 0)
            stackSize = DEFAULT_STACK_SIZE;

#if defined(PTHREAD_STACK_MIN) && !defined(ANDROID)
        if (stackSize < PTHREAD_STACK_MIN) {
            qCWarning(WARNING, "Thread::start(): stack size was set below PTHREAD_STACK_MIN");
            stackSize = PTHREAD_STACK_MIN;
        }
#endif

        pthread_attr_t attr;
        int status = pthread_attr_init(&attr);
        QT3DS_ASSERT(!status);

        status = pthread_attr_setstacksize(&attr, stackSize);
        QT3DS_ASSERT(!status);
        status = pthread_create(&mImpl->thread, &attr, NVThreadStart, this);
        QT3DS_ASSERT(!status);

#ifndef __QNX__
        // wait for thread to startup and write out TID
        // otherwise TID dependent calls like setAffinity will fail.
        while (atomicCompareExchange(&(mImpl->threadStarted), 1, 1) == 0)
            yield();
#endif

        mImpl->state = _NVThreadStarted;

        status = pthread_attr_destroy(&attr);
        QT3DS_ASSERT(!status);
    }

    static void setTid(ThreadImpl &threadImpl)
    {
#ifndef __QNX__
// query TID
#ifdef QT3DS_APPLE
        threadImpl.tid = syscall(SYS_gettid);
#elif defined(__INTEGRITY)
        pthread_t ptid = pthread_self();
        uint64_t threadId = 0;
        memcpy(&threadId, &ptid, std::min(sizeof(threadId), sizeof(ptid)));
        threadImpl.tid = threadId;
#else
        threadImpl.tid = syscall(__NR_gettid);
#endif

        // notify/unblock parent thread
        atomicCompareExchange(&(threadImpl.threadStarted), 1, 0);
#else
        QT3DS_ASSERT(false);
#endif
    }

    void *NVThreadStart(void *arg)
    {
        // run execute from base class to run gettid in the new thread's context
        ((Thread *)arg)->Thread::execute();
        return 0;
    }

    void Thread::execute(void)
    {
        // run setTid in thread's context
        setTid(*mImpl);

        // then run either the passed in function or execute from the derived class.
        if (mImpl->fn)
            (*mImpl->fn)(mImpl->arg);
        else
            this->execute();
    }

    void Thread::signalQuit() { atomicIncrement(&(mImpl->quitNow)); }

    bool Thread::waitForQuit()
    {
        if (mImpl->state == _NVThreadNotStarted)
            return false;

        pthread_join(mImpl->thread, NULL);
        return true;
    }

    bool Thread::quitIsSignalled()
    {
#ifndef __QNX__
        return atomicCompareExchange(&(mImpl->quitNow), 0, 0) != 0;
#else
        // Hope for memory locking on the arm.
        return mImpl->quitNow != 0;
#endif
    }

    void Thread::quit()
    {
        mImpl->state = _NVThreadStopped;
        pthread_exit(0);
    }

    void Thread::kill()
    {
#ifndef ANDROID
        if (mImpl->state == _NVThreadStarted)
            pthread_cancel(mImpl->thread);
        mImpl->state = _NVThreadStopped;
#else
        qCWarning(WARNING, "Thread::kill() called, but is not implemented");
#endif
    }

    void Thread::sleep(QT3DSU32 ms)
    {
        timespec sleepTime;
        QT3DSU32 remainder = ms % 1000;
        sleepTime.tv_sec = ms - remainder;
        sleepTime.tv_nsec = remainder * 1000000L;

        while (nanosleep(&sleepTime, &sleepTime) == -1)
            continue;
    }

    void Thread::yield() { sched_yield(); }

    QT3DSU32 Thread::setAffinityMask(QT3DSU32 mask)
    {
        // Same as windows impl if mask is zero
        if (!mask)
            return 0;

#ifndef __QNX__
        QT3DSU64 prevMask = 0;

// Apple doesn't support syscall with getaffinity and setaffinity
#if !defined(QT3DS_APPLE) && !defined(__INTEGRITY)
        QT3DSI32 errGet = syscall(__NR_sched_getaffinity, mImpl->tid, sizeof(prevMask), &prevMask);
        if (errGet < 0)
            return 0;

        QT3DSI32 errSet = syscall(__NR_sched_setaffinity, mImpl->tid, sizeof(mask), &mask);
        if (errSet != 0)
            return 0;
#endif

        return QT3DSU32(prevMask);
#else
        QT3DS_ASSERT(false);
        return 0;
#endif
    }

    void Thread::setName(const char *name)
    {
#if (defined(ANDROID) && (__ANDROID_API__ > 8))
        pthread_setname_np(mImpl->thread, name);
#else
// not implemented because most unix APIs expect setName()
// to be called from the thread's context. Example see next comment:

// this works only with the current thread and can rename
// the main process if used in the wrong context:
// prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name) ,0,0,0);
#endif
    }

#if !defined(QT3DS_APPLE) && !defined(__INTEGRITY)
    static ThreadPriority::Enum convertPriorityFromLinux(QT3DSU32 inPrio, int policy)
    {
        QT3DS_COMPILE_TIME_ASSERT(ThreadPriority::eLOW > ThreadPriority::eHIGH);
        QT3DS_COMPILE_TIME_ASSERT(ThreadPriority::eHIGH == 0);

        int maxL = sched_get_priority_max(policy);
        int minL = sched_get_priority_min(policy);
        int rangeL = maxL - minL;
        int rangeNV = ThreadPriority::eLOW - ThreadPriority::eHIGH;

        // case for default scheduler policy
        if (rangeL == 0)
            return ThreadPriority::eNORMAL;

        float floatPrio = (float(maxL - inPrio) * float(rangeNV)) / float(rangeL);

        return ThreadPriority::Enum(int(roundf(floatPrio)));
    }

    static int convertPriorityToLinux(ThreadPriority::Enum inPrio, int policy)
    {
        int maxL = sched_get_priority_max(policy);
        int minL = sched_get_priority_min(policy);
        int rangeL = maxL - minL;
        int rangeNV = ThreadPriority::eLOW - ThreadPriority::eHIGH;

        // case for default scheduler policy
        if (rangeL == 0)
            return 0;

        float floatPrio = (float(ThreadPriority::eLOW - inPrio) * float(rangeL)) / float(rangeNV);

        return minL + int(roundf(floatPrio));
    }
#endif

    void Thread::setPriority(ThreadPriority::Enum val)
    {
#if !defined(QT3DS_APPLE) && !defined(__INTEGRITY)
        int policy;
        sched_param s_param;
        pthread_getschedparam(mImpl->thread, &policy, &s_param);
        s_param.sched_priority = convertPriorityToLinux(val, policy);
        pthread_setschedparam(mImpl->thread, policy, &s_param);
#endif
    }

    ThreadPriority::Enum Thread::getPriority(Id pthread)
    {
#if !defined(QT3DS_APPLE) && !defined(__INTEGRITY)
        int policy;
        sched_param s_param;
        int ret = pthread_getschedparam(pthread_t(pthread), &policy, &s_param);
        if (ret == 0)
            return convertPriorityFromLinux(s_param.sched_priority, policy);
        else
            return ThreadPriority::eNORMAL;
#else
        return ThreadPriority::eNORMAL;
#endif
    }

    QT3DSU32 TlsAlloc()
    {
#if !defined(__INTEGRITY)
        pthread_key_t key;
        int status = pthread_key_create(&key, NULL);
        QT3DS_ASSERT(!status);
        (void)status;
        return (QT3DSU32)key;
#else
        QT3DS_ASSERT(false);
        return 0;
#endif
    }

    void TlsFree(QT3DSU32 index)
    {
        int status = pthread_key_delete((pthread_key_t)index);
        QT3DS_ASSERT(!status);
        (void)status;
    }

    void *TlsGet(QT3DSU32 index) { return (void *)pthread_getspecific((pthread_key_t)index); }

    QT3DSU32 TlsSet(QT3DSU32 index, void *value)
    {
        int status = pthread_setspecific((pthread_key_t)index, value);
        QT3DS_ASSERT(!status);
        return !status;
    }

    // DM: On Linux x86-32, without implementation-specific restrictions
    // the default stack size for a new thread should be 2 megabytes (kernel.org).
    // NOTE: take care of this value on other architecutres!
    const QT3DSU32 Thread::DEFAULT_STACK_SIZE = 1 << 21;

} // namespace foundation
} // namespace qt3ds