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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
#pragma once
#include "base/thread.hpp"
#include "base/threaded_list.hpp"
#include "base/logging.hpp"
#include "base/assert.hpp"
#include "std/bind.hpp"
#include "std/unique_ptr.hpp"
//#define ALLOCATED_COUNT
struct BasePoolElemFactory
{
string m_resName;
size_t m_elemSize;
size_t m_batchSize;
BasePoolElemFactory(char const * resName, size_t elemSize, size_t batchSize);
size_t BatchSize() const;
char const * ResName() const;
size_t ElemSize() const;
};
/// basic traits maintains a list of free resources.
template <typename TElem, typename TElemFactory>
struct BasePoolTraits
{
TElemFactory m_factory;
ThreadedList<TElem> m_pool;
bool m_IsDebugging;
threads::ThreadID m_MainThreadID;
typedef TElem elem_t;
BasePoolTraits(TElemFactory const & factory)
: m_factory(factory),
m_IsDebugging(false),
m_MainThreadID(threads::GetCurrentThreadID())
{
m_pool.SetName(factory.ResName());
}
virtual ~BasePoolTraits()
{}
virtual void Init()
{
Free(Reserve());
}
virtual void Free(TElem const & elem)
{
m_pool.PushBack(elem);
}
virtual TElem const Reserve()
{
return m_pool.Front(true);
}
virtual size_t Size() const
{
return m_pool.Size();
}
virtual void Cancel()
{
m_pool.Cancel();
}
virtual bool IsCancelled() const
{
return m_pool.IsCancelled();
}
virtual void UpdateState()
{
}
char const * ResName() const
{
return m_factory.ResName();
}
};
/// This traits stores the free elements in a separate pool and has
/// a separate method to merge them all into a main pool.
/// For example should be used for resources where a certain preparation operation
/// should be performed on main thread before returning resource
/// to a free pool(p.e. @see resource_manager.cpp StorageFactory)
template <typename TElemFactory, typename TBase>
struct SeparateFreePoolTraits : TBase
{
typedef TBase base_t;
typedef typename base_t::elem_t elem_t;
ThreadedList<elem_t> m_freePool;
int m_maxFreePoolSize;
SeparateFreePoolTraits(TElemFactory const & factory)
: base_t(factory), m_maxFreePoolSize(0)
{}
void Free(elem_t const & elem)
{
m_freePool.PushBack(elem);
/* if (base_t::m_IsDebugging)
{
int oldMaxFreePoolSize = m_maxFreePoolSize;
m_maxFreePoolSize = max(m_maxFreePoolSize, (int)m_freePool.Size());
if (oldMaxFreePoolSize != m_maxFreePoolSize)
LOG(LINFO, (base_t::m_pool.GetName(), "freePool maximum size has reached", m_maxFreePoolSize, "elements"));
}*/
}
void UpdateStateImpl(list<elem_t> & l)
{
for (typename list<elem_t>::const_iterator it = l.begin();
it != l.end();
++it)
{
base_t::m_factory.BeforeMerge(*it);
base_t::m_pool.PushBack(*it);
}
// if ((base_t::m_IsDebugging) && (!base_t::m_pool.GetName().empty()))
// LOG(LINFO, ("pool for", base_t::m_pool.GetName(), "has", base_t::m_pool.Size(), "elements"));
l.clear();
}
void UpdateState()
{
m_freePool.ProcessList([this] (list<elem_t> & l) { UpdateStateImpl(l); });
}
};
/// This traits maintains a fixed-size of pre-allocated resources.
template <typename TElemFactory, typename TBase >
struct FixedSizePoolTraits : TBase
{
typedef TBase base_t;
typedef typename base_t::elem_t elem_t;
size_t m_count;
bool m_isAllocated;
FixedSizePoolTraits(TElemFactory const & factory, size_t count)
: base_t(factory),
m_count(count),
m_isAllocated(false)
{}
elem_t const Reserve()
{
if (!m_isAllocated)
{
m_isAllocated = true;
LOG(LDEBUG, ("allocating ", base_t::m_factory.ElemSize() * m_count, "bytes for ", base_t::m_factory.ResName()));
for (size_t i = 0; i < m_count; ++i)
base_t::m_pool.PushBack(base_t::m_factory.Create());
}
return base_t::Reserve();
}
};
/// This traits allocates resources on demand.
template <typename TElemFactory, typename TBase>
struct AllocateOnDemandMultiThreadedPoolTraits : TBase
{
typedef TBase base_t;
typedef typename base_t::elem_t elem_t;
typedef AllocateOnDemandMultiThreadedPoolTraits<TElemFactory, base_t> self_t;
#if defined(ALLOCATED_COUNT)
size_t m_poolSize;
#endif
AllocateOnDemandMultiThreadedPoolTraits(TElemFactory const & factory, size_t )
: base_t(factory)
#if defined(ALLOCATED_COUNT)
, m_poolSize(0)
#endif
{}
void AllocateIfNeeded(list<elem_t> & l)
{
if (l.empty())
{
#if defined(ALLOCATED_COUNT)
m_poolSize += base_t::m_factory.BatchSize();
#endif
for (unsigned i = 0; i < base_t::m_factory.BatchSize(); ++i)
l.push_back(base_t::m_factory.Create());
}
}
elem_t const Reserve()
{
elem_t res;
base_t::m_pool.ProcessList([this, &res] (list<elem_t> & l) { AllocateAndReserve(l, res); });
return res;
}
void AllocateAndReserve(list<elem_t> & l, elem_t & res)
{
AllocateIfNeeded(l);
ASSERT ( !l.empty(), () );
res = l.front();
l.pop_front();
}
};
template <typename TElemFactory, typename TBase>
struct AllocateOnDemandSingleThreadedPoolTraits : TBase
{
typedef TBase base_t;
typedef typename TBase::elem_t elem_t;
typedef AllocateOnDemandSingleThreadedPoolTraits<TElemFactory, TBase> self_t;
#if defined(ALLOCATED_COUNT)
size_t m_poolSize;
#endif
AllocateOnDemandSingleThreadedPoolTraits(TElemFactory const & factory, size_t )
: base_t(factory)
#if defined(ALLOCATED_COUNT)
, m_poolSize(0)
#endif
{}
void Init()
{}
void AllocateIfNeeded(list<elem_t> & l)
{
if (l.empty())
{
#if defined(ALLOCATED_COUNT)
m_poolSize += base_t::m_factory.BatchSize();
#endif
for (unsigned i = 0; i < base_t::m_factory.BatchSize(); ++i)
l.push_back(base_t::m_factory.Create());
}
}
elem_t const Reserve()
{
elem_t res;
/// allocate resources if needed if we're on the main thread.
if (threads::GetCurrentThreadID() == base_t::m_MainThreadID)
base_t::m_pool.ProcessList([this, &res] (list<elem_t> & l) { AllocateAndReserve(l, res); });
else
res = base_t::Reserve();
return res;
}
void AllocateAndReserve(list<elem_t> & l, elem_t & res)
{
AllocateIfNeeded(l);
ASSERT ( !l.empty(), () );
res = l.front();
l.pop_front();
}
void UpdateState()
{
base_t::UpdateState();
base_t::m_pool.ProcessList([this] (list<elem_t> & l) { AllocateIfNeeded(l); });
}
};
/// resource pool interface
template <typename TElem>
class ResourcePool
{
public:
virtual ~ResourcePool(){}
virtual TElem const Reserve() = 0;
virtual void Free(TElem const & elem) = 0;
virtual size_t Size() const = 0;
virtual void EnterForeground() = 0;
virtual void EnterBackground() = 0;
virtual void Cancel() = 0;
virtual bool IsCancelled() const = 0;
virtual void UpdateState() = 0;
virtual void SetIsDebugging(bool flag) = 0;
virtual char const * ResName() const = 0;
};
// This class tracks OpenGL resources allocation in
// a multithreaded environment.
template <typename TPoolTraits>
class ResourcePoolImpl : public ResourcePool<typename TPoolTraits::elem_t>
{
private:
unique_ptr<TPoolTraits> const m_traits;
public:
typedef typename TPoolTraits::elem_t elem_t;
ResourcePoolImpl(TPoolTraits * traits)
: m_traits(traits)
{
/// quick trick to perform lazy initialization
/// on the same thread the pool was created.
m_traits->Init();
}
elem_t const Reserve()
{
return m_traits->Reserve();
}
void Free(elem_t const & elem)
{
m_traits->Free(elem);
}
size_t Size() const
{
return m_traits->Size();
}
void EnterForeground()
{}
void EnterBackground()
{}
void Cancel()
{
return m_traits->Cancel();
}
bool IsCancelled() const
{
return m_traits->IsCancelled();
}
void UpdateState()
{
m_traits->UpdateState();
}
void SetIsDebugging(bool isDebugging)
{
m_traits->m_IsDebugging = isDebugging;
}
char const * ResName() const
{
return m_traits->ResName();
}
};
|