Skip to content

Commit

Permalink
Synchronize on package-private XplatMonitor classes instead of `Int…
Browse files Browse the repository at this point in the history
…eger` instances, etc.

(followup to cl/645331066 now that cl/645490884, cl/653538661, and cl/655485586 have landed)

RELNOTES=n/a
PiperOrigin-RevId: 645481013
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jul 25, 2024
1 parent 85c6f88 commit a76b06e
Show file tree
Hide file tree
Showing 32 changed files with 162 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class TearDownStack implements TearDownAccepter {
private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());

@VisibleForTesting final Object lock = new Object();
@VisibleForTesting final XplatMonitor lock = new XplatMonitor();

@GuardedBy("lock")
final LinkedList<TearDown> stack = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
@GwtCompatible
@ElementTypesAreNonnullByDefault
public class TestLogHandler extends Handler {
private final Object lock = new Object();
private final XplatMonitor lock = new XplatMonitor();

/** We will keep a private list of all logged records */
@GuardedBy("lock")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.common.testing;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* One of our per-package copies of {@link com.google.common.base.XplatMonitor}.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected Deque<String> create() {

private static final class TestDeque<E> implements Deque<E> {
private final Deque<E> delegate = Lists.newLinkedList();
public final Object mutex = new Integer(1); // something Serializable
public final Object mutex = new XplatMonitor(); // something Serializable

@Override
public boolean offer(E o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @author Mike Bostock
*/
public class SynchronizedMapTest extends TestCase {
public final Object mutex = new Integer(1); // something Serializable
public final Object mutex = new XplatMonitor(); // something Serializable

protected <K, V> Map<K, V> create() {
TestMap<K, V> inner = new TestMap<>(new HashMap<K, V>(), mutex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected SetMultimap<String, String> create(Entry<String, String>[] entries) {
private static final class TestMultimap<K, V> extends ForwardingSetMultimap<K, V>
implements Serializable {
final SetMultimap<K, V> delegate = HashMultimap.create();
public final Object mutex = new Integer(1); // something Serializable
public final Object mutex = new XplatMonitor(); // something Serializable

@Override
protected SetMultimap<K, V> delegate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
public class SynchronizedSetTest extends TestCase {

public static final Object MUTEX = new Integer(1); // something Serializable
public static final Object MUTEX = new XplatMonitor(); // something Serializable

public static Test suite() {
return SetTestSuiteBuilder.using(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class SynchronizedTableTest extends AbstractTableTest<Character> {
private static final class TestTable<R, C, V> implements Table<R, C, V>, Serializable {
final Table<R, C, V> delegate = HashBasedTable.create();
public final Object mutex = new Integer(1); // something Serializable
public final Object mutex = new XplatMonitor(); // something Serializable

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testCancel() {
SettableFuture<String> f = SettableFuture.create();
FutureCallback<String> callback =
new FutureCallback<String>() {
private final Object monitor = new Object();
private final XplatMonitor monitor = new XplatMonitor();
private boolean called = false;

@Override
Expand Down Expand Up @@ -183,7 +183,7 @@ private final class MockCallback implements FutureCallback<String> {
@Nullable private String value = null;
@Nullable private Throwable failure = null;
private boolean wasCalled = false;
private final Object monitor = new Object();
private final XplatMonitor monitor = new XplatMonitor();

MockCallback(String expectedValue) {
this.value = expectedValue;
Expand Down
8 changes: 3 additions & 5 deletions android/guava/src/com/google/common/base/Suppliers.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ public String toString() {

@VisibleForTesting
static class MemoizingSupplier<T extends @Nullable Object> implements Supplier<T>, Serializable {
private final Object lock =
new Integer(1); // something serializable
private final XplatMonitor lock = new XplatMonitor();

final Supplier<T> delegate;
transient volatile boolean initialized;
Expand Down Expand Up @@ -163,7 +162,7 @@ public String toString() {

@VisibleForTesting
static class NonSerializableMemoizingSupplier<T extends @Nullable Object> implements Supplier<T> {
private final Object lock = new Object();
private final XplatMonitor lock = new XplatMonitor();

@SuppressWarnings("UnnecessaryLambda") // Must be a fixed singleton object
private static final Supplier<Void> SUCCESSFULLY_COMPUTED =
Expand Down Expand Up @@ -275,8 +274,7 @@ public String toString() {
@SuppressWarnings("GoodTime") // lots of violations
static class ExpiringMemoizingSupplier<T extends @Nullable Object>
implements Supplier<T>, Serializable {
private final Object lock =
new Integer(1); // something serializable
private final XplatMonitor lock = new XplatMonitor();

final Supplier<T> delegate;
final long durationNanos;
Expand Down
15 changes: 15 additions & 0 deletions android/guava/src/com/google/common/base/XplatMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.google.common.base;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* Using this class instead of Object will allow J2kt to substitute monitors in a way that works for
* Kotlin native. This class is marked as Serializable so it can be "just" included in serializable
* classes despite this not making much sense.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}
13 changes: 13 additions & 0 deletions android/guava/src/com/google/common/collect/XplatMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* One of our per-package copies of {@link com.google.common.base.XplatMonitor}.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}
13 changes: 13 additions & 0 deletions android/guava/src/com/google/common/io/XplatMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.common.io;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* One of our per-package copies of {@link com.google.common.base.XplatMonitor}.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
final class DirectExecutorService extends AbstractListeningExecutorService {

/** Lock used whenever accessing the state variables (runningTasks, shutdown) of the executor */
private final Object lock = new Object();
private final XplatMonitor lock = new XplatMonitor();

/*
* Conceptually, these two variables describe the executor being in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@GwtCompatible
@ElementTypesAreNonnullByDefault
final class LazyLogger {
private final Object lock = new Object();
private final XplatMonitor lock = new XplatMonitor();

private final String loggerName;
private volatile @Nullable Logger logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.common.util.concurrent;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* One of our per-package copies of {@link com.google.common.base.XplatMonitor}.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class TearDownStack implements TearDownAccepter {
private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());

@VisibleForTesting final Object lock = new Object();
@VisibleForTesting final XplatMonitor lock = new XplatMonitor();

@GuardedBy("lock")
final LinkedList<TearDown> stack = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
@GwtCompatible
@ElementTypesAreNonnullByDefault
public class TestLogHandler extends Handler {
private final Object lock = new Object();
private final XplatMonitor lock = new XplatMonitor();

/** We will keep a private list of all logged records */
@GuardedBy("lock")
Expand Down
13 changes: 13 additions & 0 deletions guava-testlib/src/com/google/common/testing/XplatMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.common.testing;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* One of our per-package copies of {@link com.google.common.base.XplatMonitor}.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected Deque<String> create() {

private static final class TestDeque<E> implements Deque<E> {
private final Deque<E> delegate = Lists.newLinkedList();
public final Object mutex = new Integer(1); // something Serializable
public final Object mutex = new XplatMonitor(); // something Serializable

@Override
public boolean offer(E o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @author Mike Bostock
*/
public class SynchronizedMapTest extends TestCase {
public final Object mutex = new Integer(1); // something Serializable
public final Object mutex = new XplatMonitor(); // something Serializable

protected <K, V> Map<K, V> create() {
TestMap<K, V> inner = new TestMap<>(new HashMap<K, V>(), mutex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected SetMultimap<String, String> create(Entry<String, String>[] entries) {
private static final class TestMultimap<K, V> extends ForwardingSetMultimap<K, V>
implements Serializable {
final SetMultimap<K, V> delegate = HashMultimap.create();
public final Object mutex = new Integer(1); // something Serializable
public final Object mutex = new XplatMonitor(); // something Serializable

@Override
protected SetMultimap<K, V> delegate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public class SynchronizedSetTest extends TestCase {

public static final Object MUTEX = new Integer(1); // something Serializable
public static final Object MUTEX = new XplatMonitor(); // something Serializable

public static Test suite() {
return SetTestSuiteBuilder.using(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class SynchronizedTableTest extends AbstractTableTest<Character> {
private static final class TestTable<R, C, V> implements Table<R, C, V>, Serializable {
final Table<R, C, V> delegate = HashBasedTable.create();
public final Object mutex = new Integer(1); // something Serializable
public final Object mutex = new XplatMonitor(); // something Serializable

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testCancel() {
SettableFuture<String> f = SettableFuture.create();
FutureCallback<String> callback =
new FutureCallback<String>() {
private final Object monitor = new Object();
private final XplatMonitor monitor = new XplatMonitor();
private boolean called = false;

@Override
Expand Down Expand Up @@ -183,7 +183,7 @@ private final class MockCallback implements FutureCallback<String> {
@Nullable private String value = null;
@Nullable private Throwable failure = null;
private boolean wasCalled = false;
private final Object monitor = new Object();
private final XplatMonitor monitor = new XplatMonitor();

MockCallback(String expectedValue) {
this.value = expectedValue;
Expand Down
8 changes: 3 additions & 5 deletions guava/src/com/google/common/base/Suppliers.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ public String toString() {

@VisibleForTesting
static class MemoizingSupplier<T extends @Nullable Object> implements Supplier<T>, Serializable {
private final Object lock =
new Integer(1); // something serializable
private final XplatMonitor lock = new XplatMonitor();

final Supplier<T> delegate;
transient volatile boolean initialized;
Expand Down Expand Up @@ -163,7 +162,7 @@ public String toString() {

@VisibleForTesting
static class NonSerializableMemoizingSupplier<T extends @Nullable Object> implements Supplier<T> {
private final Object lock = new Object();
private final XplatMonitor lock = new XplatMonitor();

@SuppressWarnings("UnnecessaryLambda") // Must be a fixed singleton object
private static final Supplier<Void> SUCCESSFULLY_COMPUTED =
Expand Down Expand Up @@ -275,8 +274,7 @@ public String toString() {
@SuppressWarnings("GoodTime") // lots of violations
static class ExpiringMemoizingSupplier<T extends @Nullable Object>
implements Supplier<T>, Serializable {
private final Object lock =
new Integer(1); // something serializable
private final XplatMonitor lock = new XplatMonitor();

final Supplier<T> delegate;
final long durationNanos;
Expand Down
15 changes: 15 additions & 0 deletions guava/src/com/google/common/base/XplatMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.google.common.base;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* Using this class instead of Object will allow J2kt to substitute monitors in a way that works for
* Kotlin native. This class is marked as Serializable so it can be "just" included in serializable
* classes despite this not making much sense.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}
13 changes: 13 additions & 0 deletions guava/src/com/google/common/collect/XplatMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* One of our per-package copies of {@link com.google.common.base.XplatMonitor}.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}
13 changes: 13 additions & 0 deletions guava/src/com/google/common/io/XplatMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.common.io;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* One of our per-package copies of {@link com.google.common.base.XplatMonitor}.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
final class DirectExecutorService extends AbstractListeningExecutorService {

/** Lock used whenever accessing the state variables (runningTasks, shutdown) of the executor */
private final Object lock = new Object();
private final XplatMonitor lock = new XplatMonitor();

/*
* Conceptually, these two variables describe the executor being in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@GwtCompatible
@ElementTypesAreNonnullByDefault
final class LazyLogger {
private final Object lock = new Object();
private final XplatMonitor lock = new XplatMonitor();

private final String loggerName;
private volatile @Nullable Logger logger;
Expand Down
13 changes: 13 additions & 0 deletions guava/src/com/google/common/util/concurrent/XplatMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.common.util.concurrent;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.ThreadSafe;
import java.io.Serializable;

/**
* One of our per-package copies of {@link com.google.common.base.XplatMonitor}.
*/
@GwtCompatible
@ThreadSafe
@ElementTypesAreNonnullByDefault
final class XplatMonitor implements Serializable {}

0 comments on commit a76b06e

Please sign in to comment.