Skip to content

Commit

Permalink
Synchronize on package-private J2ktCompatibleMonitor classes instea…
Browse files Browse the repository at this point in the history
…d of `Integer` instances, etc.

(followup to cl/645331066 now that cl/645490884 has landed)

RELNOTES=n/a
PiperOrigin-RevId: 645481013
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jun 24, 2024
1 parent faf36af commit 8c5bf07
Show file tree
Hide file tree
Showing 30 changed files with 174 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.google.common.testing;

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 J2ktCompatibleMonitor 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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

@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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

/** We will keep a private list of all logged records */
@GuardedBy("lock")
Expand Down
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 J2ktCompatibleMonitor(); // 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 J2ktCompatibleMonitor(); // 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 J2ktCompatibleMonitor(); // 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 J2ktCompatibleMonitor(); // 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 J2ktCompatibleMonitor(); // something Serializable

@Override
public String toString() {
Expand Down
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 J2ktCompatibleMonitor implements Serializable {}
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 @@ -121,8 +121,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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

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

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

@SuppressWarnings("UnnecessaryLambda") // Must be a fixed singleton object
private static final Supplier<Void> SUCCESSFULLY_COMPUTED =
Expand Down Expand Up @@ -277,8 +276,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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

final Supplier<T> delegate;
final long durationNanos;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.google.common.collect;

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 J2ktCompatibleMonitor implements Serializable {}
15 changes: 15 additions & 0 deletions android/guava/src/com/google/common/io/J2ktCompatibleMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.google.common.io;

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 J2ktCompatibleMonitor 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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

/*
* Conceptually, these two variables describe the executor being in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.google.common.util.concurrent;

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 J2ktCompatibleMonitor implements Serializable {}
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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

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

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 J2ktCompatibleMonitor 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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

@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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

/** We will keep a private list of all logged records */
@GuardedBy("lock")
Expand Down
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 J2ktCompatibleMonitor(); // 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 J2ktCompatibleMonitor(); // 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 J2ktCompatibleMonitor(); // 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 J2ktCompatibleMonitor(); // 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 J2ktCompatibleMonitor(); // something Serializable

@Override
public String toString() {
Expand Down
15 changes: 15 additions & 0 deletions guava/src/com/google/common/base/J2ktCompatibleMonitor.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 J2ktCompatibleMonitor implements Serializable {}
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 @@ -121,8 +121,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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

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

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

@SuppressWarnings("UnnecessaryLambda") // Must be a fixed singleton object
private static final Supplier<Void> SUCCESSFULLY_COMPUTED =
Expand Down Expand Up @@ -277,8 +276,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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

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

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 J2ktCompatibleMonitor implements Serializable {}
15 changes: 15 additions & 0 deletions guava/src/com/google/common/io/J2ktCompatibleMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.google.common.io;

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 J2ktCompatibleMonitor 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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

/*
* Conceptually, these two variables describe the executor being in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.google.common.util.concurrent;

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 J2ktCompatibleMonitor implements Serializable {}
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 J2ktCompatibleMonitor lock = new J2ktCompatibleMonitor();

private final String loggerName;
private volatile @Nullable Logger logger;
Expand Down

0 comments on commit 8c5bf07

Please sign in to comment.