1 // The MIT License (MIT)
2 //
3 // 	Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev
4 //
5 // 	Permission is hereby granted, free of charge, to any person obtaining a copy
6 // 	of this software and associated documentation files (the "Software"), to deal
7 // 	in the Software without restriction, including without limitation the rights
8 // 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // 	copies of the Software, and to permit persons to whom the Software is
10 // 	furnished to do so, subject to the following conditions:
11 //
12 //  The above copyright notice and this permission notice shall be included in
13 // 	all copies or substantial portions of the Software.
14 //
15 // 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // 	THE SOFTWARE.
22 
23 #pragma once
24 
25 #include <vector>
26 
27 #include <MTPlatform.h>
28 #include <MTTools.h>
29 
30 namespace MT
31 {
32 	template<int N>
33 	struct is_power_of_two
34 	{
35 		enum {value = N && !(N & (N - 1))};
36 	};
37 
38 
39 	/// \class ConcurrentRingBuffer
40 	/// \brief Very naive implementation of thread safe ring buffer. When ring buffer is full and a subsequent write is performed, then it starts overwriting the oldest data.
41 	template<typename T, size_t numElements>
42 	class ConcurrentRingBuffer
43 	{
44 		MT::Mutex mutex;
45 
46 		T buffer[numElements];
47 
48 		size_t writeIndex;
49 		size_t readIndex;
50 		size_t size;
51 
52 	private:
53 
54 		ConcurrentRingBuffer(const ConcurrentRingBuffer&) {}
55 		void operator=(const ConcurrentRingBuffer&) {}
56 
57 
58 		size_t NextIndex(size_t index)
59 		{
60 			size_t ret = index + 1;
61 			size_t mask = (numElements - 1);
62 			return (ret & mask);
63 		}
64 
65 	public:
66 
67 		ConcurrentRingBuffer()
68 			: writeIndex(0)
69 			, readIndex(0)
70 			, size(0)
71 		{
72 			static_assert(std::is_pod<T>::value == true, "Only POD types allowed to use in MT::ConcurrentRingBuffer");
73 			static_assert(is_power_of_two<numElements>::value == true, "NumElements used in MT::ConcurrentRingBuffer must be power of two");
74 		}
75 
76 		void Push(const T & item)
77 		{
78 			MT::ScopedGuard guard(mutex);
79 
80 			buffer[writeIndex] = item;
81 			if (size >= numElements)
82 			{
83 				// RingBuffer is full. Overwrite old data.
84 				readIndex = NextIndex(readIndex);
85 			} else
86 			{
87 				size++;
88 			}
89 
90 			writeIndex = NextIndex(writeIndex);
91 		}
92 
93 		size_t PopAll(T * dstBuffer, size_t dstBufferSize)
94 		{
95 			MT::ScopedGuard guard(mutex);
96 
97 			size_t elementsCount = size;
98 			elementsCount = Min(elementsCount, dstBufferSize);
99 
100 			for (size_t i = 0; i < elementsCount; i++)
101 			{
102 				dstBuffer[i] = buffer[readIndex];
103 				readIndex = NextIndex(readIndex);
104 			}
105 
106 			size -= elementsCount;
107 			return elementsCount;
108 		}
109 
110 
111 
112 
113 	};
114 
115 }