summaryrefslogtreecommitdiffstats
path: root/Buffer/Buffer.h
blob: 1a95821c6b589708dbbfcb1c0709a63144959ab3 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Buffer.h
 *****************************************************************************
 * Copyright (C) 2012, bitmovin Softwareentwicklung OG, All Rights Reserved
 *
 * Email: libdash-dev@vicky.bitmovin.net
 *
 * This source code and its use and distribution, is subject to the terms
 * and conditions of the applicable license agreement.
 *****************************************************************************/

#ifndef LIBDASH_FRAMEWORK_BUFFER_BUFFER_H_
#define LIBDASH_FRAMEWORK_BUFFER_BUFFER_H_

#include "../Portable/MultiThreading.h"
#include "IBufferObserver.h"
#include <deque>
#include <vector>

namespace libdash
{
namespace framework
{

namespace buffer
{
template <class T>
class Buffer
{
public:
    Buffer(uint32_t maxcapacity, BufferType type);
    virtual ~Buffer();

    bool pushBack(T *element);
    T* front();
    T* getFront();
    void popFront();
    void clearTail();
    void clear();
    void setEOS(bool value);
    uint32_t length();
    uint32_t capacity();
    void attachObserver(IBufferObserver *observer);
    void notify();

private:
    std::deque<T*>                  objects;
    std::vector<IBufferObserver *>  observer;
    bool                            eos;
    uint32_t                        maxcapacity;
    mutable CRITICAL_SECTION        monitorMutex;
    mutable CONDITION_VARIABLE      full;
    mutable CONDITION_VARIABLE      empty;
    BufferType                      type;
};
}
}
}

using namespace libdash::framework::buffer;

template <class T>
Buffer<T>::Buffer(uint32_t maxcapacity, BufferType type) :
    type         (type),
    eos          (false),
    maxcapacity  (maxcapacity)
{
    InitializeConditionVariable (&this->full);
    InitializeConditionVariable (&this->empty);
    InitializeCriticalSection   (&this->monitorMutex);
}

template <class T> Buffer<T>::~Buffer()
{
    this->clear();

    DeleteConditionVariable (&this->full);
    DeleteConditionVariable (&this->empty);
    DeleteCriticalSection   (&this->monitorMutex);
}

template <class T> bool Buffer<T>::pushBack(T *object)
{
    EnterCriticalSection(&this->monitorMutex);

    while(this->objects.size() >= this->maxcapacity && !this->eos)
        SleepConditionVariableCS(&this->empty, &this->monitorMutex, INFINITE);

    if(this->objects.size() >= this->maxcapacity)
    {
        LeaveCriticalSection(&this->monitorMutex);
        return false;
    }

    this->objects.push_back(object);

    WakeAllConditionVariable(&this->full);
    LeaveCriticalSection(&this->monitorMutex);
    this->notify();
    return true;
}

template <class T> T* Buffer<T>::front()
{
    EnterCriticalSection(&this->monitorMutex);

    while(this->objects.size() == 0 && !this->eos)
        SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);

    if(this->objects.size() == 0)
    {
        LeaveCriticalSection(&this->monitorMutex);
        return NULL;
    }

    T *object = this->objects.front();

    LeaveCriticalSection(&this->monitorMutex);

    return object;
}

template <class T> T* Buffer<T>::getFront()
{
    EnterCriticalSection(&this->monitorMutex);

    while(this->objects.size() == 0 && !this->eos)
        SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);

    if(this->objects.size() == 0)
    {
        LeaveCriticalSection(&this->monitorMutex);
        return NULL;
    }

    T *object = this->objects.front();
    this->objects.pop_front();
    this->objects.shrink_to_fit();
    WakeAllConditionVariable(&this->empty);
    LeaveCriticalSection(&this->monitorMutex);
    this->notify();

    return object;
}

template <class T> uint32_t Buffer<T>::length()
{
    EnterCriticalSection(&this->monitorMutex);

    uint32_t ret = this->objects.size();

    LeaveCriticalSection(&this->monitorMutex);

    return ret;
}

template <class T> void Buffer<T>::popFront()
{
    EnterCriticalSection(&this->monitorMutex);

    this->objects.pop_front();
    this->objects.shrink_to_fit();

    WakeAllConditionVariable(&this->empty);
    LeaveCriticalSection(&this->monitorMutex);
    this->Notify();
}

template <class T> void Buffer<T>::setEOS(bool value)
{
    EnterCriticalSection(&this->monitorMutex);

    this->eos = value;
    WakeAllConditionVariable(&this->empty);
    WakeAllConditionVariable(&this->full);
    LeaveCriticalSection(&this->monitorMutex);
}

template <class T> void Buffer<T>::attachObserver(IBufferObserver *observer)
{
    this->observer.push_back(observer);
}

template <class T> void Buffer<T>::notify()
{
    for(size_t i = 0; i < this->observer.size(); i++)
        this->observer.at(i)->onBufferStateChanged(this->type, (uint32_t)((double)this->objects.size()/(double)this->maxcapacity*100.0), this->maxcapacity);
}

template <class T> void Buffer<T>::clearTail()
{
    EnterCriticalSection(&this->monitorMutex);

    int size = this->objects.size() - 1;

    if (size < 1)
    {
        LeaveCriticalSection(&this->monitorMutex);
        return;
    }

    T* object = this->objects.front();
    this->objects.pop_front();
    for(int i=0; i < size; i++)
    {
        delete this->objects.front();
        this->objects.pop_front();
    }
    this->objects.shrink_to_fit();

    this->objects.push_back(object);
    WakeAllConditionVariable(&this->empty);
    WakeAllConditionVariable(&this->full);
    LeaveCriticalSection(&this->monitorMutex);
    this->Notify();
}

template <class T> void Buffer<T>::clear()
{
    EnterCriticalSection(&this->monitorMutex);

    for(int i = 0; i < this->objects.size(); i++)
        delete this->objects[i];

    this->objects.clear();
    this->objects.shrink_to_fit();
    WakeAllConditionVariable(&this->empty);
    WakeAllConditionVariable(&this->full);
    LeaveCriticalSection(&this->monitorMutex);
    this->notify();
}

template <class T> uint32_t Buffer<T>::capacity()
{
    return this->maxcapacity;
}

#endif /* LIBDASH_FRAMEWORK_BUFFER_BUFFER_H_ */