Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qthread.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count |
---|---|---|
1 | - | |
2 | - | |
3 | - | |
4 | - | |
5 | - | |
6 | - | |
7 | - | |
8 | - | |
9 | - | |
10 | QThreadData::QThreadData(int initialRefCount) | - |
11 | : _ref(initialRefCount), loopLevel(0), scopeLevel(0), thread(0), threadId(0), | - |
12 | eventDispatcher(0), | - |
13 | quitNow(false), canWait(true), isAdopted(false), requiresCoreApplication(true) | - |
14 | { | - |
15 | - | |
16 | } executed 582058 times by 680 tests: end of block Executed by:
| 582058 |
17 | - | |
18 | QThreadData::~QThreadData() | - |
19 | { | - |
20 | ((!(_ref.load() == 0)) ? qt_assert("_ref.load() == 0",__FILE__,6168) : qt_noop()); | - |
21 | - | |
22 | - | |
23 | - | |
24 | - | |
25 | - | |
26 | - | |
27 | - | |
28 | if (this->thread == QCoreApplicationPrivate::theMainThread) { | - |
29 | QCoreApplicationPrivate::theMainThread = 0; | - |
30 | QThreadData::clearCurrentThreadData(); | - |
31 | } | - |
32 | - | |
33 | QThread *t = thread; | - |
34 | thread = 0; | - |
35 | delete t; | - |
36 | - | |
37 | for (int i = 0; i < postEventList.size(); ++i) { | - |
38 | const QPostEvent &pe = postEventList.at(i); | - |
39 | if (pe.event) { | - |
40 | --pe.receiver->d_func()->postedEvents; | - |
41 | pe.event->posted = false; | - |
42 | delete pe.event; | - |
43 | } | - |
44 | } | - |
45 | - | |
46 | - | |
47 | } | - |
48 | - | |
49 | void QThreadData::ref() | - |
50 | { | - |
51 | - | |
52 | (void) _ref.ref(); | - |
53 | ((!(_ref.load() != 0)) ? qt_assert("_ref.load() != 0",__FILE__,94101) : qt_noop()); | - |
54 | - | |
55 | } | - |
56 | - | |
57 | void QThreadData::deref() | - |
58 | { | - |
59 | - | |
60 | if (!_ref.deref()) | - |
61 | delete this; | - |
62 | - | |
63 | } | - |
64 | - | |
65 | - | |
66 | - | |
67 | - | |
68 | - | |
69 | QAdoptedThread::QAdoptedThread(QThreadData *data) | - |
70 | : QThread(*new QThreadPrivate(data)) | - |
71 | { | - |
72 | - | |
73 | - | |
74 | - | |
75 | d_func()->running = true; | - |
76 | d_func()->finished = false; | - |
77 | init(); | - |
78 | - | |
79 | - | |
80 | - | |
81 | } | - |
82 | - | |
83 | QAdoptedThread::~QAdoptedThread() | - |
84 | { | - |
85 | - | |
86 | } | - |
87 | - | |
88 | void QAdoptedThread::run() | - |
89 | { | - |
90 | - | |
91 | QMessageLogger(__FILE__, 132139, __PRETTY_FUNCTION__).fatal("QAdoptedThread::run(): Internal error, this implementation should never be called."); | - |
92 | } | - |
93 | - | |
94 | - | |
95 | - | |
96 | - | |
97 | - | |
98 | QThreadPrivate::QThreadPrivate(QThreadData *d) | - |
99 | : QObjectPrivate(), running(false), finished(false), | - |
100 | isInFinish(false), interruptionRequested(false), | - |
101 | exited(false), returnCode(-1), | - |
102 | stackSize(0), priority(QThread::InheritPriority), data(d) | - |
103 | { | - |
104 | if (!data) | - |
105 | data = new QThreadData; | - |
106 | } | - |
107 | - | |
108 | QThreadPrivate::~QThreadPrivate() | - |
109 | { | - |
110 | data->deref(); | - |
111 | } | - |
112 | QThread *QThread::currentThread() | - |
113 | { | - |
114 | QThreadData *data = QThreadData::current(); | - |
115 | ((!(data != 0)) ? qt_assert("data != 0",__FILE__,364371) : qt_noop()); | - |
116 | return data->thread; | - |
117 | } | - |
118 | QThread::QThread(QObject *parent) | - |
119 | : QObject(*(new QThreadPrivate), parent) | - |
120 | { | - |
121 | QThreadPrivate * const d = d_func(); | - |
122 | - | |
123 | d->data->thread = this; | - |
124 | } | - |
125 | - | |
126 | - | |
127 | - | |
128 | - | |
129 | QThread::QThread(QThreadPrivate &dd, QObject *parent) | - |
130 | : QObject(dd, parent) | - |
131 | { | - |
132 | QThreadPrivate * const d = d_func(); | - |
133 | - | |
134 | d->data->thread = this; | - |
135 | } | - |
136 | QThread::~QThread() | - |
137 | { | - |
138 | QThreadPrivate * const d = d_func(); | - |
139 | { | - |
140 | QMutexLocker locker(&d->mutex); | - |
141 | if (d->isInFinish) { | - |
142 | locker.unlock(); | - |
143 | wait(); | - |
144 | locker.relock(); | - |
145 | } | - |
146 | if (d->running && !d->finished && !d->data->isAdopted) | - |
147 | QMessageLogger(__FILE__, 414421, __PRETTY_FUNCTION__).warning("QThread: Destroyed while thread is still running"); | - |
148 | - | |
149 | d->data->thread = 0; | - |
150 | } | - |
151 | } | - |
152 | - | |
153 | - | |
154 | - | |
155 | - | |
156 | - | |
157 | - | |
158 | bool QThread::isFinished() const | - |
159 | { | - |
160 | const QThreadPrivate * const d = d_func(); | - |
161 | QMutexLocker locker(&d->mutex); | - |
162 | return d->finished || d->isInFinish; | - |
163 | } | - |
164 | - | |
165 | - | |
166 | - | |
167 | - | |
168 | - | |
169 | - | |
170 | bool QThread::isRunning() const | - |
171 | { | - |
172 | const QThreadPrivate * const d = d_func(); | - |
173 | QMutexLocker locker(&d->mutex); | - |
174 | return d->running && !d->isInFinish; | - |
175 | } | - |
176 | void QThread::setStackSize(uint stackSize) | - |
177 | { | - |
178 | QThreadPrivate * const d = d_func(); | - |
179 | QMutexLocker locker(&d->mutex); | - |
180 | ((!(!d->running)) ? qt_assert_x("QThread::setStackSize", "cannot change stack size while the thread is running", | - |
181 | __FILE__ | - |
182 | , | - |
183 | 461468 | - |
184 | ) : qt_noop()) | - |
185 | ; | - |
186 | d->stackSize = stackSize; | - |
187 | } | - |
188 | - | |
189 | - | |
190 | - | |
191 | - | |
192 | - | |
193 | - | |
194 | - | |
195 | uint QThread::stackSize() const | - |
196 | { | - |
197 | const QThreadPrivate * const d = d_func(); | - |
198 | QMutexLocker locker(&d->mutex); | - |
199 | return d->stackSize; | - |
200 | } | - |
201 | int QThread::exec() | - |
202 | { | - |
203 | QThreadPrivate * const d = d_func(); | - |
204 | QMutexLocker locker(&d->mutex); | - |
205 | d->data->quitNow = false; | - |
206 | if (d->exited) { | - |
207 | d->exited = false; | - |
208 | return d->returnCode; | - |
209 | } | - |
210 | locker.unlock(); | - |
211 | - | |
212 | QEventLoop eventLoop; | - |
213 | int returnCode = eventLoop.exec(); | - |
214 | - | |
215 | locker.relock(); | - |
216 | d->exited = false; | - |
217 | d->returnCode = -1; | - |
218 | return returnCode; | - |
219 | } | - |
220 | void QThread::exit(int returnCode) | - |
221 | { | - |
222 | QThreadPrivate * const d = d_func(); | - |
223 | QMutexLocker locker(&d->mutex); | - |
224 | d->exited = true; | - |
225 | d->returnCode = returnCode; | - |
226 | d->data->quitNow = true; | - |
227 | for (int i = 0; i < d->data->eventLoops.size(); ++i) { | - |
228 | QEventLoop *eventLoop = d->data->eventLoops.at(i); | - |
229 | eventLoop->exit(returnCode); | - |
230 | } | - |
231 | } | - |
232 | void QThread::quit() | - |
233 | { exit(); } | - |
234 | void QThread::run() | - |
235 | { | - |
236 | (void) exec(); | - |
237 | } | - |
238 | void QThread::setPriority(Priority priority) | - |
239 | { | - |
240 | QThreadPrivate * const d = d_func(); | - |
241 | QMutexLocker locker(&d->mutex); | - |
242 | if (!d->running) { | - |
243 | QMessageLogger(__FILE__, 594601, __PRETTY_FUNCTION__).warning("QThread::setPriority: Cannot set priority, thread is not running"); | - |
244 | return; | - |
245 | } | - |
246 | d->setPriority(priority); | - |
247 | } | - |
248 | QThread::Priority QThread::priority() const | - |
249 | { | - |
250 | const QThreadPrivate * const d = d_func(); | - |
251 | QMutexLocker locker(&d->mutex); | - |
252 | - | |
253 | - | |
254 | return Priority(d->priority & 0xffff); | - |
255 | } | - |
256 | int QThread::loopLevel() const | - |
257 | { | - |
258 | const QThreadPrivate * const d = d_func(); | - |
259 | return d->data->eventLoops.size(); | - |
260 | } | - |
261 | QAbstractEventDispatcher *QThread::eventDispatcher() const | - |
262 | { | - |
263 | const QThreadPrivate * const d = d_func(); | - |
264 | return d->data->eventDispatcher.load(); | - |
265 | } | - |
266 | void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher) | - |
267 | { | - |
268 | QThreadPrivate * const d = d_func(); | - |
269 | if (d->data->hasEventDispatcher()) { | - |
270 | QMessageLogger(__FILE__, 788795, __PRETTY_FUNCTION__).warning("QThread::setEventDispatcher: An event dispatcher has already been created for this thread"); | - |
271 | } else { | - |
272 | eventDispatcher->moveToThread(this); | - |
273 | if (eventDispatcher->thread() == this) | - |
274 | d->data->eventDispatcher = eventDispatcher; | - |
275 | else | - |
276 | QMessageLogger(__FILE__, 794801, __PRETTY_FUNCTION__).warning("QThread::setEventDispatcher: Could not move event dispatcher to target thread"); | - |
277 | } | - |
278 | } | - |
279 | - | |
280 | - | |
281 | - | |
282 | - | |
283 | bool QThread::event(QEvent *event) | - |
284 | { | - |
285 | if (event->type() == QEvent::Quit) { | - |
286 | quit(); | - |
287 | return true; | - |
288 | } else { | - |
289 | return QObject::event(event); | - |
290 | } | - |
291 | } | - |
292 | void QThread::requestInterruption() | - |
293 | { | - |
294 | QThreadPrivate * const d = d_func(); | - |
295 | QMutexLocker locker(&d->mutex); | - |
296 | if (!d->running || d->finished || d->isInFinish) | - |
297 | return; | - |
298 | if (this == QCoreApplicationPrivate::theMainThread) { | - |
299 | QMessageLogger(__FILE__, 830837, __PRETTY_FUNCTION__).warning("QThread::requestInterruption has no effect on the main thread"); | - |
300 | return; | - |
301 | } | - |
302 | d->interruptionRequested = true; | - |
303 | } | - |
304 | bool QThread::isInterruptionRequested() const | - |
305 | { | - |
306 | const QThreadPrivate * const d = d_func(); | - |
307 | QMutexLocker locker(&d->mutex); | - |
308 | if (!d->running || d->finished || d->isInFinish) | - |
309 | return false; | - |
310 | return d->interruptionRequested; | - |
} | ||
static void setThreadDoesNotRequireCoreApplication() | ||
{ | ||
QThreadData::current()->requiresCoreApplication = false; | ||
311 | } | - |
312 | QDaemonThread::QDaemonThread(QObject *parent) | - |
313 | : QThread(parent) | - |
314 | { | - |
315 | - | |
316 | connect(this, &QThread::started, | - |
317 | setThreadDoesNotRequireCoreApplication);[](){ QThreadData::current()->requiresCoreApplication = false; } executed 189 times by 161 tests: );end of block Executed by:
| 189 |
318 | } executed 189 times by 161 tests: end of block Executed by:
| 189 |
319 | - | |
320 | QDaemonThread::~QDaemonThread() | - |
321 | { | - |
322 | } | - |
323 | - | |
324 | - | |
Switch to Source code | Preprocessed file |