mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-23 20:48:43 +00:00
Go back to using a reentrant lock for store operations.
This commit is contained in:
@@ -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();
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user