NativeEventQueue.cpp revision 6976ecae1d524fa439cc9aca14b612b262557cbe
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* $Id$ */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** @file
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * MS COM / XPCOM Abstraction Layer:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Main event queue class declaration
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
cce0c6096dee0c5353bb74431dc47b05f87a1c6dvboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Copyright (C) 2006-2010 Oracle Corporation
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * available from http://www.virtualbox.org. This file is free software;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * you can redistribute it and/or modify it under the terms of the GNU
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * General Public License (GPL) as published by the Free Software
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include "VBox/com/NativeEventQueue.h"
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <new> /* For bad_alloc. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef RT_OS_DARWIN
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# include <CoreFoundation/CFRunLoop.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#if defined(VBOX_WITH_XPCOM) && !defined(RT_OS_DARWIN) && !defined(RT_OS_OS2)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# define USE_XPCOM_QUEUE
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/err.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/time.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/thread.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/log.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef USE_XPCOM_QUEUE
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# include <errno.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncnamespace com
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync// NativeEventQueue class
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync////////////////////////////////////////////////////////////////////////////////
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# define CHECK_THREAD_RET(ret) \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync do { \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsg(GetCurrentThreadId() == mThreadId, ("Must be on event queue thread!")); \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (GetCurrentThreadId() != mThreadId) \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return ret; \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync } while (0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** Magic LPARAM value for the WM_USER messages that we're posting.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @remarks This magic value is duplicated in
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * vboxapi/PlatformMSCOM::interruptWaitEvents(). */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define EVENTQUEUE_WIN_LPARAM_MAGIC UINT32_C(0xf241b819)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# define CHECK_THREAD_RET(ret) \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync do { \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!mEventQ) \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return ret; \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync BOOL isOnCurrentThread = FALSE; \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mEventQ->IsOnCurrentThread(&isOnCurrentThread); \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsg(isOnCurrentThread, ("Must be on event queue thread!")); \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!isOnCurrentThread) \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return ret; \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync } while (0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** Pointer to the main event queue. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncNativeEventQueue *NativeEventQueue::sMainQueue = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstruct MyPLEvent : public PLEvent
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MyPLEvent(NativeEvent *e) : event(e) {}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync NativeEvent *event;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync};
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* static */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncvoid *PR_CALLBACK com::NativeEventQueue::plEventHandler(PLEvent *self)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync NativeEvent *ev = ((MyPLEvent *)self)->event;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (ev)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ev->handler();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync NativeEventQueue *eq = (NativeEventQueue *)self->owner;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(eq);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync eq->mInterrupted = true;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* static */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncvoid PR_CALLBACK com::NativeEventQueue::plEventDestructor(PLEvent *self)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync NativeEvent *ev = ((MyPLEvent *)self)->event;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (ev)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync delete ev;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync delete self;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Constructs an event queue for the current thread.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Currently, there can be only one event queue per thread, so if an event
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * queue for the current thread already exists, this object is simply attached
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * to the existing event queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncNativeEventQueue::NativeEventQueue()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mThreadId = GetCurrentThreadId();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // force the system to create the message queue for the current thread
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MSG msg;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!DuplicateHandle(GetCurrentProcess(),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GetCurrentThread(),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GetCurrentProcess(),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync &mhThread,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync 0 /*dwDesiredAccess*/,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync FALSE /*bInheritHandle*/,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync DUPLICATE_SAME_ACCESS))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mhThread = INVALID_HANDLE_VALUE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mEQCreated = false;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mInterrupted = false;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // Here we reference the global nsIEventQueueService instance and hold it
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // until we're destroyed. This is necessary to keep NS_ShutdownXPCOM() away
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // from calling StopAcceptingEvents() on all event queues upon destruction of
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // nsIEventQueueService, and makes sense when, for some reason, this happens
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // *before* we're able to send a NULL event to stop our event handler thread
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // when doing unexpected cleanup caused indirectly by NS_ShutdownXPCOM()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // that is performing a global cleanup of everything. A good example of such
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // situation is when NS_ShutdownXPCOM() is called while the VirtualBox component
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // is still alive (because it is still referenced): eventually, it results in
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // a VirtualBox::uninit() call from where it is already not possible to post
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // NULL to the event thread (because it stopped accepting events).
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync nsresult rc = NS_GetEventQueueService(getter_AddRefs(mEventQService));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (NS_SUCCEEDED(rc))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = mEventQService->GetThreadEventQueue(NS_CURRENT_THREAD,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync getter_AddRefs(mEventQ));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rc == NS_ERROR_NOT_AVAILABLE)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = mEventQService->CreateThreadEventQueue();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (NS_SUCCEEDED(rc))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mEQCreated = true;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = mEventQService->GetThreadEventQueue(NS_CURRENT_THREAD,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync getter_AddRefs(mEventQ));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertComRC(rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncNativeEventQueue::~NativeEventQueue()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (mhThread != INVALID_HANDLE_VALUE)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync CloseHandle(mhThread);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mhThread = INVALID_HANDLE_VALUE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync // process all pending events before destruction
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (mEventQ)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (mEQCreated)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mEventQ->StopAcceptingEvents();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mEventQ->ProcessPendingEvents();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mEventQService->DestroyThreadEventQueue();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mEventQ = nsnull;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mEventQService = nsnull;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Initializes the main event queue instance.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns VBox status code.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @remarks If you're using the rest of the COM/XPCOM glue library,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * com::Initialize() will take care of initializing and uninitializing
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * the NativeEventQueue class. If you don't call com::Initialize, you must
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * make sure to call this method on the same thread that did the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * XPCOM initialization or we'll end up using the wrong main queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* static */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncint NativeEventQueue::init()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(sMainQueue == NULL);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(RTThreadIsMain(RTThreadSelf()));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync try
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync sMainQueue = new NativeEventQueue();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertPtr(sMainQueue);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Check that it actually is the main event queue, i.e. that
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync we're called on the right thread. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync nsCOMPtr<nsIEventQueue> q;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync nsresult rv = NS_GetMainEventQ(getter_AddRefs(q));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(NS_SUCCEEDED(rv));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(q == sMainQueue->mEventQ);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Check that it's a native queue. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PRBool fIsNative = PR_FALSE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rv = sMainQueue->mEventQ->IsQueueNative(&fIsNative);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(NS_SUCCEEDED(rv) && fIsNative);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync catch (std::bad_alloc &ba)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync NOREF(ba);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_NO_MEMORY;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Uninitialize the global resources (i.e. the main event queue instance).
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns VINF_SUCCESS
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* static */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncint NativeEventQueue::uninit()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (sMainQueue)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Must process all events to make sure that no NULL event is left
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * after this point. It would need to modify the state of sMainQueue. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef RT_OS_DARWIN /* Do not process the native runloop, the toolkit may not be ready for it. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync sMainQueue->mEventQ->ProcessPendingEvents();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync sMainQueue->processEventQueue(0);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync delete sMainQueue;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync sMainQueue = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Get main event queue instance.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Depends on init() being called first.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* static */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncNativeEventQueue* NativeEventQueue::getMainEventQueue()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return sMainQueue;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# ifdef RT_OS_DARWIN
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Wait for events and process them (Darwin).
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VINF_SUCCESS
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_TIMEOUT
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_INTERRUPTED
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param cMsTimeout How long to wait, or RT_INDEFINITE_WAIT.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic int waitForEventsOnDarwin(RTMSINTERVAL cMsTimeout)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Wait for the requested time, if we get a hit we do a poll to process
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * any other pending messages.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Note! About 1.0e10: According to the sources anything above 3.1556952e+9
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * means indefinite wait and 1.0e10 is what CFRunLoopRun() uses.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync CFTimeInterval rdTimeout = cMsTimeout == RT_INDEFINITE_WAIT ? 1e10 : (double)cMsTimeout / 1000;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync OSStatus orc = CFRunLoopRunInMode(kCFRunLoopDefaultMode, rdTimeout, true /*returnAfterSourceHandled*/);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (orc == kCFRunLoopRunHandledSource)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync OSStatus orc2 = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, false /*returnAfterSourceHandled*/);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( orc2 == kCFRunLoopRunStopped
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || orc2 == kCFRunLoopRunFinished)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync orc = orc2;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( orc == 0 /*???*/
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || orc == kCFRunLoopRunHandledSource)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( orc == kCFRunLoopRunStopped
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || orc == kCFRunLoopRunFinished)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_INTERRUPTED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsg(orc == kCFRunLoopRunTimedOut, ("Unexpected status code from CFRunLoopRunInMode: %#x", orc));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_TIMEOUT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# else // !RT_OS_DARWIN
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Wait for events (generic XPCOM).
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VINF_SUCCESS
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_TIMEOUT
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VINF_INTERRUPTED
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_INTERNAL_ERROR_4
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pQueue The queue to wait on.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param cMsTimeout How long to wait, or RT_INDEFINITE_WAIT.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic int waitForEventsOnXPCOM(nsIEventQueue *pQueue, RTMSINTERVAL cMsTimeout)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int fd = pQueue->GetEventQueueSelectFD();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync fd_set fdsetR;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync FD_ZERO(&fdsetR);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync FD_SET(fd, &fdsetR);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync fd_set fdsetE = fdsetR;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync struct timeval tv = {0,0};
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync struct timeval *ptv;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (cMsTimeout == RT_INDEFINITE_WAIT)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ptv = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync tv.tv_sec = cMsTimeout / 1000;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync tv.tv_usec = (cMsTimeout % 1000) * 1000;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ptv = &tv;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = select(fd + 1, &fdsetR, NULL, &fdsetE, ptv);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rc > 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else if (rc == 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_TIMEOUT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else if (errno == EINTR)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VINF_INTERRUPTED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync static uint32_t s_ErrorCount = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (s_ErrorCount < 500)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogRel(("waitForEventsOnXPCOM rc=%d errno=%d\n", rc, errno));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ++s_ErrorCount;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("rc=%d errno=%d\n", rc, errno));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INTERNAL_ERROR_4;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# endif // !RT_OS_DARWIN
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Dispatch a message on Windows.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * This will pick out our events and handle them specially.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns @a rc or VERR_INTERRUPTED (WM_QUIT or NULL msg).
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pMsg The message to dispatch.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param rc The current status code.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*static*/
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncint NativeEventQueue::dispatchMessageOnWindows(MSG const *pMsg, int rc)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Check for and dispatch our events.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( pMsg->hwnd == NULL
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && pMsg->message == WM_USER)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pMsg->lParam == EVENTQUEUE_WIN_LPARAM_MAGIC)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync NativeEvent *pEvent = (NativeEvent *)pMsg->wParam;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pEvent)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pEvent->handler();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync delete pEvent;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INTERRUPTED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("lParam=%p wParam=%p\n", pMsg->lParam, pMsg->wParam));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Check for the quit message and dispatch the message the normal way.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pMsg->message == WM_QUIT)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INTERRUPTED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync TranslateMessage(pMsg);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync DispatchMessage(pMsg);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Process pending events (Windows).
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VINF_SUCCESS
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_TIMEOUT
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_INTERRUPTED.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic int processPendingEvents(void)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = VERR_TIMEOUT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MSG Msg;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (PeekMessage(&Msg, NULL /*hWnd*/, 0 /*wMsgFilterMin*/, 0 /*wMsgFilterMax*/, PM_REMOVE))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync do
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = NativeEventQueue::dispatchMessageOnWindows(&Msg, rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync while (PeekMessage(&Msg, NULL /*hWnd*/, 0 /*wMsgFilterMin*/, 0 /*wMsgFilterMax*/, PM_REMOVE));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Process pending XPCOM events.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pQueue The queue to process events on.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VINF_SUCCESS
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_TIMEOUT
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_INTERRUPTED (darwin only)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_INTERNAL_ERROR_2
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic int processPendingEvents(nsIEventQueue *pQueue)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* ProcessPendingEvents doesn't report back what it did, so check here. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PRBool fHasEvents = PR_FALSE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync nsresult hr = pQueue->PendingEvents(&fHasEvents);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (NS_FAILED(hr))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_INTERNAL_ERROR_2;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Process pending events. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (fHasEvents)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pQueue->ProcessPendingEvents();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_TIMEOUT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# ifdef RT_OS_DARWIN
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Process pending native events. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc2 = waitForEventsOnDarwin(0);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rc == VERR_TIMEOUT || rc2 == VERR_INTERRUPTED)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = rc2;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Process events pending on this event queue, and wait up to given timeout, if
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * nothing is available.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Must be called on same thread this event queue was created on.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param cMsTimeout The timeout specified as milliseconds. Use
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * RT_INDEFINITE_WAIT to wait till an event is posted on the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns VBox status code
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VINF_SUCCESS if one or more messages was processed.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_TIMEOUT if cMsTimeout expired.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_INVALID_CONTEXT if called on the wrong thread.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VERR_INTERRUPTED if interruptEventQueueProcessing was called.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * On Windows will also be returned when WM_QUIT is encountered.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * On Darwin this may also be returned when the native queue is
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * stopped or destroyed/finished.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @retval VINF_INTERRUPTED if the native system call was interrupted by a
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * an asynchronous event delivery (signal) or just felt like returning
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * out of bounds. On darwin it will also be returned if the queue is
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * stopped.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncint NativeEventQueue::processEventQueue(RTMSINTERVAL cMsTimeout)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync CHECK_THREAD_RET(VERR_INVALID_CONTEXT);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Process pending events, if none are available and we're not in a
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * poll call, wait for some to appear. (We have to be a little bit
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * careful after waiting for the events since Darwin will process
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * them as part of the wait, while the XPCOM case will not.)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Note! Unfortunately, WaitForEvent isn't interruptible with Ctrl-C,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * while select() is. So we cannot use it for indefinite waits.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = processPendingEvents(mEventQ);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( rc == VERR_TIMEOUT
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && cMsTimeout > 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# ifdef RT_OS_DARWIN
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** @todo check how Ctrl-C works on Darwin. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = waitForEventsOnDarwin(cMsTimeout);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rc == VERR_TIMEOUT)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = processPendingEvents(mEventQ);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# else // !RT_OS_DARWIN
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = waitForEventsOnXPCOM(mEventQ, cMsTimeout);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( RT_SUCCESS(rc)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || rc == VERR_TIMEOUT)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = processPendingEvents(mEventQ);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# endif // !RT_OS_DARWIN
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( ( RT_SUCCESS(rc)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || rc == VERR_INTERRUPTED
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || rc == VERR_TIMEOUT)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && mInterrupted)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mInterrupted = false;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INTERRUPTED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else // !VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (cMsTimeout == RT_INDEFINITE_WAIT)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync BOOL fRet;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MSG Msg;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync while ( rc != VERR_INTERRUPTED
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && (fRet = GetMessage(&Msg, NULL /*hWnd*/, WM_USER, WM_USER))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && fRet != -1)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = NativeEventQueue::dispatchMessageOnWindows(&Msg, rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (fRet == 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INTERRUPTED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else if (fRet == -1)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = RTErrConvertFromWin32(GetLastError());
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = processPendingEvents();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( rc == VERR_TIMEOUT
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && cMsTimeout != 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync DWORD rcW = MsgWaitForMultipleObjects(1,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync &mhThread,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync TRUE /*fWaitAll*/,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync cMsTimeout,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync QS_ALLINPUT);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgReturn(rcW == WAIT_TIMEOUT || rcW == WAIT_OBJECT_0,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ("%d\n", rcW),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync VERR_INTERNAL_ERROR_4);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = processPendingEvents();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif // !VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(rc != VERR_TIMEOUT || cMsTimeout != RT_INDEFINITE_WAIT);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Interrupt thread waiting on event queue processing.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Can be called on any thread.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns VBox status code.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncint NativeEventQueue::interruptEventQueueProcessing()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Send a NULL event. This event will be picked up and handled specially
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * both for XPCOM and Windows. It is the responsibility of the caller to
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * take care of not running the loop again in a way which will hang. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync postEvent(NULL);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Posts an event to this event loop asynchronously.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param event the event to post, must be allocated using |new|
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @return TRUE if successful and false otherwise
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncBOOL NativeEventQueue::postEvent(NativeEvent *pEvent)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Note! The event == NULL case is duplicated in vboxapi/PlatformMSCOM::interruptWaitEvents(). */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync BOOL fRc = PostThreadMessage(mThreadId, WM_USER, (WPARAM)pEvent, EVENTQUEUE_WIN_LPARAM_MAGIC);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!fRc)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync static int s_cBitchedAboutFullNativeEventQueue = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( GetLastError() == ERROR_NOT_ENOUGH_QUOTA
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && s_cBitchedAboutFullNativeEventQueue < 10)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogRel(("Warning: Asynchronous event queue (%p, thread %RI32) full, event (%p) not delivered (%d/10)\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync this, mThreadId, pEvent, ++s_cBitchedAboutFullNativeEventQueue));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertFailed();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return fRc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!mEventQ)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return FALSE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync try
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MyPLEvent *pMyEvent = new MyPLEvent(pEvent);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mEventQ->InitEvent(pMyEvent, this, com::NativeEventQueue::plEventHandler,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync com::NativeEventQueue::plEventDestructor);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync HRESULT rc = mEventQ->PostEvent(pMyEvent);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return NS_SUCCEEDED(rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync catch (std::bad_alloc &ba)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Out of memory while allocating memory for event=%p: %s\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pEvent, ba.what()));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return FALSE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif // VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Get select()'able selector for this event queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * This will return -1 on platforms and queue variants not supporting such
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * functionality.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncint NativeEventQueue::getSelectFD()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOX_WITH_XPCOM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return mEventQ->GetEventQueueSelectFD();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return -1;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* namespace com */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync