asynchrony 0.0.0
Add asynchrony to your C++ applications using standard C++20
Loading...
Searching...
No Matches
resource_pool.hpp
1/*
2 asynchrony : Add asynchrony to your apps
3
4 BSD 3-Clause License
5
6 Copyright (c) 2021, Siddiq Software LLC
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11
12 1. Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
14
15 2. Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 3. Neither the name of the copyright holder nor the names of its
20 contributors may be used to endorse or promote products derived from
21 this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#pragma once
36#ifndef RESOURCE_POOL_HPP
37#define RESOURCE_POOL_HPP
38
39#include <stdexcept>
40#include <mutex>
41#include <shared_mutex>
42#include <deque>
43
44#include "siddiqsoft/RunOnEnd.hpp"
45
46namespace siddiqsoft
47{
58 template <typename T>
59 requires std::move_constructible<T>
60 class resource_pool
61 {
62 private:
63 std::deque<T> _pool {};
64 std::mutex _poolLock {}; // FIX: Changed from recursive_mutex to mutex (no recursive locking needed)
65
66 public:
67 resource_pool() = default;
68 resource_pool(resource_pool&) = delete;
69 resource_pool(resource_pool&& src) = default;
70 resource_pool& operator=(resource_pool&) = delete;
71 resource_pool& operator=(resource_pool&& src) = default;
72
73 ~resource_pool()
74 {
75 clear();
76 }
77
80 void clear()
81 {
82 std::scoped_lock<std::mutex> l(_poolLock);
83 _pool.clear();
84 }
85
89 auto size()
90 {
91 std::scoped_lock<std::mutex> l(_poolLock);
92 return _pool.size();
93 }
94
95 [[nodiscard]] T checkout() /* throw() */
96 {
97 std::scoped_lock<std::mutex> l(_poolLock);
98 if (!_pool.empty()) {
99 RunOnEnd roe([&]() { _pool.pop_front(); });
100 return std::move(_pool.front());
101 }
102
103 throw std::runtime_error("Empty pool; add something first!");
104 }
105
111 void checkin(T&& rsrc)
112 {
113 std::scoped_lock<std::mutex> l(_poolLock);
114 _pool.push_back(std::move(rsrc));
115 }
116 };
117} // namespace siddiqsoft
118#endif
auto size()
Get the current size of the pool FIX: Removed unnecessary empty check - return size unconditionally T...
void checkin(T &&rsrc)
Insert a new element or return a borrowed element.
void clear()
Clear all items from the pool FIX: Removed unnecessary empty check - clear() is safe on empty deque.