summaryrefslogtreecommitdiff
path: root/src/VBox/Main/include/ConsoleEvents.h
blob: a4718acd6bafa60d0f56a6c2cef034966aecceb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/** @file
 *
 * VirtualBox console event handling
 */

/*
 * Copyright (C) 2006-2010 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#ifndef ____H_CONSOLEEVENTS
#define ____H_CONSOLEEVENTS

#include <iprt/semaphore.h>

template<class C> class ConsoleEventBuffer
{
public:
   /**
    * Constructor
    *
    * @param size FIFO size in elements.
    */
    ConsoleEventBuffer(size_t size) :
        sz(size), buf(new C[size]), curg(0), curp(0), full(false)
    {
        RTSemMutexCreate(&mutex);
    }

    /**
     * Destructor
     *
     */
    virtual ~ConsoleEventBuffer()
    {
        RTSemMutexDestroy(mutex);
        delete buf;
    }

    /**
     *  Opens the buffer for extraction of elements. Must be called before #get().
     */
    void open()
    {
        lock();
    }

    /**
     *  Closes the buffer previously opened by #open(). Must be called after #get().
     */
    void close()
    {
        unlock();
    }

    /**
     *  Gets the element from the buffer. Requires #open() before and #close()
     *  after. Returns the first element and removes it from the buffer. If
     *  the buffer is empty, returns an empty element (constructed with the
     *  default constructor).
     */
    C get()
    {
        C c;
        if (full || curg != curp)
        {
            c = buf[curg];
            ++curg %= sz;
            full = false;
        }
        return c;
    }

    /**
     *  Puts the element to the end of the buffer. #open()/#close() must not
     *  be used. Returns 1 if successful, or 0 if the buffer is full, or 2
     *  if the element is invalid.
     */
    size_t put(C c)
    {
        if (!c.isValid())
            return 2; // invalid element
        lock();
        size_t i = 0;
        if (!full)
        {
            buf[curp] = c;
            ++curp %= sz;
            i++;
            full = curg == curp;
        }
        unlock();
        return i;
    }

    /**
     *  Puts the number of elements to the buffer. #open()/#close() must not
     *  be used. Returns the count of elements placed. If it is less than
     *  the count passed as an argument then the buffer is full. If it is
     *  greater (special case) then the invalid element is encountered and
     *  its index is return value munis count minus 1.
     */
    size_t put(C *codes, size_t count)
    {
        lock();
        size_t i = 0;
        while (i < count && !full)
        {
            if (!codes[i].isValid())
            {
                i += count + 1; // invalid code
                break;
            }
            buf[curp] = codes[i++];
            ++curp %= sz;
            full = curg == curp;
        }
        unlock();
        return i;
    }

private:
    /**
     * Acquire the local mutex
     */
    void lock()
    {
        RTSemMutexRequest(mutex, RT_INDEFINITE_WAIT);
    }
    /**
     *  Release the local mutex
     */
    void unlock()
    {
        RTSemMutexRelease(mutex);
    }

private:
    size_t sz;
    C *buf;
    size_t curg, curp;
    bool full;
    RTSEMMUTEX mutex;
};

#endif // ____H_CONSOLEEVENTS
/* vi: set tabstop=4 shiftwidth=4 expandtab: */