TinyRobotics
Loading...
Searching...
No Matches
SynchronizedBuffer.h
1#pragma once
2#include "TinyRobotics/utils/Config.h"
3#include "TinyRobotics/utils/Buffers.h"
4#include "TinyRobotics/utils/LoggerClass.h"
5#include "Mutex.h"
6#include "LockGuard.h"
7
8 namespace tinyrobotics {
9
10/**
11 * @class SynchronizedBuffer
12 * @ingroup concurrency
13 * @brief Wrapper class that can turn any Buffer into a thread save
14 * implementation.
15 * @author Phil Schatzmann
16 *
17 * @tparam T
18 */
19
20template <typename T>
21class SynchronizedBuffer : public BaseBuffer<T> {
22public:
23 SynchronizedBuffer(BaseBuffer<T> &buffer, MutexBase &mutex, bool syncAvailable=false) {
24 p_buffer = &buffer;
25 p_mutex = &mutex;
26 is_sync_available = syncAvailable;
27 }
28
29 // reads a single value
30 bool read(T &result) override {
31 LockGuard guard(p_mutex);
32 return p_buffer->read(result);
33 }
34
35 // reads multiple values
36 int readArray(T data[], int len) {
37 LockGuard guard(p_mutex);
38 int lenResult = MIN(len, available());
39 return p_buffer->readArray(data, lenResult);
40 }
41
42 int writeArray(const T data[], int len) {
43 LockGuard guard(p_mutex);
44 return p_buffer->writeArray(data, len);
45 }
46
47 // peeks the actual entry from the buffer
48 bool peek(T &result) override {
49 LockGuard guard(p_mutex);
50 return p_buffer->peek(result);
51 }
52
53 // checks if the buffer is full
54 bool isFull() override { return p_buffer->isFull(); }
55
56 bool isEmpty() { return available() == 0; }
57
58 // write add an entry to the buffer
59 bool write(T data) override {
60 LockGuard guard(p_mutex);
61 return p_buffer->write(data);
62 }
63
64 // clears the buffer
65 void reset() override {
66 LockGuard guard(p_mutex);
67 p_buffer->reset();
68 }
69
70 // provides the number of entries that are available to read
71 int available() override {
72 if (is_sync_available) LockGuard guard(p_mutex);
73 return p_buffer->available();
74 }
75
76 // provides the number of entries that are available to write
77 int availableForWrite() override {
78 if (is_sync_available) LockGuard guard(p_mutex);
79 return p_buffer->availableForWrite();
80 }
81
82 // returns the address of the start of the physical read buffer
83 T *address() override {
84 return p_buffer->address();
85 }
86
87 size_t size() {
88 return p_buffer->size();
89 }
90
91protected:
92 BaseBuffer<T> *p_buffer = nullptr;
93 MutexBase *p_mutex = nullptr;
94 bool is_sync_available = false;
95};
96
97
98} // namespace tinyrobotics
Shared functionality of all buffers.
Definition: Buffers.h:26
RAII implementaion using a Mutex: Only a few microcontrollers provide lock guards,...
Definition: LockGuard.h:18
Empty Mutex implementation which does nothing.
Definition: Mutex.h:18
Wrapper class that can turn any Buffer into a thread save implementation.
Definition: SynchronizedBuffer.h:21
bool peek(T &result) override
peeks the actual entry from the buffer
Definition: SynchronizedBuffer.h:48
bool read(T &result) override
reads a single value
Definition: SynchronizedBuffer.h:30
bool write(T data) override
write add an entry to the buffer
Definition: SynchronizedBuffer.h:59
int available() override
provides the number of entries that are available to read
Definition: SynchronizedBuffer.h:71
int availableForWrite() override
provides the number of entries that are available to write
Definition: SynchronizedBuffer.h:77
T * address() override
returns the address of the start of the physical read buffer
Definition: SynchronizedBuffer.h:83
bool isFull() override
checks if the buffer is full
Definition: SynchronizedBuffer.h:54
int writeArray(const T data[], int len)
Fills the buffer data.
Definition: SynchronizedBuffer.h:42
void reset() override
clears the buffer
Definition: SynchronizedBuffer.h:65
int readArray(T data[], int len)
reads multiple values
Definition: SynchronizedBuffer.h:36