-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaitFreeQueueV1.java
675 lines (583 loc) · 16.2 KB
/
WaitFreeQueueV1.java
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
package main.java.com.psly.concurrent;
import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicLong;
import sun.misc.Contended;
import sun.misc.Unsafe;
/**
*
* @since 1.6
* @author Pslydhh 2018.04.19
* @param <E> the type of elements held in this collection
*/
public class WaitFreeQueueV1<T> {
public WaitFreeQueueV1() {
this.putNode = new Node<T>();
this.popNode = this.putNode;
this.putIndex = new AtomicLong();
this.popIndex = new AtomicLong();
}
private Node<T> putNode;
private Node<T> popNode;
private AtomicLong putIndex;
private AtomicLong popIndex;
// ring for handles
Handle<T> ringTail;
public void initRingTail() {
this.ringTail = null;
}
public void count() {
Handle<T> local = ringTail;
Handle<T> ele = local;
int counts = 1;
while(ele.next != local) {
++counts;
ele = ele.next;
}
System.out.println("rings: " + counts);
}
public long getPutIndex() {
return putIndex.get();
}
public long getPopIndex() {
return popIndex.get();
}
private boolean casPutNode(Node<T> cmp, Node<T> val) {
return _unsafe.compareAndSwapObject(this, putNode_offset, cmp, val);
}
private boolean casPopNode(Node<T> cmp, Node<T> val) {
return _unsafe.compareAndSwapObject(this, popNode_offset, cmp, val);
}
private boolean casRingTail(Handle<T> cmp, Handle<T> val) {
return _unsafe.compareAndSwapObject(this, ringTail_offset, cmp, val);
}
private static final long putNode_offset;
private static final long popNode_offset;
private static final long ringTail_offset;
private static final Unsafe _unsafe = UtilUnsafe.getUnsafe();
private static class UtilUnsafe {
private UtilUnsafe() {
}
public static Unsafe getUnsafe() {
if (UtilUnsafe.class.getClassLoader() == null)
return Unsafe.getUnsafe();
try {
final Field fld = Unsafe.class.getDeclaredField("theUnsafe");
fld.setAccessible(true);
return (Unsafe) fld.get(UtilUnsafe.class);
} catch (Exception e) {
throw new RuntimeException("Could not obtain access to sun.misc.Unsafe", e);
}
}
}
static {
try {
putNode_offset = _unsafe.objectFieldOffset(WaitFreeQueueV1.class.getDeclaredField("putNode"));
popNode_offset = _unsafe.objectFieldOffset(WaitFreeQueueV1.class.getDeclaredField("popNode"));
ringTail_offset = _unsafe.objectFieldOffset(WaitFreeQueueV1.class.getDeclaredField("ringTail"));
} catch (Exception e) {
throw new Error(e);
}
}
/**
* Throws NullPointerException if argument is null.
*
* @param v the element
*/
private static void checkNotNull(Object v) {
if (v == null)
throw new NullPointerException();
}
/**
* Inserts the specified element to this queue.
*/
public void enqueue(T item, Handle<T> handle) {
checkNotNull(item);
long index = -1, times = Node.PUT_TIMES;
Node<T> node;
Cell<T> cell = new Cell<T>(item), local;
for(; times-- > 0;) {
index = putIndex.getAndIncrement();
node = findCell(handle.putNode, index, handle);
if(handle.putNode.id < node.id) {
handle.putNode = node;
chasePut(node);
}
if((local = node.getCells(index & Node.CELLS_BIT)) == null) {
if(node.casCells(index & Node.CELLS_BIT, null, cell)) {
return ;
}
local = node.getCells(index & Node.CELLS_BIT);
}
if(local.val == null && local.casVal(null, item)) {
return ;
}
}
long enqId;
Node<T> reserveNode = handle.putNode;
Enq<T> enq = handle.er;
enq.val = item;
enq.setId(enqId = index);
cell.val = Cell.TOP_VAL;
cell.setEnq(enq);
for(; enq.id > 0; ){
index = putIndex.getAndIncrement();
node = findCell(handle.putNode, index, handle);
if(handle.putNode.id < node.id) {
handle.putNode = node;
chasePut(node);
}
if((local = node.getCells(index & Node.CELLS_BIT)) == null) {
if(node.casCells(index & Node.CELLS_BIT, null, cell)) {
enq.casId(enqId, -index);
break;
}
local = node.getCells(index & Node.CELLS_BIT);
}
if((local.enq == null && local.casEnq(null, enq)) || local.enq == enq) {
enq.casId(enqId, -index);
break;
}
}
enqId = -enq.id;
node = findCell(reserveNode, enqId, handle);
if(enqId > index) {
do {
index = putIndex.get();
} while(index <= enqId && !putIndex.compareAndSet(index, enqId + 1));
}
cell = node.getCells(enqId & Node.CELLS_BIT);
cell.setVal(item);
}
/**
* try to chase the newest putNode. As the unused Memory can be reclaimed
*/
private void chasePut(Node<T> node) {
Node<T> putNode = this.putNode;
while(putNode.id < node.id && !this.casPutNode(putNode, node))
putNode = this.putNode;
}
private Object help_enq(Handle<T> th, Cell<T> c, long i) {
// get the cell's val
Object val = null;
int times = Node.POP_PROBES;
for(; times-- > 0; ){
val = c.val;
if(val != null)
break;
}
if(val != null) {
if(val != Cell.TOP_VAL)
return val;
} else {
if(!c.casVal(null, Cell.TOP_VAL)){
if((val = c.val) != Cell.TOP_VAL)
return val;
}
}
// val must be Cell.TOP_VAL
Enq<T> e = c.enq;
boolean helpEnqIsOuter = false;
Enq<T> pe = null;
long id = 0;
if(e == null) {
pe = th.eh.er;
id = pe.id;
if(id > 0 && id <= i) {
if(!c.casEnq(null, pe)) {
if((e = c.enq) == Cell.TOP_ENQ)
return this.putIndex.get() <= i ? Cell.BOT_VAL: Cell.TOP_VAL;
} else {
e = pe;
}
} else {
helpEnqIsOuter = true;
if(c.casEnq(null, Cell.TOP_ENQ) || (e = c.enq) == Cell.TOP_ENQ) {
// transfer the helped enq to the next.
th.eh = th.eh.next;
return this.putIndex.get() <= i ? Cell.BOT_VAL: Cell.TOP_VAL;
}
}
} else if(e == Cell.TOP_ENQ) {
return this.putIndex.get() <= i ? Cell.BOT_VAL: Cell.TOP_VAL;
}
long ei = e.id;
T ev = e.val;
if((ei > 0 && e.casId(ei, -i)) || ((ei = e.id) == -i && c.val == Cell.TOP_VAL)){
long index;
do {
index = putIndex.get();
} while(index <= i && !putIndex.compareAndSet(index, i + 1));
c.val = ev;
}
// transfer the helped enq to the next.
if(helpEnqIsOuter || (id > 0 && id != pe.id))
th.eh = th.eh.next;
return c.val;
}
/**
* get a element from this queue.
*/
public T dequeue(Handle<T> handle) {
long index = 0, times = Node.POP_TIMES;
Node<T> node;
Object val = null;
for(; times-- > 0;) {
index = popIndex.getAndIncrement();
node = findCell(handle.popNode, index, handle);
Cell<T> cell = node.getCells(index & Node.CELLS_BIT);
if(cell == null) {
cell = new Cell<T>(null);
if(!node.casCells(index & Node.CELLS_BIT, null, cell))
cell = node.getCells(index & Node.CELLS_BIT);
}
if(handle.popNode.id < node.id) {
handle.popNode = node;
chasePop(node);
}
val = help_enq(handle, cell, index);
if(val == Cell.BOT_VAL)
break;
if(val != Cell.TOP_VAL){
if(cell.casDeq(null, Cell.TOP_DEQ))
break;
val = Cell.TOP_VAL;
}
}
if(val == Cell.TOP_VAL)
val = deq_slow(handle, index);
// has got the elements, val is the val~~
if(val != Cell.BOT_VAL) {
help_deq(handle, handle.dh);
handle.dh = handle.dh.next;
return (T) val;
}
// there is no elements in the queues.
return null;
}
Object deq_slow(Handle<T> th, long id) {
Deq<T> deq = th.dr;
deq.id = id;
deq.idx = id;
help_deq(th, th);
long i = -deq.idx;
Node<T> node = findCell(th.popNode, i, th);
Cell<T> c = node.getCells(i & Node.CELLS_BIT);
if(th.popNode.id < node.id) {
th.popNode = node;
chasePop(node);
}
Object val = c.val;
return val == Cell.TOP_VAL? Cell.BOT_VAL: val;
}
void help_deq(Handle<T> th, Handle<T> ph) {
Deq<T> deq = ph.dr;
long idx = deq.idx;
long id = deq.id;
if(idx < id)
return ;
Node<T> dp = ph.popNode;
idx = deq.idx;//deq.getIdx();
long old = id, i = id + 1, new_ = 0;
for(;;) {
for(; idx == old; ++i) {
Node<T> node = dp = findCell(dp, i, th);
Cell<T> c = node.getCells(i & Node.CELLS_BIT);
if(c == null) {
c = new Cell<T>(null);
if(!node.casCells(i & Node.CELLS_BIT, null, c))
c = node.getCells(i & Node.CELLS_BIT);
}
long di;
do {
di = this.getPopIndex();
} while(di <= i && !this.popIndex.compareAndSet(di, i + 1));
Object v = help_enq(th, c, i);
if(v == Cell.BOT_VAL) {
if(deq.casIdx(idx, -i)) {
return;
} else {
idx = deq.idx;
}
} else if(v != Cell.TOP_VAL && (c.deq == null || c.deq == deq)) {
new_ = i;
break;
} else {
idx = deq.idx;
}
}
if(new_ != 0) {
for(;;) {
long idxLocal = deq.idx;
if(idxLocal < 0 || deq.id != id)
return;
if(idxLocal >= new_) {
idx = idxLocal;
break;
}
if(deq.casIdx(idxLocal, new_)) {
idx = new_;
break;
}
}
new_ = 0;
} else {
if(idx < 0 || deq.id != id)
return;
if(idx < i) {
old = idx;
continue;
}
}
Node<T> node = dp = findCell(dp, idx, th);
Cell<T> c = node.getCells(idx & Node.CELLS_BIT);
if(c.casDeq(null, deq) || c.deq == deq) {
deq.casIdx(idx, -idx);
return;
}
old = idx;
i = idx + 1;
}
}
/*
* try to chase the newest popNode. As the unused Memory can be reclaimed
*/
private void chasePop(Node<T> node) {
Node<T> popNode = this.popNode;
while(popNode.id < node.id && !this.casPopNode(popNode, node))
popNode = this.popNode;
}
/*
* find the node as index:i, that is the node such that:
* node.id == (i >>>Node.RIght_shift)
* return node.
*/
public Node<T> findCell(Node<T> node, long i, Handle<T> handle) {
Node<T> curr = node;
long j, index = (i >>> Node.RIght_shift);
for(j = curr.id; j < index; ++j) {
Node<T> next = curr.next;
// next is null
if(next == null) {
// construct a new Node
Node<T> temp = handle.spare;
if(temp == null) {
temp = new Node<T>();
handle.spare = temp;
}
temp.id = j + 1;
// link to the prev's next;
if( curr.casNext(null, temp)) {
next = temp;
handle.spare = null;
} else {
next = curr.next;
}
}
curr = next;
}
return curr;
}
/*
* Threads must first registered for the queue as get the useful Handle.
*/
public Handle<T> register() {
// get the putNode and popNode
Handle<T> handle = new Handle<T>(null, null);
// add this handle to the handles_ring
Handle<T> tail;
if((tail = this.ringTail) == null){
handle.next = handle;
if(this.casRingTail(null, handle)) {
handle.eh = handle.next;
handle.dh = handle.next;
handle.putNode = this.putNode;
handle.popNode = this.popNode;
_unsafe.loadFence();
return handle;
}
tail = this.ringTail;
}
// add this handle to the handles_ring
for(;;){
Handle<T> next = tail.next;
handle.next = next;
if(tail.casNext(next, handle)) {
handle.eh = handle.next;
handle.dh = handle.next;
handle.putNode = this.putNode;
handle.popNode = this.popNode;
_unsafe.loadFence();
return handle;
}
}
}
/*
* Threads never use the queue again.
*/
public void unregister(Handle<T> handle) {
handle.popNode = handle.putNode = handle.spare = null;
}
public static class Handle<T>{
Node<T> putNode;
Node<T> popNode;
Node<T> spare;
public Handle(Node<T> putNode, Node<T> popNode) {
super();
this.putNode = putNode;
this.popNode = popNode;
this.spare = null;
this.er = new Enq<T>(0, null);
this.dr = new Deq<T>(0, -1);
}
boolean casNext(Handle<T> cmp, Handle<T> val) {
return _unsafe.compareAndSwapObject(this, next_offset, cmp, val);
}
Handle<T> next;
final Enq<T> er;
final Deq<T> dr;
@Contended
Handle<T> eh;
@Contended
Handle<T> dh;
private static final long next_offset;
static {
try {
next_offset = _unsafe.objectFieldOffset(Handle.class.getDeclaredField("next"));
} catch (Exception e) {
throw new Error(e);
}
}
}
static class Node<T> {
private static final int RIght_shift = 10;
private static final int CELLS_SIZE = 1 << RIght_shift;
private static final int CELLS_BIT = CELLS_SIZE - 1;
private static final int PUT_TIMES = 16;
private static final int POP_TIMES = 16;
private static final int POP_PROBES = 64;
@Contended
private Node<T> next;
@Contended
private long id;
@Contended
private volatile Object[] cells;
public Node() {
super();
this.cells = new Object[CELLS_SIZE];
this.next = null;
}
private static long rawIndex(final long idx) {
return cells_entry_base + idx * cells_entry_scale;
}
public boolean casCells(long idx, Object cmp, Object val) {
return _unsafe.compareAndSwapObject(cells, rawIndex(idx), cmp, val);
}
public Cell<T> getCells(long idx) {
// return (T) cells[(int) idx];
return (Cell<T>) _unsafe.getObjectVolatile(cells, rawIndex(idx));
}
public boolean casNext(Node<T> cmp, Node<T> val) {
return _unsafe.compareAndSwapObject(this, next_offset, cmp, val);
}
private static final long cells_entry_base;
private static final long cells_entry_scale;
private static final long next_offset;
static {
try {
cells_entry_base = _unsafe.arrayBaseOffset(Object[].class);
cells_entry_scale = _unsafe.arrayIndexScale(Object[].class);
next_offset = _unsafe.objectFieldOffset(Node.class.getDeclaredField("next"));
} catch (Exception e) {
throw new Error(e);
}
}
}
@Contended
static class Cell<T> {
static final Enq<?> TOP_ENQ = new Enq<>(0, null);
static final Deq<?> TOP_DEQ = new Deq<>(0, 0);
static final Object TOP_VAL = new Object();
static final Object BOT_VAL = new Object();
Object val;
Enq<T> enq;
Deq<T> deq;
public Cell(T val) {
super();
this.val = val;
}
public void setVal(Object val) {
_unsafe.putObjectVolatile(this, val_offset, val);
}
public boolean casVal(Object cmp, Object val) {
return _unsafe.compareAndSwapObject(this, val_offset, cmp, val);
}
public void setEnq(Enq<T> val) {
_unsafe.putObjectVolatile(this, enq_offset, val);
}
public boolean casEnq(Object cmp, Object val) {
return _unsafe.compareAndSwapObject(this, enq_offset, cmp, val);
}
public boolean casDeq(Object cmp, Object val) {
return _unsafe.compareAndSwapObject(this, deq_offset, cmp, val);
}
private static final long val_offset;
private static final long enq_offset;
private static final long deq_offset;
static {
try {
val_offset = _unsafe.objectFieldOffset(Cell.class.getDeclaredField("val"));
enq_offset = _unsafe.objectFieldOffset(Cell.class.getDeclaredField("enq"));
deq_offset = _unsafe.objectFieldOffset(Cell.class.getDeclaredField("deq"));
} catch (Exception e) {
throw new Error(e);
}
}
}
static class Enq<T> {
@Contended
long id;
@Contended
T val;
public Enq(long id, T val) {
super();
this.id = id;
this.val = val;
}
public void setId(long val) {
_unsafe.putLongVolatile(this, id_offset, val);
}
public boolean casId(long cmp, long val) {
return _unsafe.compareAndSwapLong(this, id_offset, cmp, val);
}
private static final long id_offset;
static {
try {
id_offset = _unsafe.objectFieldOffset(Enq.class.getDeclaredField("id"));
} catch (Exception e) {
throw new Error(e);
}
}
}
static class Deq<T> {
@Contended
long id;
@Contended
volatile long idx;
public Deq(long id, long idx) {
super();
this.id = id;
this.idx = idx;
}
boolean casIdx(long cmp, long val) {
return _unsafe.compareAndSwapLong(this, idx_offset, cmp, val);
}
private static final long idx_offset;
static {
try {
idx_offset = _unsafe.objectFieldOffset(Deq.class.getDeclaredField("idx"));
} catch (Exception e) {
throw new Error(e);
}
}
}
}