File: Queue.h

package info (click to toggle)
pianobooster 1.0.0-4.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,740 kB
  • sloc: cpp: 13,718; python: 155; sh: 16; makefile: 14
file content (125 lines) | stat: -rw-r--r-- 3,064 bytes parent folder | download | duplicates (4)
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
/*********************************************************************************/
/*!
@file           Queue.h

@brief          xxxx.

@author         L. J. Barman

    Copyright (c)   2008-2013, L. J. Barman, all rights reserved

    This file is part of the PianoBooster application

    PianoBooster is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PianoBooster 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PianoBooster.  If not, see <http://www.gnu.org/licenses/>.

*/
/*********************************************************************************/

#ifndef __QUEUE_H__
#define __QUEUE_H__

#include <assert.h>

// A Queue or circular buffer also also call a FIFO a First In First Out buffer
// different threads could be running each end of the queue

//
template <class TYPE>

class CQueue
{
public:
    explicit CQueue(int size)
    {
        m_size = size;
        m_buffer = new TYPE[size];
        clear();
    }

    ~CQueue()
    {
        delete [] m_buffer;
    }

    void clear()
    {
        m_count = m_head = m_tail=0;
    }

    // pushes the item into the queue and returns a pointer to the item in the buffer
    TYPE* push(TYPE c)
    {
        if (!space())
        {
            assert(false);
            return 0;
        }
        TYPE* itemPtr = &m_buffer[m_head];
        m_buffer[m_head] = c;
        m_head++;
        if (m_head >= m_size)
            m_head = 0;

        // This must be last if a different thread is using pop()
        m_count++;
        return itemPtr;
    }

    TYPE pop()
    {
        TYPE c;
        if (!length())
        {
            assert(false);
            return m_buffer[m_tail];
        }
        c = m_buffer[m_tail++];
        if (m_tail >= m_size)
            m_tail = 0;

        // This must be last if a different thread is using push()
        m_count--;
        return c;
    }

    // returns a pointer to the item starting at the end of the queue
    TYPE * indexPtr(int index)
    {
        int offset;
        if (index >= length())
        {
            assert(false);
            return &m_buffer[m_head];
        }
        offset = m_tail + index;
        if (offset >= m_size)
            offset -= m_size;
        return &m_buffer[offset];
    }

    TYPE index(int index){ return *indexPtr(index);}

    int length() {return m_count;}
    int space() {return m_size - m_count;}
private:
    TYPE * m_buffer;
    int m_size;
    int m_head;
    int m_tail;

    // this should be atomic operation when two different threads are at each end of the queue
    volatile int m_count;
};

#endif //__QUEUE_H__