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