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 7 | 3f74d0d4af4c |
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 "qeventdispatcher_symbian_p.h" |
|
43 |
#include <private/qthread_p.h> |
|
44 |
#include <qcoreapplication.h> |
|
45 |
#include <private/qcoreapplication_p.h> |
|
46 |
#include <qdatetime.h> |
|
47 |
||
48 |
#include <unistd.h> |
|
49 |
#include <errno.h> |
|
50 |
||
51 |
QT_BEGIN_NAMESPACE |
|
52 |
||
53 |
#define WAKE_UP_PRIORITY CActive::EPriorityStandard |
|
54 |
#define TIMER_PRIORITY CActive::EPriorityHigh |
|
55 |
#define NULLTIMER_PRIORITY CActive::EPriorityLow |
|
56 |
#define COMPLETE_DEFERRED_ACTIVE_OBJECTS_PRIORITY CActive::EPriorityIdle |
|
57 |
||
58 |
static inline int qt_pipe_write(int socket, const char *data, qint64 len) |
|
59 |
{ |
|
60 |
return ::write(socket, data, len); |
|
61 |
} |
|
62 |
#if defined(write) |
|
63 |
# undef write |
|
64 |
#endif |
|
65 |
||
66 |
static inline int qt_pipe_close(int socket) |
|
67 |
{ |
|
68 |
return ::close(socket); |
|
69 |
} |
|
70 |
#if defined(close) |
|
71 |
# undef close |
|
72 |
#endif |
|
73 |
||
74 |
static inline int qt_pipe_fcntl(int socket, int command) |
|
75 |
{ |
|
76 |
return ::fcntl(socket, command); |
|
77 |
} |
|
78 |
static inline int qt_pipe2_fcntl(int socket, int command, int option) |
|
79 |
{ |
|
80 |
return ::fcntl(socket, command, option); |
|
81 |
} |
|
82 |
#if defined(fcntl) |
|
83 |
# undef fcntl |
|
84 |
#endif |
|
85 |
||
86 |
static inline int qt_socket_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) |
|
87 |
{ |
|
88 |
return ::select(nfds, readfds, writefds, exceptfds, timeout); |
|
89 |
} |
|
90 |
||
91 |
// This simply interrupts the select and locks the mutex until destroyed. |
|
92 |
class QSelectMutexGrabber |
|
93 |
{ |
|
94 |
public: |
|
95 |
QSelectMutexGrabber(int fd, QMutex *mutex) |
|
96 |
: m_mutex(mutex) |
|
97 |
{ |
|
98 |
if (m_mutex->tryLock()) |
|
99 |
return; |
|
100 |
||
101 |
char dummy = 0; |
|
102 |
qt_pipe_write(fd, &dummy, 1); |
|
103 |
||
104 |
m_mutex->lock(); |
|
105 |
} |
|
106 |
||
107 |
~QSelectMutexGrabber() |
|
108 |
{ |
|
109 |
m_mutex->unlock(); |
|
110 |
} |
|
111 |
||
112 |
private: |
|
113 |
QMutex *m_mutex; |
|
114 |
}; |
|
115 |
||
116 |
/* |
|
117 |
* This class is designed to aid in implementing event handling in a more round robin fashion. We |
|
118 |
* cannot change active objects that we do not own, but the active objects that Qt owns will use |
|
119 |
* this as a base class with convenience functions. |
|
120 |
* |
|
121 |
* Here is how it works: On every RunL, the deriving class should call okToRun(). This will allow |
|
122 |
* exactly one run of the active object, and mark it as such. If it is called again, it will return |
|
123 |
* false, and add the object to a queue so it can be run later. |
|
124 |
* |
|
125 |
* The QCompleteDeferredAOs class is a special object that runs after all others, which will |
|
126 |
* reactivate the objects that were previously not run. |
|
127 |
*/ |
|
128 |
inline QActiveObject::QActiveObject(TInt priority, QEventDispatcherSymbian *dispatcher) |
|
129 |
: CActive(priority), |
|
130 |
m_dispatcher(dispatcher), |
|
131 |
m_hasAlreadyRun(false), |
|
132 |
m_hasRunAgain(false), |
|
133 |
m_iterationCount(1) |
|
134 |
{ |
|
135 |
} |
|
136 |
||
137 |
QActiveObject::~QActiveObject() |
|
138 |
{ |
|
139 |
if (m_hasRunAgain) |
|
140 |
m_dispatcher->removeDeferredActiveObject(this); |
|
141 |
} |
|
142 |
||
143 |
bool QActiveObject::okToRun() |
|
144 |
{ |
|
145 |
Q_ASSERT(!m_hasRunAgain); |
|
146 |
||
147 |
if (!m_hasAlreadyRun || m_dispatcher->iterationCount() != m_iterationCount) { |
|
148 |
// First occurrence of this event in this iteration. |
|
149 |
m_hasAlreadyRun = true; |
|
150 |
m_iterationCount = m_dispatcher->iterationCount(); |
|
151 |
return true; |
|
152 |
} else { |
|
153 |
// The event has already occurred. |
|
154 |
m_dispatcher->addDeferredActiveObject(this); |
|
155 |
m_hasRunAgain = true; |
|
156 |
return false; |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
void QActiveObject::reactivateAndComplete() |
|
161 |
{ |
|
162 |
iStatus = KRequestPending; |
|
163 |
SetActive(); |
|
164 |
TRequestStatus *status = &iStatus; |
|
165 |
QEventDispatcherSymbian::RequestComplete(status, KErrNone); |
|
166 |
||
167 |
m_hasRunAgain = false; |
|
168 |
m_hasAlreadyRun = false; |
|
169 |
} |
|
170 |
||
171 |
QWakeUpActiveObject::QWakeUpActiveObject(QEventDispatcherSymbian *dispatcher) |
|
172 |
: CActive(WAKE_UP_PRIORITY), |
|
173 |
m_dispatcher(dispatcher) |
|
174 |
{ |
|
175 |
CActiveScheduler::Add(this); |
|
176 |
iStatus = KRequestPending; |
|
177 |
SetActive(); |
|
178 |
} |
|
179 |
||
180 |
QWakeUpActiveObject::~QWakeUpActiveObject() |
|
181 |
{ |
|
182 |
Cancel(); |
|
183 |
} |
|
184 |
||
185 |
void QWakeUpActiveObject::DoCancel() |
|
186 |
{ |
|
187 |
if (iStatus.Int() == KRequestPending) { |
|
188 |
TRequestStatus *status = &iStatus; |
|
189 |
QEventDispatcherSymbian::RequestComplete(status, KErrNone); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
void QWakeUpActiveObject::RunL() |
|
194 |
{ |
|
195 |
iStatus = KRequestPending; |
|
196 |
SetActive(); |
|
197 |
QT_TRYCATCH_LEAVING(m_dispatcher->wakeUpWasCalled()); |
|
198 |
} |
|
199 |
||
200 |
QTimerActiveObject::QTimerActiveObject(QEventDispatcherSymbian *dispatcher, SymbianTimerInfo *timerInfo) |
|
201 |
: QActiveObject((timerInfo->interval) ? TIMER_PRIORITY : NULLTIMER_PRIORITY , dispatcher), |
|
202 |
m_timerInfo(timerInfo) |
|
203 |
{ |
|
204 |
} |
|
205 |
||
206 |
QTimerActiveObject::~QTimerActiveObject() |
|
207 |
{ |
|
208 |
Cancel(); |
|
209 |
} |
|
210 |
||
211 |
void QTimerActiveObject::DoCancel() |
|
212 |
{ |
|
213 |
if (m_timerInfo->interval > 0) { |
|
214 |
m_rTimer.Cancel(); |
|
215 |
m_rTimer.Close(); |
|
216 |
} else { |
|
217 |
if (iStatus.Int() == KRequestPending) { |
|
218 |
TRequestStatus *status = &iStatus; |
|
219 |
QEventDispatcherSymbian::RequestComplete(status, KErrNone); |
|
220 |
} |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
void QTimerActiveObject::RunL() |
|
225 |
{ |
|
226 |
int error; |
|
227 |
QT_TRYCATCH_ERROR(error, Run()); |
|
228 |
// All Symbian error codes are negative. |
|
229 |
if (error < 0) { |
|
230 |
CActiveScheduler::Current()->Error(error); // stop and report here, as this timer will be deleted on scope exit |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
#define MAX_SYMBIAN_TIMEOUT_MS 2000000 |
|
235 |
void QTimerActiveObject::StartTimer() |
|
236 |
{ |
|
237 |
if (m_timerInfo->msLeft > MAX_SYMBIAN_TIMEOUT_MS) { |
|
238 |
//There is loss of accuracy anyway due to needing to restart the timer every 33 minutes, |
|
239 |
//so the 1/64s res of After() is acceptable for these very long timers. |
|
240 |
m_rTimer.After(iStatus, MAX_SYMBIAN_TIMEOUT_MS * 1000); |
|
241 |
m_timerInfo->msLeft -= MAX_SYMBIAN_TIMEOUT_MS; |
|
242 |
} else { |
|
243 |
//HighRes gives the 1ms accuracy expected by Qt, the +1 is to ensure that |
|
244 |
//"Timers will never time out earlier than the specified timeout value" |
|
245 |
//condition is always met. |
|
246 |
m_rTimer.HighRes(iStatus, (m_timerInfo->msLeft + 1) * 1000); |
|
247 |
m_timerInfo->msLeft = 0; |
|
248 |
} |
|
249 |
SetActive(); |
|
250 |
} |
|
251 |
||
252 |
void QTimerActiveObject::Run() |
|
253 |
{ |
|
254 |
//restart timer immediately, if the timeout has been split because it overflows max for platform. |
|
255 |
if (m_timerInfo->msLeft > 0) { |
|
256 |
StartTimer(); |
|
257 |
return; |
|
258 |
} |
|
259 |
||
260 |
if (!okToRun()) |
|
261 |
return; |
|
262 |
||
263 |
if (m_timerInfo->interval > 0) { |
|
264 |
// Start a new timer immediately so that we don't lose time. |
|
265 |
m_timerInfo->msLeft = m_timerInfo->interval; |
|
266 |
StartTimer(); |
|
267 |
||
268 |
m_timerInfo->dispatcher->timerFired(m_timerInfo->timerId); |
|
269 |
} else { |
|
270 |
// However, we only complete zero timers after the event has finished, |
|
271 |
// in order to prevent busy looping when doing nested loops. |
|
272 |
||
273 |
// Keep the refpointer around in order to avoid deletion until the end of this function. |
|
274 |
SymbianTimerInfoPtr timerInfoPtr(m_timerInfo); |
|
275 |
||
276 |
m_timerInfo->dispatcher->timerFired(m_timerInfo->timerId); |
|
277 |
||
278 |
iStatus = KRequestPending; |
|
279 |
SetActive(); |
|
280 |
TRequestStatus *status = &iStatus; |
|
281 |
QEventDispatcherSymbian::RequestComplete(status, KErrNone); |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
void QTimerActiveObject::Start() |
|
286 |
{ |
|
287 |
CActiveScheduler::Add(this); |
|
288 |
m_timerInfo->msLeft = m_timerInfo->interval; |
|
289 |
if (m_timerInfo->interval > 0) { |
|
290 |
m_rTimer.CreateLocal(); |
|
291 |
StartTimer(); |
|
292 |
} else { |
|
293 |
iStatus = KRequestPending; |
|
294 |
SetActive(); |
|
295 |
TRequestStatus *status = &iStatus; |
|
296 |
QEventDispatcherSymbian::RequestComplete(status, KErrNone); |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
SymbianTimerInfo::SymbianTimerInfo() |
|
301 |
: timerAO(0) |
|
302 |
{ |
|
303 |
} |
|
304 |
||
305 |
SymbianTimerInfo::~SymbianTimerInfo() |
|
306 |
{ |
|
307 |
delete timerAO; |
|
308 |
} |
|
309 |
||
310 |
QCompleteDeferredAOs::QCompleteDeferredAOs(QEventDispatcherSymbian *dispatcher) |
|
311 |
: CActive(COMPLETE_DEFERRED_ACTIVE_OBJECTS_PRIORITY), |
|
312 |
m_dispatcher(dispatcher) |
|
313 |
{ |
|
314 |
CActiveScheduler::Add(this); |
|
315 |
iStatus = KRequestPending; |
|
316 |
SetActive(); |
|
317 |
} |
|
318 |
||
319 |
QCompleteDeferredAOs::~QCompleteDeferredAOs() |
|
320 |
{ |
|
321 |
Cancel(); |
|
322 |
} |
|
323 |
||
324 |
void QCompleteDeferredAOs::complete() |
|
325 |
{ |
|
326 |
if (iStatus.Int() == KRequestPending) { |
|
327 |
TRequestStatus *status = &iStatus; |
|
328 |
QEventDispatcherSymbian::RequestComplete(status, KErrNone); |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
void QCompleteDeferredAOs::DoCancel() |
|
333 |
{ |
|
334 |
if (iStatus.Int() == KRequestPending) { |
|
335 |
TRequestStatus *status = &iStatus; |
|
336 |
QEventDispatcherSymbian::RequestComplete(status, KErrNone); |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
void QCompleteDeferredAOs::RunL() |
|
341 |
{ |
|
342 |
iStatus = KRequestPending; |
|
343 |
SetActive(); |
|
344 |
||
345 |
QT_TRYCATCH_LEAVING(m_dispatcher->reactivateDeferredActiveObjects()); |
|
346 |
} |
|
347 |
||
348 |
QSelectThread::QSelectThread() |
|
349 |
: m_quit(false) |
|
350 |
{ |
|
351 |
if (::pipe(m_pipeEnds) != 0) { |
|
352 |
qWarning("Select thread was unable to open a pipe, errno: %i", errno); |
|
353 |
} else { |
|
354 |
int flags0 = qt_pipe_fcntl(m_pipeEnds[0], F_GETFL); |
|
355 |
int flags1 = qt_pipe_fcntl(m_pipeEnds[1], F_GETFL); |
|
356 |
// We should check the error code here, but Open C has a bug that returns |
|
357 |
// failure even though the operation was successful. |
|
358 |
qt_pipe2_fcntl(m_pipeEnds[0], F_SETFL, flags0 | O_NONBLOCK); |
|
359 |
qt_pipe2_fcntl(m_pipeEnds[1], F_SETFL, flags1 | O_NONBLOCK); |
|
360 |
} |
|
361 |
} |
|
362 |
||
363 |
QSelectThread::~QSelectThread() |
|
364 |
{ |
|
365 |
qt_pipe_close(m_pipeEnds[1]); |
|
366 |
qt_pipe_close(m_pipeEnds[0]); |
|
367 |
} |
|
368 |
||
369 |
void QSelectThread::run() |
|
370 |
{ |
|
371 |
Q_D(QThread); |
|
372 |
||
373 |
m_mutex.lock(); |
|
374 |
||
375 |
while (!m_quit) { |
|
376 |
fd_set readfds; |
|
377 |
fd_set writefds; |
|
378 |
fd_set exceptionfds; |
|
379 |
||
380 |
FD_ZERO(&readfds); |
|
381 |
FD_ZERO(&writefds); |
|
382 |
FD_ZERO(&exceptionfds); |
|
383 |
||
384 |
int maxfd = 0; |
|
385 |
maxfd = qMax(maxfd, updateSocketSet(QSocketNotifier::Read, &readfds)); |
|
386 |
maxfd = qMax(maxfd, updateSocketSet(QSocketNotifier::Write, &writefds)); |
|
387 |
maxfd = qMax(maxfd, updateSocketSet(QSocketNotifier::Exception, &exceptionfds)); |
|
388 |
maxfd = qMax(maxfd, m_pipeEnds[0]); |
|
389 |
maxfd++; |
|
390 |
||
391 |
FD_SET(m_pipeEnds[0], &readfds); |
|
392 |
||
393 |
int ret; |
|
394 |
int savedSelectErrno; |
|
395 |
ret = qt_socket_select(maxfd, &readfds, &writefds, &exceptionfds, 0); |
|
396 |
savedSelectErrno = errno; |
|
397 |
||
398 |
char buffer; |
|
399 |
||
400 |
while (::read(m_pipeEnds[0], &buffer, 1) > 0) {} |
|
401 |
||
402 |
if(ret == 0) { |
|
403 |
// do nothing |
|
404 |
} else if (ret < 0) { |
|
405 |
switch (savedSelectErrno) { |
|
406 |
case EBADF: |
|
407 |
case EINVAL: |
|
408 |
case ENOMEM: |
|
409 |
case EFAULT: |
|
410 |
qWarning("::select() returned an error: %i", savedSelectErrno); |
|
411 |
break; |
|
412 |
case ECONNREFUSED: |
|
413 |
case EPIPE: |
|
414 |
qWarning("::select() returned an error: %i (go through sockets)", savedSelectErrno); |
|
415 |
// prepare to go through all sockets |
|
416 |
// mark in fd sets both: |
|
417 |
// good ones |
|
418 |
// ones that return -1 in select |
|
419 |
// after loop update notifiers for all of them |
|
420 |
||
421 |
// as we dont have "exception" notifier type |
|
422 |
// we should force monitoring fd_set of this |
|
423 |
// type as well |
|
424 |
||
425 |
// clean @ start |
|
426 |
FD_ZERO(&readfds); |
|
427 |
FD_ZERO(&writefds); |
|
428 |
FD_ZERO(&exceptionfds); |
|
429 |
for (QHash<QSocketNotifier *, TRequestStatus *>::const_iterator i = m_AOStatuses.begin(); |
|
430 |
i != m_AOStatuses.end(); ++i) { |
|
431 |
||
432 |
fd_set onefds; |
|
433 |
FD_ZERO(&onefds); |
|
434 |
FD_SET(i.key()->socket(), &onefds); |
|
435 |
||
436 |
fd_set excfds; |
|
437 |
FD_ZERO(&excfds); |
|
438 |
FD_SET(i.key()->socket(), &excfds); |
|
439 |
||
440 |
maxfd = i.key()->socket() + 1; |
|
441 |
||
442 |
struct timeval timeout; |
|
443 |
timeout.tv_sec = 0; |
|
444 |
timeout.tv_usec = 0; |
|
445 |
||
446 |
ret = 0; |
|
447 |
||
448 |
if(i.key()->type() == QSocketNotifier::Read) { |
|
449 |
ret = ::select(maxfd, &onefds, 0, &excfds, &timeout); |
|
450 |
if(ret != 0) FD_SET(i.key()->socket(), &readfds); |
|
451 |
} else if(i.key()->type() == QSocketNotifier::Write) { |
|
452 |
ret = ::select(maxfd, 0, &onefds, &excfds, &timeout); |
|
453 |
if(ret != 0) FD_SET(i.key()->socket(), &writefds); |
|
454 |
} |
|
455 |
||
456 |
} // end for |
|
457 |
||
458 |
// traversed all, so update |
|
459 |
updateActivatedNotifiers(QSocketNotifier::Read, &readfds); |
|
460 |
updateActivatedNotifiers(QSocketNotifier::Write, &writefds); |
|
461 |
updateActivatedNotifiers(QSocketNotifier::Exception, &exceptionfds); |
|
462 |
||
463 |
break; |
|
464 |
case EINTR: // Should never occur on Symbian, but this is future proof! |
|
465 |
default: |
|
466 |
qWarning("::select() returned an unknown error: %i", savedSelectErrno); |
|
467 |
||
468 |
break; |
|
469 |
} |
|
470 |
} else { |
|
471 |
updateActivatedNotifiers(QSocketNotifier::Read, &readfds); |
|
472 |
updateActivatedNotifiers(QSocketNotifier::Write, &writefds); |
|
473 |
updateActivatedNotifiers(QSocketNotifier::Exception, &exceptionfds); |
|
474 |
} |
|
475 |
||
476 |
m_waitCond.wait(&m_mutex); |
|
477 |
} |
|
478 |
||
479 |
m_mutex.unlock(); |
|
480 |
} |
|
481 |
||
482 |
void QSelectThread::requestSocketEvents ( QSocketNotifier *notifier, TRequestStatus *status ) |
|
483 |
{ |
|
484 |
Q_D(QThread); |
|
485 |
||
486 |
if (!isRunning()) { |
|
487 |
start(); |
|
488 |
} |
|
489 |
||
490 |
QSelectMutexGrabber lock(m_pipeEnds[1], &m_mutex); |
|
491 |
||
492 |
Q_ASSERT(!m_AOStatuses.contains(notifier)); |
|
493 |
||
494 |
m_AOStatuses.insert(notifier, status); |
|
495 |
||
496 |
m_waitCond.wakeAll(); |
|
497 |
} |
|
498 |
||
499 |
void QSelectThread::cancelSocketEvents ( QSocketNotifier *notifier ) |
|
500 |
{ |
|
501 |
QSelectMutexGrabber lock(m_pipeEnds[1], &m_mutex); |
|
502 |
||
503 |
m_AOStatuses.remove(notifier); |
|
504 |
||
505 |
m_waitCond.wakeAll(); |
|
506 |
} |
|
507 |
||
508 |
void QSelectThread::restart() |
|
509 |
{ |
|
510 |
QSelectMutexGrabber lock(m_pipeEnds[1], &m_mutex); |
|
511 |
||
512 |
m_waitCond.wakeAll(); |
|
513 |
} |
|
514 |
||
515 |
int QSelectThread::updateSocketSet(QSocketNotifier::Type type, fd_set *fds) |
|
516 |
{ |
|
517 |
int maxfd = 0; |
|
518 |
if(m_AOStatuses.isEmpty()) { |
|
519 |
/* |
|
520 |
* Wonder if should return -1 |
|
521 |
* to signal that no descriptors |
|
522 |
* added to fds |
|
523 |
*/ |
|
524 |
return maxfd; |
|
525 |
} |
|
526 |
for ( QHash<QSocketNotifier *, TRequestStatus *>::const_iterator i = m_AOStatuses.begin(); |
|
527 |
i != m_AOStatuses.end(); ++i) { |
|
528 |
if (i.key()->type() == type) { |
|
529 |
FD_SET(i.key()->socket(), fds); |
|
530 |
maxfd = qMax(maxfd, i.key()->socket()); |
|
531 |
} else if(type == QSocketNotifier::Exception) { |
|
532 |
/* |
|
533 |
* We are registering existing sockets |
|
534 |
* always to exception set |
|
535 |
* |
|
536 |
* Doing double FD_SET shouldn't |
|
537 |
* matter |
|
538 |
*/ |
|
539 |
FD_SET(i.key()->socket(), fds); |
|
540 |
maxfd = qMax(maxfd, i.key()->socket()); |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
return maxfd; |
|
545 |
} |
|
546 |
||
547 |
void QSelectThread::updateActivatedNotifiers(QSocketNotifier::Type type, fd_set *fds) |
|
548 |
{ |
|
549 |
Q_D(QThread); |
|
550 |
if(m_AOStatuses.isEmpty()) { |
|
551 |
return; |
|
552 |
} |
|
553 |
QList<QSocketNotifier *> toRemove; |
|
554 |
for (QHash<QSocketNotifier *, TRequestStatus *>::const_iterator i = m_AOStatuses.begin(); |
|
555 |
i != m_AOStatuses.end(); ++i) { |
|
556 |
if (i.key()->type() == type && FD_ISSET(i.key()->socket(), fds)) { |
|
557 |
toRemove.append(i.key()); |
|
558 |
TRequestStatus *status = i.value(); |
|
559 |
// Thread data is still owned by the main thread. |
|
560 |
QEventDispatcherSymbian::RequestComplete(d->threadData->symbian_thread_handle, status, KErrNone); |
|
561 |
} else if(type == QSocketNotifier::Exception && FD_ISSET(i.key()->socket(), fds)) { |
|
562 |
/* |
|
563 |
* check if socket is in exception set |
|
564 |
* then signal RequestComplete for it |
|
565 |
*/ |
|
566 |
qWarning("exception on %d", i.key()->socket()); |
|
567 |
toRemove.append(i.key()); |
|
568 |
TRequestStatus *status = i.value(); |
|
569 |
QEventDispatcherSymbian::RequestComplete(d->threadData->symbian_thread_handle, status, KErrNone); |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
for (int c = 0; c < toRemove.size(); ++c) { |
|
574 |
m_AOStatuses.remove(toRemove[c]); |
|
575 |
} |
|
576 |
} |
|
577 |
||
578 |
void QSelectThread::stop() |
|
579 |
{ |
|
580 |
m_quit = true; |
|
581 |
restart(); |
|
582 |
wait(); |
|
583 |
} |
|
584 |
||
585 |
QSocketActiveObject::QSocketActiveObject(QEventDispatcherSymbian *dispatcher, QSocketNotifier *notifier) |
|
586 |
: QActiveObject(CActive::EPriorityStandard, dispatcher), |
|
587 |
m_notifier(notifier), |
|
588 |
m_inSocketEvent(false), |
|
589 |
m_deleteLater(false) |
|
590 |
{ |
|
591 |
CActiveScheduler::Add(this); |
|
592 |
iStatus = KRequestPending; |
|
593 |
SetActive(); |
|
594 |
} |
|
595 |
||
596 |
QSocketActiveObject::~QSocketActiveObject() |
|
597 |
{ |
|
598 |
Cancel(); |
|
599 |
} |
|
600 |
||
601 |
void QSocketActiveObject::DoCancel() |
|
602 |
{ |
|
603 |
if (iStatus.Int() == KRequestPending) { |
|
604 |
TRequestStatus *status = &iStatus; |
|
605 |
QEventDispatcherSymbian::RequestComplete(status, KErrNone); |
|
606 |
} |
|
607 |
} |
|
608 |
||
609 |
void QSocketActiveObject::RunL() |
|
610 |
{ |
|
611 |
if (!okToRun()) |
|
612 |
return; |
|
613 |
||
614 |
QT_TRYCATCH_LEAVING(m_dispatcher->socketFired(this)); |
|
615 |
} |
|
616 |
||
617 |
void QSocketActiveObject::deleteLater() |
|
618 |
{ |
|
619 |
if (m_inSocketEvent) { |
|
620 |
m_deleteLater = true; |
|
621 |
} else { |
|
622 |
delete this; |
|
623 |
} |
|
624 |
} |
|
625 |
||
626 |
QEventDispatcherSymbian::QEventDispatcherSymbian(QObject *parent) |
|
627 |
: QAbstractEventDispatcher(parent), |
|
628 |
m_activeScheduler(0), |
|
629 |
m_wakeUpAO(0), |
|
630 |
m_completeDeferredAOs(0), |
|
631 |
m_interrupt(false), |
|
632 |
m_wakeUpDone(0), |
|
633 |
m_iterationCount(0), |
|
634 |
m_noSocketEvents(false) |
|
635 |
{ |
|
636 |
} |
|
637 |
||
638 |
QEventDispatcherSymbian::~QEventDispatcherSymbian() |
|
639 |
{ |
|
640 |
m_processHandle.Close(); |
|
641 |
} |
|
642 |
||
643 |
void QEventDispatcherSymbian::startingUp() |
|
644 |
{ |
|
645 |
if( !CActiveScheduler::Current() ) { |
|
646 |
m_activeScheduler = q_check_ptr(new CQtActiveScheduler()); // CBase derived class needs to be checked on new |
|
647 |
CActiveScheduler::Install(m_activeScheduler); |
|
648 |
} |
|
649 |
m_wakeUpAO = q_check_ptr(new QWakeUpActiveObject(this)); |
|
650 |
m_completeDeferredAOs = q_check_ptr(new QCompleteDeferredAOs(this)); |
|
651 |
// We already might have posted events, wakeup once to process them |
|
652 |
wakeUp(); |
|
653 |
} |
|
654 |
||
655 |
void QEventDispatcherSymbian::closingDown() |
|
656 |
{ |
|
657 |
if (m_selectThread.isRunning()) { |
|
658 |
m_selectThread.stop(); |
|
659 |
} |
|
660 |
||
661 |
delete m_completeDeferredAOs; |
|
662 |
delete m_wakeUpAO; |
|
663 |
if (m_activeScheduler) { |
|
664 |
delete m_activeScheduler; |
|
665 |
} |
|
666 |
} |
|
667 |
||
668 |
bool QEventDispatcherSymbian::processEvents ( QEventLoop::ProcessEventsFlags flags ) |
|
669 |
{ |
|
670 |
bool handledAnyEvent = false; |
|
671 |
bool oldNoSocketEventsValue = m_noSocketEvents; |
|
672 |
||
673 |
QT_TRY { |
|
674 |
Q_D(QAbstractEventDispatcher); |
|
675 |
||
676 |
// It is safe if this counter overflows. The main importance is that each |
|
677 |
// iteration count is different from the last. |
|
678 |
m_iterationCount++; |
|
679 |
||
680 |
RThread &thread = d->threadData->symbian_thread_handle; |
|
681 |
||
682 |
bool block; |
|
683 |
if (flags & QEventLoop::WaitForMoreEvents) { |
|
684 |
block = true; |
|
685 |
emit aboutToBlock(); |
|
686 |
} else { |
|
687 |
block = false; |
|
688 |
} |
|
689 |
||
690 |
if (flags & QEventLoop::ExcludeSocketNotifiers) { |
|
691 |
m_noSocketEvents = true; |
|
692 |
} else { |
|
693 |
m_noSocketEvents = false; |
|
694 |
handledAnyEvent = sendDeferredSocketEvents(); |
|
695 |
} |
|
696 |
||
697 |
bool handledSymbianEvent = false; |
|
698 |
m_interrupt = false; |
|
699 |
||
700 |
/* |
|
701 |
* This QTime variable is used to measure the time it takes to finish |
|
702 |
* the event loop. If we take too long in the loop, other processes |
|
703 |
* may be starved and killed. After the first event has completed, we |
|
704 |
* take the current time, and if the remaining events take longer than |
|
705 |
* a preset time, we temporarily lower the priority to force a context |
|
706 |
* switch. For applications that do not take unecessarily long in the |
|
707 |
* event loop, the priority will not be altered. |
|
708 |
*/ |
|
709 |
QTime time; |
|
710 |
enum { |
|
711 |
FirstRun, |
|
712 |
SubsequentRun, |
|
713 |
TimeStarted |
|
714 |
} timeState = FirstRun; |
|
715 |
||
716 |
TProcessPriority priority; |
|
717 |
||
718 |
while (1) { |
|
719 |
if (block) { |
|
720 |
// This is where Qt will spend most of its time. |
|
721 |
CActiveScheduler::Current()->WaitForAnyRequest(); |
|
722 |
} else { |
|
723 |
if (thread.RequestCount() == 0) { |
|
724 |
break; |
|
725 |
} |
|
726 |
// This one should return without delay. |
|
727 |
CActiveScheduler::Current()->WaitForAnyRequest(); |
|
728 |
} |
|
729 |
||
730 |
if (timeState == SubsequentRun) { |
|
731 |
time.start(); |
|
732 |
timeState = TimeStarted; |
|
733 |
} |
|
734 |
||
735 |
TInt error; |
|
736 |
handledSymbianEvent = CActiveScheduler::RunIfReady(error, CActive::EPriorityIdle); |
|
737 |
if (error) { |
|
738 |
qWarning("CActiveScheduler::RunIfReady() returned error: %i\n", error); |
|
739 |
CActiveScheduler::Current()->Error(error); |
|
740 |
} |
|
741 |
||
742 |
if (!handledSymbianEvent) { |
|
743 |
qFatal("QEventDispatcherSymbian::processEvents(): Caught Symbian stray signal"); |
|
744 |
} |
|
745 |
handledAnyEvent = true; |
|
746 |
if (m_interrupt) { |
|
747 |
break; |
|
748 |
} |
|
749 |
block = false; |
|
750 |
if (timeState == TimeStarted && time.elapsed() > 100) { |
|
751 |
priority = m_processHandle.Priority(); |
|
752 |
m_processHandle.SetPriority(EPriorityBackground); |
|
753 |
time.start(); |
|
754 |
// Slight chance of race condition in the next lines, but nothing fatal |
|
755 |
// will happen, just wrong priority. |
|
756 |
if (m_processHandle.Priority() == EPriorityBackground) { |
|
757 |
m_processHandle.SetPriority(priority); |
|
758 |
} |
|
759 |
} |
|
760 |
if (timeState == FirstRun) |
|
761 |
timeState = SubsequentRun; |
|
762 |
}; |
|
763 |
||
764 |
emit awake(); |
|
765 |
} QT_CATCH (const std::exception& ex) { |
|
766 |
#ifndef QT_NO_EXCEPTIONS |
|
767 |
CActiveScheduler::Current()->Error(qt_symbian_exception2Error(ex)); |
|
768 |
#endif |
|
769 |
} |
|
770 |
||
771 |
m_noSocketEvents = oldNoSocketEventsValue; |
|
772 |
||
773 |
return handledAnyEvent; |
|
774 |
} |
|
775 |
||
776 |
void QEventDispatcherSymbian::timerFired(int timerId) |
|
777 |
{ |
|
778 |
QHash<int, SymbianTimerInfoPtr>::iterator i = m_timerList.find(timerId); |
|
779 |
if (i == m_timerList.end()) { |
|
780 |
// The timer has been deleted. Ignore this event. |
|
781 |
return; |
|
782 |
} |
|
783 |
||
784 |
SymbianTimerInfoPtr timerInfo = *i; |
|
785 |
||
786 |
// Prevent infinite timer recursion. |
|
787 |
if (timerInfo->inTimerEvent) { |
|
788 |
return; |
|
789 |
} |
|
790 |
||
791 |
timerInfo->inTimerEvent = true; |
|
792 |
||
793 |
QTimerEvent event(timerInfo->timerId); |
|
794 |
QCoreApplication::sendEvent(timerInfo->receiver, &event); |
|
795 |
||
796 |
timerInfo->inTimerEvent = false; |
|
797 |
||
798 |
return; |
|
799 |
} |
|
800 |
||
801 |
void QEventDispatcherSymbian::socketFired(QSocketActiveObject *socketAO) |
|
802 |
{ |
|
803 |
if (m_noSocketEvents) { |
|
804 |
m_deferredSocketEvents.append(socketAO); |
|
805 |
return; |
|
806 |
} |
|
807 |
||
808 |
QEvent e(QEvent::SockAct); |
|
809 |
socketAO->m_inSocketEvent = true; |
|
810 |
QCoreApplication::sendEvent(socketAO->m_notifier, &e); |
|
811 |
socketAO->m_inSocketEvent = false; |
|
812 |
||
813 |
if (socketAO->m_deleteLater) { |
|
814 |
delete socketAO; |
|
815 |
} else { |
|
816 |
socketAO->iStatus = KRequestPending; |
|
817 |
socketAO->SetActive(); |
|
818 |
reactivateSocketNotifier(socketAO->m_notifier); |
|
819 |
} |
|
820 |
} |
|
821 |
||
822 |
void QEventDispatcherSymbian::wakeUpWasCalled() |
|
823 |
{ |
|
824 |
// The reactivation should happen in RunL, right before the call to this function. |
|
825 |
// This is because m_wakeUpDone is the "signal" that the object can be completed |
|
826 |
// once more. |
|
827 |
// Also, by dispatching the posted events after resetting m_wakeUpDone, we guarantee |
|
828 |
// that no posted event notification will be lost. If we did it the other way |
|
829 |
// around, it would be possible for another thread to post an event right after |
|
830 |
// the sendPostedEvents was done, but before the object was ready to be completed |
|
831 |
// again. This could deadlock the application if there are no other posted events. |
|
832 |
m_wakeUpDone.fetchAndStoreOrdered(0); |
|
833 |
sendPostedEvents(); |
|
834 |
} |
|
835 |
||
836 |
void QEventDispatcherSymbian::interrupt() |
|
837 |
{ |
|
838 |
m_interrupt = true; |
|
839 |
wakeUp(); |
|
840 |
} |
|
841 |
||
842 |
void QEventDispatcherSymbian::wakeUp() |
|
843 |
{ |
|
844 |
Q_D(QAbstractEventDispatcher); |
|
845 |
||
846 |
if (m_wakeUpAO && m_wakeUpDone.testAndSetAcquire(0, 1)) { |
|
847 |
TRequestStatus *status = &m_wakeUpAO->iStatus; |
|
848 |
QEventDispatcherSymbian::RequestComplete(d->threadData->symbian_thread_handle, status, KErrNone); |
|
849 |
} |
|
850 |
} |
|
851 |
||
852 |
bool QEventDispatcherSymbian::sendPostedEvents() |
|
853 |
{ |
|
854 |
Q_D(QAbstractEventDispatcher); |
|
855 |
||
856 |
// moveToThread calls this and canWait == true -> Events will never get processed |
|
857 |
// if we check for d->threadData->canWait |
|
858 |
// |
|
859 |
// QCoreApplication::postEvent sets canWait = false, but after the object and events |
|
860 |
// are moved to a new thread, the canWait in new thread is true i.e. not changed to reflect |
|
861 |
// the flag on old thread. That's why events in a new thread will not get processed. |
|
862 |
// This migth be actually bug in moveToThread functionality, but because other platforms |
|
863 |
// do not check canWait in wakeUp (where we essentially are now) - decided to remove it from |
|
864 |
// here as well. |
|
865 |
||
866 |
//if (!d->threadData->canWait) { |
|
867 |
QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData); |
|
868 |
return true; |
|
869 |
//} |
|
870 |
//return false; |
|
871 |
} |
|
872 |
||
873 |
inline void QEventDispatcherSymbian::addDeferredActiveObject(QActiveObject *object) |
|
874 |
{ |
|
875 |
if (m_deferredActiveObjects.isEmpty()) { |
|
876 |
m_completeDeferredAOs->complete(); |
|
877 |
} |
|
878 |
m_deferredActiveObjects.append(object); |
|
879 |
} |
|
880 |
||
881 |
inline void QEventDispatcherSymbian::removeDeferredActiveObject(QActiveObject *object) |
|
882 |
{ |
|
883 |
m_deferredActiveObjects.removeAll(object); |
|
884 |
} |
|
885 |
||
886 |
void QEventDispatcherSymbian::reactivateDeferredActiveObjects() |
|
887 |
{ |
|
888 |
while (!m_deferredActiveObjects.isEmpty()) { |
|
889 |
QActiveObject *object = m_deferredActiveObjects.takeFirst(); |
|
890 |
object->reactivateAndComplete(); |
|
891 |
} |
|
892 |
||
893 |
// We do this because we want to return from processEvents. This is because |
|
894 |
// each invocation of processEvents should only run each active object once. |
|
895 |
// The active scheduler should run them continously, however. |
|
896 |
m_interrupt = true; |
|
897 |
} |
|
898 |
||
899 |
bool QEventDispatcherSymbian::sendDeferredSocketEvents() |
|
900 |
{ |
|
901 |
bool sentAnyEvents = false; |
|
902 |
while (!m_deferredSocketEvents.isEmpty()) { |
|
903 |
sentAnyEvents = true; |
|
904 |
socketFired(m_deferredSocketEvents.takeFirst()); |
|
905 |
} |
|
906 |
||
907 |
return sentAnyEvents; |
|
908 |
} |
|
909 |
||
910 |
void QEventDispatcherSymbian::flush() |
|
911 |
{ |
|
912 |
} |
|
913 |
||
914 |
bool QEventDispatcherSymbian::hasPendingEvents() |
|
915 |
{ |
|
916 |
Q_D(QAbstractEventDispatcher); |
|
917 |
return (d->threadData->symbian_thread_handle.RequestCount() != 0 |
|
918 |
|| !d->threadData->canWait || !m_deferredSocketEvents.isEmpty()); |
|
919 |
} |
|
920 |
||
921 |
void QEventDispatcherSymbian::registerSocketNotifier ( QSocketNotifier * notifier ) |
|
922 |
{ |
|
923 |
QSocketActiveObject *socketAO = q_check_ptr(new QSocketActiveObject(this, notifier)); |
|
924 |
m_notifiers.insert(notifier, socketAO); |
|
925 |
m_selectThread.requestSocketEvents(notifier, &socketAO->iStatus); |
|
926 |
} |
|
927 |
||
928 |
void QEventDispatcherSymbian::unregisterSocketNotifier ( QSocketNotifier * notifier ) |
|
929 |
{ |
|
930 |
m_selectThread.cancelSocketEvents(notifier); |
|
931 |
if (m_notifiers.contains(notifier)) { |
|
932 |
QSocketActiveObject *sockObj = *m_notifiers.find(notifier); |
|
933 |
m_deferredSocketEvents.removeAll(sockObj); |
|
934 |
sockObj->deleteLater(); |
|
935 |
m_notifiers.remove(notifier); |
|
936 |
} |
|
937 |
} |
|
938 |
||
939 |
void QEventDispatcherSymbian::reactivateSocketNotifier(QSocketNotifier *notifier) |
|
940 |
{ |
|
941 |
m_selectThread.requestSocketEvents(notifier, &m_notifiers[notifier]->iStatus); |
|
942 |
} |
|
943 |
||
944 |
void QEventDispatcherSymbian::registerTimer ( int timerId, int interval, QObject * object ) |
|
945 |
{ |
|
946 |
if (interval < 0) { |
|
947 |
qWarning("Timer interval < 0"); |
|
948 |
interval = 0; |
|
949 |
} |
|
950 |
||
951 |
SymbianTimerInfoPtr timer(new SymbianTimerInfo); |
|
952 |
timer->timerId = timerId; |
|
953 |
timer->interval = interval; |
|
954 |
timer->inTimerEvent = false; |
|
955 |
timer->receiver = object; |
|
956 |
timer->dispatcher = this; |
|
957 |
timer->timerAO = q_check_ptr(new QTimerActiveObject(this, timer.data())); |
|
958 |
m_timerList.insert(timerId, timer); |
|
959 |
||
960 |
timer->timerAO->Start(); |
|
961 |
} |
|
962 |
||
963 |
bool QEventDispatcherSymbian::unregisterTimer ( int timerId ) |
|
964 |
{ |
|
965 |
if (!m_timerList.contains(timerId)) { |
|
966 |
return false; |
|
967 |
} |
|
968 |
||
969 |
SymbianTimerInfoPtr timerInfo = m_timerList.take(timerId); |
|
970 |
||
971 |
if (!QObjectPrivate::get(timerInfo->receiver)->inThreadChangeEvent) |
|
972 |
QAbstractEventDispatcherPrivate::releaseTimerId(timerId); |
|
973 |
||
974 |
return true; |
|
975 |
} |
|
976 |
||
977 |
bool QEventDispatcherSymbian::unregisterTimers ( QObject * object ) |
|
978 |
{ |
|
979 |
if (m_timerList.isEmpty()) |
|
980 |
return false; |
|
981 |
||
982 |
bool unregistered = false; |
|
983 |
for (QHash<int, SymbianTimerInfoPtr>::iterator i = m_timerList.begin(); i != m_timerList.end(); ) { |
|
984 |
if ((*i)->receiver == object) { |
|
985 |
i = m_timerList.erase(i); |
|
986 |
unregistered = true; |
|
987 |
} else { |
|
988 |
++i; |
|
989 |
} |
|
990 |
} |
|
991 |
||
992 |
return unregistered; |
|
993 |
} |
|
994 |
||
995 |
QList<QEventDispatcherSymbian::TimerInfo> QEventDispatcherSymbian::registeredTimers ( QObject * object ) const |
|
996 |
{ |
|
997 |
QList<TimerInfo> list; |
|
998 |
for (QHash<int, SymbianTimerInfoPtr>::const_iterator i = m_timerList.begin(); i != m_timerList.end(); ++i) { |
|
999 |
if ((*i)->receiver == object) { |
|
1000 |
list.push_back(TimerInfo((*i)->timerId, (*i)->interval)); |
|
1001 |
} |
|
1002 |
} |
|
1003 |
||
1004 |
return list; |
|
1005 |
} |
|
1006 |
||
1007 |
/* |
|
1008 |
* This active scheduler class implements a simple report and continue policy, for Symbian OS leaves |
|
1009 |
* or exceptions from Qt that fall back to the scheduler. |
|
1010 |
* It will be used in cases where there is no existing active scheduler installed. |
|
1011 |
* Apps which link to qts60main.lib will have the UI active scheduler installed in the main thread |
|
1012 |
* instead of this one. But this would be used in other threads in the UI. |
|
1013 |
* An app could replace this behaviour by installing an alternative active scheduler. |
|
1014 |
*/ |
|
1015 |
void CQtActiveScheduler::Error(TInt aError) const |
|
1016 |
{ |
|
1017 |
QT_TRY { |
|
1018 |
qWarning("Error from active scheduler %d", aError); |
|
1019 |
} |
|
1020 |
QT_CATCH (const std::bad_alloc&) {} // ignore alloc fails, nothing more can be done |
|
1021 |
} |
|
1022 |
||
1023 |
QT_END_NAMESPACE |