author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
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 "qplatformdefs.h" |
|
43 |
||
44 |
#include "qcoreapplication.h" |
|
45 |
#include "qpair.h" |
|
46 |
#include "qsocketnotifier.h" |
|
47 |
#include "qthread.h" |
|
48 |
||
49 |
#include "qeventdispatcher_unix_p.h" |
|
50 |
#include <private/qthread_p.h> |
|
51 |
#include <private/qcoreapplication_p.h> |
|
52 |
#include <private/qcore_unix_p.h> |
|
53 |
||
54 |
#include <errno.h> |
|
55 |
#include <stdio.h> |
|
56 |
#include <stdlib.h> |
|
57 |
||
58 |
// VxWorks doesn't correctly set the _POSIX_... options |
|
59 |
#if defined(Q_OS_VXWORKS) |
|
60 |
# if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK <= 0) |
|
61 |
# undef _POSIX_MONOTONIC_CLOCK |
|
62 |
# define _POSIX_MONOTONIC_CLOCK 1 |
|
63 |
# endif |
|
64 |
# include <pipeDrv.h> |
|
65 |
# include <selectLib.h> |
|
66 |
#endif |
|
67 |
||
68 |
#if (_POSIX_MONOTONIC_CLOCK-0 <= 0) || defined(QT_BOOTSTRAPPED) |
|
69 |
# include <sys/times.h> |
|
70 |
#endif |
|
71 |
||
72 |
QT_BEGIN_NAMESPACE |
|
73 |
||
74 |
Q_CORE_EXPORT bool qt_disable_lowpriority_timers=false; |
|
75 |
||
76 |
/***************************************************************************** |
|
77 |
UNIX signal handling |
|
78 |
*****************************************************************************/ |
|
79 |
||
80 |
static sig_atomic_t signal_received; |
|
81 |
static sig_atomic_t signals_fired[NSIG]; |
|
82 |
||
83 |
static void signalHandler(int sig) |
|
84 |
{ |
|
85 |
signals_fired[sig] = 1; |
|
86 |
signal_received = 1; |
|
87 |
} |
|
88 |
||
89 |
||
90 |
#if defined(Q_OS_INTEGRITY) || defined(Q_OS_VXWORKS) |
|
91 |
static void initThreadPipeFD(int fd) |
|
92 |
{ |
|
93 |
int ret = fcntl(fd, F_SETFD, FD_CLOEXEC); |
|
94 |
if (ret == -1) |
|
95 |
perror("QEventDispatcherUNIXPrivate: Unable to init thread pipe"); |
|
96 |
||
97 |
int flags = fcntl(fd, F_GETFL); |
|
98 |
if (flags == -1) |
|
99 |
perror("QEventDispatcherUNIXPrivate: Unable to get flags on thread pipe"); |
|
100 |
||
101 |
ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
|
102 |
if (ret == -1) |
|
103 |
perror("QEventDispatcherUNIXPrivate: Unable to set flags on thread pipe"); |
|
104 |
} |
|
105 |
#endif |
|
106 |
||
107 |
QEventDispatcherUNIXPrivate::QEventDispatcherUNIXPrivate() |
|
108 |
{ |
|
109 |
extern Qt::HANDLE qt_application_thread_id; |
|
110 |
mainThread = (QThread::currentThreadId() == qt_application_thread_id); |
|
111 |
bool pipefail = false; |
|
112 |
||
113 |
// initialize the common parts of the event loop |
|
114 |
#if defined(Q_OS_INTEGRITY) |
|
115 |
// INTEGRITY doesn't like a "select" on pipes, so use socketpair instead |
|
116 |
if (socketpair(AF_INET, SOCK_STREAM, PF_INET, thread_pipe) == -1) { |
|
117 |
perror("QEventDispatcherUNIXPrivate(): Unable to create socket pair"); |
|
118 |
pipefail = true; |
|
119 |
} else { |
|
120 |
initThreadPipeFD(thread_pipe[0]); |
|
121 |
initThreadPipeFD(thread_pipe[1]); |
|
122 |
} |
|
123 |
#elif defined(Q_OS_VXWORKS) |
|
124 |
char name[20]; |
|
125 |
qsnprintf(name, sizeof(name), "/pipe/qt_%08x", int(taskIdCurrent)); |
|
126 |
||
127 |
// make sure there is no pipe with this name |
|
128 |
pipeDevDelete(name, true); |
|
129 |
// create the pipe |
|
130 |
if (pipeDevCreate(name, 128 /*maxMsg*/, 1 /*maxLength*/) != OK) { |
|
131 |
perror("QEventDispatcherUNIXPrivate(): Unable to create thread pipe device"); |
|
132 |
pipefail = true; |
|
133 |
} else { |
|
134 |
if ((thread_pipe[0] = open(name, O_RDWR, 0)) < 0) { |
|
135 |
perror("QEventDispatcherUNIXPrivate(): Unable to create thread pipe"); |
|
136 |
pipefail = true; |
|
137 |
} else { |
|
138 |
initThreadPipeFD(thread_pipe[0]); |
|
139 |
thread_pipe[1] = thread_pipe[0]; |
|
140 |
} |
|
141 |
} |
|
142 |
#else |
|
143 |
if (qt_safe_pipe(thread_pipe, O_NONBLOCK) == -1) { |
|
144 |
perror("QEventDispatcherUNIXPrivate(): Unable to create thread pipe"); |
|
145 |
pipefail = true; |
|
146 |
} |
|
147 |
#endif |
|
148 |
||
149 |
if (pipefail) |
|
150 |
qFatal("QEventDispatcherUNIXPrivate(): Can not continue without a thread pipe"); |
|
151 |
||
152 |
sn_highest = -1; |
|
153 |
||
154 |
interrupt = false; |
|
155 |
} |
|
156 |
||
157 |
QEventDispatcherUNIXPrivate::~QEventDispatcherUNIXPrivate() |
|
158 |
{ |
|
159 |
#if defined(Q_OS_VXWORKS) |
|
160 |
close(thread_pipe[0]); |
|
161 |
||
162 |
char name[20]; |
|
163 |
qsnprintf(name, sizeof(name), "/pipe/qt_%08x", int(taskIdCurrent)); |
|
164 |
||
165 |
pipeDevDelete(name, true); |
|
166 |
#else |
|
167 |
// cleanup the common parts of the event loop |
|
168 |
close(thread_pipe[0]); |
|
169 |
close(thread_pipe[1]); |
|
170 |
#endif |
|
171 |
||
172 |
// cleanup timers |
|
173 |
qDeleteAll(timerList); |
|
174 |
} |
|
175 |
||
176 |
int QEventDispatcherUNIXPrivate::doSelect(QEventLoop::ProcessEventsFlags flags, timeval *timeout) |
|
177 |
{ |
|
178 |
Q_Q(QEventDispatcherUNIX); |
|
179 |
||
180 |
// needed in QEventDispatcherUNIX::select() |
|
181 |
timerList.updateCurrentTime(); |
|
182 |
||
183 |
int nsel; |
|
184 |
do { |
|
185 |
if (mainThread) { |
|
186 |
while (signal_received) { |
|
187 |
signal_received = 0; |
|
188 |
for (int i = 0; i < NSIG; ++i) { |
|
189 |
if (signals_fired[i]) { |
|
190 |
signals_fired[i] = 0; |
|
191 |
emit QCoreApplication::instance()->unixSignal(i); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
} |
|
196 |
||
197 |
// Process timers and socket notifiers - the common UNIX stuff |
|
198 |
int highest = 0; |
|
199 |
if (! (flags & QEventLoop::ExcludeSocketNotifiers) && (sn_highest >= 0)) { |
|
200 |
// return the highest fd we can wait for input on |
|
201 |
sn_vec[0].select_fds = sn_vec[0].enabled_fds; |
|
202 |
sn_vec[1].select_fds = sn_vec[1].enabled_fds; |
|
203 |
sn_vec[2].select_fds = sn_vec[2].enabled_fds; |
|
204 |
highest = sn_highest; |
|
205 |
} else { |
|
206 |
FD_ZERO(&sn_vec[0].select_fds); |
|
207 |
FD_ZERO(&sn_vec[1].select_fds); |
|
208 |
FD_ZERO(&sn_vec[2].select_fds); |
|
209 |
} |
|
210 |
||
211 |
FD_SET(thread_pipe[0], &sn_vec[0].select_fds); |
|
212 |
highest = qMax(highest, thread_pipe[0]); |
|
213 |
||
214 |
nsel = q->select(highest + 1, |
|
215 |
&sn_vec[0].select_fds, |
|
216 |
&sn_vec[1].select_fds, |
|
217 |
&sn_vec[2].select_fds, |
|
218 |
timeout); |
|
219 |
} while (nsel == -1 && (errno == EINTR || errno == EAGAIN)); |
|
220 |
||
221 |
if (nsel == -1) { |
|
222 |
if (errno == EBADF) { |
|
223 |
// it seems a socket notifier has a bad fd... find out |
|
224 |
// which one it is and disable it |
|
225 |
fd_set fdset; |
|
226 |
timeval tm; |
|
227 |
tm.tv_sec = tm.tv_usec = 0l; |
|
228 |
||
229 |
for (int type = 0; type < 3; ++type) { |
|
230 |
QSockNotType::List &list = sn_vec[type].list; |
|
231 |
if (list.size() == 0) |
|
232 |
continue; |
|
233 |
||
234 |
for (int i = 0; i < list.size(); ++i) { |
|
235 |
QSockNot *sn = list[i]; |
|
236 |
||
237 |
FD_ZERO(&fdset); |
|
238 |
FD_SET(sn->fd, &fdset); |
|
239 |
||
240 |
int ret = -1; |
|
241 |
do { |
|
242 |
switch (type) { |
|
243 |
case 0: // read |
|
244 |
ret = select(sn->fd + 1, &fdset, 0, 0, &tm); |
|
245 |
break; |
|
246 |
case 1: // write |
|
247 |
ret = select(sn->fd + 1, 0, &fdset, 0, &tm); |
|
248 |
break; |
|
249 |
case 2: // except |
|
250 |
ret = select(sn->fd + 1, 0, 0, &fdset, &tm); |
|
251 |
break; |
|
252 |
} |
|
253 |
} while (ret == -1 && (errno == EINTR || errno == EAGAIN)); |
|
254 |
||
255 |
if (ret == -1 && errno == EBADF) { |
|
256 |
// disable the invalid socket notifier |
|
257 |
static const char *t[] = { "Read", "Write", "Exception" }; |
|
258 |
qWarning("QSocketNotifier: Invalid socket %d and type '%s', disabling...", |
|
259 |
sn->fd, t[type]); |
|
260 |
sn->obj->setEnabled(false); |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
} else { |
|
265 |
// EINVAL... shouldn't happen, so let's complain to stderr |
|
266 |
// and hope someone sends us a bug report |
|
267 |
perror("select"); |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
// some other thread woke us up... consume the data on the thread pipe so that |
|
272 |
// select doesn't immediately return next time |
|
273 |
int nevents = 0; |
|
274 |
if (nsel > 0 && FD_ISSET(thread_pipe[0], &sn_vec[0].select_fds)) { |
|
275 |
#if defined(Q_OS_VXWORKS) |
|
276 |
char c[16]; |
|
277 |
::read(thread_pipe[0], c, sizeof(c)); |
|
278 |
::ioctl(thread_pipe[0], FIOFLUSH, 0); |
|
279 |
#else |
|
280 |
char c[16]; |
|
281 |
while (::read(thread_pipe[0], c, sizeof(c)) > 0) |
|
282 |
; |
|
283 |
#endif |
|
284 |
if (!wakeUps.testAndSetRelease(1, 0)) { |
|
285 |
// hopefully, this is dead code |
|
286 |
qWarning("QEventDispatcherUNIX: internal error, wakeUps.testAndSetRelease(1, 0) failed!"); |
|
287 |
} |
|
288 |
++nevents; |
|
289 |
} |
|
290 |
||
291 |
// activate socket notifiers |
|
292 |
if (! (flags & QEventLoop::ExcludeSocketNotifiers) && nsel > 0 && sn_highest >= 0) { |
|
293 |
// if select says data is ready on any socket, then set the socket notifier |
|
294 |
// to pending |
|
295 |
for (int i=0; i<3; i++) { |
|
296 |
QSockNotType::List &list = sn_vec[i].list; |
|
297 |
for (int j = 0; j < list.size(); ++j) { |
|
298 |
QSockNot *sn = list[j]; |
|
299 |
if (FD_ISSET(sn->fd, &sn_vec[i].select_fds)) |
|
300 |
q->setSocketNotifierPending(sn->obj); |
|
301 |
} |
|
302 |
} |
|
303 |
} |
|
304 |
return (nevents + q->activateSocketNotifiers()); |
|
305 |
} |
|
306 |
||
307 |
/* |
|
308 |
* Internal functions for manipulating timer data structures. The |
|
309 |
* timerBitVec array is used for keeping track of timer identifiers. |
|
310 |
*/ |
|
311 |
||
312 |
QTimerInfoList::QTimerInfoList() |
|
313 |
{ |
|
314 |
currentTime = qt_gettime(); |
|
315 |
||
316 |
#if (_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC) |
|
317 |
if (!qt_gettime_is_monotonic()) { |
|
318 |
// not using monotonic timers, initialize the timeChanged() machinery |
|
319 |
previousTime = currentTime; |
|
320 |
||
321 |
tms unused; |
|
322 |
previousTicks = times(&unused); |
|
323 |
||
324 |
ticksPerSecond = sysconf(_SC_CLK_TCK); |
|
325 |
msPerTick = 1000/ticksPerSecond; |
|
326 |
} else { |
|
327 |
// detected monotonic timers |
|
328 |
previousTime.tv_sec = previousTime.tv_usec = 0; |
|
329 |
previousTicks = 0; |
|
330 |
ticksPerSecond = 0; |
|
331 |
msPerTick = 0; |
|
332 |
} |
|
333 |
#endif |
|
334 |
||
335 |
firstTimerInfo = currentTimerInfo = 0; |
|
336 |
} |
|
337 |
||
338 |
timeval QTimerInfoList::updateCurrentTime() |
|
339 |
{ |
|
340 |
return (currentTime = qt_gettime()); |
|
341 |
} |
|
342 |
||
343 |
#if ((_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC)) || defined(QT_BOOTSTRAPPED) |
|
344 |
||
345 |
template <> |
|
346 |
timeval qAbs(const timeval &t) |
|
347 |
{ |
|
348 |
timeval tmp = t; |
|
349 |
if (tmp.tv_sec < 0) { |
|
350 |
tmp.tv_sec = -tmp.tv_sec - 1; |
|
351 |
tmp.tv_usec -= 1000000; |
|
352 |
} |
|
353 |
if (tmp.tv_sec == 0 && tmp.tv_usec < 0) { |
|
354 |
tmp.tv_usec = -tmp.tv_usec; |
|
355 |
} |
|
356 |
return normalizedTimeval(tmp); |
|
357 |
} |
|
358 |
||
359 |
/* |
|
360 |
Returns true if the real time clock has changed by more than 10% |
|
361 |
relative to the processor time since the last time this function was |
|
362 |
called. This presumably means that the system time has been changed. |
|
363 |
||
364 |
If /a delta is nonzero, delta is set to our best guess at how much the system clock was changed. |
|
365 |
*/ |
|
366 |
bool QTimerInfoList::timeChanged(timeval *delta) |
|
367 |
{ |
|
368 |
struct tms unused; |
|
369 |
clock_t currentTicks = times(&unused); |
|
370 |
||
371 |
clock_t elapsedTicks = currentTicks - previousTicks; |
|
372 |
timeval elapsedTime = currentTime - previousTime; |
|
373 |
||
374 |
timeval elapsedTimeTicks; |
|
375 |
elapsedTimeTicks.tv_sec = elapsedTicks / ticksPerSecond; |
|
376 |
elapsedTimeTicks.tv_usec = (((elapsedTicks * 1000) / ticksPerSecond) % 1000) * 1000; |
|
377 |
||
378 |
timeval dummy; |
|
379 |
if (!delta) |
|
380 |
delta = &dummy; |
|
381 |
*delta = elapsedTime - elapsedTimeTicks; |
|
382 |
||
383 |
previousTicks = currentTicks; |
|
384 |
previousTime = currentTime; |
|
385 |
||
386 |
// If tick drift is more than 10% off compared to realtime, we assume that the clock has |
|
387 |
// been set. Of course, we have to allow for the tick granularity as well. |
|
388 |
timeval tickGranularity; |
|
389 |
tickGranularity.tv_sec = 0; |
|
390 |
tickGranularity.tv_usec = msPerTick * 1000; |
|
391 |
return elapsedTimeTicks < ((qAbs(*delta) - tickGranularity) * 10); |
|
392 |
} |
|
393 |
||
394 |
void QTimerInfoList::repairTimersIfNeeded() |
|
395 |
{ |
|
396 |
if (qt_gettime_is_monotonic()) |
|
397 |
return; |
|
398 |
timeval delta; |
|
399 |
if (timeChanged(&delta)) |
|
400 |
timerRepair(delta); |
|
401 |
} |
|
402 |
||
403 |
#else // !(_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(QT_BOOTSTRAPPED) |
|
404 |
||
405 |
void QTimerInfoList::repairTimersIfNeeded() |
|
406 |
{ |
|
407 |
} |
|
408 |
||
409 |
#endif |
|
410 |
||
411 |
/* |
|
412 |
insert timer info into list |
|
413 |
*/ |
|
414 |
void QTimerInfoList::timerInsert(QTimerInfo *ti) |
|
415 |
{ |
|
416 |
int index = size(); |
|
417 |
while (index--) { |
|
418 |
register const QTimerInfo * const t = at(index); |
|
419 |
if (!(ti->timeout < t->timeout)) |
|
420 |
break; |
|
421 |
} |
|
422 |
insert(index+1, ti); |
|
423 |
} |
|
424 |
||
425 |
/* |
|
426 |
repair broken timer |
|
427 |
*/ |
|
428 |
void QTimerInfoList::timerRepair(const timeval &diff) |
|
429 |
{ |
|
430 |
// repair all timers |
|
431 |
for (int i = 0; i < size(); ++i) { |
|
432 |
register QTimerInfo *t = at(i); |
|
433 |
t->timeout = t->timeout + diff; |
|
434 |
} |
|
435 |
} |
|
436 |
||
437 |
/* |
|
438 |
Returns the time to wait for the next timer, or null if no timers |
|
439 |
are waiting. |
|
440 |
*/ |
|
441 |
bool QTimerInfoList::timerWait(timeval &tm) |
|
442 |
{ |
|
443 |
timeval currentTime = updateCurrentTime(); |
|
444 |
repairTimersIfNeeded(); |
|
445 |
||
446 |
// Find first waiting timer not already active |
|
447 |
QTimerInfo *t = 0; |
|
448 |
for (QTimerInfoList::const_iterator it = constBegin(); it != constEnd(); ++it) { |
|
449 |
if (!(*it)->inTimerEvent) { |
|
450 |
t = *it; |
|
451 |
break; |
|
452 |
} |
|
453 |
} |
|
454 |
||
455 |
if (!t) |
|
456 |
return false; |
|
457 |
||
458 |
if (currentTime < t->timeout) { |
|
459 |
// time to wait |
|
460 |
tm = t->timeout - currentTime; |
|
461 |
} else { |
|
462 |
// no time to wait |
|
463 |
tm.tv_sec = 0; |
|
464 |
tm.tv_usec = 0; |
|
465 |
} |
|
466 |
||
467 |
return true; |
|
468 |
} |
|
469 |
||
470 |
void QTimerInfoList::registerTimer(int timerId, int interval, QObject *object) |
|
471 |
{ |
|
472 |
QTimerInfo *t = new QTimerInfo; |
|
473 |
t->id = timerId; |
|
474 |
t->interval.tv_sec = interval / 1000; |
|
475 |
t->interval.tv_usec = (interval % 1000) * 1000; |
|
476 |
t->timeout = updateCurrentTime() + t->interval; |
|
477 |
t->obj = object; |
|
478 |
t->inTimerEvent = false; |
|
479 |
||
480 |
timerInsert(t); |
|
481 |
} |
|
482 |
||
483 |
bool QTimerInfoList::unregisterTimer(int timerId) |
|
484 |
{ |
|
485 |
// set timer inactive |
|
486 |
for (int i = 0; i < count(); ++i) { |
|
487 |
register QTimerInfo *t = at(i); |
|
488 |
if (t->id == timerId) { |
|
489 |
// found it |
|
490 |
removeAt(i); |
|
491 |
if (t == firstTimerInfo) |
|
492 |
firstTimerInfo = 0; |
|
493 |
if (t == currentTimerInfo) |
|
494 |
currentTimerInfo = 0; |
|
495 |
||
496 |
// release the timer id |
|
497 |
if (!QObjectPrivate::get(t->obj)->inThreadChangeEvent) |
|
498 |
QAbstractEventDispatcherPrivate::releaseTimerId(timerId); |
|
499 |
||
500 |
delete t; |
|
501 |
return true; |
|
502 |
} |
|
503 |
} |
|
504 |
// id not found |
|
505 |
return false; |
|
506 |
} |
|
507 |
||
508 |
bool QTimerInfoList::unregisterTimers(QObject *object) |
|
509 |
{ |
|
510 |
if (isEmpty()) |
|
511 |
return false; |
|
512 |
for (int i = 0; i < count(); ++i) { |
|
513 |
register QTimerInfo *t = at(i); |
|
514 |
if (t->obj == object) { |
|
515 |
// object found |
|
516 |
removeAt(i); |
|
517 |
if (t == firstTimerInfo) |
|
518 |
firstTimerInfo = 0; |
|
519 |
if (t == currentTimerInfo) |
|
520 |
currentTimerInfo = 0; |
|
521 |
||
522 |
// release the timer id |
|
523 |
if (!QObjectPrivate::get(t->obj)->inThreadChangeEvent) |
|
524 |
QAbstractEventDispatcherPrivate::releaseTimerId(t->id); |
|
525 |
||
526 |
delete t; |
|
527 |
// move back one so that we don't skip the new current item |
|
528 |
--i; |
|
529 |
} |
|
530 |
} |
|
531 |
return true; |
|
532 |
} |
|
533 |
||
534 |
QList<QPair<int, int> > QTimerInfoList::registeredTimers(QObject *object) const |
|
535 |
{ |
|
536 |
QList<QPair<int, int> > list; |
|
537 |
for (int i = 0; i < count(); ++i) { |
|
538 |
register const QTimerInfo * const t = at(i); |
|
539 |
if (t->obj == object) |
|
540 |
list << QPair<int, int>(t->id, t->interval.tv_sec * 1000 + t->interval.tv_usec / 1000); |
|
541 |
} |
|
542 |
return list; |
|
543 |
} |
|
544 |
||
545 |
/* |
|
546 |
Activate pending timers, returning how many where activated. |
|
547 |
*/ |
|
548 |
int QTimerInfoList::activateTimers() |
|
549 |
{ |
|
550 |
if (qt_disable_lowpriority_timers || isEmpty()) |
|
551 |
return 0; // nothing to do |
|
552 |
||
553 |
bool firstTime = true; |
|
554 |
timeval currentTime; |
|
555 |
int n_act = 0, maxCount = count(); |
|
556 |
||
557 |
QTimerInfo *saveFirstTimerInfo = firstTimerInfo; |
|
558 |
QTimerInfo *saveCurrentTimerInfo = currentTimerInfo; |
|
559 |
firstTimerInfo = currentTimerInfo = 0; |
|
560 |
||
561 |
while (maxCount--) { |
|
562 |
currentTime = updateCurrentTime(); |
|
563 |
if (firstTime) { |
|
564 |
repairTimersIfNeeded(); |
|
565 |
firstTime = false; |
|
566 |
} |
|
567 |
||
568 |
if (isEmpty()) |
|
569 |
break; |
|
570 |
||
571 |
currentTimerInfo = first(); |
|
572 |
if (currentTime < currentTimerInfo->timeout) |
|
573 |
break; // no timer has expired |
|
574 |
||
575 |
if (!firstTimerInfo) { |
|
576 |
firstTimerInfo = currentTimerInfo; |
|
577 |
} else if (firstTimerInfo == currentTimerInfo) { |
|
578 |
// avoid sending the same timer multiple times |
|
579 |
break; |
|
580 |
} else if (currentTimerInfo->interval < firstTimerInfo->interval |
|
581 |
|| currentTimerInfo->interval == firstTimerInfo->interval) { |
|
582 |
firstTimerInfo = currentTimerInfo; |
|
583 |
} |
|
584 |
||
585 |
// remove from list |
|
586 |
removeFirst(); |
|
587 |
||
588 |
// determine next timeout time |
|
589 |
currentTimerInfo->timeout += currentTimerInfo->interval; |
|
590 |
if (currentTimerInfo->timeout < currentTime) |
|
591 |
currentTimerInfo->timeout = currentTime + currentTimerInfo->interval; |
|
592 |
||
593 |
// reinsert timer |
|
594 |
timerInsert(currentTimerInfo); |
|
595 |
if (currentTimerInfo->interval.tv_usec > 0 || currentTimerInfo->interval.tv_sec > 0) |
|
596 |
n_act++; |
|
597 |
||
598 |
if (!currentTimerInfo->inTimerEvent) { |
|
599 |
// send event, but don't allow it to recurse |
|
600 |
currentTimerInfo->inTimerEvent = true; |
|
601 |
||
602 |
QTimerEvent e(currentTimerInfo->id); |
|
603 |
QCoreApplication::sendEvent(currentTimerInfo->obj, &e); |
|
604 |
||
605 |
if (currentTimerInfo) |
|
606 |
currentTimerInfo->inTimerEvent = false; |
|
607 |
} |
|
608 |
} |
|
609 |
||
610 |
firstTimerInfo = saveFirstTimerInfo; |
|
611 |
currentTimerInfo = saveCurrentTimerInfo; |
|
612 |
||
613 |
return n_act; |
|
614 |
} |
|
615 |
||
616 |
QEventDispatcherUNIX::QEventDispatcherUNIX(QObject *parent) |
|
617 |
: QAbstractEventDispatcher(*new QEventDispatcherUNIXPrivate, parent) |
|
618 |
{ } |
|
619 |
||
620 |
QEventDispatcherUNIX::QEventDispatcherUNIX(QEventDispatcherUNIXPrivate &dd, QObject *parent) |
|
621 |
: QAbstractEventDispatcher(dd, parent) |
|
622 |
{ } |
|
623 |
||
624 |
QEventDispatcherUNIX::~QEventDispatcherUNIX() |
|
625 |
{ |
|
626 |
Q_D(QEventDispatcherUNIX); |
|
627 |
d->threadData->eventDispatcher = 0; |
|
628 |
} |
|
629 |
||
630 |
int QEventDispatcherUNIX::select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
|
631 |
timeval *timeout) |
|
632 |
{ |
|
633 |
return qt_safe_select(nfds, readfds, writefds, exceptfds, timeout); |
|
634 |
} |
|
635 |
||
636 |
/*! |
|
637 |
\internal |
|
638 |
*/ |
|
639 |
void QEventDispatcherUNIX::registerTimer(int timerId, int interval, QObject *obj) |
|
640 |
{ |
|
641 |
#ifndef QT_NO_DEBUG |
|
642 |
if (timerId < 1 || interval < 0 || !obj) { |
|
643 |
qWarning("QEventDispatcherUNIX::registerTimer: invalid arguments"); |
|
644 |
return; |
|
645 |
} else if (obj->thread() != thread() || thread() != QThread::currentThread()) { |
|
646 |
qWarning("QObject::startTimer: timers cannot be started from another thread"); |
|
647 |
return; |
|
648 |
} |
|
649 |
#endif |
|
650 |
||
651 |
Q_D(QEventDispatcherUNIX); |
|
652 |
d->timerList.registerTimer(timerId, interval, obj); |
|
653 |
} |
|
654 |
||
655 |
/*! |
|
656 |
\internal |
|
657 |
*/ |
|
658 |
bool QEventDispatcherUNIX::unregisterTimer(int timerId) |
|
659 |
{ |
|
660 |
#ifndef QT_NO_DEBUG |
|
661 |
if (timerId < 1) { |
|
662 |
qWarning("QEventDispatcherUNIX::unregisterTimer: invalid argument"); |
|
663 |
return false; |
|
664 |
} else if (thread() != QThread::currentThread()) { |
|
665 |
qWarning("QObject::killTimer: timers cannot be stopped from another thread"); |
|
666 |
return false; |
|
667 |
} |
|
668 |
#endif |
|
669 |
||
670 |
Q_D(QEventDispatcherUNIX); |
|
671 |
return d->timerList.unregisterTimer(timerId); |
|
672 |
} |
|
673 |
||
674 |
/*! |
|
675 |
\internal |
|
676 |
*/ |
|
677 |
bool QEventDispatcherUNIX::unregisterTimers(QObject *object) |
|
678 |
{ |
|
679 |
#ifndef QT_NO_DEBUG |
|
680 |
if (!object) { |
|
681 |
qWarning("QEventDispatcherUNIX::unregisterTimers: invalid argument"); |
|
682 |
return false; |
|
683 |
} else if (object->thread() != thread() || thread() != QThread::currentThread()) { |
|
684 |
qWarning("QObject::killTimers: timers cannot be stopped from another thread"); |
|
685 |
return false; |
|
686 |
} |
|
687 |
#endif |
|
688 |
||
689 |
Q_D(QEventDispatcherUNIX); |
|
690 |
return d->timerList.unregisterTimers(object); |
|
691 |
} |
|
692 |
||
693 |
QList<QEventDispatcherUNIX::TimerInfo> |
|
694 |
QEventDispatcherUNIX::registeredTimers(QObject *object) const |
|
695 |
{ |
|
696 |
if (!object) { |
|
697 |
qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument"); |
|
698 |
return QList<TimerInfo>(); |
|
699 |
} |
|
700 |
||
701 |
Q_D(const QEventDispatcherUNIX); |
|
702 |
return d->timerList.registeredTimers(object); |
|
703 |
} |
|
704 |
||
705 |
/***************************************************************************** |
|
706 |
Socket notifier type |
|
707 |
*****************************************************************************/ |
|
708 |
QSockNotType::QSockNotType() |
|
709 |
{ |
|
710 |
FD_ZERO(&select_fds); |
|
711 |
FD_ZERO(&enabled_fds); |
|
712 |
FD_ZERO(&pending_fds); |
|
713 |
} |
|
714 |
||
715 |
QSockNotType::~QSockNotType() |
|
716 |
{ |
|
717 |
for (int i = 0; i < list.size(); ++i) |
|
718 |
delete list[i]; |
|
719 |
} |
|
720 |
||
721 |
/***************************************************************************** |
|
722 |
QEventDispatcher implementations for UNIX |
|
723 |
*****************************************************************************/ |
|
724 |
||
725 |
void QEventDispatcherUNIX::registerSocketNotifier(QSocketNotifier *notifier) |
|
726 |
{ |
|
727 |
Q_ASSERT(notifier); |
|
728 |
int sockfd = notifier->socket(); |
|
729 |
int type = notifier->type(); |
|
730 |
#ifndef QT_NO_DEBUG |
|
731 |
if (sockfd < 0 |
|
732 |
|| unsigned(sockfd) >= FD_SETSIZE) { |
|
733 |
qWarning("QSocketNotifier: Internal error"); |
|
734 |
return; |
|
735 |
} else if (notifier->thread() != thread() |
|
736 |
|| thread() != QThread::currentThread()) { |
|
737 |
qWarning("QSocketNotifier: socket notifiers cannot be enabled from another thread"); |
|
738 |
return; |
|
739 |
} |
|
740 |
#endif |
|
741 |
||
742 |
Q_D(QEventDispatcherUNIX); |
|
743 |
QSockNotType::List &list = d->sn_vec[type].list; |
|
744 |
fd_set *fds = &d->sn_vec[type].enabled_fds; |
|
745 |
QSockNot *sn; |
|
746 |
||
747 |
sn = new QSockNot; |
|
748 |
sn->obj = notifier; |
|
749 |
sn->fd = sockfd; |
|
750 |
sn->queue = &d->sn_vec[type].pending_fds; |
|
751 |
||
752 |
int i; |
|
753 |
for (i = 0; i < list.size(); ++i) { |
|
754 |
QSockNot *p = list[i]; |
|
755 |
if (p->fd < sockfd) |
|
756 |
break; |
|
757 |
if (p->fd == sockfd) { |
|
758 |
static const char *t[] = { "Read", "Write", "Exception" }; |
|
759 |
qWarning("QSocketNotifier: Multiple socket notifiers for " |
|
760 |
"same socket %d and type %s", sockfd, t[type]); |
|
761 |
} |
|
762 |
} |
|
763 |
list.insert(i, sn); |
|
764 |
||
765 |
FD_SET(sockfd, fds); |
|
766 |
d->sn_highest = qMax(d->sn_highest, sockfd); |
|
767 |
} |
|
768 |
||
769 |
void QEventDispatcherUNIX::unregisterSocketNotifier(QSocketNotifier *notifier) |
|
770 |
{ |
|
771 |
Q_ASSERT(notifier); |
|
772 |
int sockfd = notifier->socket(); |
|
773 |
int type = notifier->type(); |
|
774 |
#ifndef QT_NO_DEBUG |
|
775 |
if (sockfd < 0 |
|
776 |
|| unsigned(sockfd) >= FD_SETSIZE) { |
|
777 |
qWarning("QSocketNotifier: Internal error"); |
|
778 |
return; |
|
779 |
} else if (notifier->thread() != thread() |
|
780 |
|| thread() != QThread::currentThread()) { |
|
781 |
qWarning("QSocketNotifier: socket notifiers cannot be disabled from another thread"); |
|
782 |
return; |
|
783 |
} |
|
784 |
#endif |
|
785 |
||
786 |
Q_D(QEventDispatcherUNIX); |
|
787 |
QSockNotType::List &list = d->sn_vec[type].list; |
|
788 |
fd_set *fds = &d->sn_vec[type].enabled_fds; |
|
789 |
QSockNot *sn = 0; |
|
790 |
int i; |
|
791 |
for (i = 0; i < list.size(); ++i) { |
|
792 |
sn = list[i]; |
|
793 |
if(sn->obj == notifier && sn->fd == sockfd) |
|
794 |
break; |
|
795 |
} |
|
796 |
if (i == list.size()) // not found |
|
797 |
return; |
|
798 |
||
799 |
FD_CLR(sockfd, fds); // clear fd bit |
|
800 |
FD_CLR(sockfd, sn->queue); |
|
801 |
d->sn_pending_list.removeAll(sn); // remove from activation list |
|
802 |
list.removeAt(i); // remove notifier found above |
|
803 |
delete sn; |
|
804 |
||
805 |
if (d->sn_highest == sockfd) { // find highest fd |
|
806 |
d->sn_highest = -1; |
|
807 |
for (int i=0; i<3; i++) { |
|
808 |
if (!d->sn_vec[i].list.isEmpty()) |
|
809 |
d->sn_highest = qMax(d->sn_highest, // list is fd-sorted |
|
810 |
d->sn_vec[i].list[0]->fd); |
|
811 |
} |
|
812 |
} |
|
813 |
} |
|
814 |
||
815 |
void QEventDispatcherUNIX::setSocketNotifierPending(QSocketNotifier *notifier) |
|
816 |
{ |
|
817 |
Q_ASSERT(notifier); |
|
818 |
int sockfd = notifier->socket(); |
|
819 |
int type = notifier->type(); |
|
820 |
#ifndef QT_NO_DEBUG |
|
821 |
if (sockfd < 0 |
|
822 |
|| unsigned(sockfd) >= FD_SETSIZE) { |
|
823 |
qWarning("QSocketNotifier: Internal error"); |
|
824 |
return; |
|
825 |
} |
|
826 |
Q_ASSERT(notifier->thread() == thread() && thread() == QThread::currentThread()); |
|
827 |
#endif |
|
828 |
||
829 |
Q_D(QEventDispatcherUNIX); |
|
830 |
QSockNotType::List &list = d->sn_vec[type].list; |
|
831 |
QSockNot *sn = 0; |
|
832 |
int i; |
|
833 |
for (i = 0; i < list.size(); ++i) { |
|
834 |
sn = list[i]; |
|
835 |
if(sn->obj == notifier && sn->fd == sockfd) |
|
836 |
break; |
|
837 |
} |
|
838 |
if (i == list.size()) // not found |
|
839 |
return; |
|
840 |
||
841 |
// We choose a random activation order to be more fair under high load. |
|
842 |
// If a constant order is used and a peer early in the list can |
|
843 |
// saturate the IO, it might grab our attention completely. |
|
844 |
// Also, if we're using a straight list, the callback routines may |
|
845 |
// delete other entries from the list before those other entries are |
|
846 |
// processed. |
|
847 |
if (! FD_ISSET(sn->fd, sn->queue)) { |
|
848 |
if (d->sn_pending_list.isEmpty()) { |
|
849 |
d->sn_pending_list.append(sn); |
|
850 |
} else { |
|
851 |
d->sn_pending_list.insert((qrand() & 0xff) % |
|
852 |
(d->sn_pending_list.size()+1), sn); |
|
853 |
} |
|
854 |
FD_SET(sn->fd, sn->queue); |
|
855 |
} |
|
856 |
} |
|
857 |
||
858 |
int QEventDispatcherUNIX::activateTimers() |
|
859 |
{ |
|
860 |
Q_ASSERT(thread() == QThread::currentThread()); |
|
861 |
Q_D(QEventDispatcherUNIX); |
|
862 |
return d->timerList.activateTimers(); |
|
863 |
} |
|
864 |
||
865 |
int QEventDispatcherUNIX::activateSocketNotifiers() |
|
866 |
{ |
|
867 |
Q_D(QEventDispatcherUNIX); |
|
868 |
if (d->sn_pending_list.isEmpty()) |
|
869 |
return 0; |
|
870 |
||
871 |
// activate entries |
|
872 |
int n_act = 0; |
|
873 |
QEvent event(QEvent::SockAct); |
|
874 |
while (!d->sn_pending_list.isEmpty()) { |
|
875 |
QSockNot *sn = d->sn_pending_list.takeFirst(); |
|
876 |
if (FD_ISSET(sn->fd, sn->queue)) { |
|
877 |
FD_CLR(sn->fd, sn->queue); |
|
878 |
QCoreApplication::sendEvent(sn->obj, &event); |
|
879 |
++n_act; |
|
880 |
} |
|
881 |
} |
|
882 |
return n_act; |
|
883 |
} |
|
884 |
||
885 |
bool QEventDispatcherUNIX::processEvents(QEventLoop::ProcessEventsFlags flags) |
|
886 |
{ |
|
887 |
Q_D(QEventDispatcherUNIX); |
|
888 |
d->interrupt = false; |
|
889 |
||
890 |
// we are awake, broadcast it |
|
891 |
emit awake(); |
|
892 |
QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData); |
|
893 |
||
894 |
int nevents = 0; |
|
895 |
const bool canWait = (d->threadData->canWait |
|
896 |
&& !d->interrupt |
|
897 |
&& (flags & QEventLoop::WaitForMoreEvents)); |
|
898 |
||
899 |
if (canWait) |
|
900 |
emit aboutToBlock(); |
|
901 |
||
902 |
if (!d->interrupt) { |
|
903 |
// return the maximum time we can wait for an event. |
|
904 |
timeval *tm = 0; |
|
905 |
timeval wait_tm = { 0l, 0l }; |
|
906 |
if (!(flags & QEventLoop::X11ExcludeTimers)) { |
|
907 |
if (d->timerList.timerWait(wait_tm)) |
|
908 |
tm = &wait_tm; |
|
909 |
} |
|
910 |
||
911 |
if (!canWait) { |
|
912 |
if (!tm) |
|
913 |
tm = &wait_tm; |
|
914 |
||
915 |
// no time to wait |
|
916 |
tm->tv_sec = 0l; |
|
917 |
tm->tv_usec = 0l; |
|
918 |
} |
|
919 |
||
920 |
nevents = d->doSelect(flags, tm); |
|
921 |
||
922 |
// activate timers |
|
923 |
if (! (flags & QEventLoop::X11ExcludeTimers)) { |
|
924 |
nevents += activateTimers(); |
|
925 |
} |
|
926 |
} |
|
927 |
// return true if we handled events, false otherwise |
|
928 |
return (nevents > 0); |
|
929 |
} |
|
930 |
||
931 |
bool QEventDispatcherUNIX::hasPendingEvents() |
|
932 |
{ |
|
933 |
extern uint qGlobalPostedEventsCount(); // from qapplication.cpp |
|
934 |
return qGlobalPostedEventsCount(); |
|
935 |
} |
|
936 |
||
937 |
void QEventDispatcherUNIX::wakeUp() |
|
938 |
{ |
|
939 |
Q_D(QEventDispatcherUNIX); |
|
940 |
if (d->wakeUps.testAndSetAcquire(0, 1)) { |
|
941 |
char c = 0; |
|
942 |
qt_safe_write( d->thread_pipe[1], &c, 1 ); |
|
943 |
} |
|
944 |
} |
|
945 |
||
946 |
void QEventDispatcherUNIX::interrupt() |
|
947 |
{ |
|
948 |
Q_D(QEventDispatcherUNIX); |
|
949 |
d->interrupt = true; |
|
950 |
wakeUp(); |
|
951 |
} |
|
952 |
||
953 |
void QEventDispatcherUNIX::flush() |
|
954 |
{ } |
|
955 |
||
956 |
||
957 |
||
958 |
||
959 |
void QCoreApplication::watchUnixSignal(int sig, bool watch) |
|
960 |
{ |
|
961 |
if (sig < NSIG) { |
|
962 |
struct sigaction sa; |
|
963 |
sigemptyset(&(sa.sa_mask)); |
|
964 |
sa.sa_flags = 0; |
|
965 |
if (watch) |
|
966 |
sa.sa_handler = signalHandler; |
|
967 |
else |
|
968 |
sa.sa_handler = SIG_DFL; |
|
969 |
sigaction(sig, &sa, 0); |
|
970 |
} |
|
971 |
} |
|
972 |
||
973 |
QT_END_NAMESPACE |