Qt 4.8
qthread_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QTHREAD_P_H
43 #define QTHREAD_P_H
44 
45 //
46 // W A R N I N G
47 // -------------
48 //
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 //
56 
57 #include "qplatformdefs.h"
58 #include "QtCore/qthread.h"
59 #include "QtCore/qmutex.h"
60 #include "QtCore/qstack.h"
61 #include "QtCore/qwaitcondition.h"
62 #include "QtCore/qmap.h"
63 #include "private/qobject_p.h"
64 
65 #ifdef Q_OS_SYMBIAN
66 #include <e32base.h>
67 #endif
68 
70 
72 class QEventLoop;
73 
75 {
76 public:
79  int priority;
80  inline QPostEvent()
81  : receiver(0), event(0), priority(0)
82  { }
83  inline QPostEvent(QObject *r, QEvent *e, int p)
84  : receiver(r), event(e), priority(p)
85  { }
86 };
87 inline bool operator<(int priority, const QPostEvent &pe)
88 {
89  return pe.priority < priority;
90 }
91 inline bool operator<(const QPostEvent &pe, int priority)
92 {
93  return priority < pe.priority;
94 }
95 
96 // This class holds the list of posted events.
97 // The list has to be kept sorted by priority
98 class QPostEventList : public QList<QPostEvent>
99 {
100 public:
101  // recursion == recursion count for sendPostedEvents()
103 
104  // sendOffset == the current event to start sending
106  // insertionOffset == set by sendPostedEvents to tell postEvent() where to start insertions
108 
110 
111  inline QPostEventList()
112  : QList<QPostEvent>(), recursion(0), startOffset(0), insertionOffset(0)
113  { }
114 
115  void addEvent(const QPostEvent &ev) {
116  int priority = ev.priority;
117  if (isEmpty() || last().priority >= priority) {
118  // optimization: we can simply append if the last event in
119  // the queue has higher or equal priority
120  append(ev);
121  } else {
122  // insert event in descending priority order, using upper
123  // bound for a given priority (to ensure proper ordering
124  // of events with the same priority)
125  QPostEventList::iterator at = qUpperBound(begin() + insertionOffset, end(), priority);
126  insert(at, ev);
127  }
128  }
129 private:
130  //hides because they do not keep that list sorted. addEvent must be used
133 };
134 
135 #ifndef QT_NO_THREAD
136 
138 {
140 
141 public:
143  ~QThreadPrivate();
144 
145  mutable QMutex mutex;
146 
147  bool running;
148  bool finished;
150  bool isInFinish; //when in QThreadPrivate::finish
151 
152  bool exited;
154 
157 
158  static QThread *threadForId(int id);
159 
160 #ifdef Q_OS_UNIX
161  pthread_t thread_id;
163 
164  static void *start(void *arg);
165 #if defined(Q_OS_SYMBIAN)
166  static void finish(void *arg, bool lockAnyway=true, bool closeNativeHandle=true);
167 #else
168  static void finish(void *);
169 #endif
170 
171 #endif // Q_OS_UNIX
172 
173 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
175  unsigned int id;
176  int waiters;
177 
178  static unsigned int __stdcall start(void *);
179  static void finish(void *, bool lockAnyway=true);
180 #endif // Q_OS_WIN32
181 
182 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) || defined (Q_OS_SYMBIAN)
183  bool terminationEnabled, terminatePending;
184 # endif
186 
187  static void createEventDispatcher(QThreadData *data);
188 };
189 
190 #else // QT_NO_THREAD
191 
192 class QThreadPrivate : public QObjectPrivate
193 {
194 public:
195  QThreadPrivate(QThreadData *d = 0) : data(d ? d : new QThreadData) {}
196  ~QThreadPrivate() { delete data; }
197 
198  QThreadData *data;
199 
200  static void setCurrentThread(QThread*) {}
201  static QThread *threadForId(int) { return QThread::currentThread(); }
202  static void createEventDispatcher(QThreadData *data);
203 
205 };
206 
207 #endif // QT_NO_THREAD
208 
210 {
212 
213 public:
214  QThreadData(int initialRefCount = 1);
215  ~QThreadData();
216 
217  static QThreadData *current();
218  static void clearCurrentThreadData();
219  static QThreadData *get2(QThread *thread)
220  { Q_ASSERT_X(thread != 0, "QThread", "internal error"); return thread->d_func()->data; }
221 
222 
223  void ref();
224  void deref();
225 
227  {
228  QMutexLocker locker(&postEventList.mutex);
229  return canWait;
230  }
231 
232  // This class provides per-thread (by way of being a QThreadData
233  // member) storage for qFlagLocation()
235  {
236  static const uint Count = 2;
237 
239  const char* locations[Count];
240 
241  public:
243  {
244  for (uint i = 0; i < Count; ++i)
245  locations[i] = 0;
246  }
247 
248  void store(const char* method)
249  { locations[idx++ % Count] = method; }
250 
251  bool contains(const char *method) const
252  {
253  for (uint i = 0; i < Count; ++i)
254  if (locations[i] == method)
255  return true;
256  return false;
257  }
258  };
259 
262  bool quitNow;
267  bool canWait;
269  bool isAdopted;
270 
271 # ifdef Q_OS_SYMBIAN
272  RThread symbian_thread_handle;
273 # endif
274 
276 };
277 
279 {
281 public:
283  : threadData(threadData)
284  { ++threadData->loopLevel; }
286  { --threadData->loopLevel; }
287 };
288 
289 // thread wrapper for the main() thread
290 class QAdoptedThread : public QThread
291 {
293 
294 public:
296  ~QAdoptedThread();
297  void init();
298 
299  static QThread *createThreadForAdoption();
300 private:
301  void run();
302 };
303 
305 
306 #endif // QTHREAD_P_H
bool canWaitLocked()
Definition: qthread_p.h:226
QPostEvent(QObject *r, QEvent *e, int p)
Definition: qthread_p.h:83
double d
Definition: qnumeric_p.h:62
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qUpperBound(RandomAccessIterator begin, RandomAccessIterator end, const T &value)
Definition: qalgorithms.h:262
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
int priority
Definition: qthread_p.h:79
pthread_t thread_id
Definition: qthread_p.h:161
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
#define at(className, varName)
QScopedLoopLevelCounter(QThreadData *threadData)
Definition: qthread_p.h:282
void addEvent(const QPostEvent &ev)
Definition: qthread_p.h:115
FlaggedDebugSignatures flaggedSignatures
Definition: qthread_p.h:275
The QStack class is a template class that provides a stack.
Definition: qcontainerfwd.h:63
bool canWait
Definition: qthread_p.h:267
QVector< void * > tls
Definition: qthread_p.h:268
bool isAdopted
Definition: qthread_p.h:269
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
Priority
This enum type indicates how the operating system should schedule newly created threads.
Definition: qthread.h:70
unsigned int id
Definition: qthread_p.h:175
static QThread * currentThread()
Returns a pointer to a QThread which manages the currently executing thread.
Definition: qthread.cpp:419
static QThreadData * get2(QThread *thread)
Definition: qthread_p.h:219
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int loopLevel
Definition: qthread_p.h:263
static bool isEmpty(const char *str)
The QEventLoop class provides a means of entering and leaving an event loop.
Definition: qeventloop.h:55
static bool init
QMutex mutex
Definition: qthread_p.h:109
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
QPostEventList postEventList
Definition: qthread_p.h:266
friend class iterator
Definition: qlist.h:226
void * HANDLE
Definition: qnamespace.h:1671
bool quitNow
Definition: qthread_p.h:262
QThreadData * data
Definition: qthread_p.h:185
QEvent * event
Definition: qthread_p.h:78
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
void store(const char *method)
Definition: qthread_p.h:248
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
bool operator<(int priority, const QPostEvent &pe)
Definition: qthread_p.h:87
QObject * receiver
Definition: qthread_p.h:77
QWaitCondition thread_done
Definition: qthread_p.h:162
int insertionOffset
Definition: qthread_p.h:107
QMutex mutex
Definition: qthread_p.h:145
QThreadData * threadData
Definition: qthread_p.h:280
QFuture< T > run(Function function,...)
QThread::Priority priority
Definition: qthread_p.h:156
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
QPostEvent()
Definition: qthread_p.h:80
QAtomicInt _ref
Definition: qthread_p.h:211
static const KeyPair *const end
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
QThread * thread
Definition: qthread_p.h:260
The QThread class provides a platform-independent way to manage threads.
Definition: qthread.h:59
Qt::HANDLE threadId
Definition: qthread_p.h:261
QAbstractEventDispatcher * eventDispatcher
Definition: qthread_p.h:264
bool contains(const char *method) const
Definition: qthread_p.h:251
bool terminationEnabled
Definition: qthread_p.h:183
The QAbstractEventDispatcher class provides an interface to manage Qt&#39;s event queue.
HANDLE handle
Definition: qthread_p.h:174
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QStack< QEventLoop * > eventLoops
Definition: qthread_p.h:265