Skip to content

Commit 5c3b1ae

Browse files
committed
Replace single value with run-time configurable circular buffer.
1 parent 127682a commit 5c3b1ae

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/threading/Sink.hpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include <mbed.h>
2727

28+
#include "CircularBuffer.hpp"
29+
2830
/**************************************************************************************
2931
* CLASS DECLARATION
3032
**************************************************************************************/
@@ -64,7 +66,7 @@ class SinkBlocking : public SinkBase<T>
6466
{
6567
public:
6668

67-
SinkBlocking();
69+
SinkBlocking(size_t const size);
6870
virtual ~SinkBlocking() { }
6971

7072
virtual operator T() override;
@@ -73,8 +75,7 @@ class SinkBlocking : public SinkBase<T>
7375

7476
private:
7577

76-
T _data;
77-
bool _is_data_available;
78+
CircularBuffer<T> _data;
7879
rtos::Mutex _mutex;
7980
rtos::ConditionVariable _cond_data_available;
8081
rtos::ConditionVariable _cond_slot_available;
@@ -106,8 +107,8 @@ void SinkNonBlocking<T>::inject(T const & value)
106107
**************************************************************************************/
107108

108109
template<typename T>
109-
SinkBlocking<T>::SinkBlocking()
110-
: _is_data_available{false}
110+
SinkBlocking<T>::SinkBlocking(size_t const size)
111+
: _data(size)
111112
, _cond_data_available(_mutex)
112113
, _cond_slot_available(_mutex)
113114
{ }
@@ -116,10 +117,9 @@ template<typename T>
116117
SinkBlocking<T>::operator T()
117118
{
118119
_mutex.lock();
119-
while (!_is_data_available)
120+
while (_data.isEmpty())
120121
_cond_data_available.wait();
121-
T const d = _data;
122-
_is_data_available = false;
122+
T const d = _data.read();
123123
_cond_slot_available.notify_all();
124124
_mutex.unlock();
125125
return d;
@@ -129,10 +129,9 @@ template<typename T>
129129
void SinkBlocking<T>::inject(T const & value)
130130
{
131131
_mutex.lock();
132-
while (_is_data_available)
132+
while (_data.isFull())
133133
_cond_slot_available.wait();
134-
_data = value;
135-
_is_data_available = true;
134+
_data.store(value);
136135
_cond_data_available.notify_all();
137136
_mutex.unlock();
138137
}

0 commit comments

Comments
 (0)