USRP Hardware Driver and USRP Manual Version: 3.15.0.0-MacPorts-Release
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#ifndef INCLUDED_UHD_TRANSPORT_BOUNDED_BUFFER_IPP
9#define INCLUDED_UHD_TRANSPORT_BOUNDED_BUFFER_IPP
10
11#include <uhd/config.hpp>
13#include <boost/bind.hpp>
14#include <boost/utility.hpp>
15#include <boost/function.hpp>
16#include <boost/circular_buffer.hpp>
17#include <boost/thread/condition.hpp>
18#include <boost/thread/locks.hpp>
19
20namespace uhd{ namespace transport{
21
22 template <typename elem_type> class bounded_buffer_detail : uhd::noncopyable
23 {
24 public:
25
26 bounded_buffer_detail(size_t capacity):
27 _buffer(capacity)
28 {
29 _not_full_fcn = boost::bind(&bounded_buffer_detail<elem_type>::not_full, this);
30 _not_empty_fcn = boost::bind(&bounded_buffer_detail<elem_type>::not_empty, this);
31 }
32
33 UHD_INLINE bool push_with_haste(const elem_type &elem)
34 {
35 boost::mutex::scoped_lock lock(_mutex);
36 if (_buffer.full())
37 {
38 return false;
39 }
40 _buffer.push_front(elem);
41 _empty_cond.notify_one();
42 return true;
43 }
44
45 UHD_INLINE bool push_with_pop_on_full(const elem_type &elem)
46 {
47 boost::mutex::scoped_lock lock(_mutex);
48 if (_buffer.full())
49 {
50 _buffer.pop_back();
51 _buffer.push_front(elem);
52 _empty_cond.notify_one();
53 return false;
54 }
55 else {
56 _buffer.push_front(elem);
57 _empty_cond.notify_one();
58 return true;
59 }
60 }
61
62 UHD_INLINE void push_with_wait(const elem_type &elem)
63 {
64 boost::mutex::scoped_lock lock(_mutex);
65 if (_buffer.full())
66 {
67 _full_cond.wait(lock, _not_full_fcn);
68 }
69 _buffer.push_front(elem);
70 _empty_cond.notify_one();
71 }
72
73 UHD_INLINE bool push_with_timed_wait(const elem_type &elem, double timeout)
74 {
75 boost::mutex::scoped_lock lock(_mutex);
76 if (_buffer.full())
77 {
78 if (not _full_cond.timed_wait(lock,
79 to_time_dur(timeout), _not_full_fcn))
80 {
81 return false;
82 }
83 }
84 _buffer.push_front(elem);
85 _empty_cond.notify_one();
86 return true;
87 }
88
89 UHD_INLINE bool pop_with_haste(elem_type &elem)
90 {
91 boost::mutex::scoped_lock lock(_mutex);
92 if (_buffer.empty())
93 {
94 return false;
95 }
96 this->pop_back(elem);
97 _full_cond.notify_one();
98 return true;
99 }
100
101 UHD_INLINE void pop_with_wait(elem_type &elem)
102 {
103 boost::mutex::scoped_lock lock(_mutex);
104 if (_buffer.empty())
105 {
106 _empty_cond.wait(lock, _not_empty_fcn);
107 }
108 this->pop_back(elem);
109 _full_cond.notify_one();
110 }
111
112 UHD_INLINE bool pop_with_timed_wait(elem_type &elem, double timeout)
113 {
114 boost::mutex::scoped_lock lock(_mutex);
115 if (_buffer.empty())
116 {
117 if (not _empty_cond.timed_wait(lock, to_time_dur(timeout),
118 _not_empty_fcn))
119 {
120 return false;
121 }
122 }
123 this->pop_back(elem);
124 _full_cond.notify_one();
125 return true;
126 }
127
128 private:
129 boost::mutex _mutex;
130 boost::condition _empty_cond, _full_cond;
131 boost::circular_buffer<elem_type> _buffer;
132
133 bool not_full(void) const{return not _buffer.full();}
134 bool not_empty(void) const{return not _buffer.empty();}
135
136 boost::function<bool(void)> _not_full_fcn, _not_empty_fcn;
137
144 UHD_INLINE void pop_back(elem_type &elem)
145 {
146 elem = _buffer.back();
147 _buffer.back() = elem_type();
148 _buffer.pop_back();
149 }
150
151 static UHD_INLINE boost::posix_time::time_duration to_time_dur(double timeout)
152 {
153 return boost::posix_time::microseconds(long(timeout*1e6));
154 }
155
156 };
157}} //namespace
158
159#endif /* INCLUDED_UHD_TRANSPORT_BOUNDED_BUFFER_IPP */
Definition bounded_buffer.ipp:23
UHD_INLINE bool pop_with_haste(elem_type &elem)
Definition bounded_buffer.ipp:89
UHD_INLINE bool pop_with_timed_wait(elem_type &elem, double timeout)
Definition bounded_buffer.ipp:112
UHD_INLINE void pop_with_wait(elem_type &elem)
Definition bounded_buffer.ipp:101
bounded_buffer_detail(size_t capacity)
Definition bounded_buffer.ipp:26
UHD_INLINE bool push_with_timed_wait(const elem_type &elem, double timeout)
Definition bounded_buffer.ipp:73
UHD_INLINE void push_with_wait(const elem_type &elem)
Definition bounded_buffer.ipp:62
UHD_INLINE bool push_with_haste(const elem_type &elem)
Definition bounded_buffer.ipp:33
UHD_INLINE bool push_with_pop_on_full(const elem_type &elem)
Definition bounded_buffer.ipp:45
#define UHD_INLINE
Definition config.h:53
Definition build_info.hpp:13
boost::noncopyable noncopyable
Definition noncopyable.hpp:46