USRP Hardware Driver and USRP Manual Version: 4.4.0.0
UHD and USRP Manual
 
Loading...
Searching...
No Matches
bounded_buffer.ipp
Go to the documentation of this file.
1//
2// Copyright 2010-2013 Ettus Research LLC
3// Copyright 2018 Ettus Research, a National Instruments Company
4//
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
7
8#pragma once
9
10#include <uhd/config.hpp>
12#include <boost/circular_buffer.hpp>
13#include <boost/thread/condition.hpp>
14#include <boost/utility.hpp>
15#include <functional>
16#include <mutex>
17
18namespace uhd{ namespace transport{
19
20 template <typename elem_type> class bounded_buffer_detail : uhd::noncopyable
21 {
22 public:
23
24 bounded_buffer_detail(size_t capacity):
25 _buffer(capacity)
26 {
27 _not_full_fcn = std::bind(&bounded_buffer_detail<elem_type>::not_full, this);
28 _not_empty_fcn = std::bind(&bounded_buffer_detail<elem_type>::not_empty, this);
29 }
30
31 UHD_INLINE bool push_with_haste(const elem_type &elem)
32 {
33 std::unique_lock<std::mutex> lock(_mutex);
34 if (_buffer.full()) {
35 return false;
36 }
37 _buffer.push_front(elem);
38 _empty_cond.notify_one();
39 return true;
40 }
41
42 UHD_INLINE bool push_with_pop_on_full(const elem_type &elem)
43 {
44 std::lock_guard<std::mutex> lock(_mutex);
45 if (_buffer.full()) {
46 _buffer.pop_back();
47 _buffer.push_front(elem);
48 _empty_cond.notify_one();
49 return false;
50 } else {
51 _buffer.push_front(elem);
52 _empty_cond.notify_one();
53 return true;
54 }
55 }
56
57 UHD_INLINE void push_with_wait(const elem_type &elem)
58 {
59 std::unique_lock<std::mutex> lock(_mutex);
60 if (_buffer.full())
61 {
62 _full_cond.wait(lock, _not_full_fcn);
63 }
64 _buffer.push_front(elem);
65 _empty_cond.notify_one();
66 }
67
68 UHD_INLINE bool push_with_timed_wait(const elem_type &elem, double timeout)
69 {
70 std::unique_lock<std::mutex> lock(_mutex);
71 if (_buffer.full())
72 {
73 if (not _full_cond.timed_wait(lock,
74 to_time_dur(timeout), _not_full_fcn))
75 {
76 return false;
77 }
78 }
79 _buffer.push_front(elem);
80 _empty_cond.notify_one();
81 return true;
82 }
83
84 UHD_INLINE bool pop_with_haste(elem_type &elem)
85 {
86 std::unique_lock<std::mutex> lock(_mutex);
87 if (_buffer.empty()) {
88 return false;
89 }
90 this->pop_back(elem);
91 _full_cond.notify_one();
92 return true;
93 }
94
95 UHD_INLINE void pop_with_wait(elem_type &elem)
96 {
97 std::unique_lock<std::mutex> lock(_mutex);
98 if (_buffer.empty())
99 {
100 _empty_cond.wait(lock, _not_empty_fcn);
101 }
102 this->pop_back(elem);
103 _full_cond.notify_one();
104 }
105
106 UHD_INLINE bool pop_with_timed_wait(elem_type &elem, double timeout)
107 {
108 std::unique_lock<std::mutex> lock(_mutex);
109 if (_buffer.empty()) {
110 if (not _empty_cond.timed_wait(lock, to_time_dur(timeout),
111 _not_empty_fcn))
112 {
113 return false;
114 }
115 }
116 this->pop_back(elem);
117 _full_cond.notify_one();
118 return true;
119 }
120
121 private:
122 std::mutex _mutex;
123 boost::condition _empty_cond, _full_cond;
124 boost::circular_buffer<elem_type> _buffer;
125
126 bool not_full(void) const{return not _buffer.full();}
127 bool not_empty(void) const{return not _buffer.empty();}
128
129 std::function<bool(void)> _not_full_fcn, _not_empty_fcn;
130
137 UHD_INLINE void pop_back(elem_type &elem)
138 {
139 elem = _buffer.back();
140 _buffer.back() = elem_type();
141 _buffer.pop_back();
142 }
143
144 static UHD_INLINE boost::posix_time::time_duration to_time_dur(double timeout)
145 {
146 return boost::posix_time::microseconds(long(timeout*1e6));
147 }
148
149 };
150}} //namespace
Definition bounded_buffer.ipp:21
UHD_INLINE bool pop_with_haste(elem_type &elem)
Definition bounded_buffer.ipp:84
UHD_INLINE bool pop_with_timed_wait(elem_type &elem, double timeout)
Definition bounded_buffer.ipp:106
UHD_INLINE void pop_with_wait(elem_type &elem)
Definition bounded_buffer.ipp:95
bounded_buffer_detail(size_t capacity)
Definition bounded_buffer.ipp:24
UHD_INLINE bool push_with_timed_wait(const elem_type &elem, double timeout)
Definition bounded_buffer.ipp:68
UHD_INLINE void push_with_wait(const elem_type &elem)
Definition bounded_buffer.ipp:57
UHD_INLINE bool push_with_haste(const elem_type &elem)
Definition bounded_buffer.ipp:31
UHD_INLINE bool push_with_pop_on_full(const elem_type &elem)
Definition bounded_buffer.ipp:42
#define UHD_INLINE
Definition config.h:65
Definition build_info.hpp:12
boost::noncopyable noncopyable
Definition noncopyable.hpp:45