forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcurrentSkipListMap.java
3416 lines (3181 loc) · 126 KB
/
ConcurrentSkipListMap.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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package java.util.concurrent;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedMap;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.concurrent.atomic.LongAdder;
/**
* A scalable concurrent {@link ConcurrentNavigableMap} implementation.
* The map is sorted according to the {@linkplain Comparable natural
* ordering} of its keys, or by a {@link Comparator} provided at map
* creation time, depending on which constructor is used.
*
* <p>This class implements a concurrent variant of <a
* href="http://en.wikipedia.org/wiki/Skip_list" target="_top">SkipLists</a>
* providing expected average <i>log(n)</i> time cost for the
* {@code containsKey}, {@code get}, {@code put} and
* {@code remove} operations and their variants. Insertion, removal,
* update, and access operations safely execute concurrently by
* multiple threads.
*
* <p>Iterators and spliterators are
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
*
* <p>Ascending key ordered views and their iterators are faster than
* descending ones.
*
* <p>All {@code Map.Entry} pairs returned by methods in this class
* and its views represent snapshots of mappings at the time they were
* produced. They do <em>not</em> support the {@code Entry.setValue}
* method. (Note however that it is possible to change mappings in the
* associated map using {@code put}, {@code putIfAbsent}, or
* {@code replace}, depending on exactly which effect you need.)
*
* <p>Beware that bulk operations {@code putAll}, {@code equals},
* {@code toArray}, {@code containsValue}, and {@code clear} are
* <em>not</em> guaranteed to be performed atomically. For example, an
* iterator operating concurrently with a {@code putAll} operation
* might view only some of the added elements.
*
* <p>This class and its views and iterators implement all of the
* <em>optional</em> methods of the {@link Map} and {@link Iterator}
* interfaces. Like most other concurrent collections, this class does
* <em>not</em> permit the use of {@code null} keys or values because some
* null return values cannot be reliably distinguished from the absence of
* elements.
*
* <p>This class is a member of the
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
* Java Collections Framework</a>.
*
* @author Doug Lea
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @since 1.6
*/
public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
implements ConcurrentNavigableMap<K,V>, Cloneable, Serializable {
/*
* This class implements a tree-like two-dimensionally linked skip
* list in which the index levels are represented in separate
* nodes from the base nodes holding data. There are two reasons
* for taking this approach instead of the usual array-based
* structure: 1) Array based implementations seem to encounter
* more complexity and overhead 2) We can use cheaper algorithms
* for the heavily-traversed index lists than can be used for the
* base lists. Here's a picture of some of the basics for a
* possible list with 2 levels of index:
*
* Head nodes Index nodes
* +-+ right +-+ +-+
* |2|---------------->| |--------------------->| |->null
* +-+ +-+ +-+
* | down | |
* v v v
* +-+ +-+ +-+ +-+ +-+ +-+
* |1|----------->| |->| |------>| |----------->| |------>| |->null
* +-+ +-+ +-+ +-+ +-+ +-+
* v | | | | |
* Nodes next v v v v v
* +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
* | |->|A|->|B|->|C|->|D|->|E|->|F|->|G|->|H|->|I|->|J|->|K|->null
* +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
*
* The base lists use a variant of the HM linked ordered set
* algorithm. See Tim Harris, "A pragmatic implementation of
* non-blocking linked lists"
* http://www.cl.cam.ac.uk/~tlh20/publications.html and Maged
* Michael "High Performance Dynamic Lock-Free Hash Tables and
* List-Based Sets"
* http://www.research.ibm.com/people/m/michael/pubs.htm. The
* basic idea in these lists is to mark the "next" pointers of
* deleted nodes when deleting to avoid conflicts with concurrent
* insertions, and when traversing to keep track of triples
* (predecessor, node, successor) in order to detect when and how
* to unlink these deleted nodes.
*
* Rather than using mark-bits to mark list deletions (which can
* be slow and space-intensive using AtomicMarkedReference), nodes
* use direct CAS'able next pointers. On deletion, instead of
* marking a pointer, they splice in another node that can be
* thought of as standing for a marked pointer (see method
* unlinkNode). Using plain nodes acts roughly like "boxed"
* implementations of marked pointers, but uses new nodes only
* when nodes are deleted, not for every link. This requires less
* space and supports faster traversal. Even if marked references
* were better supported by JVMs, traversal using this technique
* might still be faster because any search need only read ahead
* one more node than otherwise required (to check for trailing
* marker) rather than unmasking mark bits or whatever on each
* read.
*
* This approach maintains the essential property needed in the HM
* algorithm of changing the next-pointer of a deleted node so
* that any other CAS of it will fail, but implements the idea by
* changing the pointer to point to a different node (with
* otherwise illegal null fields), not by marking it. While it
* would be possible to further squeeze space by defining marker
* nodes not to have key/value fields, it isn't worth the extra
* type-testing overhead. The deletion markers are rarely
* encountered during traversal, are easily detected via null
* checks that are needed anyway, and are normally quickly garbage
* collected. (Note that this technique would not work well in
* systems without garbage collection.)
*
* In addition to using deletion markers, the lists also use
* nullness of value fields to indicate deletion, in a style
* similar to typical lazy-deletion schemes. If a node's value is
* null, then it is considered logically deleted and ignored even
* though it is still reachable.
*
* Here's the sequence of events for a deletion of node n with
* predecessor b and successor f, initially:
*
* +------+ +------+ +------+
* ... | b |------>| n |----->| f | ...
* +------+ +------+ +------+
*
* 1. CAS n's value field from non-null to null.
* Traversals encountering a node with null value ignore it.
* However, ongoing insertions and deletions might still modify
* n's next pointer.
*
* 2. CAS n's next pointer to point to a new marker node.
* From this point on, no other nodes can be appended to n.
* which avoids deletion errors in CAS-based linked lists.
*
* +------+ +------+ +------+ +------+
* ... | b |------>| n |----->|marker|------>| f | ...
* +------+ +------+ +------+ +------+
*
* 3. CAS b's next pointer over both n and its marker.
* From this point on, no new traversals will encounter n,
* and it can eventually be GCed.
* +------+ +------+
* ... | b |----------------------------------->| f | ...
* +------+ +------+
*
* A failure at step 1 leads to simple retry due to a lost race
* with another operation. Steps 2-3 can fail because some other
* thread noticed during a traversal a node with null value and
* helped out by marking and/or unlinking. This helping-out
* ensures that no thread can become stuck waiting for progress of
* the deleting thread.
*
* Skip lists add indexing to this scheme, so that the base-level
* traversals start close to the locations being found, inserted
* or deleted -- usually base level traversals only traverse a few
* nodes. This doesn't change the basic algorithm except for the
* need to make sure base traversals start at predecessors (here,
* b) that are not (structurally) deleted, otherwise retrying
* after processing the deletion.
*
* Index levels are maintained using CAS to link and unlink
* successors ("right" fields). Races are allowed in index-list
* operations that can (rarely) fail to link in a new index node.
* (We can't do this of course for data nodes.) However, even
* when this happens, the index lists correctly guide search.
* This can impact performance, but since skip lists are
* probabilistic anyway, the net result is that under contention,
* the effective "p" value may be lower than its nominal value.
*
* Index insertion and deletion sometimes require a separate
* traversal pass occurring after the base-level action, to add or
* remove index nodes. This adds to single-threaded overhead, but
* improves contended multithreaded performance by narrowing
* interference windows, and allows deletion to ensure that all
* index nodes will be made unreachable upon return from a public
* remove operation, thus avoiding unwanted garbage retention.
*
* Indexing uses skip list parameters that maintain good search
* performance while using sparser-than-usual indices: The
* hardwired parameters k=1, p=0.5 (see method doPut) mean that
* about one-quarter of the nodes have indices. Of those that do,
* half have one level, a quarter have two, and so on (see Pugh's
* Skip List Cookbook, sec 3.4), up to a maximum of 62 levels
* (appropriate for up to 2^63 elements). The expected total
* space requirement for a map is slightly less than for the
* current implementation of java.util.TreeMap.
*
* Changing the level of the index (i.e, the height of the
* tree-like structure) also uses CAS. Creation of an index with
* height greater than the current level adds a level to the head
* index by CAS'ing on a new top-most head. To maintain good
* performance after a lot of removals, deletion methods
* heuristically try to reduce the height if the topmost levels
* appear to be empty. This may encounter races in which it is
* possible (but rare) to reduce and "lose" a level just as it is
* about to contain an index (that will then never be
* encountered). This does no structural harm, and in practice
* appears to be a better option than allowing unrestrained growth
* of levels.
*
* This class provides concurrent-reader-style memory consistency,
* ensuring that read-only methods report status and/or values no
* staler than those holding at method entry. This is done by
* performing all publication and structural updates using
* (volatile) CAS, placing an acquireFence in a few access
* methods, and ensuring that linked objects are transitively
* acquired via dependent reads (normally once) unless performing
* a volatile-mode CAS operation (that also acts as an acquire and
* release). This form of fence-hoisting is similar to RCU and
* related techniques (see McKenney's online book
* https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html)
* It minimizes overhead that may otherwise occur when using so
* many volatile-mode reads. Using explicit acquireFences is
* logistically easier than targeting particular fields to be read
* in acquire mode: fences are just hoisted up as far as possible,
* to the entry points or loop headers of a few methods. A
* potential disadvantage is that these few remaining fences are
* not easily optimized away by compilers under exclusively
* single-thread use. It requires some care to avoid volatile
* mode reads of other fields. (Note that the memory semantics of
* a reference dependently read in plain mode exactly once are
* equivalent to those for atomic opaque mode.) Iterators and
* other traversals encounter each node and value exactly once.
* Other operations locate an element (or position to insert an
* element) via a sequence of dereferences. This search is broken
* into two parts. Method findPredecessor (and its specialized
* embeddings) searches index nodes only, returning a base-level
* predecessor of the key. Callers carry out the base-level
* search, restarting if encountering a marker preventing link
* modification. In some cases, it is possible to encounter a
* node multiple times while descending levels. For mutative
* operations, the reported value is validated using CAS (else
* retrying), preserving linearizability with respect to each
* other. Others may return any (non-null) value holding in the
* course of the method call. (Search-based methods also include
* some useless-looking explicit null checks designed to allow
* more fields to be nulled out upon removal, to reduce floating
* garbage, but which is not currently done, pending discovery of
* a way to do this with less impact on other operations.)
*
* To produce random values without interference across threads,
* we use within-JDK thread local random support (via the
* "secondary seed", to avoid interference with user-level
* ThreadLocalRandom.)
*
* For explanation of algorithms sharing at least a couple of
* features with this one, see Mikhail Fomitchev's thesis
* (http://www.cs.yorku.ca/~mikhail/), Keir Fraser's thesis
* (http://www.cl.cam.ac.uk/users/kaf24/), and Hakan Sundell's
* thesis (http://www.cs.chalmers.se/~phs/).
*
* Notation guide for local variables
* Node: b, n, f, p for predecessor, node, successor, aux
* Index: q, r, d for index node, right, down.
* Head: h
* Keys: k, key
* Values: v, value
* Comparisons: c
*/
private static final long serialVersionUID = -8627078645895051609L;
/**
* The comparator used to maintain order in this map, or null if
* using natural ordering. (Non-private to simplify access in
* nested classes.)
* @serial
*/
final Comparator<? super K> comparator;
/** Lazily initialized topmost index of the skiplist. */
private transient Index<K,V> head;
/** Lazily initialized element count */
private transient LongAdder adder;
/** Lazily initialized key set */
private transient KeySet<K,V> keySet;
/** Lazily initialized values collection */
private transient Values<K,V> values;
/** Lazily initialized entry set */
private transient EntrySet<K,V> entrySet;
/** Lazily initialized descending map */
private transient SubMap<K,V> descendingMap;
/**
* Nodes hold keys and values, and are singly linked in sorted
* order, possibly with some intervening marker nodes. The list is
* headed by a header node accessible as head.node. Headers and
* marker nodes have null keys. The val field (but currently not
* the key field) is nulled out upon deletion.
*/
static final class Node<K,V> {
final K key; // currently, never detached
V val;
Node<K,V> next;
Node(K key, V value, Node<K,V> next) {
this.key = key;
this.val = value;
this.next = next;
}
}
/**
* Index nodes represent the levels of the skip list.
*/
static final class Index<K,V> {
final Node<K,V> node; // currently, never detached
final Index<K,V> down;
Index<K,V> right;
Index(Node<K,V> node, Index<K,V> down, Index<K,V> right) {
this.node = node;
this.down = down;
this.right = right;
}
}
/* ---------------- Utilities -------------- */
/**
* Compares using comparator or natural ordering if null.
* Called only by methods that have performed required type checks.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
static int cpr(Comparator c, Object x, Object y) {
return (c != null) ? c.compare(x, y) : ((Comparable)x).compareTo(y);
}
/**
* Returns the header for base node list, or null if uninitialized
*/
final Node<K,V> baseHead() {
Index<K,V> h;
VarHandle.acquireFence();
return ((h = head) == null) ? null : h.node;
}
/**
* Tries to unlink deleted node n from predecessor b (if both
* exist), by first splicing in a marker if not already present.
* Upon return, node n is sure to be unlinked from b, possibly
* via the actions of some other thread.
*
* @param b if nonnull, predecessor
* @param n if nonnull, node known to be deleted
*/
static <K,V> void unlinkNode(Node<K,V> b, Node<K,V> n) {
if (b != null && n != null) {
Node<K,V> f, p;
for (;;) {
if ((f = n.next) != null && f.key == null) {
p = f.next; // already marked
break;
}
else if (NEXT.compareAndSet(n, f,
new Node<K,V>(null, null, f))) {
p = f; // add marker
break;
}
}
NEXT.compareAndSet(b, n, p);
}
}
/**
* Adds to element count, initializing adder if necessary
*
* @param c count to add
*/
private void addCount(long c) {
LongAdder a;
do {} while ((a = adder) == null &&
!ADDER.compareAndSet(this, null, a = new LongAdder()));
a.add(c);
}
/**
* Returns element count, initializing adder if necessary.
*/
final long getAdderCount() {
LongAdder a; long c;
do {} while ((a = adder) == null &&
!ADDER.compareAndSet(this, null, a = new LongAdder()));
return ((c = a.sum()) <= 0L) ? 0L : c; // ignore transient negatives
}
/* ---------------- Traversal -------------- */
/**
* Returns an index node with key strictly less than given key.
* Also unlinks indexes to deleted nodes found along the way.
* Callers rely on this side-effect of clearing indices to deleted
* nodes.
*
* @param key if nonnull the key
* @return a predecessor node of key, or null if uninitialized or null key
*/
private Node<K,V> findPredecessor(Object key, Comparator<? super K> cmp) {
Index<K,V> q;
VarHandle.acquireFence();
if ((q = head) == null || key == null)
return null;
else {
for (Index<K,V> r, d;;) {
while ((r = q.right) != null) {
Node<K,V> p; K k;
if ((p = r.node) == null || (k = p.key) == null ||
p.val == null) // unlink index to deleted node
RIGHT.compareAndSet(q, r, r.right);
else if (cpr(cmp, key, k) > 0)
q = r;
else
break;
}
if ((d = q.down) != null)
q = d;
else
return q.node;
}
}
}
/**
* Returns node holding key or null if no such, clearing out any
* deleted nodes seen along the way. Repeatedly traverses at
* base-level looking for key starting at predecessor returned
* from findPredecessor, processing base-level deletions as
* encountered. Restarts occur, at traversal step encountering
* node n, if n's key field is null, indicating it is a marker, so
* its predecessor is deleted before continuing, which we help do
* by re-finding a valid predecessor. The traversal loops in
* doPut, doRemove, and findNear all include the same checks.
*
* @param key the key
* @return node holding key, or null if no such
*/
private Node<K,V> findNode(Object key) {
if (key == null)
throw new NullPointerException(); // don't postpone errors
Comparator<? super K> cmp = comparator;
Node<K,V> b;
outer: while ((b = findPredecessor(key, cmp)) != null) {
for (;;) {
Node<K,V> n; K k; V v; int c;
if ((n = b.next) == null)
break outer; // empty
else if ((k = n.key) == null)
break; // b is deleted
else if ((v = n.val) == null)
unlinkNode(b, n); // n is deleted
else if ((c = cpr(cmp, key, k)) > 0)
b = n;
else if (c == 0)
return n;
else
break outer;
}
}
return null;
}
/**
* Gets value for key. Same idea as findNode, except skips over
* deletions and markers, and returns first encountered value to
* avoid possibly inconsistent rereads.
*
* @param key the key
* @return the value, or null if absent
*/
private V doGet(Object key) {
Index<K,V> q;
VarHandle.acquireFence();
if (key == null)
throw new NullPointerException();
Comparator<? super K> cmp = comparator;
V result = null;
if ((q = head) != null) {
outer: for (Index<K,V> r, d;;) {
while ((r = q.right) != null) {
Node<K,V> p; K k; V v; int c;
if ((p = r.node) == null || (k = p.key) == null ||
(v = p.val) == null)
RIGHT.compareAndSet(q, r, r.right);
else if ((c = cpr(cmp, key, k)) > 0)
q = r;
else if (c == 0) {
result = v;
break outer;
}
else
break;
}
if ((d = q.down) != null)
q = d;
else {
Node<K,V> b, n;
if ((b = q.node) != null) {
while ((n = b.next) != null) {
V v; int c;
K k = n.key;
if ((v = n.val) == null || k == null ||
(c = cpr(cmp, key, k)) > 0)
b = n;
else {
if (c == 0)
result = v;
break;
}
}
}
break;
}
}
}
return result;
}
/* ---------------- Insertion -------------- */
/**
* Main insertion method. Adds element if not present, or
* replaces value if present and onlyIfAbsent is false.
*
* @param key the key
* @param value the value that must be associated with key
* @param onlyIfAbsent if should not insert if already present
* @return the old value, or null if newly inserted
*/
private V doPut(K key, V value, boolean onlyIfAbsent) {
if (key == null)
throw new NullPointerException();
Comparator<? super K> cmp = comparator;
for (;;) {
Index<K,V> h; Node<K,V> b;
VarHandle.acquireFence();
int levels = 0; // number of levels descended
if ((h = head) == null) { // try to initialize
Node<K,V> base = new Node<K,V>(null, null, null);
h = new Index<K,V>(base, null, null);
b = (HEAD.compareAndSet(this, null, h)) ? base : null;
}
else {
for (Index<K,V> q = h, r, d;;) { // count while descending
while ((r = q.right) != null) {
Node<K,V> p; K k;
if ((p = r.node) == null || (k = p.key) == null ||
p.val == null)
RIGHT.compareAndSet(q, r, r.right);
else if (cpr(cmp, key, k) > 0)
q = r;
else
break;
}
if ((d = q.down) != null) {
++levels;
q = d;
}
else {
b = q.node;
break;
}
}
}
if (b != null) {
Node<K,V> z = null; // new node, if inserted
for (;;) { // find insertion point
Node<K,V> n, p; K k; V v; int c;
if ((n = b.next) == null) {
if (b.key == null) // if empty, type check key now
cpr(cmp, key, key);
c = -1;
}
else if ((k = n.key) == null)
break; // can't append; restart
else if ((v = n.val) == null) {
unlinkNode(b, n);
c = 1;
}
else if ((c = cpr(cmp, key, k)) > 0)
b = n;
else if (c == 0 &&
(onlyIfAbsent || VAL.compareAndSet(n, v, value)))
return v;
if (c < 0 &&
NEXT.compareAndSet(b, n,
p = new Node<K,V>(key, value, n))) {
z = p;
break;
}
}
if (z != null) {
int lr = ThreadLocalRandom.nextSecondarySeed();
if ((lr & 0x3) == 0) { // add indices with 1/4 prob
int hr = ThreadLocalRandom.nextSecondarySeed();
long rnd = ((long)hr << 32) | ((long)lr & 0xffffffffL);
int skips = levels; // levels to descend before add
Index<K,V> x = null;
for (;;) { // create at most 62 indices
x = new Index<K,V>(z, x, null);
if (rnd >= 0L || --skips < 0)
break;
else
rnd <<= 1;
}
if (addIndices(h, skips, x, cmp) && skips < 0 &&
head == h) { // try to add new level
Index<K,V> hx = new Index<K,V>(z, x, null);
Index<K,V> nh = new Index<K,V>(h.node, h, hx);
HEAD.compareAndSet(this, h, nh);
}
if (z.val == null) // deleted while adding indices
findPredecessor(key, cmp); // clean
}
addCount(1L);
return null;
}
}
}
}
/**
* Add indices after an insertion. Descends iteratively to the
* highest level of insertion, then recursively, to chain index
* nodes to lower ones. Returns null on (staleness) failure,
* disabling higher-level insertions. Recursion depths are
* exponentially less probable.
*
* @param q starting index for current level
* @param skips levels to skip before inserting
* @param x index for this insertion
* @param cmp comparator
*/
static <K,V> boolean addIndices(Index<K,V> q, int skips, Index<K,V> x,
Comparator<? super K> cmp) {
Node<K,V> z; K key;
if (x != null && (z = x.node) != null && (key = z.key) != null &&
q != null) { // hoist checks
boolean retrying = false;
for (;;) { // find splice point
Index<K,V> r, d; int c;
if ((r = q.right) != null) {
Node<K,V> p; K k;
if ((p = r.node) == null || (k = p.key) == null ||
p.val == null) {
RIGHT.compareAndSet(q, r, r.right);
c = 0;
}
else if ((c = cpr(cmp, key, k)) > 0)
q = r;
else if (c == 0)
break; // stale
}
else
c = -1;
if (c < 0) {
if ((d = q.down) != null && skips > 0) {
--skips;
q = d;
}
else if (d != null && !retrying &&
!addIndices(d, 0, x.down, cmp))
break;
else {
x.right = r;
if (RIGHT.compareAndSet(q, r, x))
return true;
else
retrying = true; // re-find splice point
}
}
}
}
return false;
}
/* ---------------- Deletion -------------- */
/**
* Main deletion method. Locates node, nulls value, appends a
* deletion marker, unlinks predecessor, removes associated index
* nodes, and possibly reduces head index level.
*
* @param key the key
* @param value if non-null, the value that must be
* associated with key
* @return the node, or null if not found
*/
final V doRemove(Object key, Object value) {
if (key == null)
throw new NullPointerException();
Comparator<? super K> cmp = comparator;
V result = null;
Node<K,V> b;
outer: while ((b = findPredecessor(key, cmp)) != null &&
result == null) {
for (;;) {
Node<K,V> n; K k; V v; int c;
if ((n = b.next) == null)
break outer;
else if ((k = n.key) == null)
break;
else if ((v = n.val) == null)
unlinkNode(b, n);
else if ((c = cpr(cmp, key, k)) > 0)
b = n;
else if (c < 0)
break outer;
else if (value != null && !value.equals(v))
break outer;
else if (VAL.compareAndSet(n, v, null)) {
result = v;
unlinkNode(b, n);
break; // loop to clean up
}
}
}
if (result != null) {
tryReduceLevel();
addCount(-1L);
}
return result;
}
/**
* Possibly reduce head level if it has no nodes. This method can
* (rarely) make mistakes, in which case levels can disappear even
* though they are about to contain index nodes. This impacts
* performance, not correctness. To minimize mistakes as well as
* to reduce hysteresis, the level is reduced by one only if the
* topmost three levels look empty. Also, if the removed level
* looks non-empty after CAS, we try to change it back quick
* before anyone notices our mistake! (This trick works pretty
* well because this method will practically never make mistakes
* unless current thread stalls immediately before first CAS, in
* which case it is very unlikely to stall again immediately
* afterwards, so will recover.)
*
* We put up with all this rather than just let levels grow
* because otherwise, even a small map that has undergone a large
* number of insertions and removals will have a lot of levels,
* slowing down access more than would an occasional unwanted
* reduction.
*/
private void tryReduceLevel() {
Index<K,V> h, d, e;
if ((h = head) != null && h.right == null &&
(d = h.down) != null && d.right == null &&
(e = d.down) != null && e.right == null &&
HEAD.compareAndSet(this, h, d) &&
h.right != null) // recheck
HEAD.compareAndSet(this, d, h); // try to backout
}
/* ---------------- Finding and removing first element -------------- */
/**
* Gets first valid node, unlinking deleted nodes if encountered.
* @return first node or null if empty
*/
final Node<K,V> findFirst() {
Node<K,V> b, n;
if ((b = baseHead()) != null) {
while ((n = b.next) != null) {
if (n.val == null)
unlinkNode(b, n);
else
return n;
}
}
return null;
}
/**
* Entry snapshot version of findFirst
*/
final AbstractMap.SimpleImmutableEntry<K,V> findFirstEntry() {
Node<K,V> b, n; V v;
if ((b = baseHead()) != null) {
while ((n = b.next) != null) {
if ((v = n.val) == null)
unlinkNode(b, n);
else
return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v);
}
}
return null;
}
/**
* Removes first entry; returns its snapshot.
* @return null if empty, else snapshot of first entry
*/
private AbstractMap.SimpleImmutableEntry<K,V> doRemoveFirstEntry() {
Node<K,V> b, n; V v;
if ((b = baseHead()) != null) {
while ((n = b.next) != null) {
if ((v = n.val) == null || VAL.compareAndSet(n, v, null)) {
K k = n.key;
unlinkNode(b, n);
if (v != null) {
tryReduceLevel();
findPredecessor(k, comparator); // clean index
addCount(-1L);
return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
}
}
}
}
return null;
}
/* ---------------- Finding and removing last element -------------- */
/**
* Specialized version of find to get last valid node.
* @return last node or null if empty
*/
final Node<K,V> findLast() {
outer: for (;;) {
Index<K,V> q; Node<K,V> b;
VarHandle.acquireFence();
if ((q = head) == null)
break;
for (Index<K,V> r, d;;) {
while ((r = q.right) != null) {
Node<K,V> p;
if ((p = r.node) == null || p.val == null)
RIGHT.compareAndSet(q, r, r.right);
else
q = r;
}
if ((d = q.down) != null)
q = d;
else {
b = q.node;
break;
}
}
if (b != null) {
for (;;) {
Node<K,V> n;
if ((n = b.next) == null) {
if (b.key == null) // empty
break outer;
else
return b;
}
else if (n.key == null)
break;
else if (n.val == null)
unlinkNode(b, n);
else
b = n;
}
}
}
return null;
}
/**
* Entry version of findLast
* @return Entry for last node or null if empty
*/
final AbstractMap.SimpleImmutableEntry<K,V> findLastEntry() {
for (;;) {
Node<K,V> n; V v;
if ((n = findLast()) == null)
return null;
if ((v = n.val) != null)
return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v);
}
}
/**
* Removes last entry; returns its snapshot.
* Specialized variant of doRemove.
* @return null if empty, else snapshot of last entry
*/
private Map.Entry<K,V> doRemoveLastEntry() {
outer: for (;;) {
Index<K,V> q; Node<K,V> b;
VarHandle.acquireFence();
if ((q = head) == null)
break;
for (;;) {
Index<K,V> d, r; Node<K,V> p;
while ((r = q.right) != null) {
if ((p = r.node) == null || p.val == null)
RIGHT.compareAndSet(q, r, r.right);
else if (p.next != null)
q = r; // continue only if a successor
else
break;
}
if ((d = q.down) != null)
q = d;
else {
b = q.node;
break;
}
}
if (b != null) {
for (;;) {
Node<K,V> n; K k; V v;
if ((n = b.next) == null) {
if (b.key == null) // empty
break outer;
else
break; // retry
}
else if ((k = n.key) == null)
break;
else if ((v = n.val) == null)
unlinkNode(b, n);
else if (n.next != null)
b = n;
else if (VAL.compareAndSet(n, v, null)) {
unlinkNode(b, n);
tryReduceLevel();
findPredecessor(k, comparator); // clean index
addCount(-1L);
return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
}
}
}
}
return null;
}
/* ---------------- Relational operations -------------- */