summaryrefslogtreecommitdiff
path: root/src/base/Queue.h
blob: 36a0eba6905a8c153d394082ffcc01ddd090025d (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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#ifndef _Queue_H_
#define _Queue_H_

#include "../api.h"
#include "Exception.h"

#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/shared_ptr.hpp>

#include <deque>
#include <iostream>

namespace avg {

typedef boost::unique_lock<boost::mutex> unique_lock;

template<class QElement>
class AVG_TEMPLATE_API Queue 
{
public:
    typedef boost::shared_ptr<QElement> QElementPtr;

    Queue(int maxSize=-1);
    virtual ~Queue();

    bool empty() const;
    QElementPtr pop(bool bBlock = true);
    void clear();
    void push(const QElementPtr& pElem);
    QElementPtr peek(bool bBlock = true) const;
    int size() const;
    int getMaxSize() const;

private:
    QElementPtr getFrontElement(bool bBlock, unique_lock& Lock) const;

    std::deque<QElementPtr> m_pElements;
    mutable boost::mutex m_Mutex;
    mutable boost::condition m_Cond;
    int m_MaxSize;
};

template<class QElement>
Queue<QElement>::Queue(int maxSize)
    : m_MaxSize(maxSize)
{
}

template<class QElement>
Queue<QElement>::~Queue()
{
}

template<class QElement>
bool Queue<QElement>::empty() const
{
    unique_lock Lock(m_Mutex);
    return m_pElements.empty();
}

template<class QElement>
typename Queue<QElement>::QElementPtr Queue<QElement>::pop(bool bBlock)
{
    unique_lock lock(m_Mutex);
    QElementPtr pElem = getFrontElement(bBlock, lock); 
    if (pElem) {
        m_pElements.pop_front();
        m_Cond.notify_one();
    }
    return pElem;
}

template<class QElement>
void Queue<QElement>::clear()
{
    QElementPtr pElem;
    do {
        pElem = pop(false);
    } while (pElem);
}

template<class QElement>
typename Queue<QElement>::QElementPtr Queue<QElement>::peek(bool bBlock) const
{
    unique_lock lock(m_Mutex);
    QElementPtr pElem = getFrontElement(bBlock, lock); 
    if (pElem) {
        m_Cond.notify_one();
    }
    return pElem;
}

template<class QElement>
void Queue<QElement>::push(const QElementPtr& pElem)
{
    assert(pElem);
    unique_lock lock(m_Mutex);
    if (m_pElements.size() == (unsigned)m_MaxSize) {
        while (m_pElements.size() == (unsigned)m_MaxSize) {
            m_Cond.wait(lock);
        }
    }
    m_pElements.push_back(pElem);
    m_Cond.notify_one();
}

template<class QElement>
int Queue<QElement>::size() const
{
    unique_lock lock(m_Mutex);
    return int(m_pElements.size());
}

template<class QElement>
int Queue<QElement>::getMaxSize() const
{
    unique_lock lock(m_Mutex);
    return m_MaxSize;
}

template<class QElement>
typename Queue<QElement>::QElementPtr 
        Queue<QElement>::getFrontElement(bool bBlock, unique_lock& lock) const
{
    if (m_pElements.empty()) {
        if (bBlock) {
            while (m_pElements.empty()) {
                m_Cond.wait(lock);
            }
        } else {
            return QElementPtr();
        }
    }
    return m_pElements.front();
}

}
#endif