GM6000 Digital Heater Controller Branch: main
SDX-1330
RingBufferMT.h
Go to the documentation of this file.
1#ifndef Cpl_Container_RingBuffer_MT_h_
2#define Cpl_Container_RingBuffer_MT_h_
3/*-----------------------------------------------------------------------------
4* This file is part of the Colony.Core Project. The Colony.Core Project is an
5* open source project with a BSD type of licensing agreement. See the license
6* agreement (license.txt) in the top/ directory or on the Internet at
7* http://integerfox.com/colony.core/license.txt
8*
9* Copyright (c) 2014-2022 John T. Taylor
10*
11* Redistributions of the source code must retain the above copyright notice.
12*----------------------------------------------------------------------------*/
13/** @file */
14
16#include "Cpl/System/Mutex.h"
17
18///
19namespace Cpl {
20///
21namespace Container {
22
23
24/** This template class implements a THREAD SAFE Ring Buffer. The size of the
25 ring buffer is limited by number of bits in platform's 'unsigned'
26 data type.
27
28 Template Args:
29 ITEM:= Type of the data stored in the Ring Buffer
30 */
31template <class ITEM>
32class RingBufferMT: public RingBuffer<ITEM>
33{
34public:
35 /** Constructor. The application is responsible for providing the memory
36 for the Ring Buffer. The argument ''maxElements' is the number of
37 items that will fit in the memory allocated by 'memoryForElements' - it
38 is NOT the number of bytes of 'memoryForElements'.
39 */
41
42
43
44public:
45 /// See Cpl::Container::RingBuffer.
47
48 /// See Cpl::Container::RingBuffer.
49 bool add( const ITEM& item ) noexcept { Cpl::System::Mutex::ScopeBlock criticalSection( m_lock ); return RingBuffer<ITEM>::add( item ); }
50
51 /// See Cpl::Container::RingBuffer.
53
54 /// See Cpl::Container::RingBuffer.
56
57public:
58 /** This method returns a pointer to the next item to be added. In addition
59 it returns the number of elements that can be added as linear/flat
60 buffer (i.e. without wrapping around raw buffer memory)
61
62 If the Ring buffer is full, a null pointer is returned (and 'dstNumFlatElements'
63 is set to zero).
64
65 This method with addElements() method provide an ATOMIC update of
66 the ring buffer. This means that addElements() MUST ALWAYS be called
67 after calling peekNextAddItems().
68 */
70
71 /** This method 'adds' N elements - that were populated using the
72 pointer returned from peekNextAddItems - to the ring buffer. Basically
73 its updates the tail pointer to reflect items added using direct
74 memory access.
75
76 'numElementsAdded' be less than or equal to the 'dstNumFlatElements'
77 returned from peekNextAddItems(). If the call to peekNextAddTiems, THEN
78 'numElementsAdded' MUST BE SET to zero.
79
80 CAUTION:
81 1. IF YOU DON'T UNDERSTAND THE USE CASE FOR THIS METHOD - THEN
82 DON'T USE IT. If this method is used improperly, it WILL
83 CORRUPT the Ring Buffer!
84 2. MUST ALWAYS BE CALLED following a call to peekNextAddItems().
85 */
87
88public:
89 /** This method returns a pointer to the next item to be removed. In addition
90 it returns the number of elements that can be removed as linear/flat
91 buffer (i.e. without wrapping around raw buffer memory)
92
93 If the Ring buffer is empty, a null pointer is returned (and 'dstNumFlatElements'
94 is set to zero).
95
96 This method with removeElements() method provide an ATOMIC update of
97 the ring buffer. This means that removeElements() MUST ALWAYS be called
98 after calling peekNextRemoveItems().
99 */
101
102 /** This method 'removes' N elements - that were removed using the
103 pointer returned from peekNextRemoveItems - from the ring buffer.
104 Basically it updates the head pointer to reflect items removed using
105 direct memory access.
106
107 'numElementsToRemove' be less than or equal to the 'dstNumFlatElements'
108 returned from peekNextRemoveItems().
109
110 CAUTION:
111 1. IF YOU DON'T UNDERSTAND THE USE CASE FOR THIS METHOD - THEN
112 DON'T USE IT. If this method is used improperly, it WILL
113 CORRUPT the Ring Buffer!
114 2. MUST ALWAYS BE CALLED following a call to peekNextRemoveItems().
115 */
117
118
119public:
120 /// See Cpl::Container::RingBuffer.
122
123 /// See Cpl::Container::RingBuffer.
125
126 /// See Cpl::Container::RingBuffer.
128
129 /// See Cpl::Container::RingBuffer.
131
132public:
133 /// See Cpl::Container::RingBuffer.
135
136
137
138private:
139 /// Prevent access to the copy constructor -->Containers can not be copied!
140 RingBufferMT( const RingBufferMT& m );
141
142 /// Prevent access to the assignment operator -->Containers can not be copied!
143 const RingBufferMT& operator=( const RingBufferMT& m );
144
145protected:
146 /// Mutex for critical sections
148};
149
150
151}; // end namespaces
152};
153#endif // end header latch
This template class implements a Ring Buffer.
Definition RingBuffer.h:46
ITEM * peekTail(void) const noexcept
Returns a pointer to the last item in the Buffer.
Definition RingBuffer.h:321
bool isFull(void) const noexcept
This method returns true if the Ring Buffer is full.
Definition RingBuffer.h:345
ITEM * peekHead(void) const noexcept
Returns a pointer to the first item in the Buffer.
Definition RingBuffer.h:310
void clearTheBuffer() noexcept
Empties the Ring Buffer.
Definition RingBuffer.h:210
void removeElements(unsigned numElementsToRemove) noexcept
This method 'removes' N elements - that were removed using the pointer returned from peekNextRemoveIt...
Definition RingBuffer.h:269
ITEM * peekNextAddItems(unsigned &dstNumFlatElements) noexcept
This method returns a pointer to the next item to be added.
Definition RingBuffer.h:280
unsigned getNumItems(void) const noexcept
This method returns the current number of items in the Ring Buffer.
Definition RingBuffer.h:356
ITEM * peekNextRemoveItems(unsigned &dstNumFlatElements) noexcept
This method returns a pointer to the next item to be removed.
Definition RingBuffer.h:251
unsigned getMaxItems(void) const noexcept
This method returns the maximum number of items that can be stored in the Ring buffer.
Definition RingBuffer.h:368
bool add(const ITEM &item) noexcept
The contents of 'item' will be copied into the Ring Buffer as the 'last' item in the buffer.
Definition RingBuffer.h:217
bool isEmpty(void) const noexcept
This method returns true if the Ring Buffer is empty.
Definition RingBuffer.h:339
bool remove(ITEM &dst) noexcept
Removes the first item in the Buffer.
Definition RingBuffer.h:234
void addElements(unsigned numElementsAdded) noexcept
This method 'adds' N elements - that were populated using the pointer returned from peekNextAddItems ...
Definition RingBuffer.h:299
This template class implements a THREAD SAFE Ring Buffer.
Definition RingBufferMT.h:33
RingBufferMT(unsigned maxElements, ITEM memoryForElements[]) noexcept
Constructor.
Definition RingBufferMT.h:40
bool remove(ITEM &dst) noexcept
See Cpl::Container::RingBuffer.
Definition RingBufferMT.h:46
void addElements(unsigned numElementsAdded) noexcept
This method 'adds' N elements - that were populated using the pointer returned from peekNextAddItems ...
Definition RingBufferMT.h:86
ITEM * peekNextAddItems(unsigned &dstNumFlatElements) noexcept
This method returns a pointer to the next item to be added.
Definition RingBufferMT.h:69
bool add(const ITEM &item) noexcept
See Cpl::Container::RingBuffer.
Definition RingBufferMT.h:49
unsigned getNumItems(void) const noexcept
See Cpl::Container::RingBuffer.
Definition RingBufferMT.h:127
Cpl::System::Mutex m_lock
Mutex for critical sections.
Definition RingBufferMT.h:147
void clearTheBuffer() noexcept
See Cpl::Container::RingBuffer.
Definition RingBufferMT.h:134
bool isEmpty(void) const noexcept
See Cpl::Container::RingBuffer.
Definition RingBufferMT.h:121
ITEM * peekNextRemoveItems(unsigned &dstNumFlatElements) noexcept
This method returns a pointer to the next item to be removed.
Definition RingBufferMT.h:100
void removeElements(unsigned numElementsToRemove) noexcept
This method 'removes' N elements - that were removed using the pointer returned from peekNextRemoveIt...
Definition RingBufferMT.h:116
ITEM * peekHead(void) const noexcept
See Cpl::Container::RingBuffer.
Definition RingBufferMT.h:52
ITEM * peekTail(void) const noexcept
See Cpl::Container::RingBuffer.
Definition RingBufferMT.h:55
unsigned getMaxItems(void) const noexcept
See Cpl::Container::RingBuffer.
Definition RingBufferMT.h:130
bool isFull(void) const noexcept
See Cpl::Container::RingBuffer.
Definition RingBufferMT.h:124
This concrete class provides a simple mechanism for providing mutex protection for a "scope block".
Definition Mutex.h:77
This mutex class defines the interface for a mutex that has "recursive" semantics.
Definition Mutex.h:33
void unlock(void)
This method is invoke at the end of a critical section.
void lock(void)
This method is invoked prior to entering a critical section.
The 'Cpl' namespace is the root name space for the Colony.
Definition Api16.h:20