diff --git a/core/src/main/java/com/seibel/lod/core/util/AtomicsUtil.java b/core/src/main/java/com/seibel/lod/core/util/AtomicsUtil.java index ccb26fad3..3dd3d9da7 100644 --- a/core/src/main/java/com/seibel/lod/core/util/AtomicsUtil.java +++ b/core/src/main/java/com/seibel/lod/core/util/AtomicsUtil.java @@ -6,18 +6,29 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.function.Predicate; -public class AtomicsUtil { - // While java 8 does have built in atomic operations, there doesn't seem to be any Compare And Exchange operation... - // So here we implement our own. - - public static T compareAndExchange(AtomicReference atomic, T expected, T newValue) { - while (true) { - T oldValue = atomic.get(); - if (oldValue != expected) return oldValue; - if (atomic.weakCompareAndSet(expected, newValue)) return expected; - } - } - +/** + * While java 8 does have built in atomic operations, there doesn't seem to be any Compare And Exchange operation...
+ * So here we implement our own. + */ +public class AtomicsUtil +{ + + public static T compareAndExchange(AtomicReference atomic, T expected, T newValue) + { + while (true) + { + T oldValue = atomic.get(); + if (oldValue != expected) + { + return oldValue; + } + else if (atomic.weakCompareAndSet(expected, newValue)) + { + return expected; + } + } + } + public static BooleanObjectImmutablePair compareAndExchangeWeak(AtomicReference atomic, T expected, T newValue) { T oldValue = atomic.get(); if (oldValue == expected && atomic.weakCompareAndSet(expected, newValue)) { @@ -26,7 +37,7 @@ public class AtomicsUtil { return new BooleanObjectImmutablePair<>(false, oldValue); } } - + public static T conditionalAndExchange(AtomicReference atomic, Predicate requirement, T newValue) { while (true) { T oldValue = atomic.get(); @@ -34,7 +45,7 @@ public class AtomicsUtil { if (atomic.weakCompareAndSet(oldValue, newValue)) return oldValue; } } - + public static BooleanObjectImmutablePair conditionalAndExchangeWeak(AtomicReference atomic, Predicate requirement, T newValue) { T oldValue = atomic.get(); if (requirement.test(oldValue) && atomic.weakCompareAndSet(oldValue, newValue)) { @@ -43,9 +54,11 @@ public class AtomicsUtil { return new BooleanObjectImmutablePair<>(false, oldValue); } } - - // Additionally, we implement some helper methods for frequently used atomic operations. - + + + + // Additionally, we implement some helper methods for frequently used atomic operations. // + // Compare with expected value and set new value if equal. Then return whatever value the atomic now contains. public static T compareAndSetThenGet(AtomicReference atomic, T expected, T newValue) { while (true) { @@ -54,9 +67,11 @@ public class AtomicsUtil { if (atomic.weakCompareAndSet(expected, newValue)) return newValue; } } - - - // Below is the array version of the above. + + + + // Below is the array version of the above. // + public static T compareAndExchange(AtomicReferenceArray array, int index, T expected, T newValue) { while (true) { T oldValue = array.get(index); @@ -64,7 +79,7 @@ public class AtomicsUtil { if (array.weakCompareAndSet(index, expected, newValue)) return expected; } } - + public static BooleanObjectImmutablePair compareAndExchangeWeak(AtomicReferenceArray array, int index, T expected, T newValue) { T oldValue = array.get(index); if (oldValue == expected && array.weakCompareAndSet(index, expected, newValue)) { @@ -73,7 +88,7 @@ public class AtomicsUtil { return new BooleanObjectImmutablePair<>(false, oldValue); } } - + public static T compareAndSetThenGet(AtomicReferenceArray array, int index, T expected, T newValue) { while (true) { T oldValue = array.get(index); @@ -81,5 +96,5 @@ public class AtomicsUtil { if (array.weakCompareAndSet(index, expected, newValue)) return newValue; } } - + }