VirtualBox

source: vbox/trunk/include/VBox/com/EventQueue.h@ 22911

Last change on this file since 22911 was 22911, checked in by vboxsync, 15 years ago

event queues cleaned up

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * Event and EventQueue class declaration
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31#ifndef ___VBox_com_EventQueue_h
32#define ___VBox_com_EventQueue_h
33
34#if !defined (VBOX_WITH_XPCOM)
35#include <windows.h>
36#else
37#include <nsEventQueueUtils.h>
38#endif
39
40#include <VBox/com/defs.h>
41#include <VBox/com/assert.h>
42
43namespace com
44{
45
46class EventQueue;
47
48/**
49 * Base class for all events. Intended to be subclassed to introduce new events
50 * and handlers for them.
51 *
52 * Subclasses usually reimplement virtual #handler() (that does nothing by
53 * default) and add new data members describing the event.
54 */
55class Event
56{
57public:
58
59 Event() {}
60
61protected:
62
63 virtual ~Event() {};
64
65 /**
66 * Event handler. Called in the context of the event queue's thread.
67 * Always reimplemented by subclasses
68 *
69 * @return reserved, should be NULL.
70 */
71 virtual void *handler() { return NULL; }
72
73 friend class EventQueue;
74};
75
76/**
77 * Simple event queue.
78 *
79 * On Linux, if this queue is created on the main thread, it automatically
80 * processes XPCOM/IPC events while waiting for its own (Event) events.
81 */
82class EventQueue
83{
84public:
85
86 EventQueue();
87 ~EventQueue();
88
89 BOOL postEvent (Event *event);
90 BOOL waitForEvent (Event **event);
91 BOOL handleEvent (Event *event);
92 /**
93 * Process events pending on this event queue, and wait
94 * up to given timeout, if nothing is available.
95 * Must be called on same thread this event queue was created on.
96 */
97 int processEventQueue(uint32_t cMsTimeout);
98 /**
99 * Interrupt thread waiting on event queue processing.
100 * Can be called on any thread.
101 */
102 int interruptEventQueueProcessing();
103 /**
104 * Get select()'able selector for this event queue, can be -1
105 * on platforms not supporting such functionality.
106 */
107 int getSelectFD();
108 /**
109 * Initialize/deinitialize event queues.
110 */
111 static int init();
112 static int deinit();
113 /**
114 * Get main event queue instance.
115 */
116 static EventQueue* getMainEventQueue();
117
118private:
119 static EventQueue* mMainQueue;
120
121#if !defined (VBOX_WITH_XPCOM)
122
123 DWORD mThreadId;
124
125#else
126
127 BOOL mEQCreated;
128
129 nsCOMPtr <nsIEventQueue> mEventQ;
130 nsCOMPtr <nsIEventQueueService> mEventQService;
131
132 Event *mLastEvent;
133 BOOL mGotEvent;
134
135 struct MyPLEvent : public PLEvent
136 {
137 MyPLEvent (Event *e) : event (e) {}
138 Event *event;
139 };
140
141 static void * PR_CALLBACK plEventHandler (PLEvent* self)
142 {
143 // nsIEventQueue doesn't expose PL_GetEventOwner(), so use an internal
144 // field of PLEvent directly (hackish, but doesn' require an extra lib)
145 EventQueue *owner = (EventQueue *) self->owner;
146 Assert (owner);
147 owner->mLastEvent = ((MyPLEvent *) self)->event;
148 owner->mGotEvent = TRUE;
149 return 0;
150 }
151
152 static void PR_CALLBACK plEventDestructor (PLEvent* self) { delete self; }
153
154#endif
155};
156
157} /* namespace com */
158
159#endif
160
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette