1 | /* $Id: EventQueue.cpp 31589 2010-08-11 23:04:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer:
|
---|
4 | * Event and EventQueue class declaration
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "VBox/com/EventQueue.h"
|
---|
20 |
|
---|
21 | #ifdef RT_OS_DARWIN
|
---|
22 | # include <CoreFoundation/CFRunLoop.h>
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | #if defined(VBOX_WITH_XPCOM) && !defined(RT_OS_DARWIN) && !defined(RT_OS_OS2)
|
---|
26 | # define USE_XPCOM_QUEUE
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | #include <iprt/err.h>
|
---|
30 | #include <iprt/time.h>
|
---|
31 | #include <iprt/thread.h>
|
---|
32 | #ifdef USE_XPCOM_QUEUE
|
---|
33 | # include <errno.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | namespace com
|
---|
37 | {
|
---|
38 |
|
---|
39 | // EventQueue class
|
---|
40 | ////////////////////////////////////////////////////////////////////////////////
|
---|
41 |
|
---|
42 | #ifndef VBOX_WITH_XPCOM
|
---|
43 |
|
---|
44 | # define CHECK_THREAD_RET(ret) \
|
---|
45 | do { \
|
---|
46 | AssertMsg(GetCurrentThreadId() == mThreadId, ("Must be on event queue thread!")); \
|
---|
47 | if (GetCurrentThreadId() != mThreadId) \
|
---|
48 | return ret; \
|
---|
49 | } while (0)
|
---|
50 |
|
---|
51 | /** Magic LPARAM value for the WM_USER messages that we're posting. */
|
---|
52 | # if ARCH_BITS == 64
|
---|
53 | # define EVENTQUEUE_WIN_LPARAM_MAGIC UINT64_C(0xf241b8196623bb4c)
|
---|
54 | # else
|
---|
55 | # define EVENTQUEUE_WIN_LPARAM_MAGIC UINT32_C(0xf241b819)
|
---|
56 | # endif
|
---|
57 |
|
---|
58 |
|
---|
59 | #else // VBOX_WITH_XPCOM
|
---|
60 |
|
---|
61 | # define CHECK_THREAD_RET(ret) \
|
---|
62 | do { \
|
---|
63 | if (!mEventQ) \
|
---|
64 | return ret; \
|
---|
65 | BOOL isOnCurrentThread = FALSE; \
|
---|
66 | mEventQ->IsOnCurrentThread(&isOnCurrentThread); \
|
---|
67 | AssertMsg(isOnCurrentThread, ("Must be on event queue thread!")); \
|
---|
68 | if (!isOnCurrentThread) \
|
---|
69 | return ret; \
|
---|
70 | } while (0)
|
---|
71 |
|
---|
72 | #endif // VBOX_WITH_XPCOM
|
---|
73 |
|
---|
74 | /** Pointer to the main event queue. */
|
---|
75 | EventQueue *EventQueue::sMainQueue = NULL;
|
---|
76 |
|
---|
77 |
|
---|
78 | #ifdef VBOX_WITH_XPCOM
|
---|
79 |
|
---|
80 | struct MyPLEvent : public PLEvent
|
---|
81 | {
|
---|
82 | MyPLEvent(Event *e) : event(e) {}
|
---|
83 | Event *event;
|
---|
84 | };
|
---|
85 |
|
---|
86 | /* static */
|
---|
87 | void *PR_CALLBACK com::EventQueue::plEventHandler(PLEvent *self)
|
---|
88 | {
|
---|
89 | Event *ev = ((MyPLEvent *)self)->event;
|
---|
90 | if (ev)
|
---|
91 | ev->handler();
|
---|
92 | else
|
---|
93 | {
|
---|
94 | EventQueue *eq = (EventQueue *)self->owner;
|
---|
95 | Assert(eq);
|
---|
96 | eq->mInterrupted = true;
|
---|
97 | }
|
---|
98 | return NULL;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* static */
|
---|
102 | void PR_CALLBACK com::EventQueue::plEventDestructor(PLEvent *self)
|
---|
103 | {
|
---|
104 | Event *ev = ((MyPLEvent *)self)->event;
|
---|
105 | if (ev)
|
---|
106 | delete ev;
|
---|
107 | delete self;
|
---|
108 | }
|
---|
109 |
|
---|
110 | #endif // VBOX_WITH_XPCOM
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Constructs an event queue for the current thread.
|
---|
114 | *
|
---|
115 | * Currently, there can be only one event queue per thread, so if an event
|
---|
116 | * queue for the current thread already exists, this object is simply attached
|
---|
117 | * to the existing event queue.
|
---|
118 | */
|
---|
119 | EventQueue::EventQueue()
|
---|
120 | {
|
---|
121 | #ifndef VBOX_WITH_XPCOM
|
---|
122 |
|
---|
123 | mThreadId = GetCurrentThreadId();
|
---|
124 | // force the system to create the message queue for the current thread
|
---|
125 | MSG msg;
|
---|
126 | PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
---|
127 |
|
---|
128 | if (!DuplicateHandle(GetCurrentProcess(),
|
---|
129 | GetCurrentThread(),
|
---|
130 | GetCurrentProcess(),
|
---|
131 | &mhThread,
|
---|
132 | 0 /*dwDesiredAccess*/,
|
---|
133 | FALSE /*bInheritHandle*/,
|
---|
134 | DUPLICATE_SAME_ACCESS))
|
---|
135 | mhThread = INVALID_HANDLE_VALUE;
|
---|
136 |
|
---|
137 | #else // VBOX_WITH_XPCOM
|
---|
138 |
|
---|
139 | mEQCreated = false;
|
---|
140 | mInterrupted = false;
|
---|
141 |
|
---|
142 | // Here we reference the global nsIEventQueueService instance and hold it
|
---|
143 | // until we're destroyed. This is necessary to keep NS_ShutdownXPCOM() away
|
---|
144 | // from calling StopAcceptingEvents() on all event queues upon destruction of
|
---|
145 | // nsIEventQueueService, and makes sense when, for some reason, this happens
|
---|
146 | // *before* we're able to send a NULL event to stop our event handler thread
|
---|
147 | // when doing unexpected cleanup caused indirectly by NS_ShutdownXPCOM()
|
---|
148 | // that is performing a global cleanup of everything. A good example of such
|
---|
149 | // situation is when NS_ShutdownXPCOM() is called while the VirtualBox component
|
---|
150 | // is still alive (because it is still referenced): eventually, it results in
|
---|
151 | // a VirtualBox::uninit() call from where it is already not possible to post
|
---|
152 | // NULL to the event thread (because it stopped accepting events).
|
---|
153 |
|
---|
154 | nsresult rc = NS_GetEventQueueService(getter_AddRefs(mEventQService));
|
---|
155 |
|
---|
156 | if (NS_SUCCEEDED(rc))
|
---|
157 | {
|
---|
158 | rc = mEventQService->GetThreadEventQueue(NS_CURRENT_THREAD,
|
---|
159 | getter_AddRefs(mEventQ));
|
---|
160 | if (rc == NS_ERROR_NOT_AVAILABLE)
|
---|
161 | {
|
---|
162 | rc = mEventQService->CreateThreadEventQueue();
|
---|
163 | if (NS_SUCCEEDED(rc))
|
---|
164 | {
|
---|
165 | mEQCreated = true;
|
---|
166 | rc = mEventQService->GetThreadEventQueue(NS_CURRENT_THREAD,
|
---|
167 | getter_AddRefs(mEventQ));
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 | AssertComRC(rc);
|
---|
172 |
|
---|
173 | #endif // VBOX_WITH_XPCOM
|
---|
174 | }
|
---|
175 |
|
---|
176 | EventQueue::~EventQueue()
|
---|
177 | {
|
---|
178 | #ifndef VBOX_WITH_XPCOM
|
---|
179 | if (mhThread != INVALID_HANDLE_VALUE)
|
---|
180 | {
|
---|
181 | CloseHandle(mhThread);
|
---|
182 | mhThread = INVALID_HANDLE_VALUE;
|
---|
183 | }
|
---|
184 | #else // VBOX_WITH_XPCOM
|
---|
185 | // process all pending events before destruction
|
---|
186 | if (mEventQ)
|
---|
187 | {
|
---|
188 | if (mEQCreated)
|
---|
189 | {
|
---|
190 | mEventQ->StopAcceptingEvents();
|
---|
191 | mEventQ->ProcessPendingEvents();
|
---|
192 | mEventQService->DestroyThreadEventQueue();
|
---|
193 | }
|
---|
194 | mEventQ = nsnull;
|
---|
195 | mEventQService = nsnull;
|
---|
196 | }
|
---|
197 | #endif // VBOX_WITH_XPCOM
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Initializes the main event queue instance.
|
---|
202 | * @returns VBox status code.
|
---|
203 | *
|
---|
204 | * @remarks If you're using the rest of the COM/XPCOM glue library,
|
---|
205 | * com::Initialize() will take care of initializing and uninitializing
|
---|
206 | * the EventQueue class. If you don't call com::Initialize, you must
|
---|
207 | * make sure to call this method on the same thread that did the
|
---|
208 | * XPCOM initialization or we'll end up using the wrong main queue.
|
---|
209 | */
|
---|
210 | /* static */
|
---|
211 | int EventQueue::init()
|
---|
212 | {
|
---|
213 | Assert(sMainQueue == NULL);
|
---|
214 | Assert(RTThreadIsMain(RTThreadSelf()));
|
---|
215 | sMainQueue = new EventQueue();
|
---|
216 |
|
---|
217 | #ifdef VBOX_WITH_XPCOM
|
---|
218 | /* Check that it actually is the main event queue, i.e. that
|
---|
219 | we're called on the right thread. */
|
---|
220 | nsCOMPtr<nsIEventQueue> q;
|
---|
221 | nsresult rv = NS_GetMainEventQ(getter_AddRefs(q));
|
---|
222 | Assert(NS_SUCCEEDED(rv));
|
---|
223 | Assert(q == sMainQueue->mEventQ);
|
---|
224 |
|
---|
225 | /* Check that it's a native queue. */
|
---|
226 | PRBool fIsNative = PR_FALSE;
|
---|
227 | rv = sMainQueue->mEventQ->IsQueueNative(&fIsNative);
|
---|
228 | Assert(NS_SUCCEEDED(rv) && fIsNative);
|
---|
229 | #endif // VBOX_WITH_XPCOM
|
---|
230 |
|
---|
231 | return VINF_SUCCESS;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Uninitialize the global resources (i.e. the main event queue instance).
|
---|
236 | * @returns VINF_SUCCESS
|
---|
237 | */
|
---|
238 | /* static */
|
---|
239 | int EventQueue::uninit()
|
---|
240 | {
|
---|
241 | Assert(sMainQueue);
|
---|
242 | /* Must process all events to make sure that no NULL event is left
|
---|
243 | * after this point. It would need to modify the state of sMainQueue. */
|
---|
244 | sMainQueue->processEventQueue(0);
|
---|
245 | delete sMainQueue;
|
---|
246 | sMainQueue = NULL;
|
---|
247 | return VINF_SUCCESS;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Get main event queue instance.
|
---|
252 | *
|
---|
253 | * Depends on init() being called first.
|
---|
254 | */
|
---|
255 | /* static */
|
---|
256 | EventQueue* EventQueue::getMainEventQueue()
|
---|
257 | {
|
---|
258 | return sMainQueue;
|
---|
259 | }
|
---|
260 |
|
---|
261 | #ifdef VBOX_WITH_XPCOM
|
---|
262 | # ifdef RT_OS_DARWIN
|
---|
263 | /**
|
---|
264 | * Wait for events and process them (Darwin).
|
---|
265 | *
|
---|
266 | * @retval VINF_SUCCESS
|
---|
267 | * @retval VERR_TIMEOUT
|
---|
268 | * @retval VERR_INTERRUPTED
|
---|
269 | *
|
---|
270 | * @param cMsTimeout How long to wait, or RT_INDEFINITE_WAIT.
|
---|
271 | */
|
---|
272 | static int waitForEventsOnDarwin(RTMSINTERVAL cMsTimeout)
|
---|
273 | {
|
---|
274 | /*
|
---|
275 | * Wait for the requested time, if we get a hit we do a poll to process
|
---|
276 | * any other pending messages.
|
---|
277 | *
|
---|
278 | * Note! About 1.0e10: According to the sources anything above 3.1556952e+9
|
---|
279 | * means indefinite wait and 1.0e10 is what CFRunLoopRun() uses.
|
---|
280 | */
|
---|
281 | CFTimeInterval rdTimeout = cMsTimeout == RT_INDEFINITE_WAIT ? 1e10 : (double)cMsTimeout / 1000;
|
---|
282 | OSStatus orc = CFRunLoopRunInMode(kCFRunLoopDefaultMode, rdTimeout, true /*returnAfterSourceHandled*/);
|
---|
283 | /** @todo Not entire sure if the poll actually processes more than one message.
|
---|
284 | * Feel free to check the sources anyone. */
|
---|
285 | if (orc == kCFRunLoopRunHandledSource)
|
---|
286 | orc = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, false /*returnAfterSourceHandled*/);
|
---|
287 | if ( orc == 0
|
---|
288 | || orc == kCFRunLoopRunHandledSource)
|
---|
289 | return VINF_SUCCESS;
|
---|
290 | if ( orc == kCFRunLoopRunStopped
|
---|
291 | || orc == kCFRunLoopRunFinished)
|
---|
292 | return VERR_INTERRUPTED;
|
---|
293 | AssertMsg(orc == kCFRunLoopRunTimedOut, ("Unexpected status code from CFRunLoopRunInMode: %#x", orc));
|
---|
294 | return VERR_TIMEOUT;
|
---|
295 | }
|
---|
296 | # else // !RT_OS_DARWIN
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Wait for events (generic XPCOM).
|
---|
300 | *
|
---|
301 | * @retval VINF_SUCCESS
|
---|
302 | * @retval VERR_TIMEOUT
|
---|
303 | * @retval VERR_INTERRUPTED
|
---|
304 | * @retval VERR_INTERNAL_ERROR_4
|
---|
305 | *
|
---|
306 | * @param pQueue The queue to wait on.
|
---|
307 | * @param cMsTimeout How long to wait, or RT_INDEFINITE_WAIT.
|
---|
308 | */
|
---|
309 | static int waitForEventsOnXPCOM(nsIEventQueue *pQueue, RTMSINTERVAL cMsTimeout)
|
---|
310 | {
|
---|
311 | int fd = pQueue->GetEventQueueSelectFD();
|
---|
312 | fd_set fdsetR;
|
---|
313 | FD_ZERO(&fdsetR);
|
---|
314 | FD_SET(fd, &fdsetR);
|
---|
315 |
|
---|
316 | fd_set fdsetE = fdsetR;
|
---|
317 |
|
---|
318 | struct timeval tv = {0,0};
|
---|
319 | struct timeval *ptv;
|
---|
320 | if (cMsTimeout == RT_INDEFINITE_WAIT)
|
---|
321 | ptv = NULL;
|
---|
322 | else
|
---|
323 | {
|
---|
324 | tv.tv_sec = cMsTimeout / 1000;
|
---|
325 | tv.tv_usec = (cMsTimeout % 1000) * 1000;
|
---|
326 | ptv = &tv;
|
---|
327 | }
|
---|
328 |
|
---|
329 | int rc = select(fd + 1, &fdsetR, NULL, &fdsetE, ptv);
|
---|
330 | if (rc > 0)
|
---|
331 | rc = VINF_SUCCESS;
|
---|
332 | else if (rc == 0)
|
---|
333 | rc = VERR_TIMEOUT;
|
---|
334 | else if (errno == EINTR)
|
---|
335 | rc = VERR_INTERRUPTED;
|
---|
336 | else
|
---|
337 | {
|
---|
338 | AssertMsgFailed(("rc=%d errno=%d\n", rc, errno));
|
---|
339 | rc = VERR_INTERNAL_ERROR_4;
|
---|
340 | }
|
---|
341 | return rc;
|
---|
342 | }
|
---|
343 |
|
---|
344 | # endif // !RT_OS_DARWIN
|
---|
345 | #endif // VBOX_WITH_XPCOM
|
---|
346 |
|
---|
347 | #ifndef VBOX_WITH_XPCOM
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Dispatch a message on Windows.
|
---|
351 | *
|
---|
352 | * This will pick out our events and handle them specially.
|
---|
353 | *
|
---|
354 | * @returns @a rc or VERR_INTERRUPTED (WM_QUIT or NULL msg).
|
---|
355 | * @param pMsg The message to dispatch.
|
---|
356 | * @param rc The current status code.
|
---|
357 | */
|
---|
358 | /*static*/
|
---|
359 | int EventQueue::dispatchMessageOnWindows(MSG const *pMsg, int rc)
|
---|
360 | {
|
---|
361 | /*
|
---|
362 | * Check for and dispatch our events.
|
---|
363 | */
|
---|
364 | if ( pMsg->hwnd == NULL
|
---|
365 | && pMsg->message == WM_USER)
|
---|
366 | {
|
---|
367 | if (pMsg->lParam == EVENTQUEUE_WIN_LPARAM_MAGIC)
|
---|
368 | {
|
---|
369 | Event *pEvent = (Event *)pMsg->wParam;
|
---|
370 | if (pEvent)
|
---|
371 | {
|
---|
372 | pEvent->handler();
|
---|
373 | delete pEvent;
|
---|
374 | }
|
---|
375 | else
|
---|
376 | rc = VERR_INTERRUPTED;
|
---|
377 | return rc;
|
---|
378 | }
|
---|
379 | AssertMsgFailed(("lParam=%p wParam=%p\n", pMsg->lParam, pMsg->wParam));
|
---|
380 | }
|
---|
381 |
|
---|
382 | /*
|
---|
383 | * Check for the quit message and dispatch the message the normal way.
|
---|
384 | */
|
---|
385 | if (pMsg->message == WM_QUIT)
|
---|
386 | rc = VERR_INTERRUPTED;
|
---|
387 | TranslateMessage(pMsg);
|
---|
388 | DispatchMessage(pMsg);
|
---|
389 |
|
---|
390 | return rc;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Process pending events (Windows).
|
---|
396 | *
|
---|
397 | * @retval VINF_SUCCESS
|
---|
398 | * @retval VERR_TIMEOUT
|
---|
399 | * @retval VERR_INTERRUPTED.
|
---|
400 | */
|
---|
401 | static int processPendingEvents(void)
|
---|
402 | {
|
---|
403 | int rc = VERR_TIMEOUT;
|
---|
404 | MSG Msg;
|
---|
405 | if (PeekMessage(&Msg, NULL /*hWnd*/, 0 /*wMsgFilterMin*/, 0 /*wMsgFilterMax*/, PM_REMOVE))
|
---|
406 | {
|
---|
407 | rc = VINF_SUCCESS;
|
---|
408 | do
|
---|
409 | rc = EventQueue::dispatchMessageOnWindows(&Msg, rc);
|
---|
410 | while (PeekMessage(&Msg, NULL /*hWnd*/, 0 /*wMsgFilterMin*/, 0 /*wMsgFilterMax*/, PM_REMOVE));
|
---|
411 | }
|
---|
412 | return rc;
|
---|
413 | }
|
---|
414 |
|
---|
415 | #else // VBOX_WITH_XPCOM
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Process pending XPCOM events.
|
---|
419 | * @param pQueue The queue to process events on.
|
---|
420 | * @returns VINF_SUCCESS or VERR_TIMEOUT.
|
---|
421 | */
|
---|
422 | static int processPendingEvents(nsIEventQueue *pQueue)
|
---|
423 | {
|
---|
424 | /* Check for timeout condition so the caller can be a bit more lazy. */
|
---|
425 | PRBool fHasEvents = PR_FALSE;
|
---|
426 | nsresult hr = pQueue->PendingEvents(&fHasEvents);
|
---|
427 | if (NS_FAILED(hr))
|
---|
428 | return VERR_INTERNAL_ERROR_2;
|
---|
429 | if (!fHasEvents)
|
---|
430 | return VERR_TIMEOUT;
|
---|
431 |
|
---|
432 | pQueue->ProcessPendingEvents();
|
---|
433 | return VINF_SUCCESS;
|
---|
434 | }
|
---|
435 |
|
---|
436 | #endif // VBOX_WITH_XPCOM
|
---|
437 |
|
---|
438 | /**
|
---|
439 | * Process events pending on this event queue, and wait up to given timeout, if
|
---|
440 | * nothing is available.
|
---|
441 | *
|
---|
442 | * Must be called on same thread this event queue was created on.
|
---|
443 | *
|
---|
444 | * @param cMsTimeout The timeout specified as milliseconds. Use
|
---|
445 | * RT_INDEFINITE_WAIT to wait till an event is posted on the
|
---|
446 | * queue.
|
---|
447 | *
|
---|
448 | * @returns VBox status code
|
---|
449 | * @retval VINF_SUCCESS if one or more messages was processed.
|
---|
450 | * @retval VERR_TIMEOUT if cMsTimeout expired.
|
---|
451 | * @retval VERR_INVALID_CONTEXT if called on the wrong thread.
|
---|
452 | * @retval VERR_INTERRUPTED if interruptEventQueueProcessing was called.
|
---|
453 | * On Windows will also be returned when WM_QUIT is encountered.
|
---|
454 | * On UNIXy systems this may also be returned when a signal is
|
---|
455 | * dispatched on the calling thread.
|
---|
456 | * On Darwin this may also be returned when the native queue is
|
---|
457 | * stopped or destroyed/finished.
|
---|
458 | * @todo Change this method to use some status other than VERR_INTERRUPTED
|
---|
459 | * for indicating harmless interruptions by the system, or some other
|
---|
460 | * status for indicating interruptEventQueueProcessing/WM_QUIT. Maybe
|
---|
461 | * VINF_INTERRUPTED for system interruption would be best appropriate.
|
---|
462 | */
|
---|
463 | int EventQueue::processEventQueue(RTMSINTERVAL cMsTimeout)
|
---|
464 | {
|
---|
465 | int rc;
|
---|
466 | CHECK_THREAD_RET(VERR_INVALID_CONTEXT);
|
---|
467 |
|
---|
468 | #ifdef VBOX_WITH_XPCOM
|
---|
469 | /*
|
---|
470 | * Process pending events, if none are available and we're not in a
|
---|
471 | * poll call, wait for some to appear. (We have to be a little bit
|
---|
472 | * careful after waiting for the events since Darwin will process
|
---|
473 | * them as part of the wait, while the XPCOM case will not.)
|
---|
474 | *
|
---|
475 | * Note! Unfortunately, WaitForEvent isn't interruptible with Ctrl-C,
|
---|
476 | * while select() is. So we cannot use it for indefinite waits.
|
---|
477 | */
|
---|
478 | rc = processPendingEvents(mEventQ);
|
---|
479 | if ( rc == VERR_TIMEOUT
|
---|
480 | && cMsTimeout > 0)
|
---|
481 | {
|
---|
482 | # ifdef RT_OS_DARWIN
|
---|
483 | /** @todo check how Ctrl-C works on Darwin. */
|
---|
484 | rc = waitForEventsOnDarwin(cMsTimeout);
|
---|
485 | if (rc == VERR_TIMEOUT)
|
---|
486 | rc = processPendingEvents(mEventQ);
|
---|
487 | # else // !RT_OS_DARWIN
|
---|
488 | rc = waitForEventsOnXPCOM(mEventQ, cMsTimeout);
|
---|
489 | if ( RT_SUCCESS(rc)
|
---|
490 | || rc == VERR_TIMEOUT)
|
---|
491 | rc = processPendingEvents(mEventQ);
|
---|
492 | # endif // !RT_OS_DARWIN
|
---|
493 | }
|
---|
494 |
|
---|
495 | if ( ( RT_SUCCESS(rc)
|
---|
496 | || rc == VERR_INTERRUPTED)
|
---|
497 | && mInterrupted)
|
---|
498 | {
|
---|
499 | mInterrupted = false;
|
---|
500 | rc = VERR_INTERRUPTED;
|
---|
501 | }
|
---|
502 |
|
---|
503 | #else // !VBOX_WITH_XPCOM
|
---|
504 | if (cMsTimeout == RT_INDEFINITE_WAIT)
|
---|
505 | {
|
---|
506 | BOOL fRet;
|
---|
507 | MSG Msg;
|
---|
508 | int rc = VINF_SUCCESS;
|
---|
509 | while ( (fRet = GetMessage(&Msg, NULL /*hWnd*/, WM_USER, WM_USER))
|
---|
510 | && fRet != -1
|
---|
511 | && rc != VERR_INTERRUPTED)
|
---|
512 | rc = EventQueue::dispatchMessageOnWindows(&Msg, rc);
|
---|
513 | if (fRet == 0)
|
---|
514 | rc = VERR_INTERRUPTED;
|
---|
515 | else if (fRet == -1)
|
---|
516 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
517 | }
|
---|
518 | else
|
---|
519 | {
|
---|
520 | rc = processPendingEvents();
|
---|
521 | if ( rc == VERR_TIMEOUT
|
---|
522 | && cMsTimeout != 0)
|
---|
523 | {
|
---|
524 | DWORD rcW = MsgWaitForMultipleObjects(1,
|
---|
525 | &mhThread,
|
---|
526 | TRUE /*fWaitAll*/,
|
---|
527 | cMsTimeout,
|
---|
528 | QS_ALLINPUT);
|
---|
529 | AssertMsgReturn(rcW == WAIT_TIMEOUT || rcW == WAIT_OBJECT_0,
|
---|
530 | ("%d\n", rcW),
|
---|
531 | VERR_INTERNAL_ERROR_4);
|
---|
532 | rc = processPendingEvents();
|
---|
533 | }
|
---|
534 | }
|
---|
535 | #endif // !VBOX_WITH_XPCOM
|
---|
536 |
|
---|
537 | return rc;
|
---|
538 | }
|
---|
539 |
|
---|
540 | /**
|
---|
541 | * Interrupt thread waiting on event queue processing.
|
---|
542 | *
|
---|
543 | * Can be called on any thread.
|
---|
544 | *
|
---|
545 | * @returns VBox status code.
|
---|
546 | */
|
---|
547 | int EventQueue::interruptEventQueueProcessing()
|
---|
548 | {
|
---|
549 | /* Send a NULL event. This event will be picked up and handled specially
|
---|
550 | * both for XPCOM and Windows. It is the responsibility of the caller to
|
---|
551 | * take care of not running the loop again in a way which will hang. */
|
---|
552 | postEvent(NULL);
|
---|
553 | return VINF_SUCCESS;
|
---|
554 | }
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Posts an event to this event loop asynchronously.
|
---|
558 | *
|
---|
559 | * @param event the event to post, must be allocated using |new|
|
---|
560 | * @return TRUE if successful and false otherwise
|
---|
561 | */
|
---|
562 | BOOL EventQueue::postEvent(Event *event)
|
---|
563 | {
|
---|
564 | #ifndef VBOX_WITH_XPCOM
|
---|
565 |
|
---|
566 | return PostThreadMessage(mThreadId, WM_USER, (WPARAM)event, EVENTQUEUE_WIN_LPARAM_MAGIC);
|
---|
567 |
|
---|
568 | #else // VBOX_WITH_XPCOM
|
---|
569 |
|
---|
570 | if (!mEventQ)
|
---|
571 | return FALSE;
|
---|
572 |
|
---|
573 | MyPLEvent *ev = new MyPLEvent(event);
|
---|
574 | mEventQ->InitEvent(ev, this, com::EventQueue::plEventHandler,
|
---|
575 | com::EventQueue::plEventDestructor);
|
---|
576 | HRESULT rc = mEventQ->PostEvent(ev);
|
---|
577 | return NS_SUCCEEDED(rc);
|
---|
578 |
|
---|
579 | #endif // VBOX_WITH_XPCOM
|
---|
580 | }
|
---|
581 |
|
---|
582 |
|
---|
583 | /**
|
---|
584 | * Get select()'able selector for this event queue.
|
---|
585 | * This will return -1 on platforms and queue variants not supporting such
|
---|
586 | * functionality.
|
---|
587 | */
|
---|
588 | int EventQueue::getSelectFD()
|
---|
589 | {
|
---|
590 | #ifdef VBOX_WITH_XPCOM
|
---|
591 | return mEventQ->GetEventQueueSelectFD();
|
---|
592 | #else
|
---|
593 | return -1;
|
---|
594 | #endif
|
---|
595 | }
|
---|
596 |
|
---|
597 | }
|
---|
598 | /* namespace com */
|
---|