VirtualBox

source: vbox/trunk/src/VBox/Main/include/ConsoleEvents.h@ 37618

Last change on this file since 37618 was 30764, checked in by vboxsync, 14 years ago

back out r63543, r63544 until windows build problems can be solved properly

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1/** @file
2 *
3 * VirtualBox console event handling
4 */
5
6/*
7 * Copyright (C) 2006-2007 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
18#ifndef ____H_CONSOLEEVENTS
19#define ____H_CONSOLEEVENTS
20
21#include <iprt/semaphore.h>
22
23template<class C> class ConsoleEventBuffer
24{
25public:
26 /**
27 * Constructor
28 *
29 * @param size FIFO size in elements.
30 */
31 ConsoleEventBuffer(size_t size) :
32 sz(size), buf(new C[size]), curg(0), curp(0), full(false)
33 {
34 RTSemMutexCreate(&mutex);
35 }
36
37 /**
38 * Destructor
39 *
40 */
41 virtual ~ConsoleEventBuffer()
42 {
43 RTSemMutexDestroy(mutex);
44 delete buf;
45 }
46
47 /**
48 * Opens the buffer for extraction of elements. Must be called before #get().
49 */
50 void open()
51 {
52 lock();
53 }
54
55 /**
56 * Closes the buffer previously opened by #open(). Must be called after #get().
57 */
58 void close()
59 {
60 unlock();
61 }
62
63 /**
64 * Gets the element from the buffer. Requires #open() before and #close()
65 * after. Returns the first element and removes it from the buffer. If
66 * the buffer is empty, returns an empty element (constructed with the
67 * default constructor).
68 */
69 C get()
70 {
71 C c;
72 if (full || curg != curp)
73 {
74 c = buf[curg];
75 ++curg %= sz;
76 full = false;
77 }
78 return c;
79 }
80
81 /**
82 * Puts the element to the end of the buffer. #open()/#close() must not
83 * be used. Returns 1 if successful, or 0 if the buffer is full, or 2
84 * if the element is invalid.
85 */
86 size_t put(C c)
87 {
88 if (!c.isValid())
89 return 2; // invalid element
90 lock();
91 size_t i = 0;
92 if (!full)
93 {
94 buf[curp] = c;
95 ++curp %= sz;
96 i++;
97 full = curg == curp;
98 }
99 unlock();
100 return i;
101 }
102
103 /**
104 * Puts the number of elements to the buffer. #open()/#close() must not
105 * be used. Returns the count of elements placed. If it is less than
106 * the count passed as an argument then the buffer is full. If it is
107 * greater (special case) then the invalid element is encountered and
108 * its index is return value munis count minus 1.
109 */
110 size_t put(C *codes, size_t count)
111 {
112 lock();
113 size_t i = 0;
114 while (i < count && !full)
115 {
116 if (!codes[i].isValid())
117 {
118 i += count + 1; // invalid code
119 break;
120 }
121 buf[curp] = codes[i++];
122 ++curp %= sz;
123 full = curg == curp;
124 }
125 unlock();
126 return i;
127 }
128
129private:
130 /**
131 * Acquire the local mutex
132 */
133 void lock()
134 {
135 RTSemMutexRequest(mutex, RT_INDEFINITE_WAIT);
136 }
137 /**
138 * Release the local mutex
139 */
140 void unlock()
141 {
142 RTSemMutexRelease(mutex);
143 }
144
145private:
146 size_t sz;
147 C *buf;
148 size_t curg, curp;
149 bool full;
150 RTSEMMUTEX mutex;
151};
152
153#endif // ____H_CONSOLEEVENTS
154/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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