Initial code check-in for renaming `invalidation tracker` related files in `room-runtime` from *.java to *.kt.
Test: N/A
Bug: 206859674
Relnote: Initial code check-in for renaming `invalidation tracker` related files in `room-runtime` from *.java to *.kt.
Change-Id: I5939c2c5b835290b1ad5014fc78d7a81b393a0e2
diff --git a/room/room-runtime/src/main/java/androidx/room/InvalidationLiveDataContainer.java b/room/room-runtime/src/main/java/androidx/room/InvalidationLiveDataContainer.kt
similarity index 100%
rename from room/room-runtime/src/main/java/androidx/room/InvalidationLiveDataContainer.java
rename to room/room-runtime/src/main/java/androidx/room/InvalidationLiveDataContainer.kt
diff --git a/room/room-runtime/src/main/java/androidx/room/InvalidationTracker.java b/room/room-runtime/src/main/java/androidx/room/InvalidationTracker.kt
similarity index 95%
rename from room/room-runtime/src/main/java/androidx/room/InvalidationTracker.java
rename to room/room-runtime/src/main/java/androidx/room/InvalidationTracker.kt
index 3a0c5ca..fe0ae9a 100644
--- a/room/room-runtime/src/main/java/androidx/room/InvalidationTracker.java
+++ b/room/room-runtime/src/main/java/androidx/room/InvalidationTracker.kt
@@ -104,7 +104,7 @@
@SuppressWarnings("WeakerAccess") /* synthetic access */
volatile SupportSQLiteStatement mCleanupStatement;
- private final ObservedTableTracker mObservedTableTracker;
+ private ObservedTableTracker mObservedTableTracker;
private final InvalidationLiveDataContainer mInvalidationLiveDataContainer;
@@ -115,8 +115,6 @@
private MultiInstanceInvalidationClient mMultiInstanceInvalidationClient;
- private final Object mSyncTriggersLock = new Object();
-
/**
* Used by the generated code.
*
@@ -539,13 +537,14 @@
return;
}
try {
- Lock closeLock = mDatabase.getCloseLock();
- closeLock.lock();
- try {
- // Serialize adding and removing table trackers, this is specifically important
- // to avoid missing invalidation before a transaction starts but there are
- // pending (possibly concurrent) observer changes.
- synchronized (mSyncTriggersLock) {
+ // This method runs in a while loop because while changes are synced to db, another
+ // runnable may be skipped. If we cause it to skip, we need to do its work.
+ while (true) {
+ Lock closeLock = mDatabase.getCloseLock();
+ closeLock.lock();
+ try {
+ // there is a potential race condition where another mSyncTriggers runnable
+ // can start running right after we get the tables list to sync.
final int[] tablesToSync = mObservedTableTracker.getTablesToSync();
if (tablesToSync == null) {
return;
@@ -567,9 +566,10 @@
} finally {
database.endTransaction();
}
+ mObservedTableTracker.onSyncCompleted();
+ } finally {
+ closeLock.unlock();
}
- } finally {
- closeLock.unlock();
}
} catch (IllegalStateException | SQLiteException exception) {
// may happen if db is closed. just log.
@@ -789,6 +789,13 @@
boolean mNeedsSync;
+ /**
+ * After we return non-null value from getTablesToSync, we expect a onSyncCompleted before
+ * returning any non-null value from getTablesToSync.
+ * This allows us to workaround any multi-threaded state syncing issues.
+ */
+ boolean mPendingSync;
+
ObservedTableTracker(int tableCount) {
mTableObservers = new long[tableCount];
mTriggerStates = new boolean[tableCount];
@@ -845,7 +852,7 @@
}
/**
- * If this returns non-null there are no pending sync operations.
+ * If this returns non-null, you must call onSyncCompleted.
*
* @return int[] An int array where the index for each tableId has the action for that
* table.
@@ -853,7 +860,7 @@
@Nullable
int[] getTablesToSync() {
synchronized (this) {
- if (!mNeedsSync) {
+ if (!mNeedsSync || mPendingSync) {
return null;
}
final int tableCount = mTableObservers.length;
@@ -866,8 +873,19 @@
}
mTriggerStates[i] = newState;
}
+ mPendingSync = true;
mNeedsSync = false;
- return mTriggerStateChanges.clone();
+ return mTriggerStateChanges;
+ }
+ }
+
+ /**
+ * if getTablesToSync returned non-null, the called should call onSyncCompleted once it
+ * is done.
+ */
+ void onSyncCompleted() {
+ synchronized (this) {
+ mPendingSync = false;
}
}
}
diff --git a/room/room-runtime/src/main/java/androidx/room/MultiInstanceInvalidationClient.java b/room/room-runtime/src/main/java/androidx/room/MultiInstanceInvalidationClient.kt
similarity index 100%
rename from room/room-runtime/src/main/java/androidx/room/MultiInstanceInvalidationClient.java
rename to room/room-runtime/src/main/java/androidx/room/MultiInstanceInvalidationClient.kt
diff --git a/room/room-runtime/src/main/java/androidx/room/MultiInstanceInvalidationService.java b/room/room-runtime/src/main/java/androidx/room/MultiInstanceInvalidationService.kt
similarity index 100%
rename from room/room-runtime/src/main/java/androidx/room/MultiInstanceInvalidationService.java
rename to room/room-runtime/src/main/java/androidx/room/MultiInstanceInvalidationService.kt
diff --git a/room/room-runtime/src/main/java/androidx/room/Room.java b/room/room-runtime/src/main/java/androidx/room/Room.kt
similarity index 100%
rename from room/room-runtime/src/main/java/androidx/room/Room.java
rename to room/room-runtime/src/main/java/androidx/room/Room.kt
diff --git a/room/room-runtime/src/main/java/androidx/room/RoomTrackingLiveData.java b/room/room-runtime/src/main/java/androidx/room/RoomTrackingLiveData.kt
similarity index 100%
rename from room/room-runtime/src/main/java/androidx/room/RoomTrackingLiveData.java
rename to room/room-runtime/src/main/java/androidx/room/RoomTrackingLiveData.kt