Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
pool.h
Go to the documentation of this file.
1
2
3// NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
4// software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
5// derivative works of the software or any portion of the software, and you may copy and distribute such modifications
6// or works. Modified works should carry a notice stating that you changed the software and should note the date and
7// nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
8// source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
9// EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
10// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
11// WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
12// CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
13// THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
14// are solely responsible for determining the appropriateness of using and distributing the software and you assume
15// all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
16// with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
17// operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
18// damage to property. The software developed by NIST employees is not subject to copyright protection within the
19// United States.
20
21#ifndef HEDGEHOG_POOL_H_
22#define HEDGEHOG_POOL_H_
23
24#pragma once
25
26#include <deque>
27#include <memory>
28#include <sstream>
29#include <condition_variable>
30
32namespace hh {
34namespace tool {
35
38template<class T>
39class Pool {
40 private:
41 size_t const capacity_ = 1;
42 std::deque<std::shared_ptr<T>> queue_ = {};
43 std::mutex mutex_ = {};
44 std::unique_ptr<std::condition_variable>
45 conditionVariable_ = std::make_unique<std::condition_variable>();
47 public:
50 explicit Pool(size_t const &capacity) : capacity_(capacity == 0 ? 1 : capacity) {
51 queue_ = std::deque<std::shared_ptr<T >>(capacity_);
52 }
53
56 typename std::deque<std::shared_ptr<T>>::iterator begin() { return this->queue_.begin(); }
57
60 typename std::deque<std::shared_ptr<T>>::iterator end() { return this->queue_.end(); }
61
64 size_t size() {
65 mutex_.lock();
66 auto s = queue_.size();
67 mutex_.unlock();
68 return s;
69 }
70
73 bool empty() {
74 mutex_.lock();
75 auto e = queue_.empty();
76 mutex_.unlock();
77 return e;
78 }
79
82 [[nodiscard]] size_t capacity() const { return capacity_; }
83
88 void push_back(std::shared_ptr<T> const &data) {
89 mutex_.lock();
90 this->queue_.push_back(data);
91
92 if (this->queue_.size() > capacity_) {
93 std::ostringstream oss;
94 oss << "The queue is overflowing, the same data " << data
95 << " has been returned to the memory manager too many times: " << __FUNCTION__;
96 throw (std::runtime_error(oss.str()));
97 }
98 mutex_.unlock();
99 conditionVariable_->notify_one();
100 }
101
104 std::shared_ptr<T> pop_front() {
105 std::unique_lock<std::mutex> lock(mutex_);
106 std::shared_ptr<T> ret = nullptr;
107 conditionVariable_->wait(lock, [this]() { return !queue_.empty(); });
108 ret = queue_.front();
109 queue_.pop_front();
110 return ret;
111 }
112};
113}
114}
115#endif //HEDGEHOG_POOL_H_
Hedgehog main namespace.
Pool of data used by the memory manager.
Definition: pool.h:39
std::deque< std::shared_ptr< T > >::iterator end()
Getter to the iterator to the end of the Pool.
Definition: pool.h:60
std::unique_ptr< std::condition_variable > conditionVariable_
Condition variable to wake up a thread waiting for data.
Definition: pool.h:45
std::shared_ptr< T > pop_front()
Extract an element from the queue. If none is available wait until one become available.
Definition: pool.h:104
Pool(size_t const &capacity)
Create a pool with a certain capacity.
Definition: pool.h:50
std::deque< std::shared_ptr< T > >::iterator begin()
Getter to the iterator to the beginning of the Pool.
Definition: pool.h:56
size_t size()
Getter to the pool's size.
Definition: pool.h:64
size_t capacity() const
Getter to the pool's capacity.
Definition: pool.h:82
bool empty()
Returns true if the Pool is empty. (Thus begin() would equal end()).
Definition: pool.h:73
size_t const capacity_
Capacity of the pool.
Definition: pool.h:41
std::mutex mutex_
Mutex used to protect the queue.
Definition: pool.h:43
void push_back(std::shared_ptr< T > const &data)
The function creates an element at the end of the pool and assigns the given data to it....
Definition: pool.h:88
std::deque< std::shared_ptr< T > > queue_
Actual storage used by the pool.
Definition: pool.h:42