-
Notifications
You must be signed in to change notification settings - Fork 318
/
bdlma_concurrentpool.cpp
386 lines (327 loc) · 13.3 KB
/
bdlma_concurrentpool.cpp
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// bdlma_concurrentpool.cpp -*-C++-*-
#include <bdlma_concurrentpool.h>
#include <bsls_ident.h>
BSLS_IDENT_RCSID(bdlma_concurrentpool_cpp,"$Id$ $CSID$")
#include <bslmt_lockguard.h>
#include <bsls_alignmentutil.h>
#include <bsls_assert.h>
#include <bsls_performancehint.h>
#include <bsl_algorithm.h> // for 'max()'
#include <bsl_cstddef.h> // for 'offsetof()'
#include <bsl_cstdlib.h>
namespace BloombergLP {
namespace {
// -----
// TYPES
// -----
struct LLink {
// This 'struct' implements a link data structure that stores the address
// of the next link, used to implement the internal linked list of free
// memory blocks. Note that this type is copied from
// 'bdlma_concurrentpool.h' to provide access to this type from static
// methods.
union {
bsls::AtomicOperations::AtomicTypes::Int d_refCount;
bsls::AlignmentUtil::MaxAlignedType d_dummy;
};
LLink *d_next_p;
};
// ---------
// CONSTANTS
// ---------
enum {
k_INITIAL_CHUNK_SIZE = 1, // default 'numObjects' value
k_MAX_CHUNK_SIZE = 32 // minimum 'd_numObjects' value beyond which
// 'd_numObjects' becomes positive
};
} // close unnamed namespace
// implementation details of private support functions
static inline
bsls::Types::size_type roundUp(bsls::Types::size_type x,
bsls::Types::size_type y)
// Round up the specified 'x' to the nearest whole integer multiple of the
// specified 'y'.
{
return (x + y - 1) / y * y;
}
static inline
LLink *toLink(char *address)
// Return a linked-list link at the specified 'address'.
{
// Note that a 'char *' cannot be converted directly to a 'LLink *'.
return static_cast<LLink *>(static_cast<void *>(address));
}
// private support functions
static inline
bsls::Types::size_type computeInternalBlockSize(
bsls::Types::size_type blockSize)
// Return the number of bytes that must be allocated to provide an aligned
// block of memory of the specified 'blockSize' that can also be used to
// represent a 'object' 'LLink' (on the 'bdlma::ConcurrentPool' objects
// free list). Note that this value is the maximum of either the size of a
// 'LLink' object or 'blockSize' rounded up to the alignment required for a
// 'LLink' object (i.e., the maximum platform alignment).
{
const bsls::Types::size_type HEADER_LENGTH = offsetof(LLink, d_next_p);
const bsls::Types::size_type MINIMUM_LENGTH = sizeof(LLink);
return roundUp(bsl::max(blockSize + HEADER_LENGTH, MINIMUM_LENGTH),
bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT);
}
static
void replenishImp(bsls::AtomicPointer<LLink> *nextList,
bdlma::InfrequentDeleteBlockList *blockList,
bsls::Types::size_type blockSize,
int numBlocks)
// Append to the specified 'nextList', 'numBlocks' free memory blocks each
// having the specified 'blockSize', using memory provided by the specified
// 'blockList'. The behavior is undefined unless '1 <= blockSize' and
// '1 <= numBlocks'.
{
using namespace BloombergLP;
BSLS_ASSERT(blockList);
BSLS_ASSERT(1 <= blockSize);
BSLS_ASSERT(1 <= numBlocks);
char *start = static_cast<char *>(
blockList->allocate(numBlocks * blockSize));
char *end = start + (numBlocks - 1) * blockSize;
for (char *p = start; p < end; p += blockSize) {
LLink *nextLink = toLink(p);
bsls::AtomicOperations::initInt(&nextLink->d_refCount, 0 );
nextLink->d_next_p = toLink(p + blockSize);
}
bsls::AtomicOperations::initInt(&toLink(end)->d_refCount, 0 );
LLink *old;
do {
old = *nextList;
toLink(end)->d_next_p = old;
} while (old != nextList->testAndSwap(old, toLink(start)));
}
namespace bdlma {
// --------------------
// class ConcurrentPool
// --------------------
// PRIVATE MANIPULATORS
void ConcurrentPool::replenish()
{
replenishImp(reinterpret_cast<bsls::AtomicPointer<LLink> *>(&d_freeList),
&d_blockList,
d_internalBlockSize,
d_chunkSize);
if (bsls::BlockGrowth::BSLS_GEOMETRIC == d_growthStrategy
&& d_chunkSize < d_maxBlocksPerChunk) {
if (d_chunkSize * 2 <= d_maxBlocksPerChunk) {
d_chunkSize = d_chunkSize * 2;
}
else {
d_chunkSize = d_maxBlocksPerChunk;
}
}
}
// CREATORS
ConcurrentPool::ConcurrentPool(bsls::Types::size_type blockSize,
bslma::Allocator *basicAllocator)
: d_blockSize(blockSize)
, d_chunkSize(k_INITIAL_CHUNK_SIZE)
, d_maxBlocksPerChunk(k_MAX_CHUNK_SIZE)
, d_growthStrategy(bsls::BlockGrowth::BSLS_GEOMETRIC)
, d_freeList(0)
, d_blockList(basicAllocator)
{
BSLS_ASSERT(1 <= blockSize);
d_internalBlockSize = computeInternalBlockSize(blockSize);
}
ConcurrentPool::ConcurrentPool(bsls::Types::size_type blockSize,
bsls::BlockGrowth::Strategy growthStrategy,
bslma::Allocator *basicAllocator)
: d_blockSize(blockSize)
, d_chunkSize(bsls::BlockGrowth::BSLS_CONSTANT == growthStrategy
? k_MAX_CHUNK_SIZE : k_INITIAL_CHUNK_SIZE)
, d_maxBlocksPerChunk(k_MAX_CHUNK_SIZE)
, d_growthStrategy(growthStrategy)
, d_freeList(0)
, d_blockList(basicAllocator)
{
BSLS_ASSERT(1 <= blockSize);
d_internalBlockSize = computeInternalBlockSize(blockSize);
}
ConcurrentPool::ConcurrentPool(bsls::Types::size_type blockSize,
bsls::BlockGrowth::Strategy growthStrategy,
int maxBlocksPerChunk,
bslma::Allocator *basicAllocator)
: d_blockSize(blockSize)
, d_chunkSize(bsls::BlockGrowth::BSLS_CONSTANT == growthStrategy
? maxBlocksPerChunk : k_INITIAL_CHUNK_SIZE)
, d_maxBlocksPerChunk(maxBlocksPerChunk)
, d_growthStrategy(growthStrategy)
, d_freeList(0)
, d_blockList(basicAllocator)
{
BSLS_ASSERT(1 <= blockSize);
BSLS_ASSERT(1 <= maxBlocksPerChunk);
d_internalBlockSize = computeInternalBlockSize(blockSize);
}
ConcurrentPool::~ConcurrentPool()
{
BSLS_ASSERT(static_cast<int>(sizeof(LLink)) <= d_internalBlockSize);
BSLS_ASSERT(0 != d_chunkSize);
}
// MANIPULATORS
void *ConcurrentPool::allocate()
{
Link *p;
for (;;) {
p = d_freeList.loadRelaxed();
if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(!p)) {
BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
p = d_freeList;
if (!p) {
replenish();
continue;
}
}
if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY
(2 != bsls::AtomicOperations::addIntNv(&p->d_refCount, 2))) {
BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
for (int i = 0; i < 3; ++i) {
// To avoid unnecessary contention, assume that if we did not
// get the first reference, then the other thread is about to
// complete the pop. Wait for a few cycles until it does. If
// it does not complete then go on and try to acquire it
// ourselves.
if (d_freeList.loadRelaxed() != p) {
break;
}
}
}
// Force a dependent read of 'd_next_p' to make sure that we're not
// racing against another thread calling 'deallocate' for 'p' and that
// checked the refcount *before* we incremented it, put back 'p' in the
// freelist with a potentially different 'd_next_p'.
//
// There are two possibilities in this particular case:
// - The following 'loadRelaxed()' will return the new 'freelist'
// value (== p) and because of the release barrier before the last
// CAS in deallocate, we can observe the new 'd_next_p' value (this
// relies on dependent load ordering)
// - loadRelaxed() will return the "old" (!= p) and the CAS and thus
// the condition will be false.
//
// Note that 'h' is made volatile so that the compiler does not replace
// the 'h->d_inUse' load with 'p->d_inUse' (and thus removing the data
// dependency). TBD to be completely thorough 'h->d_next_p' needs a
// load dependent barrier (no-op on all current architectures though).
const Link * volatile h = d_freeList.loadRelaxed();
// gcc 4.3, 4.4 seems to have trouble processing likely(a && b), using
// likely(a) && likely(b) fixes the problem. 3.4.6 seems to generate
// the proper code though.
if (BSLS_PERFORMANCEHINT_PREDICT_LIKELY(h == p)
&& BSLS_PERFORMANCEHINT_PREDICT_LIKELY(
d_freeList.testAndSwap(p, h->d_next_p) == p)) {
break;
}
BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
for (;;) {
int refCount = bsls::AtomicOperations::getInt(&p->d_refCount);
if (refCount & 1) {
if (refCount ==
bsls::AtomicOperations::testAndSwapInt(
&p->d_refCount,
refCount,
refCount^1)) {
// The node is now free but not on the free list. Try to
// take it.
return static_cast<void *>(const_cast<Link **>(
&p->d_next_p)); // RETURN
}
}
else if (refCount ==
bsls::AtomicOperations::testAndSwapInt(
&p->d_refCount,
refCount,
refCount - 2)) {
break;
}
}
}
return static_cast<void *>(const_cast<Link **>(&p->d_next_p));
}
void ConcurrentPool::deallocate(void *address)
{
Link *p = static_cast<Link *>(static_cast<void *>(
static_cast<char *>(address) - offsetof(Link, d_next_p)));
int refCount = bsls::AtomicOperations::getIntRelaxed(&p->d_refCount);
for (;;) {
if (BSLS_PERFORMANCEHINT_PREDICT_LIKELY(2 == refCount)) {
refCount = bsls::AtomicOperations::testAndSwapInt(&p->d_refCount,
2,
0);
if (BSLS_PERFORMANCEHINT_PREDICT_LIKELY(2 == refCount)) {
break;
}
}
BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
const int oldRefCount = refCount;
refCount = bsls::AtomicOperations::testAndSwapInt(&p->d_refCount,
refCount,
refCount - 1);
if (oldRefCount == refCount) {
// Someone else is still trying to pop this item. Just let them
// have it.
return; // RETURN
}
}
Link *old = d_freeList.loadRelaxed();
for (;;) {
p->d_next_p = old;
const Link * const swap = old;
old = d_freeList.testAndSwap(old, p); // release
if (BSLS_PERFORMANCEHINT_PREDICT_LIKELY(swap == old)) {
break;
}
BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
}
}
void ConcurrentPool::reserveCapacity(int numBlocks)
{
BSLS_ASSERT(0 <= numBlocks);
bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
Link *list = d_freeList.swap(0);
Link *last = list;
while (last) {
--numBlocks;
if (!last->d_next_p) break;
last = last->d_next_p;
}
if (last) {
Link *old;
do {
old = d_freeList;
last->d_next_p = old;
} while (old != d_freeList.testAndSwap(old, list));
}
if (numBlocks > 0) {
replenishImp(
reinterpret_cast<bsls::AtomicPointer<LLink> *>(&d_freeList),
&d_blockList,
d_internalBlockSize,
numBlocks);
}
}
} // close package namespace
} // close enterprise namespace
// ----------------------------------------------------------------------------
// Copyright 2016 Bloomberg Finance L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------- END-OF-FILE ----------------------------------