From 2219fc19f188a097162f0700aadb0e9c860b7f58 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 8 Nov 2023 13:00:09 +0100 Subject: [PATCH] Use critical section for room tracking --- .../androidx/room/RoomTrackingLiveData.java | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/androidx/room/RoomTrackingLiveData.java b/app/src/main/java/androidx/room/RoomTrackingLiveData.java index 10c4fbcf8b..26ed3031aa 100644 --- a/app/src/main/java/androidx/room/RoomTrackingLiveData.java +++ b/app/src/main/java/androidx/room/RoomTrackingLiveData.java @@ -29,9 +29,6 @@ import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; - -import eu.faircode.email.EntityLog; /** * A LiveData implementation that closely works with {@link InvalidationTracker} to implement @@ -62,8 +59,8 @@ class RoomTrackingLiveData extends LiveData { @SuppressWarnings("WeakerAccess") final InvalidationTracker.Observer mObserver; - final AtomicInteger queued = new AtomicInteger(0); - final AtomicInteger running = new AtomicInteger(0); + private int queued = 0; + private final Object lock = new Object(); @SuppressWarnings("WeakerAccess") final AtomicBoolean mRegisteredObserver = new AtomicBoolean(false); @@ -73,39 +70,41 @@ class RoomTrackingLiveData extends LiveData { @WorkerThread @Override public void run() { + synchronized (lock) { + queued--; + if (queued < 0) { + eu.faircode.email.Log.e(mComputeFunction + " queued=" + queued); + queued = 0; + } + } + if (mRegisteredObserver.compareAndSet(false, true)) { mDatabase.getInvalidationTracker().addWeakObserver(mObserver); } - try { - running.incrementAndGet(); - - T value = null; - boolean computed = false; - synchronized (mComputeFunction) { - int retry = 0; - while (!computed) { + + T value = null; + boolean computed = false; + synchronized (mComputeFunction) { + int retry = 0; + while (!computed) { + try { + value = mComputeFunction.call(); + computed = true; + } catch (Throwable e) { + if (++retry > 5) { + eu.faircode.email.Log.e(e); + break; + } + eu.faircode.email.Log.w(e); try { - value = mComputeFunction.call(); - computed = true; - } catch (Throwable e) { - if (++retry > 5) { - eu.faircode.email.Log.e(e); - break; - } - eu.faircode.email.Log.w(e); - try { - Thread.sleep(2000L); - } catch (InterruptedException ignored) { - } + Thread.sleep(2000L); + } catch (InterruptedException ignored) { } } } - if (computed) { - postValue(value); - } - } finally { - queued.decrementAndGet(); - running.decrementAndGet(); + } + if (computed) { + postValue(value); } } }; @@ -115,16 +114,17 @@ class RoomTrackingLiveData extends LiveData { @MainThread @Override public void run() { - if (running.get() == 0 && queued.get() > 0) { - eu.faircode.email.Log.persist(EntityLog.Type.Debug, - mComputeFunction + " running=" + running + " queued=" + queued); - return; - } boolean isActive = hasActiveObservers(); - if (isActive) { - queued.incrementAndGet(); - getQueryExecutor().execute(mRefreshRunnable); - } + if (isActive) + synchronized (lock) { + if (queued > 0) + eu.faircode.email.Log.persist(eu.faircode.email.EntityLog.Type.Debug, + mComputeFunction + " queued=" + queued); + else { + queued++; + getQueryExecutor().execute(mRefreshRunnable); + } + } } };