1 | /* $Id: EventQueue.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - Event queue class declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
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 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
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
|
---|
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.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef VBOX_INCLUDED_com_EventQueue_h
|
---|
38 | #define VBOX_INCLUDED_com_EventQueue_h
|
---|
39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
40 | # pragma once
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <list>
|
---|
44 |
|
---|
45 | #include <iprt/asm.h>
|
---|
46 | #include <iprt/critsect.h>
|
---|
47 |
|
---|
48 | #include <VBox/com/defs.h>
|
---|
49 | #include <VBox/com/assert.h>
|
---|
50 |
|
---|
51 |
|
---|
52 | /** @defgroup grp_com_evtqueue Event Queue Classes
|
---|
53 | * @ingroup grp_com
|
---|
54 | * @{
|
---|
55 | */
|
---|
56 |
|
---|
57 | namespace com
|
---|
58 | {
|
---|
59 |
|
---|
60 | class EventQueue;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Base class for all events. Intended to be subclassed to introduce new
|
---|
64 | * events and handlers for them.
|
---|
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 |
|
---|
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:
|
---|
79 |
|
---|
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 |
|
---|
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 | */
|
---|
97 | virtual void *handler(void) { return NULL; }
|
---|
98 |
|
---|
99 | friend class EventQueue;
|
---|
100 |
|
---|
101 | protected:
|
---|
102 |
|
---|
103 | /** The event's reference count. */
|
---|
104 | uint32_t mRefCount;
|
---|
105 | };
|
---|
106 |
|
---|
107 | typedef std::list< Event* > EventQueueList;
|
---|
108 | typedef std::list< Event* >::iterator EventQueueListIterator;
|
---|
109 | typedef std::list< Event* >::const_iterator EventQueueListIteratorConst;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Simple event queue.
|
---|
113 | */
|
---|
114 | class EventQueue
|
---|
115 | {
|
---|
116 | public:
|
---|
117 |
|
---|
118 | EventQueue(void);
|
---|
119 | virtual ~EventQueue(void);
|
---|
120 |
|
---|
121 | public:
|
---|
122 |
|
---|
123 | BOOL postEvent(Event *event);
|
---|
124 | int processEventQueue(RTMSINTERVAL cMsTimeout);
|
---|
125 | int processPendingEvents(size_t cNumEvents);
|
---|
126 | int interruptEventQueueProcessing();
|
---|
127 |
|
---|
128 | private:
|
---|
129 |
|
---|
130 | /** Critical section for serializing access to this
|
---|
131 | * event queue. */
|
---|
132 | RTCRITSECT mCritSect;
|
---|
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;
|
---|
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;
|
---|
144 | };
|
---|
145 |
|
---|
146 | } /* namespace com */
|
---|
147 |
|
---|
148 | /** @} */
|
---|
149 |
|
---|
150 | #endif /* !VBOX_INCLUDED_com_EventQueue_h */
|
---|
151 |
|
---|