Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
pool.hh
Go to the documentation of this file.
1#pragma once
3
4#include <functional>
5#include <limits>
6#include <list>
7#include <memory>
8#include <cassert>
9
10#include "nix/util/fun.hh"
11#include "nix/util/sync.hh"
12#include "nix/util/ref.hh"
13
14namespace nix {
15
33template<class R>
34class Pool
35{
36public:
37
41 typedef fun<ref<R>()> Factory;
42
47 typedef fun<bool(const ref<R> &)> Validator;
48
49private:
50
51 Factory factory;
52 Validator validator;
53
54 struct State
55 {
56 size_t inUse = 0;
57 size_t max;
58 std::vector<ref<R>> idle;
59 };
60
61 Sync<State> state;
62
63 std::condition_variable wakeup;
64
65public:
66
73 size_t max, const Factory & factory, const Validator & validator = [](ref<R> r) { return true; })
74 : factory(factory)
75 , validator(validator)
76 {
77 auto state_(state.lock());
78 state_->max = max;
79 }
80
81 void incCapacity()
82 {
83 auto state_(state.lock());
84 state_->max++;
85 /* we could wakeup here, but this is only used when we're
86 * about to nest Pool usages, and we want to save the slot for
87 * the nested use if we can
88 */
89 }
90
91 void decCapacity()
92 {
93 auto state_(state.lock());
94 state_->max--;
95 }
96
97 ~Pool()
98 {
99 auto state_(state.lock());
100 assert(!state_->inUse);
101 state_->max = 0;
102 state_->idle.clear();
103 }
104
105 class Handle
106 {
107 private:
108 Pool & pool;
109 std::shared_ptr<R> r;
110 bool bad = false;
111
112 friend Pool;
113
114 Handle(Pool & pool, std::shared_ptr<R> r)
115 : pool(pool)
116 , r(r)
117 {
118 }
119
120 public:
121 // NOTE: Copying std::shared_ptr and calling a .reset() on it is always noexcept.
122 Handle(Handle && h) noexcept
123 : pool(h.pool)
124 , r(h.r)
125 {
126 static_assert(noexcept(h.r.reset()));
127 static_assert(noexcept(std::shared_ptr(h.r)));
128 h.r.reset();
129 }
130
131 Handle(const Handle & l) = delete;
132
133 ~Handle()
134 {
135 if (!r)
136 return;
137 {
138 auto state_(pool.state.lock());
139 if (!bad)
140 state_->idle.push_back(ref<R>(r));
141 assert(state_->inUse);
142 state_->inUse--;
143 }
144 pool.wakeup.notify_one();
145 }
146
147 R * operator->()
148 {
149 return &*r;
150 }
151
152 R & operator*()
153 {
154 return *r;
155 }
156
157 void markBad()
158 {
159 bad = true;
160 }
161 };
162
163 Handle get()
164 {
165 {
166 auto state_(state.lock());
167
168 /* If we're over the maximum number of instance, we need
169 to wait until a slot becomes available. */
170 while (state_->idle.empty() && state_->inUse >= state_->max)
171 state_.wait(wakeup);
172
173 while (!state_->idle.empty()) {
174 auto p = state_->idle.back();
175 state_->idle.pop_back();
176 if (validator(p)) {
177 state_->inUse++;
178 return Handle(*this, p);
179 }
180 }
181
182 state_->inUse++;
183 }
184
185 /* We need to create a new instance. Because that might take a
186 while, we don't hold the lock in the meantime. */
187 try {
188 Handle h(*this, factory());
189 return h;
190 } catch (...) {
191 auto state_(state.lock());
192 state_->inUse--;
193 wakeup.notify_one();
194 throw;
195 }
196 }
197
198 size_t count()
199 {
200 auto state_(state.lock());
201 return state_->idle.size() + state_->inUse;
202 }
203
204 size_t capacity()
205 {
206 return state.lock()->max;
207 }
208
209 void flushBad()
210 {
211 auto state_(state.lock());
212 std::vector<ref<R>> left;
213 for (auto & p : state_->idle)
214 if (validator(p))
215 left.push_back(p);
216 std::swap(state_->idle, left);
217 }
218
219 std::vector<ref<R>> clear()
220 {
221 auto state_(state.lock());
222 return std::exchange(state_->idle, {});
223 }
224};
225
226} // namespace nix
Definition pool.hh:106
Pool(size_t max, const Factory &factory, const Validator &validator=[](ref< R > r) { return true;})
Definition pool.hh:72
fun< ref< R >()> Factory
Definition pool.hh:41
fun< bool(const ref< R > &)> Validator
Definition pool.hh:47
WriteLock lock()
Definition sync.hh:139
Definition fun.hh:19
Definition ref.hh:58