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