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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
|
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qthread.h"
#include "qthreadstorage.h"
#include "qmutex.h"
#include "qmutexpool_p.h"
#include "qreadwritelock.h"
#include "qabstracteventdispatcher.h"
#include <qeventloop.h>
#include <qhash.h>
#include "qthread_p.h"
#include "private/qcoreapplication_p.h"
/*
#ifdef Q_OS_WIN32
# include "qt_windows.h"
#else
# include <unistd.h>
# include <netinet/in.h>
# include <sys/utsname.h>
# include <sys/socket.h>
*/
/*
# elif defined(Q_OS_HPUX)
# include <sys/pstat.h>
# elif defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_MAC)
# include <sys/sysctl.h>
# endif
#endif
*/
QT_BEGIN_NAMESPACE
/*
QThreadData
*/
QThreadData::QThreadData(int initialRefCount)
: _ref(initialRefCount), thread(0), threadId(0),
quitNow(false), loopLevel(0), eventDispatcher(0), canWait(true), isAdopted(false)
{
// fprintf(stderr, "QThreadData %p created\n", this);
}
QThreadData::~QThreadData()
{
Q_ASSERT(_ref == 0);
// In the odd case that Qt is running on a secondary thread, the main
// thread instance will have been dereffed asunder because of the deref in
// QThreadData::current() and the deref in the pthread_destroy. To avoid
// crashing during QCoreApplicationData's global static cleanup we need to
// safeguard the main thread here.. This fix is a bit crude, but it solves
// the problem...
if (this->thread == QCoreApplicationPrivate::theMainThread) {
QCoreApplicationPrivate::theMainThread = 0;
}
QThread *t = thread;
thread = 0;
delete t;
for (int i = 0; i < postEventList.size(); ++i) {
const QPostEvent &pe = postEventList.at(i);
if (pe.event) {
--pe.receiver->d_func()->postedEvents;
pe.event->posted = false;
delete pe.event;
}
}
// fprintf(stderr, "QThreadData %p destroyed\n", this);
}
void QThreadData::ref()
{
#ifndef QT_NO_THREAD
(void) _ref.ref();
Q_ASSERT(_ref != 0);
#endif
}
void QThreadData::deref()
{
#ifndef QT_NO_THREAD
if (!_ref.deref())
delete this;
#endif
}
/*
QAdoptedThread
*/
QAdoptedThread::QAdoptedThread(QThreadData *data)
: QThread(*new QThreadPrivate(data))
{
// thread should be running and not finished for the lifetime
// of the application (even if QCoreApplication goes away)
#ifndef QT_NO_THREAD
d_func()->running = true;
d_func()->finished = false;
init();
#endif
// fprintf(stderr, "new QAdoptedThread = %p\n", this);
}
QAdoptedThread::~QAdoptedThread()
{
// fprintf(stderr, "~QAdoptedThread = %p\n", this);
}
QThread *QAdoptedThread::createThreadForAdoption()
{
QScopedPointer<QThread> t(new QAdoptedThread(0));
t->moveToThread(t.data());
return t.take();
}
void QAdoptedThread::run()
{
// this function should never be called
qFatal("QAdoptedThread::run(): Internal error, this implementation should never be called.");
}
#ifndef QT_NO_THREAD
/*
QThreadPrivate
*/
QThreadPrivate::QThreadPrivate(QThreadData *d)
: QObjectPrivate(), running(false), finished(false), terminated(false),
isInFinish(false), exited(false), returnCode(-1),
stackSize(0), priority(QThread::InheritPriority), data(d)
{
#if defined (Q_OS_UNIX)
thread_id = 0;
#elif defined (Q_WS_WIN)
handle = 0;
id = 0;
waiters = 0;
#endif
#if defined (Q_WS_WIN) || defined (Q_OS_SYMBIAN)
terminationEnabled = true;
terminatePending = false;
#endif
if (!data)
data = new QThreadData;
}
QThreadPrivate::~QThreadPrivate()
{
data->deref();
}
/*!
\class QThread
\brief The QThread class provides a platform-independent way to
manage threads.
\ingroup thread
A QThread object manages one thread of control within the
program. To make code run in a separate thread, simply create a
QThread, change the thread affinity of the QObject(s) that
contain the code, and start() the new event loop. For example:
\snippet doc/src/snippets/code/src_corelib_thread_qthread.cpp 0
The code inside the Worker's slot would then execute in a
separate thread. In this example, the QThread triggers the
Worker's doWork() slot upon starting, and frees the Worker's
memory upon terminating. However, you are free to connect the
Worker's slots to any signal, from any object, in any thread. It
is safe to connect signals and slots across different threads,
thanks to a mechanism called \l{Qt::QueuedConnection}{queued
connections}.
\note If you interact with an object, using any technique other
than queued signal/slot connections (e.g. direct function calls),
then the usual multithreading precautions need to be taken.
\note It is not possible to change the thread affinity of GUI
objects; they must remain in the main thread.
\section1 Managing threads
QThread will notifiy you via a signal
when the thread is started(), finished(), and terminated(), or
you can use isFinished() and isRunning() to query the state of
the thread.
You can stop the thread by calling exit() or quit(). In extreme
cases, you may want to forcibly terminate() an executing thread.
However, doing so is dangerous and discouraged. Please read the
documentation for terminate() and setTerminationEnabled() for
detailed information.
From Qt 4.8 onwards, it is possible to deallocate objects that
live in a thread that has just ended, by connecting the
finished() signal to QObject::deleteLater().
Use wait() to block the calling thread, until the other thread
has finished execution (or until a specified time has passed).
The static functions currentThreadId() and currentThread() return
identifiers for the currently executing thread. The former
returns a platform specific ID for the thread; the latter returns
a QThread pointer.
To choose the name that your thread will be given (as identified
by the command \c{ps -L} on Linux, for example), you can call
\l{QObject::setObjectName()}{setObjectName()} before starting the thread.
If you don't call \l{QObject::setObjectName()}{setObjectName()},
the name given to your thread will be the class name of the runtime
type of your thread object (for example, \c "RenderThread" in the case of the
\l{Mandelbrot Example}, as that is the name of the QThread subclass).
Note that this is currently not available with release builds on Windows.
\section1 Subclassing QThread
Subclassing QThread is unnecessary for most purposes, since
QThread provides fully-functional thread management capabilities.
Nonetheless, QThread can be subclassed if you wish to implement
advanced thread management. This is done by adding new member
functions to the subclass, and/or by reimplementing run().
QThread's run() function is analogous to an application's main()
function -- it is executed when the thread is started, and the
thread will end when it returns.
\note Prior to Qt 4.4, the only way to use QThread for parallel
processing was to subclass it and implement the processing code
inside run(). This approach is now considered \b {bad practice};
a QThread should only manage a thread, not process data.
If you require event handling and signal/slot connections to
work in your thread, and if you reimplement run(), you must
explicitly call exec() at the end of your reimplementation:
\snippet doc/src/snippets/code/src_corelib_thread_qthread.cpp 1
QThread also provides static, platform independent sleep
functions: sleep(), msleep(), and usleep() allow full second,
millisecond, and microsecond resolution respectively.
\note wait() and the sleep() functions should be unnecessary in
general, since Qt is an event-driven framework. Instead of
wait(), consider listening for the finished() signal. Instead of
the sleep() functions, consider using QTimer.
It is important to remember that a QThread object usually lives
in the thread where it was created, not in the thread that it
manages. This oft-overlooked detail means that a QThread's slots
will be executed in the context of its home thread, not in the
context of the thread it is managing. For this reason,
implementing new slots in a QThread subclass is error-prone and
discouraged.
\sa {Thread Support in Qt}, QThreadStorage, QMutex, QSemaphore, QWaitCondition,
{Mandelbrot Example}, {Semaphores Example}, {Wait Conditions Example}
*/
/*!
\fn Qt::HANDLE QThread::currentThreadId()
Returns the thread handle of the currently executing thread.
\warning The handle returned by this function is used for internal
purposes and should not be used in any application code.
\warning On Windows, the returned value is a pseudo-handle for the
current thread. It can't be used for numerical comparison. i.e.,
this function returns the DWORD (Windows-Thread ID) returned by
the Win32 function getCurrentThreadId(), not the HANDLE
(Windows-Thread HANDLE) returned by the Win32 function
getCurrentThread().
*/
/*!
\fn int QThread::idealThreadCount()
Returns the ideal number of threads that can be run on the system. This is done querying
the number of processor cores, both real and logical, in the system. This function returns -1
if the number of processor cores could not be detected.
*/
/*!
\fn void QThread::yieldCurrentThread()
Yields execution of the current thread to another runnable thread,
if any. Note that the operating system decides to which thread to
switch.
*/
/*!
\fn void QThread::start(Priority priority)
Begins execution of the thread by calling run(). The
operating system will schedule the thread according to the \a
priority parameter. If the thread is already running, this
function does nothing.
The effect of the \a priority parameter is dependent on the
operating system's scheduling policy. In particular, the \a priority
will be ignored on systems that do not support thread priorities
(such as on Linux, see http://linux.die.net/man/2/sched_setscheduler
for more details).
\sa run(), terminate()
*/
/*!
\fn void QThread::started()
This signal is emitted when the thread starts executing.
\sa finished(), terminated()
*/
/*!
\fn void QThread::finished()
This signal is emitted when the thread has finished executing.
\sa started(), terminated()
*/
/*!
\fn void QThread::terminated()
This signal is emitted when the thread is terminated.
\sa started(), finished()
*/
/*!
\enum QThread::Priority
This enum type indicates how the operating system should schedule
newly created threads.
\value IdlePriority scheduled only when no other threads are
running.
\value LowestPriority scheduled less often than LowPriority.
\value LowPriority scheduled less often than NormalPriority.
\value NormalPriority the default priority of the operating
system.
\value HighPriority scheduled more often than NormalPriority.
\value HighestPriority scheduled more often than HighPriority.
\value TimeCriticalPriority scheduled as often as possible.
\value InheritPriority use the same priority as the creating
thread. This is the default.
*/
/*!
Returns a pointer to a QThread which manages the currently
executing thread.
*/
QThread *QThread::currentThread()
{
QThreadData *data = QThreadData::current();
Q_ASSERT(data != 0);
return data->thread;
}
/*!
Constructs a new QThread to manage a new thread. The \a parent
takes ownership of the QThread. The thread does not begin
executing until start() is called.
\sa start()
*/
QThread::QThread(QObject *parent)
: QObject(*(new QThreadPrivate), parent)
{
Q_D(QThread);
// fprintf(stderr, "QThreadData %p created for thread %p\n", d->data, this);
d->data->thread = this;
}
/*! \internal
*/
QThread::QThread(QThreadPrivate &dd, QObject *parent)
: QObject(dd, parent)
{
Q_D(QThread);
// fprintf(stderr, "QThreadData %p taken from private data for thread %p\n", d->data, this);
d->data->thread = this;
}
/*!
Destroys the QThread.
Note that deleting a QThread object will not stop the execution
of the thread it manages. Deleting a running QThread (i.e.
isFinished() returns false) will probably result in a program
crash. Wait for the finished() signal before deleting the
QThread.
*/
QThread::~QThread()
{
Q_D(QThread);
{
QMutexLocker locker(&d->mutex);
if (d->isInFinish) {
locker.unlock();
wait();
locker.relock();
}
if (d->running && !d->finished && !d->data->isAdopted)
qWarning("QThread: Destroyed while thread is still running");
d->data->thread = 0;
}
}
/*!
Returns true if the thread is finished; otherwise returns false.
\sa isRunning()
*/
bool QThread::isFinished() const
{
Q_D(const QThread);
QMutexLocker locker(&d->mutex);
return d->finished;
}
/*!
Returns true if the thread is running; otherwise returns false.
\sa isFinished()
*/
bool QThread::isRunning() const
{
Q_D(const QThread);
QMutexLocker locker(&d->mutex);
#ifdef Q_OS_SYMBIAN
// app shutdown on Symbian can terminate threads and invalidate their stacks without notification,
// check the thread is still alive.
if (d->data->symbian_thread_handle.Handle() && d->data->symbian_thread_handle.ExitType() != EExitPending)
return false;
#endif
return d->running;
}
/*!
Sets the maximum stack size for the thread to \a stackSize. If \a
stackSize is greater than zero, the maximum stack size is set to
\a stackSize bytes, otherwise the maximum stack size is
automatically determined by the operating system.
\warning Most operating systems place minimum and maximum limits
on thread stack sizes. The thread will fail to start if the stack
size is outside these limits.
\sa stackSize()
*/
void QThread::setStackSize(uint stackSize)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
Q_ASSERT_X(!d->running, "QThread::setStackSize",
"cannot change stack size while the thread is running");
d->stackSize = stackSize;
}
/*!
Returns the maximum stack size for the thread (if set with
setStackSize()); otherwise returns zero.
\sa setStackSize()
*/
uint QThread::stackSize() const
{
Q_D(const QThread);
QMutexLocker locker(&d->mutex);
return d->stackSize;
}
/*!
Enters the event loop and waits until exit() is called, returning the value
that was passed to exit(). The value returned is 0 if exit() is called via
quit().
It is necessary to call this function to start event handling.
\sa quit(), exit()
*/
int QThread::exec()
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
d->data->quitNow = false;
if (d->exited) {
d->exited = false;
return d->returnCode;
}
locker.unlock();
QEventLoop eventLoop;
int returnCode = eventLoop.exec();
locker.relock();
d->exited = false;
d->returnCode = -1;
return returnCode;
}
/*!
Tells the thread's event loop to exit with a return code.
After calling this function, the thread leaves the event loop and
returns from the call to QEventLoop::exec(). The
QEventLoop::exec() function returns \a returnCode.
By convention, a \a returnCode of 0 means success, any non-zero value
indicates an error.
Note that unlike the C library function of the same name, this
function \e does return to the caller -- it is event processing
that stops.
No QEventLoops will be started anymore in this thread until
QThread::exec() has been called again. If the eventloop in QThread::exec()
is not running then the next call to QThread::exec() will also return
immediately.
\sa quit() QEventLoop
*/
void QThread::exit(int returnCode)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
d->exited = true;
d->returnCode = returnCode;
d->data->quitNow = true;
for (int i = 0; i < d->data->eventLoops.size(); ++i) {
QEventLoop *eventLoop = d->data->eventLoops.at(i);
eventLoop->exit(returnCode);
}
}
/*!
Tells the thread's event loop to exit with return code 0 (success).
Equivalent to calling QThread::exit(0).
This function does nothing if the thread does not have an event
loop.
\sa exit() QEventLoop
*/
void QThread::quit()
{ exit(); }
/*!
The starting point for the thread. After calling start(), the
newly created thread calls this function. The default
implementation simply calls exec().
You can reimplement this function to facilitate advanced thread
management. Returning from this method will end the execution of
the thread.
\sa start() wait()
*/
void QThread::run()
{
(void) exec();
}
/*! \internal
Initializes the QThread system.
*/
#if defined (Q_OS_WIN)
void qt_create_tls();
#endif
void QThread::initialize()
{
if (qt_global_mutexpool)
return;
qt_global_mutexpool = QMutexPool::instance();
#if defined (Q_OS_WIN)
qt_create_tls();
#endif
}
/*! \internal
Cleans up the QThread system.
*/
void QThread::cleanup()
{
qt_global_mutexpool = 0;
}
/*!
\fn bool QThread::finished() const
Use isFinished() instead.
*/
/*!
\fn bool QThread::running() const
Use isRunning() instead.
*/
/*! \fn void QThread::setPriority(Priority priority)
\since 4.1
This function sets the \a priority for a running thread. If the
thread is not running, this function does nothing and returns
immediately. Use start() to start a thread with a specific
priority.
The \a priority argument can be any value in the \c
QThread::Priority enum except for \c InheritPriorty.
The effect of the \a priority parameter is dependent on the
operating system's scheduling policy. In particular, the \a priority
will be ignored on systems that do not support thread priorities
(such as on Linux, see http://linux.die.net/man/2/sched_setscheduler
for more details).
\sa Priority priority() start()
*/
/*!
\since 4.1
Returns the priority for a running thread. If the thread is not
running, this function returns \c InheritPriority.
\sa Priority setPriority() start()
*/
QThread::Priority QThread::priority() const
{
Q_D(const QThread);
QMutexLocker locker(&d->mutex);
// mask off the high bits that are used for flags
return Priority(d->priority & 0xffff);
}
/*!
\fn void QThread::sleep(unsigned long secs)
Forces the current thread to sleep for \a secs seconds.
\sa msleep(), usleep()
*/
/*!
\fn void QThread::msleep(unsigned long msecs)
Forces the current thread to sleep for \a msecs milliseconds.
\sa sleep(), usleep()
*/
/*!
\fn void QThread::usleep(unsigned long usecs)
Forces the current thread to sleep for \a usecs microseconds.
\sa sleep(), msleep()
*/
/*!
\fn void QThread::terminate()
Terminates the execution of the thread. The thread may or may not
be terminated immediately, depending on the operating system's
scheduling policies. Listen for the terminated() signal, or use
QThread::wait() after terminate(), to be sure.
When the thread is terminated, all threads waiting for the thread
to finish will be woken up.
\warning This function is dangerous and its use is discouraged.
The thread can be terminated at any point in its code path.
Threads can be terminated while modifying data. There is no
chance for the thread to clean up after itself, unlock any held
mutexes, etc. In short, use this function only if absolutely
necessary.
Termination can be explicitly enabled or disabled by calling
QThread::setTerminationEnabled(). Calling this function while
termination is disabled results in the termination being
deferred, until termination is re-enabled. See the documentation
of QThread::setTerminationEnabled() for more information.
\sa setTerminationEnabled()
*/
/*!
\fn bool QThread::wait(unsigned long time)
Blocks the thread until either of these conditions is met:
\list
\o The thread associated with this QThread object has finished
execution (i.e. when it returns from \l{run()}). This function
will return true if the thread has finished. It also returns
true if the thread has not been started yet.
\o \a time milliseconds has elapsed. If \a time is ULONG_MAX (the
default), then the wait will never timeout (the thread must
return from \l{run()}). This function will return false if the
wait timed out.
\endlist
This provides similar functionality to the POSIX \c
pthread_join() function.
\sa sleep(), terminate()
*/
/*!
\fn void QThread::setTerminationEnabled(bool enabled)
Enables or disables termination of the current thread based on the
\a enabled parameter. The thread must have been started by
QThread.
When \a enabled is false, termination is disabled. Future calls
to QThread::terminate() will return immediately without effect.
Instead, the termination is deferred until termination is enabled.
When \a enabled is true, termination is enabled. Future calls to
QThread::terminate() will terminate the thread normally. If
termination has been deferred (i.e. QThread::terminate() was
called with termination disabled), this function will terminate
the calling thread \e immediately. Note that this function will
not return in this case.
\sa terminate()
*/
#else // QT_NO_THREAD
QThread::QThread(QObject *parent)
: QObject(*(new QThreadPrivate), (QObject*)0){
Q_D(QThread);
d->data->thread = this;
}
QThread *QThread::currentThread()
{
return QThreadData::current()->thread;
}
QThreadData* QThreadData::current()
{
static QThreadData *data = 0; // reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key));
if (!data) {
QScopedPointer<QThreadData> newdata(new QThreadData);
newdata->thread = new QAdoptedThread(newdata.data());
data = newdata.take();
data->deref();
}
return data;
}
/*! \internal
*/
QThread::QThread(QThreadPrivate &dd, QObject *parent)
: QObject(dd, parent)
{
Q_D(QThread);
// fprintf(stderr, "QThreadData %p taken from private data for thread %p\n", d->data, this);
d->data->thread = this;
}
#endif // QT_NO_THREAD
QT_END_NAMESPACE
|