Skip to content

Commit 21651b2

Browse files
committed
Use same table resize strategy for both concurrent maps
I don't see a benefit for storing an additional field on the TableEntry for ConcurrentLong2ReferenceChainedHashTable and this brings both maps closer to the same implementation. Additionally, change all bin node retrievals to use acquire ordering instead of volatile. The table is retrieved using volatile ordering, which is all we need to guarantee the total ordering of the map.
1 parent f648347 commit 21651b2

2 files changed

Lines changed: 107 additions & 111 deletions

File tree

src/main/java/ca/spottedleaf/concurrentutil/map/ConcurrentLong2LongChainedHashTable.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public final float getLoadFactor() {
151151
return this.loadFactor;
152152
}
153153

154-
protected static TableEntry getAtIndexVolatile(final TableEntry[] table, final int index) {
154+
protected static TableEntry getAtIndexAcquire(final TableEntry[] table, final int index) {
155155
return (TableEntry)TableEntry.TABLE_ENTRY_ARRAY_HANDLE.getVolatile(table, index);
156156
}
157157

@@ -194,7 +194,7 @@ protected final TableEntry getNode(final long key) {
194194

195195
TableEntry[] table = this.table;
196196
for (;;) {
197-
TableEntry node = getAtIndexVolatile(table, hash & (table.length - 1));
197+
TableEntry node = getAtIndexAcquire(table, hash & (table.length - 1));
198198

199199
if (node == null) {
200200
// node == null
@@ -361,7 +361,7 @@ private void resize(final long sum) {
361361
final TableEntry[] work = new TableEntry[1 << capDiffShift]; // typically, capDiffShift = 1
362362

363363
for (int i = 0, len = oldTable.length; i < len; ++i) {
364-
TableEntry binNode = getAtIndexVolatile(oldTable, i);
364+
TableEntry binNode = getAtIndexAcquire(oldTable, i);
365365

366366
for (;;) {
367367
if (binNode == null) {
@@ -373,7 +373,7 @@ private void resize(final long sum) {
373373

374374
// need write lock to block other writers
375375
synchronized (binNode) {
376-
if (binNode != (binNode = getAtIndexVolatile(oldTable, i))) {
376+
if (binNode != (binNode = getAtIndexAcquire(oldTable, i))) {
377377
continue;
378378
}
379379

@@ -457,7 +457,7 @@ public long put(final long key, final long value) {
457457
for (;;) {
458458
final int index = hash & (table.length - 1);
459459

460-
TableEntry node = getAtIndexVolatile(table, index);
460+
TableEntry node = getAtIndexAcquire(table, index);
461461
node_loop:
462462
for (;;) {
463463
if (node == null) {
@@ -474,7 +474,7 @@ public long put(final long key, final long value) {
474474
}
475475

476476
synchronized (node) {
477-
if (node != (node = getAtIndexVolatile(table, index))) {
477+
if (node != (node = getAtIndexAcquire(table, index))) {
478478
continue node_loop;
479479
}
480480
// plain reads are fine during synchronised access, as we are the only writer
@@ -513,7 +513,7 @@ public long putIfAbsent(final long key, final long value) {
513513
for (;;) {
514514
final int index = hash & (table.length - 1);
515515

516-
TableEntry node = getAtIndexVolatile(table, index);
516+
TableEntry node = getAtIndexAcquire(table, index);
517517
node_loop:
518518
for (;;) {
519519
if (node == null) {
@@ -535,7 +535,7 @@ public long putIfAbsent(final long key, final long value) {
535535
}
536536

537537
synchronized (node) {
538-
if (node != (node = getAtIndexVolatile(table, index))) {
538+
if (node != (node = getAtIndexAcquire(table, index))) {
539539
continue node_loop;
540540
}
541541
// plain reads are fine during synchronised access, as we are the only writer
@@ -572,7 +572,7 @@ public long replace(final long key, final long value) {
572572
for (;;) {
573573
final int index = hash & (table.length - 1);
574574

575-
TableEntry node = getAtIndexVolatile(table, index);
575+
TableEntry node = getAtIndexAcquire(table, index);
576576
node_loop:
577577
for (;;) {
578578
if (node == null) {
@@ -585,7 +585,7 @@ public long replace(final long key, final long value) {
585585
}
586586

587587
synchronized (node) {
588-
if (node != (node = getAtIndexVolatile(table, index))) {
588+
if (node != (node = getAtIndexAcquire(table, index))) {
589589
continue node_loop;
590590
}
591591

@@ -622,7 +622,7 @@ public long replace(final long key, final long expect, final long update) {
622622
for (;;) {
623623
final int index = hash & (table.length - 1);
624624

625-
TableEntry node = getAtIndexVolatile(table, index);
625+
TableEntry node = getAtIndexAcquire(table, index);
626626
node_loop:
627627
for (;;) {
628628
if (node == null) {
@@ -635,7 +635,7 @@ public long replace(final long key, final long expect, final long update) {
635635
}
636636

637637
synchronized (node) {
638-
if (node != (node = getAtIndexVolatile(table, index))) {
638+
if (node != (node = getAtIndexAcquire(table, index))) {
639639
continue node_loop;
640640
}
641641

@@ -673,7 +673,7 @@ public long remove(final long key) {
673673
for (;;) {
674674
final int index = hash & (table.length - 1);
675675

676-
TableEntry node = getAtIndexVolatile(table, index);
676+
TableEntry node = getAtIndexAcquire(table, index);
677677
node_loop:
678678
for (;;) {
679679
if (node == null) {
@@ -689,7 +689,7 @@ public long remove(final long key) {
689689
long ret = 0L;
690690

691691
synchronized (node) {
692-
if (node != (node = getAtIndexVolatile(table, index))) {
692+
if (node != (node = getAtIndexAcquire(table, index))) {
693693
continue node_loop;
694694
}
695695

@@ -741,7 +741,7 @@ public long remove(final long key, final long expect) {
741741
for (;;) {
742742
final int index = hash & (table.length - 1);
743743

744-
TableEntry node = getAtIndexVolatile(table, index);
744+
TableEntry node = getAtIndexAcquire(table, index);
745745
node_loop:
746746
for (;;) {
747747
if (node == null) {
@@ -757,7 +757,7 @@ public long remove(final long key, final long expect) {
757757
long ret = 0L;
758758

759759
synchronized (node) {
760-
if (node != (node = getAtIndexVolatile(table, index))) {
760+
if (node != (node = getAtIndexAcquire(table, index))) {
761761
continue node_loop;
762762
}
763763

@@ -817,7 +817,7 @@ public long removeIf(final long key, final LongPredicate predicate) {
817817
for (;;) {
818818
final int index = hash & (table.length - 1);
819819

820-
TableEntry node = getAtIndexVolatile(table, index);
820+
TableEntry node = getAtIndexAcquire(table, index);
821821
node_loop:
822822
for (;;) {
823823
if (node == null) {
@@ -833,7 +833,7 @@ public long removeIf(final long key, final LongPredicate predicate) {
833833
long ret = 0L;
834834

835835
synchronized (node) {
836-
if (node != (node = getAtIndexVolatile(table, index))) {
836+
if (node != (node = getAtIndexAcquire(table, index))) {
837837
continue node_loop;
838838
}
839839

@@ -884,7 +884,7 @@ public long addTo(final long key, final long increment, final long defaultValue)
884884
for (;;) {
885885
final int index = hash & (table.length - 1);
886886

887-
TableEntry node = getAtIndexVolatile(table, index);
887+
TableEntry node = getAtIndexAcquire(table, index);
888888
node_loop:
889889
for (;;) {
890890
if (node == null) {
@@ -901,7 +901,7 @@ public long addTo(final long key, final long increment, final long defaultValue)
901901
}
902902

903903
synchronized (node) {
904-
if (node != (node = getAtIndexVolatile(table, index))) {
904+
if (node != (node = getAtIndexAcquire(table, index))) {
905905
continue node_loop;
906906
}
907907
// plain reads are fine during synchronised access, as we are the only writer
@@ -942,7 +942,7 @@ public long decFrom(final long key, final long decrement, final long threshold)
942942
for (;;) {
943943
final int index = hash & (table.length - 1);
944944

945-
TableEntry node = getAtIndexVolatile(table, index);
945+
TableEntry node = getAtIndexAcquire(table, index);
946946
node_loop:
947947
for (;;) {
948948
if (node == null) {
@@ -959,7 +959,7 @@ public long decFrom(final long key, final long decrement, final long threshold)
959959
long ret = 0L;
960960

961961
synchronized (node) {
962-
if (node != (node = getAtIndexVolatile(table, index))) {
962+
if (node != (node = getAtIndexAcquire(table, index))) {
963963
continue node_loop;
964964
}
965965

@@ -1320,7 +1320,7 @@ protected final TableEntry findNext() {
13201320
}
13211321
}
13221322

1323-
final TableEntry entry = getAtIndexVolatile(table, idx);
1323+
final TableEntry entry = getAtIndexAcquire(table, idx);
13241324
if (entry == null) {
13251325
idx += increment;
13261326
continue;

0 commit comments

Comments
 (0)