Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 600bdd82 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Breaking sleep after yield into small quanta.

Rationale: there is no reason to sleep for 4 seconds between
transactions if there is no one using the database.  This long
sleep only makes sense if two threads are actively using 
the database yielding to each other every several seconds.
parent 5e2a385c
Loading
Loading
Loading
Loading
+68 −55
Original line number Diff line number Diff line
@@ -189,6 +189,8 @@ public class SQLiteDatabase extends SQLiteClosable {
    private static final int LOCK_ACQUIRED_WARNING_THREAD_TIME_IN_MS = 100;
    private static final int LOCK_ACQUIRED_WARNING_TIME_IN_MS_ALWAYS_PRINT = 2000;

    private static final int SLEEP_AFTER_YIELD_QUANTUM = 500;

    private long mLastLockMessageTime = 0L;

    /** Used by native code, do not rename */
@@ -567,11 +569,22 @@ public class SQLiteDatabase extends SQLiteClosable {
            }
        }
        if (sleepAfterYieldDelay > 0) {
            // Sleep for up to sleepAfterYieldDelay milliseconds, waking up periodically to
            // check if anyone is using the database.  If the database is not contended,
            // retake the lock and return.
            long remainingDelay = sleepAfterYieldDelay;
            while (remainingDelay > 0) {
                try {
                Thread.sleep(sleepAfterYieldDelay);
                    Thread.sleep(remainingDelay < SLEEP_AFTER_YIELD_QUANTUM ?
                            remainingDelay : SLEEP_AFTER_YIELD_QUANTUM);
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
                remainingDelay -= SLEEP_AFTER_YIELD_QUANTUM;
                if (mLock.getQueueLength() == 0) {
                    break;
                }
            }
        }
        beginTransaction();
        return true;