Go back to using a reentrant lock for store operations.

This commit is contained in:
Greyson Parrelli 2021-03-01 09:38:33 -05:00 committed by GitHub
parent 4b862cf4c7
commit 1b9efeb049
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,8 +4,11 @@ import net.sqlcipher.database.SQLiteDatabase;
import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies; import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.util.FeatureFlags;
import org.whispersystems.signalservice.api.SignalSessionLock; import org.whispersystems.signalservice.api.SignalSessionLock;
import java.util.concurrent.locks.ReentrantLock;
/** /**
* An implementation of {@link SignalSessionLock} that effectively re-uses our database lock. * An implementation of {@link SignalSessionLock} that effectively re-uses our database lock.
*/ */
@ -15,25 +18,32 @@ public enum DatabaseSessionLock implements SignalSessionLock {
public static final long NO_OWNER = -1; public static final long NO_OWNER = -1;
private static final ReentrantLock LEGACY_LOCK = new ReentrantLock();
private volatile long ownerThreadId = NO_OWNER; private volatile long ownerThreadId = NO_OWNER;
@Override @Override
public Lock acquire() { public Lock acquire() {
SQLiteDatabase db = DatabaseFactory.getInstance(ApplicationDependencies.getApplication()).getRawDatabase(); if (FeatureFlags.internalUser()) {
SQLiteDatabase db = DatabaseFactory.getInstance(ApplicationDependencies.getApplication()).getRawDatabase();
if (db.isDbLockedByCurrentThread()) { if (db.isDbLockedByCurrentThread()) {
return () -> {}; return () -> {};
}
db.beginTransaction();
ownerThreadId = Thread.currentThread().getId();
return () -> {
ownerThreadId = -1;
db.setTransactionSuccessful();
db.endTransaction();
};
} else {
LEGACY_LOCK.lock();
return LEGACY_LOCK::unlock;
} }
db.beginTransaction();
ownerThreadId = Thread.currentThread().getId();
return () -> {
ownerThreadId = -1;
db.setTransactionSuccessful();
db.endTransaction();
};
} }
/** /**