1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
#ifndef ADHOCUTIL_RESOURCEPOOL_IMPL_H
#define ADHOCUTIL_RESOURCEPOOL_IMPL_H
#include "lockHelpers.h"
#include "resourcePool.h" // IWYU pragma: export
#include "safeMapFind.h"
#ifdef __cpp_lib_semaphore
# include <semaphore>
#else
# include "polyfill-semaphore.h"
#endif
#include <boost/assert.hpp>
#include <cstddef>
#include <exception>
#include <memory>
#include <thread>
#include <typeinfo>
namespace AdHoc {
//
// ResourceHandle
//
template<typename R> ResourceHandle<R>::ResourceHandle(std::shared_ptr<Object> o) noexcept : resource(std::move(o))
{
}
template<typename R> ResourceHandle<R>::~ResourceHandle() noexcept
{
if (resource) {
decRef();
}
}
template<typename R>
std::size_t
ResourceHandle<R>::handleCount() const
{
BOOST_ASSERT(resource);
// InUse has one, we don't count that
return static_cast<std::size_t>(resource.use_count() - 1);
}
template<typename R>
R *
ResourceHandle<R>::get() const noexcept
{
BOOST_ASSERT(resource);
return std::get<0>(*resource).get();
}
template<typename R>
void
ResourceHandle<R>::release() noexcept
{
decRef();
}
template<typename R> ResourceHandle<R>::operator bool() const noexcept
{
return resource.operator bool();
}
template<typename R>
R *
ResourceHandle<R>::operator->() const noexcept
{
BOOST_ASSERT(resource);
return std::get<0>(*resource).get();
}
template<typename R>
ResourceHandle<R> &
ResourceHandle<R>::operator=(const ResourceHandle & rh) noexcept
{
if (&rh != this) {
if (resource) {
decRef();
}
resource = rh.resource;
}
return *this;
}
template<typename R>
ResourceHandle<R> &
ResourceHandle<R>::operator=(ResourceHandle && rh) noexcept
{
if (&rh != this) {
if (resource) {
decRef();
}
resource = std::move(rh.resource);
}
return *this;
}
template<typename R>
void
ResourceHandle<R>::decRef() noexcept
{
BOOST_ASSERT(resource);
// InUse has one, we don't count that
if (resource.use_count() == 2) {
if (auto pool = std::get<1>(*resource)) {
if (std::uncaught_exceptions()) {
pool->discard(std::get<0>(*resource));
}
else {
pool->putBack(std::get<0>(*resource));
}
}
}
resource.reset();
}
//
// ResourcePool
//
template<typename R> ResourcePool<R>::ResourcePool(std::ptrdiff_t max, std::size_t k) : poolSize(max), keep(k) { }
template<typename R> ResourcePool<R>::~ResourcePool()
{
for (auto & r : inUse) {
std::get<1>(*r.second) = nullptr;
}
}
template<typename R>
void
ResourcePool<R>::testResource(R const *) const
{
}
template<typename R>
void
ResourcePool<R>::returnTestResource(R const *) const
{
}
template<typename R>
std::size_t
ResourcePool<R>::inUseCount() const
{
SharedLock(lock);
return inUse.size();
}
template<typename R>
std::size_t
ResourcePool<R>::availableCount() const
{
SharedLock(lock);
return available.size();
}
#ifndef __cpp_lib_semaphore
template<typename R>
std::ptrdiff_t
ResourcePool<R>::freeCount() const
{
SharedLock(lock);
return poolSize.freeCount();
}
#endif
template<typename R>
ResourceHandle<R>
ResourcePool<R>::getMine()
{
Lock(lock);
return ResourceHandle(safeMapLookup<NoCurrentResourceT<R>>(inUse, std::this_thread::get_id()));
}
template<typename R>
void
ResourcePool<R>::idle()
{
Lock(lock);
available.clear();
}
template<typename R>
ResourceHandle<R>
ResourcePool<R>::get()
{
poolSize.acquire();
try {
return ResourceHandle(getOne());
}
catch (...) {
poolSize.release();
throw;
}
}
template<typename R>
ResourceHandle<R>
ResourcePool<R>::get(const std::chrono::milliseconds timeout)
{
if (!poolSize.try_acquire_for(timeout)) {
throw TimeOutOnResourcePoolT<R>();
}
try {
return getOne();
}
catch (...) {
poolSize.release();
throw;
}
}
template<typename R>
ResourceHandle<R>
ResourcePool<R>::get(unsigned int ms)
{
return get(std::chrono::milliseconds(ms));
}
template<typename R>
ResourceHandle<R>
ResourcePool<R>::getOne()
{
Lock(lock);
while (!available.empty()) {
auto & r = available.front();
try {
testResource(r.get());
auto ro = std::make_shared<typename ResourceHandle<R>::Object>(r, this);
available.pop_front();
inUse.insert({std::this_thread::get_id(), ro});
return ResourceHandle(ro);
}
catch (...) {
available.pop_front();
}
}
auto ro = std::make_shared<typename ResourceHandle<R>::Object>(createResource(), this);
inUse.insert({std::this_thread::get_id(), ro});
return ResourceHandle(ro);
}
template<typename R>
void
ResourcePool<R>::putBack(const std::shared_ptr<R> & r)
{
Lock(lock);
removeFrom(r, inUse);
try {
returnTestResource(r.get());
if (available.size() < keep) {
available.push_back(r);
}
}
catch (...) {
}
poolSize.release();
}
template<typename R>
void
ResourcePool<R>::discard(const std::shared_ptr<R> & r)
{
Lock(lock);
removeFrom(r, inUse);
poolSize.release();
}
template<typename R>
void
ResourcePool<R>::removeFrom(const std::shared_ptr<R> & r, InUse & inUse)
{
auto rs = inUse.equal_range(std::this_thread::get_id());
for (auto & ri = rs.first; ri != rs.second; ri++) {
if (std::get<0>(*ri->second) == r) {
inUse.erase(ri);
return;
}
}
}
template<typename R> TimeOutOnResourcePoolT<R>::TimeOutOnResourcePoolT() : TimeOutOnResourcePool(typeid(R).name())
{
}
template<typename R>
NoCurrentResourceT<R>::NoCurrentResourceT(const std::thread::id & id) : NoCurrentResource(id, typeid(R).name())
{
}
}
#endif
|