VirtualBox

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

Last change on this file since 2453 was 1, checked in by vboxsync, 55 years ago

import

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