Go back to using a reentrant lock for store operations.
This commit is contained in:
parent
4b862cf4c7
commit
1b9efeb049
1 changed files with 23 additions and 13 deletions
|
@ -4,8 +4,11 @@ import net.sqlcipher.database.SQLiteDatabase;
|
|||
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.util.FeatureFlags;
|
||||
import org.whispersystems.signalservice.api.SignalSessionLock;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
private static final ReentrantLock LEGACY_LOCK = new ReentrantLock();
|
||||
|
||||
private volatile long ownerThreadId = NO_OWNER;
|
||||
|
||||
@Override
|
||||
public Lock acquire() {
|
||||
SQLiteDatabase db = DatabaseFactory.getInstance(ApplicationDependencies.getApplication()).getRawDatabase();
|
||||
if (FeatureFlags.internalUser()) {
|
||||
SQLiteDatabase db = DatabaseFactory.getInstance(ApplicationDependencies.getApplication()).getRawDatabase();
|
||||
|
||||
if (db.isDbLockedByCurrentThread()) {
|
||||
return () -> {};
|
||||
if (db.isDbLockedByCurrentThread()) {
|
||||
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();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue