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

Commit 6d970255 authored by Vasu Nori's avatar Vasu Nori
Browse files

STOPSHIP - add illegalStateException to catch potential deadlocks in db

database lock() should not be called from a synchronized methods
in database layer. add code to check.
this will catch potential deadlock situations sooner

Change-Id: I9d913f5e2af304f4d9ad5b2641c3a768c4bc97f9
parent 060e25bf
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -410,8 +410,16 @@ public class SQLiteDatabase extends SQLiteClosable {
     * @see #unlock()
     */
    /* package */ void lock() {
        lock(false);
    }
    private void lock(boolean forced) {
        // make sure this method is NOT being called from a 'synchronized' method
        if (Thread.holdsLock(this)) {
            // STOPSHIP change the following line to Log.w()
            throw new IllegalStateException("don't lock() while in a synchronized method");
        }
        verifyDbIsOpen();
        if (!mLockingEnabled) return;
        if (!forced && !mLockingEnabled) return;
        mLock.lock();
        if (SQLiteDebug.DEBUG_LOCK_TIME_TRACKING) {
            if (mLock.getHoldCount() == 1) {
@@ -421,7 +429,6 @@ public class SQLiteDatabase extends SQLiteClosable {
            }
        }
    }

    /**
     * Locks the database for exclusive access. The database lock must be held when
     * touch the native sqlite3* object since it is single threaded and uses
@@ -431,15 +438,7 @@ public class SQLiteDatabase extends SQLiteClosable {
     * @see #unlockForced()
     */
    private void lockForced() {
        verifyDbIsOpen();
        mLock.lock();
        if (SQLiteDebug.DEBUG_LOCK_TIME_TRACKING) {
            if (mLock.getHoldCount() == 1) {
                // Use elapsed real-time since the CPU may sleep when waiting for IO
                mLockAcquiredWallTime = SystemClock.elapsedRealtime();
                mLockAcquiredThreadTime = Debug.threadCpuTimeNanos();
            }
        }
        lock(true);
    }

    /**