Rudiments
resourcepoolinlines.h
1// Copyright (c) 1999-2018 David Muse
2// See the COPYING file for more information
3
4#include <rudiments/stdio.h>
5#include <rudiments/private/nodeinlines.h>
6
7template <class valuetype>
8inline
9resourcepool<valuetype>::resourcepool() : minval(0), maxval(10),
10 growby(1), total(0),
11 initialized(false),
12 mtx(NULL) {
13}
14
15template <class valuetype>
16inline
20
21template <class valuetype>
22inline
24 minval=min;
25 if (maxval<min) {
26 maxval=min;
27 }
28}
29
30template <class valuetype>
31inline
33 return minval;
34}
35
36template <class valuetype>
37inline
39 maxval=max;
40}
41
42template <class valuetype>
43inline
45 return maxval;
46}
47
48template <class valuetype>
49inline
51 this->growby=(!growby)?1:growby;
52}
53
54template <class valuetype>
55inline
57 return growby;
58}
59
60template <class valuetype>
61inline
63
64 // lock mutex
65 if (mtx && !mtx->lock()) {
66 return false;
67 }
68
69 for (uint64_t i=0; i<minval; i++) {
70 valuetype *v=createResource();
71 if (!v) {
72 clearDelegate();
73
74 // unlock mutex
75 if (mtx) {
76 mtx->unlock();
77 }
78
79 return false;
80 }
81 initiallist.append(v);
82 }
83 total=minval;
84 initialized=true;
85
86 // unlock mutex
87 if (mtx) {
88 mtx->unlock();
89 }
90
91 return true;
92}
93
94template <class valuetype>
95inline
97
98 // lock mutex
99 if (mtx && !mtx->lock()) {
100 return false;
101 }
102
103 clearDelegate();
104
105 // unlock mutex
106 if (mtx) {
107 mtx->unlock();
108 }
109 return true;
110}
111
112template <class valuetype>
113inline
115
116 // lock mutex
117 if (mtx && !mtx->lock()) {
118 return false;
119 }
120
121 clearDelegate();
122 minval=0;
123 maxval=10;
124 growby=1;
125
126 // unlock mutex
127 if (mtx) {
128 mtx->unlock();
129 }
130 return true;
131}
132
133template <class valuetype>
134inline
136
137 initiallist.setManageValues(true);
138 initiallist.clear();
139 initiallist.setManageValues(false);
140
141 ondemandlist.setManageValues(true);
142 ondemandlist.clear();
143 ondemandlist.setManageValues(false);
144
145 initialized=false;
146}
147
148template <class valuetype>
149inline
151
152 // lock mutex
153 if (mtx && !mtx->lock()) {
154 return NULL;
155 }
156
157 // if we have initial resources available to loan out,
158 // then loan one out
159 if (initiallist.getCount()) {
160
161 // the list contains resources available to be loaned out and
162 // the tree contains resources that have been loaned out,
163 // move the resource from the list to the tree and return it
164 listnode<valuetype *> *node=initiallist.getFirst();
165 valuetype *v=node->getValue();
166 initialtree.insert(v);
167 initiallist.remove(node);
168
169 // unlock mutex
170 if (mtx) {
171 mtx->unlock();
172 }
173
174 // return the resource
175 return v;
176
177 }
178
179 // if we don't have any on-demand resources available to loan out,
180 // then grow, if we can
181 if (!ondemandlist.getCount()) {
182 for (uint64_t i=0; i<growby && total<maxval; i++) {
183 valuetype *v=createResource();
184 ondemandlist.append(v);
185 total++;
186 }
187 }
188
189 // if we have on-demand resources available to loan out,
190 // then loan one out
191 if (ondemandlist.getCount()) {
192
193 // the list contains resources available to be loaned out and
194 // the tree contains resources that have been loaned out,
195 // move the resource from the list to the tree and return it
196 listnode<valuetype *> *node=ondemandlist.getFirst();
197 valuetype *v=node->getValue();
198 ondemandtree.insert(v);
199 ondemandlist.remove(node);
200
201 // unlock mutex
202 if (mtx) {
203 mtx->unlock();
204 }
205
206 // return the resource
207 return v;
208 }
209
210 // unlock mutex
211 if (mtx) {
212 mtx->unlock();
213 }
214
215 // if no resources are available then return NULL
216 return NULL;
217}
218
219template <class valuetype>
220inline
222
223 if (mtx) {
224 if (!mtx->lock()) {
225 return false;
226 }
227 }
228
229 // if this is one of the initial resources...
230 treenode<valuetype *> *node=initialtree.find(resource);
231 if (node) {
232
233 // the tree contains resources that have been loaned out and
234 // the list contains resources available to be loaned out,
235 // move the resource from the tree to the list
236 initiallist.append(node->getValue());
237 initialtree.remove(node);
238
239 } else {
240
241 // if this is one of the on-demand resources...
242 treenode<valuetype *> *node=ondemandtree.find(resource);
243 if (node) {
244
245 // the tree contains resources that have been loaned
246 // out and the list contains resources available to be
247 // loaned out, delete the resource and remove its tree
248 // node
249 delete node->getValue();
250 ondemandtree.remove(node);
251 }
252 }
253
254 if (mtx) {
255 if (!mtx->unlock()) {
256 return false;
257 }
258 }
259 return true;
260}
261
262template <class valuetype>
263inline
265 return new valuetype;
266}
267
268template <class valuetype>
269inline
271 this->mtx=mtx;
272}
273
274template <class valuetype>
275inline
277 return initiallist.getCount();
278}
279
280
281template <class valuetype>
282inline
284 return ondemandlist.getCount();
285}
Definition avltree.h:11
avltreenode(valuetype value)
Definition avltreeinlines.h:555
valuetype getValue()
Definition avltreeinlines.h:578
Definition resourcepool.h:14
bool returnResource(valuetype *resource)
Definition resourcepoolinlines.h:221
void setGrowBy(uint64_t growby)
Definition resourcepoolinlines.h:50
virtual ~resourcepool()
Definition resourcepoolinlines.h:17
bool clear()
Definition resourcepoolinlines.h:96
void setMutex(threadmutex *mtx)
Definition resourcepoolinlines.h:270
uint64_t getMax()
Definition resourcepoolinlines.h:44
uint64_t getAvailableOnDemandResourceCount()
Definition resourcepoolinlines.h:283
uint64_t getGrowBy()
Definition resourcepoolinlines.h:56
bool reset()
Definition resourcepoolinlines.h:114
resourcepool()
Definition resourcepoolinlines.h:9
bool create()
Definition resourcepoolinlines.h:62
uint64_t getMin()
Definition resourcepoolinlines.h:32
valuetype * borrowResource()
Definition resourcepoolinlines.h:150
void setMax(uint64_t max)
Definition resourcepoolinlines.h:38
void setMin(uint64_t min)
Definition resourcepoolinlines.h:23
uint64_t getAvailableInitialResourceCount()
Definition resourcepoolinlines.h:276
Definition threadmutex.h:11
bool unlock()