20 #ifndef RTC_RINGBUFFER_H
21 #define RTC_RINGBUFFER_H
36 #define RINGBUFFER_DEFAULT_LENGTH 8
88 template <
class DataType>
119 : m_overwrite(true), m_readback(true),
120 m_timedwrite(false), m_timedread(false),
121 m_wtimeout(1, 0), m_rtimeout(1, 0),
123 m_wpos(0), m_rpos(0), m_fillcount(0), m_wcount(0),
190 initWritePolicy(prop);
191 initReadPolicy(prop);
216 Guard guard(m_posmutex);
274 Guard guard(m_posmutex);
305 virtual DataType*
wptr(
long int n = 0)
307 Guard guard(m_posmutex);
308 return &m_buffer[(m_wpos + n + m_length) % m_length];
343 Guard guard(m_posmutex);
344 if ((n > 0 && n > static_cast<long int>(m_length - m_fillcount)) ||
345 (n < 0 && n < static_cast<long int>(-m_fillcount)))
350 m_wpos = (m_wpos + n + m_length) % m_length;
382 virtual ReturnCode
put(
const DataType& value)
384 Guard guard(m_posmutex);
385 m_buffer[m_wpos] = value;
430 virtual ReturnCode
write(
const DataType& value,
431 long int sec = -1,
long int nsec = 0)
434 Guard guard(m_full.mutex);
439 bool timedwrite(m_timedwrite);
440 bool overwrite(m_overwrite);
448 if (overwrite && !timedwrite)
452 else if (!overwrite && !timedwrite)
456 else if (!overwrite && timedwrite)
460 sec = m_wtimeout.
sec();
461 nsec = m_wtimeout.
usec() * 1000;
464 if (!m_full.cond.wait(sec, nsec))
479 Guard eguard(m_empty.mutex);
484 m_empty.cond.signal();
517 Guard guard(m_posmutex);
518 return m_length - m_fillcount;
542 Guard guard(m_posmutex);
543 return m_length == m_fillcount;
567 virtual DataType*
rptr(
long int n = 0)
569 Guard guard(m_posmutex);
570 return &(m_buffer[(m_rpos + n + m_length) % m_length]);
602 Guard guard(m_posmutex);
603 if ((n > 0 && n > static_cast<long int>(m_fillcount)) ||
604 (n < 0 && n < static_cast<long int>(m_fillcount - m_length)))
609 m_rpos = (m_rpos + n + m_length) % m_length;
638 virtual ReturnCode
get(DataType& value)
640 Guard gaurd(m_posmutex);
641 value = m_buffer[m_rpos];
663 virtual DataType&
get()
665 Guard gaurd(m_posmutex);
666 return m_buffer[m_rpos];
711 virtual ReturnCode
read(DataType& value,
712 long int sec = -1,
long int nsec = 0)
715 Guard gaurd(m_empty.mutex);
719 bool timedread(m_timedread);
720 bool readback(m_readback);
726 sec = m_rtimeout.
sec();
727 nsec = m_rtimeout.
usec() * 1000;
730 if (readback && !timedread)
738 else if (!readback && !timedread)
742 else if (!readback && timedread)
746 sec = m_rtimeout.
sec();
747 nsec = m_rtimeout.
usec() * 1000;
750 if (!m_empty.cond.wait(sec, nsec))
765 Guard fguard(m_full.mutex);
770 m_full.cond.signal();
806 Guard guard(m_posmutex);
831 Guard guard(m_posmutex);
832 return m_fillcount == 0;
838 if (!prop[
"length"].
empty())
853 std::string policy(prop[
"write.full_policy"]);
855 if (policy ==
"overwrite")
858 m_timedwrite =
false;
860 else if (policy ==
"do_nothing")
863 m_timedwrite =
false;
865 else if (policy ==
"block")
883 std::string policy(prop[
"read.empty_policy"]);
884 if (policy ==
"readback")
889 else if (policy ==
"do_nothing")
894 else if (policy ==
"block")
1012 std::vector<DataType> m_buffer;
1023 condition() : cond(mutex) {}
1057 #endif // RTC_RINGBUFFER_H