57 std::vector<ref<R>> idle;
62 std::condition_variable wakeup;
66 Pool(
size_t max = std::numeric_limits<size_t>::max(),
67 const Factory & factory = []() {
return make_ref<R>(); },
68 const Validator & validator = [](ref<R> r) {
return true; })
70 , validator(validator)
72 auto state_(state.lock());
78 auto state_(state.lock());
88 auto state_(state.lock());
94 auto state_(state.lock());
95 assert(!state_->inUse);
104 std::shared_ptr<R> r;
109 Handle(Pool & pool, std::shared_ptr<R> r) : pool(pool), r(r) { }
113 Handle(Handle && h) noexcept
117 static_assert(
noexcept(h.r.reset()));
118 static_assert(
noexcept(std::shared_ptr(h.r)));
122 Handle(
const Handle & l) =
delete;
128 auto state_(pool.state.lock());
130 state_->idle.push_back(
ref<R>(r));
131 assert(state_->inUse);
134 pool.wakeup.notify_one();
137 R * operator -> () {
return &*r; }
138 R & operator * () {
return *r; }
140 void markBad() { bad =
true; }
146 auto state_(state.lock());
150 while (state_->idle.empty() && state_->inUse >= state_->max)
153 while (!state_->idle.empty()) {
154 auto p = state_->idle.back();
155 state_->idle.pop_back();
168 Handle h(*
this, factory());
171 auto state_(state.lock());
180 auto state_(state.lock());
181 return state_->idle.size() + state_->inUse;
186 return state.lock()->max;
191 auto state_(state.lock());
192 std::vector<ref<R>> left;
193 for (
auto &
p : state_->idle)
196 std::swap(state_->idle, left);