summaryrefslogtreecommitdiff
path: root/Runtime/Containers/ExtendedRingbuffer.h
blob: 0eb6d7b43d823cd697ec29c584513085d68f238a (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
#ifndef RUNTIME_CONTAINERS_RINGBUFFERS_H
#define RUNTIME_CONTAINERS_RINGBUFFERS_H

#include "GrowingRingbuffer.h"
#include "Ringbuffer.h"
#include "TransactionalRingbuffer.h"
#include "Runtime/Threads/Semaphore.h"
#include "Runtime/Utilities/NonCopyable.h"

#if SUPPORT_THREADS
namespace RingbufferTemplates
{
	// ------------------------------------------------------------------------
	// Support base classes used to avoid casting to 'normal' ringbuffer impl
	// ------------------------------------------------------------------------
	template <class Ringbuffer>
	class NonCastable : protected Ringbuffer
	{
	public:
		using Ringbuffer::WritePtr;
		using Ringbuffer::WritePtrUpdate;
		using Ringbuffer::ReadPtr;
		using Ringbuffer::ReadPtrUpdate;
		using Ringbuffer::GetSize;
		using Ringbuffer::GetFreeSize;
		using Ringbuffer::GetAllocatedSize;
		using Ringbuffer::GetAvailableSize;
		using Ringbuffer::Reset;

	protected:
		NonCastable(MemLabelId label, UInt32 size) : Ringbuffer(label, size) {}
	};

	// ------------------------------------------------------------------------
	// Adds support for notifications whenever data or free space is available
	// ------------------------------------------------------------------------
	template <class Ringbuffer>
	class AbstractNotificationSupport : public NonCastable<Ringbuffer>, public NonCopyable
	{
	public:
		AbstractNotificationSupport(MemLabelId label, UInt32 size) : NonCastable<Ringbuffer>(label, size) {}

		void ReleaseBlockedThreads(bool indefinitely = false)
		{
			m_FreeSemaphore.Suspend(indefinitely);
			m_AvailableSemaphore.Suspend(indefinitely);
		}

		void BlockUntilFree()
		{
			if (Ringbuffer::GetFreeSize())
				return;

			m_FreeSemaphore.Resume(true);
			if (Ringbuffer::GetFreeSize() == 0)
				m_FreeSemaphore.WaitForSignal();
			m_FreeSemaphore.Suspend();
		}

		void BlockUntilAvailable()
		{
			if (Ringbuffer::GetAvailableSize())
				return;

			m_AvailableSemaphore.Resume(true);
			if (Ringbuffer::GetAvailableSize() == 0)
				m_AvailableSemaphore.WaitForSignal();
			m_AvailableSemaphore.Suspend();
		}

	protected:
		SuspendableSemaphore 	m_AvailableSemaphore;
		SuspendableSemaphore 	m_FreeSemaphore;
	};

	template <class Ringbuffer>
	class RNotificationSupport : public AbstractNotificationSupport<Ringbuffer>
	{
	public:
		RNotificationSupport(MemLabelId label, UInt32 size) : AbstractNotificationSupport<Ringbuffer>(label, size) {}

		void WritePtrUpdate(void* writePtr, UInt32 nBytesWritten)
		{
			Ringbuffer::WritePtrUpdate(writePtr, nBytesWritten);
			AbstractNotificationSupport<Ringbuffer>::m_AvailableSemaphore.Signal();
		}

		void ReadPtrUpdate(const void* readPtr, UInt32 nBytesRead)
		{
			Ringbuffer::ReadPtrUpdate(readPtr, nBytesRead);
			AbstractNotificationSupport<Ringbuffer>::m_FreeSemaphore.Signal();
		}
	};


	template <class TransactionalRingbuffer>
	class TRNotificationSupport	: public AbstractNotificationSupport<TransactionalRingbuffer>
	{
	public:
		TRNotificationSupport(MemLabelId label, UInt32 size) : AbstractNotificationSupport<TransactionalRingbuffer>(label, size) {}

		using AbstractNotificationSupport<TransactionalRingbuffer>::ReadPtrReset;
		using AbstractNotificationSupport<TransactionalRingbuffer>::WritePtrReset;

		void WritePtrCommit()
		{
			AbstractNotificationSupport<TransactionalRingbuffer>::WritePtrCommit();
			AbstractNotificationSupport<TransactionalRingbuffer>::m_AvailableSemaphore.Signal();
		}

		void ReadPtrCommit()
		{
			AbstractNotificationSupport<TransactionalRingbuffer>::ReadPtrCommit();
			AbstractNotificationSupport<TransactionalRingbuffer>::m_FreeSemaphore.Signal();
		}
	};

}

typedef RingbufferTemplates::RNotificationSupport<Ringbuffer>					ExtendedRingbuffer;
typedef RingbufferTemplates::RNotificationSupport<GrowingRingbuffer>			ExtendedGrowingRingbuffer;
typedef RingbufferTemplates::TRNotificationSupport<TransactionalRingbuffer> 	ExtendedTransactionalRingbuffer;

#else // no thread support

namespace RingbufferTemplates
{
	template <class Ringbuffer>
	class NotificationSupport : public Ringbuffer, public NonCopyable
	{
	public:
		NotificationSupport(MemLabelId label, UInt32 size) : Ringbuffer(label, size) {}

		void ReleaseBlockedThreads(bool indefinitely = false) {}
		void BlockUntilFree() {}
		void BlockUntilAvailable() {}
	};
}

typedef RingbufferTemplates::NotificationSupport<Ringbuffer>				ExtendedRingbuffer;
typedef RingbufferTemplates::NotificationSupport<GrowingRingbuffer>			ExtendedGrowingRingbuffer;
typedef RingbufferTemplates::NotificationSupport<TransactionalRingbuffer> 	ExtendedTransactionalRingbuffer;

#endif

#endif //RUNTIME_CONTAINERS_RINGBUFFERS_H