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