author | eckhart.koppen@nokia.com |
Wed, 31 Mar 2010 11:06:36 +0300 | |
changeset 7 | f7bc934e204c |
parent 3 | 41300fa6a67c |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
#include "qthread.h" |
|
43 |
||
44 |
#include "qplatformdefs.h" |
|
45 |
||
46 |
#include <private/qcoreapplication_p.h> |
|
47 |
#if !defined(QT_NO_GLIB) |
|
48 |
# include "../kernel/qeventdispatcher_glib_p.h" |
|
49 |
#endif |
|
50 |
||
51 |
#ifdef Q_OS_SYMBIAN |
|
52 |
#include <private/qeventdispatcher_symbian_p.h> |
|
53 |
#else |
|
54 |
#include <private/qeventdispatcher_unix_p.h> |
|
55 |
#endif |
|
56 |
||
57 |
#include "qthreadstorage.h" |
|
58 |
||
59 |
#include "qthread_p.h" |
|
60 |
||
61 |
#include "qdebug.h" |
|
62 |
||
63 |
#include <sched.h> |
|
64 |
#include <errno.h> |
|
65 |
||
66 |
#ifdef Q_OS_BSD4 |
|
67 |
#include <sys/sysctl.h> |
|
68 |
#endif |
|
69 |
#ifdef Q_OS_VXWORKS |
|
70 |
# if (_WRS_VXWORKS_MAJOR > 6) || ((_WRS_VXWORKS_MAJOR == 6) && (_WRS_VXWORKS_MINOR >= 6)) |
|
71 |
# include <vxCpuLib.h> |
|
72 |
# include <cpuset.h> |
|
73 |
# define QT_VXWORKS_HAS_CPUSET |
|
74 |
# endif |
|
75 |
#endif |
|
76 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
#ifdef Q_OS_HPUX |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
78 |
#include <sys/pstat.h> |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
79 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
80 |
|
0 | 81 |
#if defined(Q_OS_MAC) |
82 |
# ifdef qDebug |
|
83 |
# define old_qDebug qDebug |
|
84 |
# undef qDebug |
|
85 |
# endif |
|
86 |
# include <CoreServices/CoreServices.h> |
|
87 |
||
88 |
# ifdef old_qDebug |
|
89 |
# undef qDebug |
|
90 |
# define qDebug QT_NO_QDEBUG_MACRO |
|
91 |
# undef old_qDebug |
|
92 |
# endif |
|
93 |
#endif |
|
94 |
||
95 |
QT_BEGIN_NAMESPACE |
|
96 |
||
97 |
#ifndef QT_NO_THREAD |
|
98 |
||
99 |
static pthread_once_t current_thread_data_once = PTHREAD_ONCE_INIT; |
|
100 |
static pthread_key_t current_thread_data_key; |
|
101 |
||
102 |
static void destroy_current_thread_data(void *p) |
|
103 |
{ |
|
104 |
#if defined(Q_OS_VXWORKS) |
|
105 |
// Calling setspecific(..., 0) sets the value to 0 for ALL threads. |
|
106 |
// The 'set to 1' workaround adds a bit of an overhead though, |
|
107 |
// since this function is called twice now. |
|
108 |
if (p == (void *)1) |
|
109 |
return; |
|
110 |
#endif |
|
111 |
// POSIX says the value in our key is set to zero before calling |
|
112 |
// this destructor function, so we need to set it back to the |
|
113 |
// right value... |
|
114 |
pthread_setspecific(current_thread_data_key, p); |
|
115 |
reinterpret_cast<QThreadData *>(p)->deref(); |
|
116 |
// ... but we must reset it to zero before returning so we aren't |
|
117 |
// called again (POSIX allows implementations to call destructor |
|
118 |
// functions repeatedly until all values are zero) |
|
119 |
pthread_setspecific(current_thread_data_key, |
|
120 |
#if defined(Q_OS_VXWORKS) |
|
121 |
(void *)1); |
|
122 |
#else |
|
123 |
0); |
|
124 |
#endif |
|
125 |
} |
|
126 |
||
127 |
static void create_current_thread_data_key() |
|
128 |
{ |
|
129 |
pthread_key_create(¤t_thread_data_key, destroy_current_thread_data); |
|
130 |
} |
|
131 |
||
132 |
QThreadData *QThreadData::current() |
|
133 |
{ |
|
134 |
pthread_once(¤t_thread_data_once, create_current_thread_data_key); |
|
135 |
||
136 |
QThreadData *data = reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key)); |
|
137 |
if (!data) { |
|
138 |
void *a; |
|
139 |
if (QInternal::activateCallbacks(QInternal::AdoptCurrentThread, &a)) { |
|
140 |
QThread *adopted = static_cast<QThread*>(a); |
|
141 |
Q_ASSERT(adopted); |
|
142 |
data = QThreadData::get2(adopted); |
|
143 |
pthread_setspecific(current_thread_data_key, data); |
|
144 |
adopted->d_func()->running = true; |
|
145 |
adopted->d_func()->finished = false; |
|
146 |
static_cast<QAdoptedThread *>(adopted)->init(); |
|
147 |
} else { |
|
148 |
data = new QThreadData; |
|
149 |
pthread_setspecific(current_thread_data_key, data); |
|
150 |
QT_TRY { |
|
151 |
data->thread = new QAdoptedThread(data); |
|
152 |
} QT_CATCH(...) { |
|
153 |
pthread_setspecific(current_thread_data_key, 0); |
|
154 |
data->deref(); |
|
155 |
data = 0; |
|
156 |
QT_RETHROW; |
|
157 |
} |
|
158 |
data->deref(); |
|
159 |
} |
|
160 |
if (!QCoreApplicationPrivate::theMainThread) |
|
161 |
QCoreApplicationPrivate::theMainThread = data->thread; |
|
162 |
} |
|
163 |
return data; |
|
164 |
} |
|
165 |
||
166 |
||
167 |
void QAdoptedThread::init() |
|
168 |
{ |
|
169 |
Q_D(QThread); |
|
170 |
d->thread_id = pthread_self(); |
|
171 |
#ifdef Q_OS_SYMBIAN |
|
172 |
d->data->symbian_thread_handle = RThread(); |
|
173 |
TThreadId threadId = d->data->symbian_thread_handle.Id(); |
|
174 |
d->data->symbian_thread_handle.Open(threadId); |
|
175 |
#endif |
|
176 |
} |
|
177 |
||
178 |
/* |
|
179 |
QThreadPrivate |
|
180 |
*/ |
|
181 |
||
182 |
#if defined(Q_C_CALLBACKS) |
|
183 |
extern "C" { |
|
184 |
#endif |
|
185 |
||
186 |
typedef void*(*QtThreadCallback)(void*); |
|
187 |
||
188 |
#if defined(Q_C_CALLBACKS) |
|
189 |
} |
|
190 |
#endif |
|
191 |
||
192 |
#endif // QT_NO_THREAD |
|
193 |
||
194 |
void QThreadPrivate::createEventDispatcher(QThreadData *data) |
|
195 |
{ |
|
196 |
#if !defined(QT_NO_GLIB) |
|
197 |
if (qgetenv("QT_NO_GLIB").isEmpty() |
|
198 |
&& qgetenv("QT_NO_THREADED_GLIB").isEmpty() |
|
199 |
&& QEventDispatcherGlib::versionSupported()) |
|
200 |
data->eventDispatcher = new QEventDispatcherGlib; |
|
201 |
else |
|
202 |
#endif |
|
203 |
#ifdef Q_OS_SYMBIAN |
|
204 |
data->eventDispatcher = new QEventDispatcherSymbian; |
|
205 |
#else |
|
206 |
data->eventDispatcher = new QEventDispatcherUNIX; |
|
207 |
#endif |
|
208 |
data->eventDispatcher->startingUp(); |
|
209 |
} |
|
210 |
||
211 |
#ifndef QT_NO_THREAD |
|
212 |
||
213 |
void *QThreadPrivate::start(void *arg) |
|
214 |
{ |
|
215 |
// Symbian Open C supports neither thread cancellation nor cleanup_push. |
|
216 |
#ifndef Q_OS_SYMBIAN |
|
217 |
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); |
|
218 |
pthread_cleanup_push(QThreadPrivate::finish, arg); |
|
219 |
#endif |
|
220 |
||
221 |
QThread *thr = reinterpret_cast<QThread *>(arg); |
|
222 |
QThreadData *data = QThreadData::get2(thr); |
|
223 |
||
224 |
#ifdef Q_OS_SYMBIAN |
|
225 |
// Because Symbian Open C does not provide a way to convert between |
|
226 |
// RThread and pthread_t, we must delay initialization of the RThread |
|
227 |
// handle when creating a thread, until we are running in the new thread. |
|
228 |
// Here, we pick up the current thread and assign that to the handle. |
|
229 |
data->symbian_thread_handle = RThread(); |
|
230 |
TThreadId threadId = data->symbian_thread_handle.Id(); |
|
231 |
data->symbian_thread_handle.Open(threadId); |
|
232 |
#endif |
|
233 |
||
234 |
pthread_once(¤t_thread_data_once, create_current_thread_data_key); |
|
235 |
pthread_setspecific(current_thread_data_key, data); |
|
236 |
||
237 |
data->ref(); |
|
238 |
data->quitNow = false; |
|
239 |
||
240 |
// ### TODO: allow the user to create a custom event dispatcher |
|
241 |
createEventDispatcher(data); |
|
242 |
||
243 |
emit thr->started(); |
|
244 |
#ifndef Q_OS_SYMBIAN |
|
245 |
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); |
|
246 |
pthread_testcancel(); |
|
247 |
#endif |
|
248 |
thr->run(); |
|
249 |
||
250 |
#ifdef Q_OS_SYMBIAN |
|
251 |
QThreadPrivate::finish(arg); |
|
252 |
#else |
|
253 |
pthread_cleanup_pop(1); |
|
254 |
#endif |
|
255 |
||
256 |
return 0; |
|
257 |
} |
|
258 |
||
259 |
#ifdef Q_OS_SYMBIAN |
|
260 |
void QThreadPrivate::finish(void *arg, bool lockAnyway, bool closeNativeHandle) |
|
261 |
#else |
|
262 |
void QThreadPrivate::finish(void *arg) |
|
263 |
#endif |
|
264 |
{ |
|
265 |
QThread *thr = reinterpret_cast<QThread *>(arg); |
|
266 |
QThreadPrivate *d = thr->d_func(); |
|
267 |
#ifdef Q_OS_SYMBIAN |
|
268 |
if (lockAnyway) |
|
269 |
#endif |
|
270 |
d->mutex.lock(); |
|
271 |
||
272 |
d->priority = QThread::InheritPriority; |
|
273 |
d->running = false; |
|
274 |
d->finished = true; |
|
275 |
if (d->terminated) |
|
276 |
emit thr->terminated(); |
|
277 |
d->terminated = false; |
|
278 |
emit thr->finished(); |
|
279 |
||
280 |
if (d->data->eventDispatcher) { |
|
281 |
d->data->eventDispatcher->closingDown(); |
|
282 |
QAbstractEventDispatcher *eventDispatcher = d->data->eventDispatcher; |
|
283 |
d->data->eventDispatcher = 0; |
|
284 |
delete eventDispatcher; |
|
285 |
} |
|
286 |
||
287 |
void *data = &d->data->tls; |
|
288 |
QThreadStorageData::finish((void **)data); |
|
289 |
||
290 |
d->thread_id = 0; |
|
291 |
#ifdef Q_OS_SYMBIAN |
|
292 |
if (closeNativeHandle) |
|
293 |
d->data->symbian_thread_handle.Close(); |
|
294 |
#endif |
|
295 |
d->thread_done.wakeAll(); |
|
296 |
#ifdef Q_OS_SYMBIAN |
|
297 |
if (lockAnyway) |
|
298 |
#endif |
|
299 |
d->mutex.unlock(); |
|
300 |
} |
|
301 |
||
302 |
||
303 |
||
304 |
||
305 |
/************************************************************************** |
|
306 |
** QThread |
|
307 |
*************************************************************************/ |
|
308 |
||
309 |
Qt::HANDLE QThread::currentThreadId() |
|
310 |
{ |
|
311 |
// requires a C cast here otherwise we run into trouble on AIX |
|
312 |
return (Qt::HANDLE)pthread_self(); |
|
313 |
} |
|
314 |
||
315 |
#if defined(QT_LINUXBASE) && !defined(_SC_NPROCESSORS_ONLN) |
|
316 |
// LSB doesn't define _SC_NPROCESSORS_ONLN. |
|
317 |
# define _SC_NPROCESSORS_ONLN 84 |
|
318 |
#endif |
|
319 |
||
320 |
int QThread::idealThreadCount() |
|
321 |
{ |
|
322 |
int cores = -1; |
|
323 |
||
324 |
#if defined(Q_OS_MAC) |
|
325 |
// Mac OS X |
|
326 |
cores = MPProcessorsScheduled(); |
|
327 |
#elif defined(Q_OS_HPUX) |
|
328 |
// HP-UX |
|
329 |
struct pst_dynamic psd; |
|
330 |
if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) == -1) { |
|
331 |
perror("pstat_getdynamic"); |
|
332 |
cores = -1; |
|
333 |
} else { |
|
334 |
cores = (int)psd.psd_proc_cnt; |
|
335 |
} |
|
336 |
#elif defined(Q_OS_BSD4) |
|
337 |
// FreeBSD, OpenBSD, NetBSD, BSD/OS |
|
338 |
size_t len = sizeof(cores); |
|
339 |
int mib[2]; |
|
340 |
mib[0] = CTL_HW; |
|
341 |
mib[1] = HW_NCPU; |
|
342 |
if (sysctl(mib, 2, &cores, &len, NULL, 0) != 0) { |
|
343 |
perror("sysctl"); |
|
344 |
cores = -1; |
|
345 |
} |
|
346 |
#elif defined(Q_OS_IRIX) |
|
347 |
// IRIX |
|
348 |
cores = (int)sysconf(_SC_NPROC_ONLN); |
|
349 |
#elif defined(Q_OS_INTEGRITY) |
|
350 |
// as of aug 2008 Integrity only supports one single core CPU |
|
351 |
cores = 1; |
|
352 |
#elif defined(Q_OS_SYMBIAN) |
|
353 |
// ### TODO - Get the number of cores from HAL? when multicore architectures (SMP) are supported |
|
354 |
cores = 1; |
|
355 |
#elif defined(Q_OS_VXWORKS) |
|
356 |
// VxWorks |
|
357 |
# if defined(QT_VXWORKS_HAS_CPUSET) |
|
358 |
cpuset_t cpus = vxCpuEnabledGet(); |
|
359 |
cores = 0; |
|
360 |
||
361 |
// 128 cores should be enough for everyone ;) |
|
362 |
for (int i = 0; i < 128 && !CPUSET_ISZERO(cpus); ++i) { |
|
363 |
if (CPUSET_ISSET(cpus, i)) { |
|
364 |
CPUSET_CLR(cpus, i); |
|
365 |
cores++; |
|
366 |
} |
|
367 |
} |
|
368 |
# else |
|
369 |
// as of aug 2008 VxWorks < 6.6 only supports one single core CPU |
|
370 |
cores = 1; |
|
371 |
# endif |
|
372 |
#else |
|
373 |
// the rest: Linux, Solaris, AIX, Tru64 |
|
374 |
cores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
|
375 |
#endif |
|
376 |
||
377 |
return cores; |
|
378 |
} |
|
379 |
||
380 |
void QThread::yieldCurrentThread() |
|
381 |
{ |
|
382 |
sched_yield(); |
|
383 |
} |
|
384 |
||
385 |
/* \internal |
|
386 |
helper function to do thread sleeps, since usleep()/nanosleep() |
|
387 |
aren't reliable enough (in terms of behavior and availability) |
|
388 |
*/ |
|
389 |
static void thread_sleep(struct timespec *ti) |
|
390 |
{ |
|
391 |
pthread_mutex_t mtx; |
|
392 |
pthread_cond_t cnd; |
|
393 |
||
394 |
pthread_mutex_init(&mtx, 0); |
|
395 |
pthread_cond_init(&cnd, 0); |
|
396 |
||
397 |
pthread_mutex_lock(&mtx); |
|
398 |
(void) pthread_cond_timedwait(&cnd, &mtx, ti); |
|
399 |
pthread_mutex_unlock(&mtx); |
|
400 |
||
401 |
pthread_cond_destroy(&cnd); |
|
402 |
pthread_mutex_destroy(&mtx); |
|
403 |
} |
|
404 |
||
405 |
void QThread::sleep(unsigned long secs) |
|
406 |
{ |
|
407 |
struct timeval tv; |
|
408 |
gettimeofday(&tv, 0); |
|
409 |
struct timespec ti; |
|
410 |
ti.tv_sec = tv.tv_sec + secs; |
|
411 |
ti.tv_nsec = (tv.tv_usec * 1000); |
|
412 |
thread_sleep(&ti); |
|
413 |
} |
|
414 |
||
415 |
void QThread::msleep(unsigned long msecs) |
|
416 |
{ |
|
417 |
struct timeval tv; |
|
418 |
gettimeofday(&tv, 0); |
|
419 |
struct timespec ti; |
|
420 |
||
421 |
ti.tv_nsec = (tv.tv_usec + (msecs % 1000) * 1000) * 1000; |
|
422 |
ti.tv_sec = tv.tv_sec + (msecs / 1000) + (ti.tv_nsec / 1000000000); |
|
423 |
ti.tv_nsec %= 1000000000; |
|
424 |
thread_sleep(&ti); |
|
425 |
} |
|
426 |
||
427 |
void QThread::usleep(unsigned long usecs) |
|
428 |
{ |
|
429 |
struct timeval tv; |
|
430 |
gettimeofday(&tv, 0); |
|
431 |
struct timespec ti; |
|
432 |
||
433 |
ti.tv_nsec = (tv.tv_usec + (usecs % 1000000)) * 1000; |
|
434 |
ti.tv_sec = tv.tv_sec + (usecs / 1000000) + (ti.tv_nsec / 1000000000); |
|
435 |
ti.tv_nsec %= 1000000000; |
|
436 |
thread_sleep(&ti); |
|
437 |
} |
|
438 |
||
439 |
void QThread::start(Priority priority) |
|
440 |
{ |
|
441 |
Q_D(QThread); |
|
442 |
QMutexLocker locker(&d->mutex); |
|
443 |
if (d->running) |
|
444 |
return; |
|
445 |
||
446 |
d->running = true; |
|
447 |
d->finished = false; |
|
448 |
d->terminated = false; |
|
449 |
||
450 |
pthread_attr_t attr; |
|
451 |
pthread_attr_init(&attr); |
|
452 |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
|
453 |
||
454 |
d->priority = priority; |
|
455 |
||
456 |
#if defined(Q_OS_DARWIN) || !defined(Q_OS_OPENBSD) && !defined(Q_OS_SYMBIAN) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) |
|
457 |
// ### Need to implement thread sheduling and priorities for symbian os. Implementation removed for now |
|
458 |
switch (priority) { |
|
459 |
case InheritPriority: |
|
460 |
{ |
|
461 |
pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); |
|
462 |
break; |
|
463 |
} |
|
464 |
||
465 |
default: |
|
466 |
{ |
|
467 |
int sched_policy; |
|
468 |
if (pthread_attr_getschedpolicy(&attr, &sched_policy) != 0) { |
|
469 |
// failed to get the scheduling policy, don't bother |
|
470 |
// setting the priority |
|
471 |
qWarning("QThread::start: Cannot determine default scheduler policy"); |
|
472 |
break; |
|
473 |
} |
|
474 |
||
475 |
int prio_min = sched_get_priority_min(sched_policy); |
|
476 |
int prio_max = sched_get_priority_max(sched_policy); |
|
477 |
if (prio_min == -1 || prio_max == -1) { |
|
478 |
// failed to get the scheduling parameters, don't |
|
479 |
// bother setting the priority |
|
480 |
qWarning("QThread::start: Cannot determine scheduler priority range"); |
|
481 |
break; |
|
482 |
} |
|
483 |
||
484 |
int prio; |
|
485 |
switch (priority) { |
|
486 |
case IdlePriority: |
|
487 |
prio = prio_min; |
|
488 |
break; |
|
489 |
||
490 |
case TimeCriticalPriority: |
|
491 |
prio = prio_max; |
|
492 |
break; |
|
493 |
||
494 |
default: |
|
495 |
// crudely scale our priority enum values to the prio_min/prio_max |
|
496 |
prio = (priority * (prio_max - prio_min) / TimeCriticalPriority) + prio_min; |
|
497 |
prio = qMax(prio_min, qMin(prio_max, prio)); |
|
498 |
break; |
|
499 |
} |
|
500 |
||
501 |
sched_param sp; |
|
502 |
sp.sched_priority = prio; |
|
503 |
||
504 |
if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0 |
|
505 |
|| pthread_attr_setschedpolicy(&attr, sched_policy) != 0 |
|
506 |
|| pthread_attr_setschedparam(&attr, &sp) != 0) { |
|
507 |
// could not set scheduling hints, fallback to inheriting them |
|
508 |
pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); |
|
509 |
} |
|
510 |
break; |
|
511 |
} |
|
512 |
} |
|
513 |
#endif // _POSIX_THREAD_PRIORITY_SCHEDULING |
|
514 |
||
515 |
#ifdef Q_OS_SYMBIAN |
|
516 |
if (d->stackSize == 0) |
|
517 |
// The default stack size on Symbian is very small, making even basic |
|
518 |
// operations like file I/O fail, so we increase it by default. |
|
519 |
d->stackSize = 0x14000; // Maximum stack size on Symbian. |
|
520 |
#endif |
|
521 |
||
522 |
if (d->stackSize > 0) { |
|
523 |
#if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0 > 0) |
|
524 |
int code = pthread_attr_setstacksize(&attr, d->stackSize); |
|
525 |
#else |
|
526 |
int code = ENOSYS; // stack size not supported, automatically fail |
|
527 |
#endif // _POSIX_THREAD_ATTR_STACKSIZE |
|
528 |
||
529 |
if (code) { |
|
530 |
qWarning("QThread::start: Thread stack size error: %s", |
|
531 |
qPrintable(qt_error_string(code))); |
|
532 |
||
533 |
// we failed to set the stacksize, and as the documentation states, |
|
534 |
// the thread will fail to run... |
|
535 |
d->running = false; |
|
536 |
d->finished = false; |
|
537 |
return; |
|
538 |
} |
|
539 |
} |
|
540 |
||
541 |
int code = |
|
542 |
pthread_create(&d->thread_id, &attr, QThreadPrivate::start, this); |
|
543 |
if (code == EPERM) { |
|
544 |
// caller does not have permission to set the scheduling |
|
545 |
// parameters/policy |
|
546 |
#ifndef Q_OS_SYMBIAN |
|
547 |
pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); |
|
548 |
#endif |
|
549 |
code = |
|
550 |
pthread_create(&d->thread_id, &attr, QThreadPrivate::start, this); |
|
551 |
} |
|
552 |
||
553 |
pthread_attr_destroy(&attr); |
|
554 |
||
555 |
if (code) { |
|
556 |
qWarning("QThread::start: Thread creation error: %s", qPrintable(qt_error_string(code))); |
|
557 |
||
558 |
d->running = false; |
|
559 |
d->finished = false; |
|
560 |
d->thread_id = 0; |
|
561 |
#ifdef Q_OS_SYMBIAN |
|
562 |
d->data->symbian_thread_handle.Close(); |
|
563 |
#endif |
|
564 |
} |
|
565 |
} |
|
566 |
||
567 |
void QThread::terminate() |
|
568 |
{ |
|
569 |
Q_D(QThread); |
|
570 |
QMutexLocker locker(&d->mutex); |
|
571 |
||
572 |
if (!d->thread_id) |
|
573 |
return; |
|
574 |
||
575 |
#ifndef Q_OS_SYMBIAN |
|
576 |
int code = pthread_cancel(d->thread_id); |
|
577 |
if (code) { |
|
578 |
qWarning("QThread::start: Thread termination error: %s", |
|
579 |
qPrintable(qt_error_string((code)))); |
|
580 |
} else { |
|
581 |
d->terminated = true; |
|
582 |
} |
|
583 |
#else |
|
584 |
if (!d->running) |
|
585 |
return; |
|
586 |
if (!d->terminationEnabled) { |
|
587 |
d->terminatePending = true; |
|
588 |
return; |
|
589 |
} |
|
590 |
||
591 |
d->terminated = true; |
|
592 |
// "false, false" meaning: |
|
593 |
// 1. lockAnyway = false. Don't lock the mutex because it's already locked |
|
594 |
// (see above). |
|
595 |
// 2. closeNativeSymbianHandle = false. We don't want to close the thread handle, |
|
596 |
// because we need it here to terminate the thread. |
|
597 |
QThreadPrivate::finish(this, false, false); |
|
598 |
d->data->symbian_thread_handle.Terminate(KErrNone); |
|
599 |
d->data->symbian_thread_handle.Close(); |
|
600 |
#endif |
|
601 |
||
602 |
||
603 |
} |
|
604 |
||
605 |
bool QThread::wait(unsigned long time) |
|
606 |
{ |
|
607 |
Q_D(QThread); |
|
608 |
QMutexLocker locker(&d->mutex); |
|
609 |
||
610 |
if (d->thread_id == pthread_self()) { |
|
611 |
qWarning("QThread::wait: Thread tried to wait on itself"); |
|
612 |
return false; |
|
613 |
} |
|
614 |
||
615 |
if (d->finished || !d->running) |
|
616 |
return true; |
|
617 |
||
618 |
while (d->running) { |
|
619 |
if (!d->thread_done.wait(locker.mutex(), time)) |
|
620 |
return false; |
|
621 |
} |
|
622 |
return true; |
|
623 |
} |
|
624 |
||
625 |
void QThread::setTerminationEnabled(bool enabled) |
|
626 |
{ |
|
627 |
QThread *thr = currentThread(); |
|
628 |
Q_ASSERT_X(thr != 0, "QThread::setTerminationEnabled()", |
|
629 |
"Current thread was not started with QThread."); |
|
630 |
#ifndef Q_OS_SYMBIAN |
|
631 |
Q_UNUSED(thr) |
|
632 |
pthread_setcancelstate(enabled ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE, NULL); |
|
633 |
if (enabled) |
|
634 |
pthread_testcancel(); |
|
635 |
#else |
|
636 |
QThreadPrivate *d = thr->d_func(); |
|
637 |
QMutexLocker locker(&d->mutex); |
|
638 |
d->terminationEnabled = enabled; |
|
639 |
if (enabled && d->terminatePending) { |
|
640 |
d->terminated = true; |
|
641 |
// "false" meaning: |
|
642 |
// - lockAnyway = false. Don't lock the mutex because it's already locked |
|
643 |
// (see above). |
|
644 |
QThreadPrivate::finish(thr, false); |
|
645 |
locker.unlock(); // don't leave the mutex locked! |
|
646 |
pthread_exit(NULL); |
|
647 |
} |
|
648 |
#endif |
|
649 |
} |
|
650 |
||
651 |
void QThread::setPriority(Priority priority) |
|
652 |
{ |
|
653 |
Q_D(QThread); |
|
654 |
QMutexLocker locker(&d->mutex); |
|
655 |
if (!d->running) { |
|
656 |
qWarning("QThread::setPriority: Cannot set priority, thread is not running"); |
|
657 |
return; |
|
658 |
} |
|
659 |
||
660 |
d->priority = priority; |
|
661 |
||
662 |
// copied from start() with a few modifications: |
|
663 |
||
664 |
#if defined(Q_OS_DARWIN) || !defined(Q_OS_OPENBSD) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) |
|
665 |
int sched_policy; |
|
666 |
sched_param param; |
|
667 |
||
668 |
if (pthread_getschedparam(d->thread_id, &sched_policy, ¶m) != 0) { |
|
669 |
// failed to get the scheduling policy, don't bother setting |
|
670 |
// the priority |
|
671 |
qWarning("QThread::setPriority: Cannot get scheduler parameters"); |
|
672 |
return; |
|
673 |
} |
|
674 |
||
675 |
int prio_min = sched_get_priority_min(sched_policy); |
|
676 |
int prio_max = sched_get_priority_max(sched_policy); |
|
677 |
if (prio_min == -1 || prio_max == -1) { |
|
678 |
// failed to get the scheduling parameters, don't |
|
679 |
// bother setting the priority |
|
680 |
qWarning("QThread::setPriority: Cannot determine scheduler priority range"); |
|
681 |
return; |
|
682 |
} |
|
683 |
||
684 |
int prio; |
|
685 |
switch (priority) { |
|
686 |
case InheritPriority: |
|
687 |
qWarning("QThread::setPriority: Argument cannot be InheritPriority"); |
|
688 |
return; |
|
689 |
||
690 |
case IdlePriority: |
|
691 |
prio = prio_min; |
|
692 |
break; |
|
693 |
||
694 |
case TimeCriticalPriority: |
|
695 |
prio = prio_max; |
|
696 |
break; |
|
697 |
||
698 |
default: |
|
699 |
// crudely scale our priority enum values to the prio_min/prio_max |
|
700 |
prio = (priority * (prio_max - prio_min) / TimeCriticalPriority) + prio_min; |
|
701 |
prio = qMax(prio_min, qMin(prio_max, prio)); |
|
702 |
break; |
|
703 |
} |
|
704 |
||
705 |
param.sched_priority = prio; |
|
706 |
pthread_setschedparam(d->thread_id, sched_policy, ¶m); |
|
707 |
#endif |
|
708 |
} |
|
709 |
||
710 |
#endif // QT_NO_THREAD |
|
711 |
||
712 |
QT_END_NAMESPACE |
|
713 |