[46649] | 1 | /* $Id: EventQueue.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[58110] | 3 | * MS COM / XPCOM Abstraction Layer - Event queue class declaration.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2013-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
[76558] | 37 | #ifndef VBOX_INCLUDED_com_EventQueue_h
|
---|
| 38 | #define VBOX_INCLUDED_com_EventQueue_h
|
---|
[76507] | 39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 40 | # pragma once
|
---|
| 41 | #endif
|
---|
[1] | 42 |
|
---|
[46649] | 43 | #include <list>
|
---|
[1] | 44 |
|
---|
[46649] | 45 | #include <iprt/asm.h>
|
---|
| 46 | #include <iprt/critsect.h>
|
---|
| 47 |
|
---|
[1] | 48 | #include <VBox/com/defs.h>
|
---|
| 49 | #include <VBox/com/assert.h>
|
---|
| 50 |
|
---|
[58110] | 51 |
|
---|
| 52 | /** @defgroup grp_com_evtqueue Event Queue Classes
|
---|
| 53 | * @ingroup grp_com
|
---|
| 54 | * @{
|
---|
| 55 | */
|
---|
| 56 |
|
---|
[1] | 57 | namespace com
|
---|
| 58 | {
|
---|
| 59 |
|
---|
| 60 | class EventQueue;
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
[31579] | 63 | * Base class for all events. Intended to be subclassed to introduce new
|
---|
| 64 | * events and handlers for them.
|
---|
[1] | 65 | *
|
---|
| 66 | * Subclasses usually reimplement virtual #handler() (that does nothing by
|
---|
| 67 | * default) and add new data members describing the event.
|
---|
| 68 | */
|
---|
| 69 | class Event
|
---|
| 70 | {
|
---|
| 71 | public:
|
---|
| 72 |
|
---|
[46649] | 73 | Event(void) :
|
---|
| 74 | mRefCount(0) { }
|
---|
| 75 | virtual ~Event(void) { AssertMsg(!mRefCount,
|
---|
| 76 | ("Reference count of event=%p not 0 on destruction (is %RU32)\n",
|
---|
| 77 | this, mRefCount)); }
|
---|
| 78 | public:
|
---|
[1] | 79 |
|
---|
[46649] | 80 | uint32_t AddRef(void) { return ASMAtomicIncU32(&mRefCount); }
|
---|
| 81 | void Release(void)
|
---|
| 82 | {
|
---|
| 83 | Assert(mRefCount);
|
---|
| 84 | uint32_t cRefs = ASMAtomicDecU32(&mRefCount);
|
---|
| 85 | if (!cRefs)
|
---|
| 86 | delete this;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[1] | 89 | protected:
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * Event handler. Called in the context of the event queue's thread.
|
---|
| 93 | * Always reimplemented by subclasses
|
---|
| 94 | *
|
---|
| 95 | * @return reserved, should be NULL.
|
---|
| 96 | */
|
---|
[46649] | 97 | virtual void *handler(void) { return NULL; }
|
---|
[1] | 98 |
|
---|
| 99 | friend class EventQueue;
|
---|
[46649] | 100 |
|
---|
| 101 | protected:
|
---|
| 102 |
|
---|
| 103 | /** The event's reference count. */
|
---|
| 104 | uint32_t mRefCount;
|
---|
[1] | 105 | };
|
---|
| 106 |
|
---|
[46649] | 107 | typedef std::list< Event* > EventQueueList;
|
---|
| 108 | typedef std::list< Event* >::iterator EventQueueListIterator;
|
---|
| 109 | typedef std::list< Event* >::const_iterator EventQueueListIteratorConst;
|
---|
| 110 |
|
---|
[1] | 111 | /**
|
---|
| 112 | * Simple event queue.
|
---|
| 113 | */
|
---|
| 114 | class EventQueue
|
---|
| 115 | {
|
---|
| 116 | public:
|
---|
| 117 |
|
---|
[46649] | 118 | EventQueue(void);
|
---|
| 119 | virtual ~EventQueue(void);
|
---|
[1] | 120 |
|
---|
[46649] | 121 | public:
|
---|
| 122 |
|
---|
[31579] | 123 | BOOL postEvent(Event *event);
|
---|
[31598] | 124 | int processEventQueue(RTMSINTERVAL cMsTimeout);
|
---|
[47853] | 125 | int processPendingEvents(size_t cNumEvents);
|
---|
[22847] | 126 | int interruptEventQueueProcessing();
|
---|
[1] | 127 |
|
---|
| 128 | private:
|
---|
| 129 |
|
---|
[46649] | 130 | /** Critical section for serializing access to this
|
---|
| 131 | * event queue. */
|
---|
| 132 | RTCRITSECT mCritSect;
|
---|
[47853] | 133 | /** Number of concurrent users. At the moment we
|
---|
| 134 | * only support one concurrent user at a time when
|
---|
| 135 | calling processEventQueue(). */
|
---|
| 136 | uint32_t mUserCnt;
|
---|
[46649] | 137 | /** Event semaphore for getting notified on new
|
---|
| 138 | * events being handled. */
|
---|
| 139 | RTSEMEVENT mSemEvent;
|
---|
| 140 | /** The actual event queue, implemented as a list. */
|
---|
| 141 | EventQueueList mEvents;
|
---|
| 142 | /** Shutdown indicator. */
|
---|
| 143 | bool mShutdown;
|
---|
[1] | 144 | };
|
---|
| 145 |
|
---|
[5658] | 146 | } /* namespace com */
|
---|
[1] | 147 |
|
---|
[58110] | 148 | /** @} */
|
---|
| 149 |
|
---|
[76585] | 150 | #endif /* !VBOX_INCLUDED_com_EventQueue_h */
|
---|
[45125] | 151 |
|
---|