|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtCore module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
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 |
|
69 QT_BEGIN_NAMESPACE |
|
70 |
|
71 class QAbstractEventDispatcher; |
|
72 class QEventLoop; |
|
73 |
|
74 class QPostEvent |
|
75 { |
|
76 public: |
|
77 QObject *receiver; |
|
78 QEvent *event; |
|
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 class QPostEventList : public QList<QPostEvent> |
|
97 { |
|
98 public: |
|
99 // recursion == recursion count for sendPostedEvents() |
|
100 int recursion; |
|
101 |
|
102 // sendOffset == the current event to start sending |
|
103 int startOffset; |
|
104 // insertionOffset == set by sendPostedEvents to tell postEvent() where to start insertions |
|
105 int insertionOffset; |
|
106 |
|
107 QMutex mutex; |
|
108 |
|
109 inline QPostEventList() |
|
110 : QList<QPostEvent>(), recursion(0), startOffset(0), insertionOffset(0) |
|
111 { } |
|
112 }; |
|
113 |
|
114 #ifndef QT_NO_THREAD |
|
115 class QThreadPrivate : public QObjectPrivate |
|
116 { |
|
117 Q_DECLARE_PUBLIC(QThread) |
|
118 |
|
119 public: |
|
120 QThreadPrivate(QThreadData *d = 0); |
|
121 ~QThreadPrivate(); |
|
122 |
|
123 mutable QMutex mutex; |
|
124 |
|
125 bool running; |
|
126 bool finished; |
|
127 bool terminated; |
|
128 |
|
129 uint stackSize; |
|
130 QThread::Priority priority; |
|
131 |
|
132 static QThread *threadForId(int id); |
|
133 |
|
134 #ifdef Q_OS_UNIX |
|
135 pthread_t thread_id; |
|
136 QWaitCondition thread_done; |
|
137 |
|
138 static void *start(void *arg); |
|
139 #if defined(Q_OS_SYMBIAN) |
|
140 static void finish(void *arg, bool lockAnyway=true, bool closeNativeHandle=true); |
|
141 #else |
|
142 static void finish(void *); |
|
143 #endif |
|
144 #endif // Q_OS_UNIX |
|
145 |
|
146 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) |
|
147 HANDLE handle; |
|
148 unsigned int id; |
|
149 int waiters; |
|
150 |
|
151 static unsigned int __stdcall start(void *); |
|
152 static void finish(void *, bool lockAnyway=true); |
|
153 #endif // Q_OS_WIN32 |
|
154 |
|
155 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) || defined (Q_OS_SYMBIAN) |
|
156 bool terminationEnabled, terminatePending; |
|
157 # endif |
|
158 QThreadData *data; |
|
159 |
|
160 static void createEventDispatcher(QThreadData *data); |
|
161 }; |
|
162 |
|
163 #else // QT_NO_THREAD |
|
164 |
|
165 class QThreadPrivate : public QObjectPrivate |
|
166 { |
|
167 public: |
|
168 QThreadPrivate(QThreadData *d = 0) : data(d ? d : new QThreadData) {} |
|
169 ~QThreadPrivate() { delete data; } |
|
170 |
|
171 QThreadData *data; |
|
172 |
|
173 static void setCurrentThread(QThread*) {} |
|
174 static QThread *threadForId(int) { return QThread::currentThread(); } |
|
175 static void createEventDispatcher(QThreadData *data); |
|
176 |
|
177 Q_DECLARE_PUBLIC(QThread) |
|
178 }; |
|
179 |
|
180 #endif // QT_NO_THREAD |
|
181 |
|
182 class QThreadData |
|
183 { |
|
184 QAtomicInt _ref; |
|
185 |
|
186 public: |
|
187 QThreadData(int initialRefCount = 1); |
|
188 ~QThreadData(); |
|
189 |
|
190 static QThreadData *current(); |
|
191 static QThreadData *get2(QThread *thread) |
|
192 { Q_ASSERT_X(thread != 0, "QThread", "internal error"); return thread->d_func()->data; } |
|
193 |
|
194 |
|
195 void ref(); |
|
196 void deref(); |
|
197 |
|
198 QThread *thread; |
|
199 bool quitNow; |
|
200 int loopLevel; |
|
201 QAbstractEventDispatcher *eventDispatcher; |
|
202 QStack<QEventLoop *> eventLoops; |
|
203 QPostEventList postEventList; |
|
204 bool canWait; |
|
205 QMap<int, void *> tls; |
|
206 |
|
207 QMutex mutex; |
|
208 |
|
209 # ifdef Q_OS_SYMBIAN |
|
210 RThread symbian_thread_handle; |
|
211 # endif |
|
212 }; |
|
213 |
|
214 // thread wrapper for the main() thread |
|
215 class QAdoptedThread : public QThread |
|
216 { |
|
217 Q_DECLARE_PRIVATE(QThread) |
|
218 |
|
219 public: |
|
220 QAdoptedThread(QThreadData *data = 0); |
|
221 ~QAdoptedThread(); |
|
222 void init(); |
|
223 |
|
224 static QThread *createThreadForAdoption(); |
|
225 private: |
|
226 void run(); |
|
227 }; |
|
228 |
|
229 QT_END_NAMESPACE |
|
230 |
|
231 #endif // QTHREAD_P_H |