Audio Processing Framework (APF) version 0.5.0
lockfreefifo.h
Go to the documentation of this file.
1/******************************************************************************
2 Copyright (c) 2012-2016 Institut für Nachrichtentechnik, Universität Rostock
3 Copyright (c) 2006-2012 Quality & Usability Lab
4 Deutsche Telekom Laboratories, TU Berlin
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23*******************************************************************************/
24
25// https://AudioProcessingFramework.github.io/
26
29
30#ifndef APF_LOCKFREEFIFO_H
31#define APF_LOCKFREEFIFO_H
32
33#include "apf/math.h" // for next_power_of_2()
34#include "apf/misc.h" // for NonCopyable
35#include "apf/container.h" // for fixed_vector
36
37namespace apf
38{
39
40template<typename T> class LockFreeFifo; // undefined, use LockFreeFifo<T*>!
41
50template<typename T>
51class LockFreeFifo<T*> : NonCopyable
52{
53 public:
54 explicit LockFreeFifo(size_t size);
55
56 bool push(T* item);
57 T* pop();
58 bool empty() const;
59
60 private:
61 volatile size_t _write_index;
62 volatile size_t _read_index;
63 const size_t _size;
64 const size_t _size_mask;
65 fixed_vector<T*> _data;
66};
67
71template<typename T>
73 : _write_index(0)
74 , _read_index(0)
75 , _size(apf::math::next_power_of_2(size))
76 , _size_mask(_size - 1)
77 , _data(_size)
78{}
79
86template<typename T>
87bool
89{
90 if (item == nullptr) return false;
91
92 // Concurrent reading and writing is safe for one reader and writer. Once
93 // the _read_index is read the _write_index won't change before reading
94 // it, because it is modified only in this function. This won't work for
95 // multiple readers/writers.
96 auto r = _read_index;
97 auto w = _write_index;
98
99 // Move write pointer by FIFO-size in order to compute the distance to
100 // the read pointer in next step.
101 if (w < r) w += _size;
102
103 // Check if FIFO is full and return false instead of waiting until space is
104 // freed. (Prevent read pointer to overtake write pointer.)
105 if (w-r > _size-2) return false;
106
107 _data[w & _size_mask] = item;
108
109 // Set _write_index to next memory location (applying modulo operation)
110 _write_index = ++w & _size_mask;
111 return true;
112}
113
117template<typename T>
118T*
120{
121 T* retval = nullptr;
122
123 if (this->empty()) return retval;
124
125 auto r = _read_index;
126
127 retval = _data[r];
128
129 // Set _read_index to next memory location (applying modulo operation)
130 _read_index = ++r & _size_mask;
131 return retval;
132}
133
137template<typename T>
138bool
140{
141 return _read_index == _write_index;
142}
143
144} // namespace apf
145
146#endif
T * pop()
Get an item and remove it from the queue.
Definition: lockfreefifo.h:119
LockFreeFifo(size_t size)
ctor.
Definition: lockfreefifo.h:72
bool push(T *item)
Add an item to the queue.
Definition: lockfreefifo.h:88
bool empty() const
Check if queue is empty.
Definition: lockfreefifo.h:139
Classes derived from this class cannot be copied (but still moved).
Definition: misc.h:60
Some containers.
Mathematical constants and helper functions.
Miscellaneous helper classes.
T next_power_of_2(T number)
Find a power of 2 which is >= a given number.
Definition: math.h:196
Audio Processing Framework.
Definition: iterator.h:61