-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathconcurrent_queue_v2.cpp
More file actions
367 lines (317 loc) · 12 KB
/
concurrent_queue_v2.cpp
File metadata and controls
367 lines (317 loc) · 12 KB
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
/*
Copyright (c) 2005-2017 Intel Corporation
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.
*/
#include "concurrent_queue_v2.h"
#include "tbb/cache_aligned_allocator.h"
#include "tbb/spin_mutex.h"
#include "tbb/atomic.h"
#include <cstring>
#include <stdio.h>
#if defined(_MSC_VER) && defined(_Wp64)
// Workaround for overzealous compiler warnings in /Wp64 mode
#pragma warning (disable: 4267)
#endif
#define RECORD_EVENTS 0
using namespace std;
namespace tbb {
namespace internal {
class concurrent_queue_rep;
//! A queue using simple locking.
/** For efficiency, this class has no constructor.
The caller is expected to zero-initialize it. */
struct micro_queue {
typedef concurrent_queue_base::page page;
typedef size_t ticket;
atomic<page*> head_page;
atomic<ticket> head_counter;
atomic<page*> tail_page;
atomic<ticket> tail_counter;
spin_mutex page_mutex;
class push_finalizer: no_copy {
ticket my_ticket;
micro_queue& my_queue;
public:
push_finalizer( micro_queue& queue, ticket k ) :
my_ticket(k), my_queue(queue)
{}
~push_finalizer() {
my_queue.tail_counter = my_ticket;
}
};
void push( const void* item, ticket k, concurrent_queue_base& base );
class pop_finalizer: no_copy {
ticket my_ticket;
micro_queue& my_queue;
page* my_page;
public:
pop_finalizer( micro_queue& queue, ticket k, page* p ) :
my_ticket(k), my_queue(queue), my_page(p)
{}
~pop_finalizer() {
page* p = my_page;
if( p ) {
spin_mutex::scoped_lock lock( my_queue.page_mutex );
page* q = p->next;
my_queue.head_page = q;
if( !q ) {
my_queue.tail_page = NULL;
}
}
my_queue.head_counter = my_ticket;
if( p )
operator delete(p);
}
};
bool pop( void* dst, ticket k, concurrent_queue_base& base );
};
//! Internal representation of a ConcurrentQueue.
/** For efficiency, this class has no constructor.
The caller is expected to zero-initialize it. */
class concurrent_queue_rep {
public:
typedef size_t ticket;
private:
friend struct micro_queue;
//! Approximately n_queue/golden ratio
static const size_t phi = 3;
public:
//! Must be power of 2
static const size_t n_queue = 8;
//! Map ticket to an array index
static size_t index( ticket k ) {
return k*phi%n_queue;
}
atomic<ticket> head_counter;
char pad1[NFS_MaxLineSize-sizeof(atomic<ticket>)];
atomic<ticket> tail_counter;
char pad2[NFS_MaxLineSize-sizeof(atomic<ticket>)];
micro_queue array[n_queue];
micro_queue& choose( ticket k ) {
// The formula here approximates LRU in a cache-oblivious way.
return array[index(k)];
}
//! Value for effective_capacity that denotes unbounded queue.
static const ptrdiff_t infinite_capacity = ptrdiff_t(~size_t(0)/2);
};
#if _MSC_VER && !defined(__INTEL_COMPILER)
// unary minus operator applied to unsigned type, result still unsigned
#pragma warning( push )
#pragma warning( disable: 4146 )
#endif
//------------------------------------------------------------------------
// micro_queue
//------------------------------------------------------------------------
void micro_queue::push( const void* item, ticket k, concurrent_queue_base& base ) {
k &= -concurrent_queue_rep::n_queue;
page* p = NULL;
size_t index = modulo_power_of_two( k/concurrent_queue_rep::n_queue, base.items_per_page );
if( !index ) {
size_t n = sizeof(page) + base.items_per_page*base.item_size;
p = static_cast<page*>(operator new( n ));
p->mask = 0;
p->next = NULL;
}
{
push_finalizer finalizer( *this, k+concurrent_queue_rep::n_queue );
spin_wait_until_eq( tail_counter, k );
if( p ) {
spin_mutex::scoped_lock lock( page_mutex );
if( page* q = tail_page )
q->next = p;
else
head_page = p;
tail_page = p;
} else {
p = tail_page;
}
base.copy_item( *p, index, item );
// If no exception was thrown, mark item as present.
p->mask |= uintptr_t(1)<<index;
}
}
bool micro_queue::pop( void* dst, ticket k, concurrent_queue_base& base ) {
k &= -concurrent_queue_rep::n_queue;
spin_wait_until_eq( head_counter, k );
spin_wait_while_eq( tail_counter, k );
page *p = head_page;
__TBB_ASSERT( p, NULL );
size_t index = modulo_power_of_two( k/concurrent_queue_rep::n_queue, base.items_per_page );
bool success = false;
{
pop_finalizer finalizer( *this, k+concurrent_queue_rep::n_queue, index==base.items_per_page-1 ? p : NULL );
if( p->mask & uintptr_t(1)<<index ) {
success = true;
base.assign_and_destroy_item( dst, *p, index );
}
}
return success;
}
#if _MSC_VER && !defined(__INTEL_COMPILER)
#pragma warning( pop )
#endif
//------------------------------------------------------------------------
// concurrent_queue_base
//------------------------------------------------------------------------
concurrent_queue_base::concurrent_queue_base( size_t item_sz ) {
items_per_page = item_sz<= 8 ? 32 :
item_sz<= 16 ? 16 :
item_sz<= 32 ? 8 :
item_sz<= 64 ? 4 :
item_sz<=128 ? 2 :
1;
my_capacity = size_t(-1)/(item_sz>1 ? item_sz : 2);
my_rep = cache_aligned_allocator<concurrent_queue_rep>().allocate(1);
__TBB_ASSERT( (size_t)my_rep % NFS_GetLineSize()==0, "alignment error" );
__TBB_ASSERT( (size_t)&my_rep->head_counter % NFS_GetLineSize()==0, "alignment error" );
__TBB_ASSERT( (size_t)&my_rep->tail_counter % NFS_GetLineSize()==0, "alignment error" );
__TBB_ASSERT( (size_t)&my_rep->array % NFS_GetLineSize()==0, "alignment error" );
memset((void*) my_rep, 0, sizeof(concurrent_queue_rep));
this->item_size = item_sz;
}
concurrent_queue_base::~concurrent_queue_base() {
size_t nq = my_rep->n_queue;
for( size_t i=0; i<nq; i++ ) {
page* tp = my_rep->array[i].tail_page;
__TBB_ASSERT( my_rep->array[i].head_page==tp, "at most one page should remain" );
if( tp!=NULL )
delete tp;
}
cache_aligned_allocator<concurrent_queue_rep>().deallocate(my_rep,1);
}
void concurrent_queue_base::internal_push( const void* src ) {
concurrent_queue_rep& r = *my_rep;
concurrent_queue_rep::ticket k = r.tail_counter++;
if( my_capacity<concurrent_queue_rep::infinite_capacity ) {
// Capacity is limited, wait to not exceed it
atomic_backoff backoff;
while( (ptrdiff_t)(k-r.head_counter)>=const_cast<volatile ptrdiff_t&>(my_capacity) )
backoff.pause();
}
r.choose(k).push(src,k,*this);
}
void concurrent_queue_base::internal_pop( void* dst ) {
concurrent_queue_rep& r = *my_rep;
concurrent_queue_rep::ticket k;
do {
k = r.head_counter++;
} while( !r.choose(k).pop(dst,k,*this) );
}
bool concurrent_queue_base::internal_pop_if_present( void* dst ) {
concurrent_queue_rep& r = *my_rep;
concurrent_queue_rep::ticket k;
do {
for( atomic_backoff b;;b.pause() ) {
k = r.head_counter;
if( r.tail_counter<=k ) {
// Queue is empty
return false;
}
// Queue had item with ticket k when we looked. Attempt to get that item.
if( r.head_counter.compare_and_swap(k+1,k)==k ) {
break;
}
// Another thread snatched the item, so pause and retry.
}
} while( !r.choose(k).pop(dst,k,*this) );
return true;
}
bool concurrent_queue_base::internal_push_if_not_full( const void* src ) {
concurrent_queue_rep& r = *my_rep;
concurrent_queue_rep::ticket k;
for( atomic_backoff b;;b.pause() ) {
k = r.tail_counter;
if( (ptrdiff_t)(k-r.head_counter)>=my_capacity ) {
// Queue is full
return false;
}
// Queue had empty slot with ticket k when we looked. Attempt to claim that slot.
if( r.tail_counter.compare_and_swap(k+1,k)==k )
break;
// Another thread claimed the slot, so pause and retry.
}
r.choose(k).push(src,k,*this);
return true;
}
ptrdiff_t concurrent_queue_base::internal_size() const {
__TBB_ASSERT( sizeof(ptrdiff_t)<=sizeof(size_t), NULL );
return ptrdiff_t(my_rep->tail_counter-my_rep->head_counter);
}
void concurrent_queue_base::internal_set_capacity( ptrdiff_t capacity, size_t /*item_sz*/ ) {
my_capacity = capacity<0 ? concurrent_queue_rep::infinite_capacity : capacity;
}
//------------------------------------------------------------------------
// concurrent_queue_iterator_rep
//------------------------------------------------------------------------
class concurrent_queue_iterator_rep: no_assign {
public:
typedef concurrent_queue_rep::ticket ticket;
ticket head_counter;
const concurrent_queue_base& my_queue;
concurrent_queue_base::page* array[concurrent_queue_rep::n_queue];
concurrent_queue_iterator_rep( const concurrent_queue_base& queue ) :
head_counter(queue.my_rep->head_counter),
my_queue(queue)
{
const concurrent_queue_rep& rep = *queue.my_rep;
for( size_t k=0; k<concurrent_queue_rep::n_queue; ++k )
array[k] = rep.array[k].head_page;
}
//! Get pointer to kth element
void* choose( size_t k ) {
if( k==my_queue.my_rep->tail_counter )
return NULL;
else {
concurrent_queue_base::page* p = array[concurrent_queue_rep::index(k)];
__TBB_ASSERT(p,NULL);
size_t i = modulo_power_of_two( k/concurrent_queue_rep::n_queue, my_queue.items_per_page );
return static_cast<unsigned char*>(static_cast<void*>(p+1)) + my_queue.item_size*i;
}
}
};
//------------------------------------------------------------------------
// concurrent_queue_iterator_base
//------------------------------------------------------------------------
concurrent_queue_iterator_base::concurrent_queue_iterator_base( const concurrent_queue_base& queue ) {
my_rep = new concurrent_queue_iterator_rep(queue);
my_item = my_rep->choose(my_rep->head_counter);
}
void concurrent_queue_iterator_base::assign( const concurrent_queue_iterator_base& other ) {
if( my_rep!=other.my_rep ) {
if( my_rep ) {
delete my_rep;
my_rep = NULL;
}
if( other.my_rep ) {
my_rep = new concurrent_queue_iterator_rep( *other.my_rep );
}
}
my_item = other.my_item;
}
void concurrent_queue_iterator_base::advance() {
__TBB_ASSERT( my_item, "attempt to increment iterator past end of queue" );
size_t k = my_rep->head_counter;
const concurrent_queue_base& queue = my_rep->my_queue;
__TBB_ASSERT( my_item==my_rep->choose(k), NULL );
size_t i = modulo_power_of_two( k/concurrent_queue_rep::n_queue, queue.items_per_page );
if( i==queue.items_per_page-1 ) {
concurrent_queue_base::page*& root = my_rep->array[concurrent_queue_rep::index(k)];
root = root->next;
}
my_rep->head_counter = k+1;
my_item = my_rep->choose(k+1);
}
concurrent_queue_iterator_base::~concurrent_queue_iterator_base() {
delete my_rep;
my_rep = NULL;
}
} // namespace internal
} // namespace tbb