Hedgehog  0.0.0
A library to generate hybrid pipeline workflow systems
pool.h
1 // NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2 // software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3 // derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4 // or works. Modified works should carry a notice stating that you changed the software and should note the date and
5 // nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6 // source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7 // EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9 // WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10 // CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11 // THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12 // are solely responsible for determining the appropriateness of using and distributing the software and you assume
13 // all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14 // with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15 // operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16 // damage to property. The software developed by NIST employees is not subject to copyright protection within the
17 // United States.
18 
19 
20 #ifndef HEDGEHOG_POOL_H
21 #define HEDGEHOG_POOL_H
22 
23 #include <memory>
24 #include <deque>
25 #include <algorithm>
26 #include <cassert>
27 #include <mutex>
28 #include <condition_variable>
29 
31 namespace hh::behavior {
32 
35 template<class ManagedData>
36 class Pool {
37  private:
38  size_t capacity_ = 1;
39  std::deque<std::shared_ptr<ManagedData>> queue_ = {};
40  std::mutex mutex_ = {};
41  std::unique_ptr<std::condition_variable>
42  conditionVariable_ = std::make_unique<std::condition_variable>();
43 
45  public:
49  explicit Pool(size_t const &capacity) {
50  capacity_ = capacity == 0 ? 1 : capacity;
51  queue_ = std::deque<std::shared_ptr<ManagedData>>(capacity_);
52  }
53 
57  std::deque<std::shared_ptr<ManagedData>> const &queue() const { return queue_; }
58 
62  typename std::deque<std::shared_ptr<ManagedData>>::iterator begin() { return this->queue_.begin(); }
63 
67  typename std::deque<std::shared_ptr<ManagedData>>::iterator end() { return this->queue_.end(); }
68 
72  size_t size() {
73  std::lock_guard<std::mutex> lock(mutex_);
74  return queue_.size();
75  }
76 
80  bool empty() { return queue_.empty(); }
81 
85  [[nodiscard]] size_t capacity() const { return capacity_; }
86 
90  void push_back(std::shared_ptr<ManagedData> const &data) {
91  std::lock_guard<std::mutex> lock(mutex_);
92  this->queue_.push_back(data);
93  if (this->queue_.size() > capacity_) {
94  std::ostringstream oss;
95  oss << "The queue is overflowing, the same data " << data << " has been returned to the memory manager too many "
96  << "times: "
97  << __FUNCTION__;
98  HLOG_SELF(0, oss.str())
99  throw (std::runtime_error(oss.str()));
100  }
101  conditionVariable_->notify_one();
102  }
103 
106  std::shared_ptr<ManagedData> pop_front() {
107  std::unique_lock<std::mutex> lock(mutex_);
108  std::shared_ptr<ManagedData> ret = nullptr;
109  // Wait if the queue is empty
110  conditionVariable_->wait(lock, [this]() { return !queue_.empty(); });
111  ret = queue_.front();
112  queue_.pop_front();
113  conditionVariable_->notify_one();
114  return ret;
115  }
116 
117 };
118 }
119 
120 #endif //HEDGEHOG_POOL_H
size_t capacity_
Pool capacity, maximum size.
Definition: pool.h:38
void push_back(std::shared_ptr< ManagedData > const &data)
Push back data into the pool.
Definition: pool.h:90
std::deque< std::shared_ptr< ManagedData > > queue_
Container used to store pool&#39;s data.
Definition: pool.h:39
std::mutex mutex_
Pool mutex to protect pool access.
Definition: pool.h:40
Pool(size_t const &capacity)
Pool constructor defining capacity.
Definition: pool.h:49
size_t size()
Queue size accessor.
Definition: pool.h:72
std::deque< std::shared_ptr< ManagedData > >::iterator end()
End iterator accessor for the queue&#39;s container.
Definition: pool.h:67
std::unique_ptr< std::condition_variable > conditionVariable_
Condition variable to wait on data for empty queue.
Definition: pool.h:42
Pool that is used by the memory manager.
Definition: pool.h:36
std::shared_ptr< ManagedData > pop_front()
Return and pop the fist element from the queue, it the queue is empty wait for an element to come bac...
Definition: pool.h:106
size_t capacity() const
Capacity accessor.
Definition: pool.h:85
Hedgehog behavior namespace.
Definition: state_manager.h:35
std::deque< std::shared_ptr< ManagedData > >::iterator begin()
Begin iterator accessor for the queue&#39;s container.
Definition: pool.h:62
std::deque< std::shared_ptr< ManagedData > > const & queue() const
Queue container accessor.
Definition: pool.h:57
bool empty()
Emptiness queue accessor.
Definition: pool.h:80