Updated and added more LockFree maps
This commit is contained in:
parent
e9e4c6b2ee
commit
60e0bc87a2
@ -16,12 +16,13 @@
|
|||||||
package dorkbox.util.collections;
|
package dorkbox.util.collections;
|
||||||
|
|
||||||
|
|
||||||
import com.esotericsoftware.kryo.util.IdentityMap;
|
|
||||||
import dorkbox.util.Property;
|
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
||||||
|
|
||||||
|
import com.esotericsoftware.kryo.util.IdentityMap;
|
||||||
|
|
||||||
|
import dorkbox.util.Property;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author dorkbox, llc
|
* @author dorkbox, llc
|
||||||
*/
|
*/
|
||||||
@ -37,7 +38,7 @@ class ConcurrentIterator<T> {
|
|||||||
private final int ID = ID_COUNTER.getAndIncrement();
|
private final int ID = ID_COUNTER.getAndIncrement();
|
||||||
|
|
||||||
// This is only touched by a single thread, maintains a map of entries for FAST lookup during remove.
|
// This is only touched by a single thread, maintains a map of entries for FAST lookup during remove.
|
||||||
private final IdentityMap<Object, ConcurrentEntry> entries = new IdentityMap<Object, ConcurrentEntry>(32, LOAD_FACTOR);
|
private final IdentityMap<T, ConcurrentEntry> entries = new IdentityMap<T, ConcurrentEntry>(32, LOAD_FACTOR);
|
||||||
|
|
||||||
// this is still inside the single-writer, and can use the same techniques as subscription manager (for thread safe publication)
|
// this is still inside the single-writer, and can use the same techniques as subscription manager (for thread safe publication)
|
||||||
@SuppressWarnings("FieldCanBeLocal")
|
@SuppressWarnings("FieldCanBeLocal")
|
||||||
@ -53,7 +54,10 @@ class ConcurrentIterator<T> {
|
|||||||
ConcurrentIterator() {
|
ConcurrentIterator() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// called on shutdown for GC purposes
|
/**
|
||||||
|
* single writer principle!
|
||||||
|
* called from within SYNCHRONIZE
|
||||||
|
*/
|
||||||
public final
|
public final
|
||||||
void clear() {
|
void clear() {
|
||||||
this.entries.clear();
|
this.entries.clear();
|
||||||
@ -66,12 +70,12 @@ class ConcurrentIterator<T> {
|
|||||||
*
|
*
|
||||||
* @param listener the object that will receive messages during publication
|
* @param listener the object that will receive messages during publication
|
||||||
*/
|
*/
|
||||||
public
|
public synchronized
|
||||||
void add(final Object listener) {
|
void add(final T listener) {
|
||||||
ConcurrentEntry head = headREF.get(this);
|
ConcurrentEntry<T> head = headREF.get(this);
|
||||||
|
|
||||||
if (!entries.containsKey(listener)) {
|
if (!entries.containsKey(listener)) {
|
||||||
head = new ConcurrentEntry<Object>(listener, head);
|
head = new ConcurrentEntry<T>(listener, head);
|
||||||
|
|
||||||
entries.put(listener, head);
|
entries.put(listener, head);
|
||||||
headREF.lazySet(this, head);
|
headREF.lazySet(this, head);
|
||||||
@ -84,12 +88,12 @@ class ConcurrentIterator<T> {
|
|||||||
*
|
*
|
||||||
* @param listener the object that will NO LONGER receive messages during publication
|
* @param listener the object that will NO LONGER receive messages during publication
|
||||||
*/
|
*/
|
||||||
public
|
public synchronized
|
||||||
void remove(final Object listener) {
|
boolean remove(final T listener) {
|
||||||
ConcurrentEntry concurrentEntry = entries.get(listener);
|
ConcurrentEntry<T> concurrentEntry = entries.get(listener);
|
||||||
|
|
||||||
if (concurrentEntry != null) {
|
if (concurrentEntry != null) {
|
||||||
ConcurrentEntry head = headREF.get(this);
|
ConcurrentEntry<T> head = headREF.get(this);
|
||||||
|
|
||||||
if (concurrentEntry == head) {
|
if (concurrentEntry == head) {
|
||||||
// if it was second, now it's first
|
// if it was second, now it's first
|
||||||
@ -102,7 +106,19 @@ class ConcurrentIterator<T> {
|
|||||||
|
|
||||||
headREF.lazySet(this, head);
|
headREF.lazySet(this, head);
|
||||||
this.entries.remove(listener);
|
this.entries.remove(listener);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* single writer principle!
|
||||||
|
* called from within SYNCHRONIZE
|
||||||
|
*/
|
||||||
|
public synchronized
|
||||||
|
int size() {
|
||||||
|
return entries.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -35,7 +35,7 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
|||||||
public final
|
public final
|
||||||
class LockFreeHashMap<K, V> implements Map<K, V>, Cloneable, Serializable {
|
class LockFreeHashMap<K, V> implements Map<K, V>, Cloneable, Serializable {
|
||||||
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||||
private static final AtomicReferenceFieldUpdater<LockFreeHashMap, HashMap> deviceREF = AtomicReferenceFieldUpdater.newUpdater(
|
private static final AtomicReferenceFieldUpdater<LockFreeHashMap, HashMap> mapREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||||
LockFreeHashMap.class,
|
LockFreeHashMap.class,
|
||||||
HashMap.class,
|
HashMap.class,
|
||||||
"hashMap");
|
"hashMap");
|
||||||
@ -102,47 +102,47 @@ class LockFreeHashMap<K, V> implements Map<K, V>, Cloneable, Serializable {
|
|||||||
public
|
public
|
||||||
Map<K, V> getMap() {
|
Map<K, V> getMap() {
|
||||||
// use the SWP to get a lock-free get of the map. It's values are only valid at the moment this method is called.
|
// use the SWP to get a lock-free get of the map. It's values are only valid at the moment this method is called.
|
||||||
return Collections.unmodifiableMap(deviceREF.get(this));
|
return Collections.unmodifiableMap(mapREF.get(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
int size() {
|
int size() {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.size();
|
.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
boolean isEmpty() {
|
boolean isEmpty() {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.isEmpty();
|
.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
boolean containsKey(final Object key) {
|
boolean containsKey(final Object key) {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.containsKey(key);
|
.containsKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
boolean containsValue(final Object value) {
|
boolean containsValue(final Object value) {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.containsValue(value);
|
.containsValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
V get(final Object key) {
|
V get(final Object key) {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return (V) deviceREF.get(this)
|
return (V) mapREF.get(this)
|
||||||
.get(key);
|
.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -186,4 +186,25 @@ class LockFreeHashMap<K, V> implements Map<K, V>, Cloneable, Serializable {
|
|||||||
Set<Entry<K, V>> entrySet() {
|
Set<Entry<K, V>> entrySet() {
|
||||||
return getMap().entrySet();
|
return getMap().entrySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
boolean equals(final Object o) {
|
||||||
|
return mapREF.get(this)
|
||||||
|
.equals(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
int hashCode() {
|
||||||
|
return mapREF.get(this)
|
||||||
|
.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
String toString() {
|
||||||
|
return mapREF.get(this)
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,15 +42,15 @@ import dorkbox.util.collections.IntMap.Keys;
|
|||||||
public
|
public
|
||||||
class LockFreeIntBiMap<V> {
|
class LockFreeIntBiMap<V> {
|
||||||
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||||
private static final AtomicReferenceFieldUpdater<LockFreeIntBiMap, IntMap> forwardREF =
|
private static final AtomicReferenceFieldUpdater<LockFreeIntBiMap, IntMap> forwardREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||||
AtomicReferenceFieldUpdater.newUpdater(LockFreeIntBiMap.class,
|
LockFreeIntBiMap.class,
|
||||||
IntMap.class,
|
IntMap.class,
|
||||||
"forwardHashMap");
|
"forwardHashMap");
|
||||||
|
|
||||||
private static final AtomicReferenceFieldUpdater<LockFreeIntBiMap, ObjectIntMap> reverseREF =
|
private static final AtomicReferenceFieldUpdater<LockFreeIntBiMap, ObjectIntMap> reverseREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||||
AtomicReferenceFieldUpdater.newUpdater(LockFreeIntBiMap.class,
|
LockFreeIntBiMap.class,
|
||||||
ObjectIntMap.class,
|
ObjectIntMap.class,
|
||||||
"reverseHashMap");
|
"reverseHashMap");
|
||||||
|
|
||||||
private volatile IntMap<V> forwardHashMap;
|
private volatile IntMap<V> forwardHashMap;
|
||||||
private volatile ObjectIntMap<V> reverseHashMap;
|
private volatile ObjectIntMap<V> reverseHashMap;
|
||||||
@ -377,32 +377,20 @@ class LockFreeIntBiMap<V> {
|
|||||||
.size;
|
.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identity equals only!
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
boolean equals(final Object o) {
|
boolean equals(final Object o) {
|
||||||
if (this == o) {
|
return this == o;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (o == null || getClass() != o.getClass()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
final LockFreeIntBiMap<?> that = (LockFreeIntBiMap<?>) o;
|
|
||||||
|
|
||||||
if (defaultReturnValue != that.defaultReturnValue) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!forwardHashMap.equals(that.forwardHashMap)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return reverseHashMap.equals(that.reverseHashMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
int hashCode() {
|
int hashCode() {
|
||||||
int result = forwardHashMap.hashCode();
|
int result = forwardREF.get(this).hashCode();
|
||||||
result = 31 * result + reverseHashMap.hashCode();
|
result = 31 * result + reverseREF.get(this).hashCode();
|
||||||
result = 31 * result + defaultReturnValue;
|
result = 31 * result + defaultReturnValue;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ import dorkbox.util.collections.IntMap.Values;
|
|||||||
public final
|
public final
|
||||||
class LockFreeIntMap<V> implements Cloneable, Serializable {
|
class LockFreeIntMap<V> implements Cloneable, Serializable {
|
||||||
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||||
private static final AtomicReferenceFieldUpdater<LockFreeIntMap, IntMap> deviceREF = AtomicReferenceFieldUpdater.newUpdater(
|
private static final AtomicReferenceFieldUpdater<LockFreeIntMap, IntMap> mapREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||||
LockFreeIntMap.class,
|
LockFreeIntMap.class,
|
||||||
IntMap.class,
|
IntMap.class,
|
||||||
"map");
|
"map");
|
||||||
@ -98,22 +98,22 @@ class LockFreeIntMap<V> implements Cloneable, Serializable {
|
|||||||
public
|
public
|
||||||
int size() {
|
int size() {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.size;
|
.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
boolean isEmpty() {
|
boolean isEmpty() {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.size == 0;
|
.size == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
boolean containsKey(final int key) {
|
boolean containsKey(final int key) {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.containsKey(key);
|
.containsKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,15 +126,15 @@ class LockFreeIntMap<V> implements Cloneable, Serializable {
|
|||||||
public
|
public
|
||||||
boolean containsValue(final Object value, boolean identity) {
|
boolean containsValue(final Object value, boolean identity) {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.containsValue(value, identity);
|
.containsValue(value, identity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
V get(final int key) {
|
V get(final int key) {
|
||||||
// use the SWP to get a lock-free get of the value
|
// use the SWP to get a lock-free get of the value
|
||||||
return (V) deviceREF.get(this)
|
return (V) mapREF.get(this)
|
||||||
.get(key);
|
.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized
|
public synchronized
|
||||||
@ -157,13 +157,36 @@ class LockFreeIntMap<V> implements Cloneable, Serializable {
|
|||||||
map.clear();
|
map.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identity equals only!
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
boolean equals(final Object o) {
|
||||||
|
return this == o;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
int hashCode() {
|
||||||
|
return mapREF.get(this)
|
||||||
|
.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
String toString() {
|
||||||
|
return mapREF.get(this)
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DO NOT MODIFY THE MAP VIA THIS! It will result in unknown object visibility!
|
* DO NOT MODIFY THE MAP VIA THIS! It will result in unknown object visibility!
|
||||||
*/
|
*/
|
||||||
public
|
public
|
||||||
Keys keySet() {
|
Keys keySet() {
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.keys();
|
.keys();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -171,7 +194,7 @@ class LockFreeIntMap<V> implements Cloneable, Serializable {
|
|||||||
*/
|
*/
|
||||||
public
|
public
|
||||||
Values<V> values() {
|
Values<V> values() {
|
||||||
return deviceREF.get(this)
|
return mapREF.get(this)
|
||||||
.values();
|
.values();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,15 +42,15 @@ import dorkbox.util.collections.IntMap.Keys;
|
|||||||
public
|
public
|
||||||
class LockFreeObjectIntBiMap<V> {
|
class LockFreeObjectIntBiMap<V> {
|
||||||
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||||
private static final AtomicReferenceFieldUpdater<LockFreeObjectIntBiMap, ObjectIntMap> forwardREF =
|
private static final AtomicReferenceFieldUpdater<LockFreeObjectIntBiMap, ObjectIntMap> forwardREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||||
AtomicReferenceFieldUpdater.newUpdater(LockFreeObjectIntBiMap.class,
|
LockFreeObjectIntBiMap.class,
|
||||||
ObjectIntMap.class,
|
ObjectIntMap.class,
|
||||||
"forwardHashMap");
|
"forwardHashMap");
|
||||||
|
|
||||||
private static final AtomicReferenceFieldUpdater<LockFreeObjectIntBiMap, IntMap> reverseREF =
|
private static final AtomicReferenceFieldUpdater<LockFreeObjectIntBiMap, IntMap> reverseREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||||
AtomicReferenceFieldUpdater.newUpdater(LockFreeObjectIntBiMap.class,
|
LockFreeObjectIntBiMap.class,
|
||||||
IntMap.class,
|
IntMap.class,
|
||||||
"reverseHashMap");
|
"reverseHashMap");
|
||||||
|
|
||||||
private volatile ObjectIntMap<V> forwardHashMap;
|
private volatile ObjectIntMap<V> forwardHashMap;
|
||||||
private volatile IntMap<V> reverseHashMap;
|
private volatile IntMap<V> reverseHashMap;
|
||||||
@ -353,32 +353,20 @@ class LockFreeObjectIntBiMap<V> {
|
|||||||
.size;
|
.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identity equals only!
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
boolean equals(final Object o) {
|
boolean equals(final Object o) {
|
||||||
if (this == o) {
|
return this == o;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (o == null || getClass() != o.getClass()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
final LockFreeObjectIntBiMap<?> that = (LockFreeObjectIntBiMap<?>) o;
|
|
||||||
|
|
||||||
if (defaultReturnValue != that.defaultReturnValue) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!forwardHashMap.equals(that.forwardHashMap)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return reverseHashMap.equals(that.reverseHashMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
int hashCode() {
|
int hashCode() {
|
||||||
int result = forwardHashMap.hashCode();
|
int result = forwardREF.get(this).hashCode();
|
||||||
result = 31 * result + reverseHashMap.hashCode();
|
result = 31 * result + reverseREF.get(this).hashCode();
|
||||||
result = 31 * result + defaultReturnValue;
|
result = 31 * result + defaultReturnValue;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -388,7 +376,7 @@ class LockFreeObjectIntBiMap<V> {
|
|||||||
String toString() {
|
String toString() {
|
||||||
StringBuilder builder = new StringBuilder("LockFreeObjectIntBiMap {");
|
StringBuilder builder = new StringBuilder("LockFreeObjectIntBiMap {");
|
||||||
|
|
||||||
Iterator<V> keys = keys();
|
Iterator<V> keys = keys();
|
||||||
Keys values = values();
|
Keys values = values();
|
||||||
|
|
||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
|
210
src/dorkbox/util/collections/LockFreeObjectIntMap.java
Normal file
210
src/dorkbox/util/collections/LockFreeObjectIntMap.java
Normal file
@ -0,0 +1,210 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.collections;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class uses the "single-writer-principle" for lock-free publication.
|
||||||
|
*
|
||||||
|
* Since there are only 2 methods to guarantee that modifications can only be called one-at-a-time (either it is only called by
|
||||||
|
* one thread, or only one thread can access it at a time) -- we chose the 2nd option -- and use 'synchronized' to make sure that only
|
||||||
|
* one thread can access this modification methods at a time. Getting or checking the presence of values can then happen in a lock-free
|
||||||
|
* manner.
|
||||||
|
*
|
||||||
|
* According to my benchmarks, this is approximately 25% faster than ConcurrentHashMap for (all types of) reads, and a lot slower for
|
||||||
|
* contended writes.
|
||||||
|
*
|
||||||
|
* This data structure is for many-read/few-write scenarios
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class LockFreeObjectIntMap<V> {
|
||||||
|
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||||
|
private static final AtomicReferenceFieldUpdater<LockFreeObjectIntMap, ObjectIntMap> mapREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||||
|
LockFreeObjectIntMap.class,
|
||||||
|
ObjectIntMap.class,
|
||||||
|
"map");
|
||||||
|
|
||||||
|
private volatile ObjectIntMap<V> map;
|
||||||
|
|
||||||
|
private final int defaultReturnValue;
|
||||||
|
|
||||||
|
// synchronized is used here to ensure the "single writer principle", and make sure that ONLY one thread at a time can enter this
|
||||||
|
// section. Because of this, we can have unlimited reader threads all going at the same time, without contention (which is our
|
||||||
|
// use-case 99% of the time)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new map using @{link Integer#MIN_VALUE}.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
LockFreeObjectIntMap() {
|
||||||
|
this(Integer.MIN_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default return value is used for various get/put operations on the ObjectIntMap.
|
||||||
|
*
|
||||||
|
* @param defaultReturnValue value used for various get/put operations on the ObjectIntMap.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
LockFreeObjectIntMap(int defaultReturnValue) {
|
||||||
|
this(new ObjectIntMap<V>(), defaultReturnValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default return value is used for various get/put operations on the ObjectIntMap.
|
||||||
|
*
|
||||||
|
* @param defaultReturnValue value used for various get/put operations on the ObjectIntMap.
|
||||||
|
*/
|
||||||
|
LockFreeObjectIntMap(ObjectIntMap<V> forwardHashMap, int defaultReturnValue) {
|
||||||
|
this.map = forwardHashMap;
|
||||||
|
this.defaultReturnValue = defaultReturnValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all of the mappings from this map.
|
||||||
|
*
|
||||||
|
* The map will be empty after this call returns.
|
||||||
|
*/
|
||||||
|
public synchronized
|
||||||
|
void clear() {
|
||||||
|
map.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized
|
||||||
|
int put(final V key, final int value) {
|
||||||
|
int prevForwardValue = this.map.get(key, defaultReturnValue);
|
||||||
|
this.map.put(key, value);
|
||||||
|
|
||||||
|
return prevForwardValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies all of the mappings from the specified map to this map.
|
||||||
|
* These mappings will replace any mappings that this map had for
|
||||||
|
* any of the keys currently in the specified map.
|
||||||
|
*
|
||||||
|
* @param hashMap mappings to be stored in this map
|
||||||
|
*
|
||||||
|
* @throws NullPointerException if the specified map is null
|
||||||
|
*/
|
||||||
|
public synchronized
|
||||||
|
void putAll(final Map<V, Integer> hashMap) throws IllegalArgumentException {
|
||||||
|
try {
|
||||||
|
ObjectIntMap<V> map = this.map;
|
||||||
|
for (Map.Entry<V, Integer> entry : hashMap.entrySet()) {
|
||||||
|
V key = entry.getKey();
|
||||||
|
Integer value = entry.getValue();
|
||||||
|
|
||||||
|
map.put(key, value);
|
||||||
|
}
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// do nothing if there is an exception
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the mapping for the specified key from this map if present.
|
||||||
|
*
|
||||||
|
* @param key key whose mapping is to be removed from the map
|
||||||
|
*
|
||||||
|
* @return the previous value associated with <tt>key</tt>, or
|
||||||
|
* <tt>defaultReturnValue</tt> if there was no mapping for <tt>key</tt>.
|
||||||
|
* (A <tt>defaultReturnValue</tt> return can also indicate that the map
|
||||||
|
* previously associated <tt>defaultReturnValue</tt> with <tt>key</tt>.)
|
||||||
|
*/
|
||||||
|
public synchronized
|
||||||
|
int remove(final V key) {
|
||||||
|
int value = map.remove(key, defaultReturnValue);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value to which the specified key is mapped,
|
||||||
|
* or {@code defaultReturnValue} if this map contains no mapping for the key.
|
||||||
|
* <p>
|
||||||
|
* <p>More formally, if this map contains a mapping from a key
|
||||||
|
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
|
||||||
|
* key.equals(k))}, then this method returns {@code v}; otherwise
|
||||||
|
* it returns {@code defaultReturnValue}. (There can be at most one such mapping.)
|
||||||
|
* <p>
|
||||||
|
* <p>A return value of {@code defaultReturnValue} does not <i>necessarily</i>
|
||||||
|
* indicate that the map contains no mapping for the key; it's also
|
||||||
|
* possible that the map explicitly maps the key to {@code null}.
|
||||||
|
* The {@link HashMap#containsKey containsKey} operation may be used to
|
||||||
|
* distinguish these two cases.
|
||||||
|
*
|
||||||
|
* @see #put(Object, int)
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public
|
||||||
|
int get(final V key) {
|
||||||
|
// use the SWP to get a lock-free get of the value
|
||||||
|
return mapREF.get(this).get(key, defaultReturnValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns <tt>true</tt> if this map contains no key-value mappings.
|
||||||
|
*
|
||||||
|
* @return <tt>true</tt> if this map contains no key-value mappings
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
boolean isEmpty() {
|
||||||
|
// use the SWP to get a lock-free get of the value
|
||||||
|
return mapREF.get(this)
|
||||||
|
.size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of key-value mappings in this map. If the
|
||||||
|
* map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
|
||||||
|
* <tt>Integer.MAX_VALUE</tt>.
|
||||||
|
*
|
||||||
|
* @return the number of key-value mappings in this map
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
int size() {
|
||||||
|
// use the SWP to get a lock-free get of the value
|
||||||
|
return mapREF.get(this)
|
||||||
|
.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identity equals only!
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
boolean equals(final Object o) {
|
||||||
|
return this == o;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
int hashCode() {
|
||||||
|
return mapREF.get(this).hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
String toString() {
|
||||||
|
return mapREF.get(this)
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
}
|
192
src/dorkbox/util/collections/LockFreeObjectMap.java
Normal file
192
src/dorkbox/util/collections/LockFreeObjectMap.java
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.collections;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
||||||
|
|
||||||
|
import com.esotericsoftware.kryo.util.ObjectMap;
|
||||||
|
import com.esotericsoftware.kryo.util.ObjectMap.Entries;
|
||||||
|
import com.esotericsoftware.kryo.util.ObjectMap.Keys;
|
||||||
|
import com.esotericsoftware.kryo.util.ObjectMap.Values;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class uses the "single-writer-principle" for lock-free publication.
|
||||||
|
* <p>
|
||||||
|
* Since there are only 2 methods to guarantee that modifications can only be called one-at-a-time (either it is only called by
|
||||||
|
* one thread, or only one thread can access it at a time) -- we chose the 2nd option -- and use 'synchronized' to make sure that only
|
||||||
|
* one thread can access this modification methods at a time. Getting or checking the presence of values can then happen in a lock-free
|
||||||
|
* manner.
|
||||||
|
* <p>
|
||||||
|
* According to my benchmarks, this is approximately 25% faster than ConcurrentHashMap for (all types of) reads, and a lot slower for
|
||||||
|
* contended writes.
|
||||||
|
* <p>
|
||||||
|
* This data structure is for many-read/few-write scenarios
|
||||||
|
*/
|
||||||
|
public final
|
||||||
|
class LockFreeObjectMap<K, V> implements Cloneable, Serializable {
|
||||||
|
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||||
|
private static final AtomicReferenceFieldUpdater<LockFreeObjectMap, ObjectMap> mapREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||||
|
LockFreeObjectMap.class,
|
||||||
|
ObjectMap.class,
|
||||||
|
"hashMap");
|
||||||
|
|
||||||
|
private volatile ObjectMap<K, V> hashMap;
|
||||||
|
|
||||||
|
// synchronized is used here to ensure the "single writer principle", and make sure that ONLY one thread at a time can enter this
|
||||||
|
// section. Because of this, we can have unlimited reader threads all going at the same time, without contention (which is our
|
||||||
|
// use-case 99% of the time)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
|
||||||
|
* (16) and the default load factor (0.75).
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
LockFreeObjectMap() {
|
||||||
|
hashMap = new ObjectMap<K, V>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an empty <tt>HashMap</tt> with the specified initial
|
||||||
|
* capacity and the default load factor (0.75).
|
||||||
|
*
|
||||||
|
* @param initialCapacity the initial capacity.
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException if the initial capacity is negative.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
LockFreeObjectMap(int initialCapacity) {
|
||||||
|
hashMap = new ObjectMap<K, V>(initialCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an empty <tt>HashMap</tt> with the specified initial
|
||||||
|
* capacity and load factor.
|
||||||
|
*
|
||||||
|
* @param initialCapacity the initial capacity
|
||||||
|
* @param loadFactor the load factor
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException if the initial capacity is negative
|
||||||
|
* or the load factor is nonpositive
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
LockFreeObjectMap(int initialCapacity, float loadFactor) {
|
||||||
|
this.hashMap = new ObjectMap<K, V>(initialCapacity, loadFactor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
int size() {
|
||||||
|
// use the SWP to get a lock-free get of the value
|
||||||
|
return mapREF.get(this)
|
||||||
|
.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
boolean isEmpty() {
|
||||||
|
// use the SWP to get a lock-free get of the value
|
||||||
|
return mapREF.get(this)
|
||||||
|
.size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
boolean containsKey(final K key) {
|
||||||
|
// use the SWP to get a lock-free get of the value
|
||||||
|
return mapREF.get(this)
|
||||||
|
.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
boolean containsValue(final V value, boolean identity) {
|
||||||
|
// use the SWP to get a lock-free get of the value
|
||||||
|
return mapREF.get(this)
|
||||||
|
.containsValue(value, identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public
|
||||||
|
V get(final K key) {
|
||||||
|
// use the SWP to get a lock-free get of the value
|
||||||
|
return (V) mapREF.get(this)
|
||||||
|
.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized
|
||||||
|
V put(final K key, final V value) {
|
||||||
|
return hashMap.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized
|
||||||
|
V remove(final K key) {
|
||||||
|
return hashMap.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized
|
||||||
|
void putAll(final ObjectMap<K, V> map) {
|
||||||
|
this.hashMap.putAll(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized
|
||||||
|
void clear() {
|
||||||
|
hashMap.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DO NOT MODIFY THE MAP VIA THIS! It will result in unknown object visibility!
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
Keys<K> keySet() {
|
||||||
|
return mapREF.get(this).keys();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DO NOT MODIFY THE MAP VIA THIS! It will result in unknown object visibility!
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
Values<V> values() {
|
||||||
|
return mapREF.get(this).values();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DO NOT MODIFY THE MAP VIA THIS! It will result in unknown object visibility!
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
Entries<K, V> entrySet() {
|
||||||
|
return mapREF.get(this).entries();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identity equals only!
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
boolean equals(final Object o) {
|
||||||
|
return this == o;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
int hashCode() {
|
||||||
|
return mapREF.get(this)
|
||||||
|
.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
String toString() {
|
||||||
|
return mapREF.get(this)
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -15,11 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package dorkbox.util.collections;
|
package dorkbox.util.collections;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -196,4 +192,24 @@ class LockFreeSet<E> implements Set<E>, Cloneable, java.io.Serializable {
|
|||||||
void clear() {
|
void clear() {
|
||||||
hashSet.clear();
|
hashSet.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
boolean equals(final Object o) {
|
||||||
|
return setREF.get(this).equals(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
int hashCode() {
|
||||||
|
return setREF.get(this)
|
||||||
|
.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
String toString() {
|
||||||
|
return setREF.get(this)
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,31 +43,6 @@ public class ObjectIntMap<K> {
|
|||||||
private int stashCapacity;
|
private int stashCapacity;
|
||||||
private int pushIterations;
|
private int pushIterations;
|
||||||
|
|
||||||
// public static
|
|
||||||
// void main(String[] args) {
|
|
||||||
// ObjectIntMap<String> test = new ObjectIntMap<String>(4);
|
|
||||||
// String one = "One";
|
|
||||||
// String four = "Four";
|
|
||||||
//
|
|
||||||
// test.put(one, 1);
|
|
||||||
// test.put("Two", 2);
|
|
||||||
// test.put("Three", 3);
|
|
||||||
// test.put(four, 4);
|
|
||||||
// test.put(four, 1);
|
|
||||||
// test.put(one, 13);
|
|
||||||
//
|
|
||||||
// ObjectIntMap<String> test2 = new ObjectIntMap<String>(2);
|
|
||||||
// test2.put(one, 11);
|
|
||||||
// test2.put(four, 44);
|
|
||||||
// test2.put("Five", 55);
|
|
||||||
//
|
|
||||||
// test2.putAll(test);
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// System.out.println(test.toString());
|
|
||||||
// System.out.println(test2.toString());
|
|
||||||
// }
|
|
||||||
|
|
||||||
/** Creates a new map with an initial capacity of 32 and a load factor of 0.8. This map will hold 25 items before growing the
|
/** Creates a new map with an initial capacity of 32 and a load factor of 0.8. This map will hold 25 items before growing the
|
||||||
* backing table. */
|
* backing table. */
|
||||||
public ObjectIntMap () {
|
public ObjectIntMap () {
|
||||||
|
Loading…
Reference in New Issue
Block a user