GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
rpcbufferedget.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2015 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef RPCBUFFEREDGET_H
12 #define RPCBUFFEREDGET_H
13 
14 #include <condition_variable>
15 #include <mutex>
16 
17 template <typename TdataType>
19 {
20 public:
21  rpcbufferedget(const unsigned int init_buffer_size = 4096)
22  : d_data_needed(false),
23  d_data_ready(),
24  d_buffer_lock(),
25  d_buffer(init_buffer_size)
26  {
27  ;
28  }
29 
30  ~rpcbufferedget() { d_data_ready.notify_all(); }
31 
32  void offer_data(const TdataType& data)
33  {
34  if (!d_data_needed)
35  return;
36  {
37  std::scoped_lock lock(d_buffer_lock);
38  d_buffer = data;
39  d_data_needed = false;
40  }
41  d_data_ready.notify_one();
42  }
43 
44  TdataType get()
45  {
46  std::unique_lock lock(d_buffer_lock);
47  d_data_needed = true;
48  d_data_ready.wait(lock);
49  return d_buffer;
50  }
51 
52 private:
53  bool d_data_needed;
54  std::condition_variable d_data_ready;
55  std::mutex d_buffer_lock;
56  TdataType d_buffer;
57 };
58 
59 #endif
Definition: rpcbufferedget.h:19
~rpcbufferedget()
Definition: rpcbufferedget.h:30
void offer_data(const TdataType &data)
Definition: rpcbufferedget.h:32
TdataType get()
Definition: rpcbufferedget.h:44
rpcbufferedget(const unsigned int init_buffer_size=4096)
Definition: rpcbufferedget.h:21
boost::mutex mutex
Definition: thread.h:37
boost::unique_lock< boost::mutex > scoped_lock
Definition: thread.h:38
boost::condition_variable condition_variable
Definition: thread.h:39