Skip to content

Commit

Permalink
Under the JRE, use java.util.concurrent.atomic.LongAdder unconditio…
Browse files Browse the repository at this point in the history
…nally and (except when we need to support GWT/J2CL) directly instead of through our `LongAddable` interfaces.

It's available [as of Java 8](https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/util/concurrent/atomic/LongAdder.html).

This eliminates our [usages of `Unsafe`](#6806) in `Striped64`, a part of the implementation of our `LongAdder`.

On the Android side, we'll have to wait [until we require API Level 24](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder).

RELNOTES=n/a
PiperOrigin-RevId: 712973075
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jan 8, 2025
1 parent 400af25 commit 138ce9e
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 1,250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@
*/
final class LongAddables {
public static LongAddable create() {
return new LongAdder();
return new GwtLongAddable();
}

private static final class GwtLongAddable implements LongAddable {
private long value;

public void increment() {
value++;
}

public void add(long x) {
value += x;
}

public long sum() {
return value;
}
}
}

This file was deleted.

42 changes: 0 additions & 42 deletions guava-tests/test/com/google/common/cache/LongAdderTest.java

This file was deleted.

50 changes: 4 additions & 46 deletions guava/src/com/google/common/cache/LongAddables.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,18 @@
package com.google.common.cache;

import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Supplier;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
* Source of {@link LongAddable} objects that deals with GWT, Unsafe, and all that.
* Source of {@link LongAddable} objects that deals with GWT and all that.
*
* @author Louis Wasserman
*/
@GwtCompatible(emulated = true)
final class LongAddables {
private static final Supplier<LongAddable> SUPPLIER;

static {
Supplier<LongAddable> supplier;
try {
// trigger static initialization of the LongAdder class, which may fail
LongAdder unused = new LongAdder();
supplier =
new Supplier<LongAddable>() {
@Override
public LongAddable get() {
return new LongAdder();
}
};
} catch (Throwable t) { // we really want to catch *everything*
supplier =
new Supplier<LongAddable>() {
@Override
public LongAddable get() {
return new PureJavaLongAddable();
}
};
}
SUPPLIER = supplier;
}

public static LongAddable create() {
return SUPPLIER.get();
return new JavaUtilConcurrentLongAdder();
}

private static final class PureJavaLongAddable extends AtomicLong implements LongAddable {
@Override
public void increment() {
getAndIncrement();
}

@Override
public void add(long x) {
getAndAdd(x);
}

@Override
public long sum() {
return get();
}
}
private static final class JavaUtilConcurrentLongAdder extends LongAdder implements LongAddable {}
}
193 changes: 0 additions & 193 deletions guava/src/com/google/common/cache/LongAdder.java

This file was deleted.

Loading

0 comments on commit 138ce9e

Please sign in to comment.