No longer use a lock for RecipientCache.getSelf()
First, the only lock we can use for the time being is the database lock, because if we use some other lock we could deadlock. That said, it seems like we could avoid using a lock at all. The purpose of the lock was to eliminate double-lookups, but if we have to acquire the database lock to check if we need to do the lookup, we've lost the advantage of doing so at all. We *could* just do a traditional check-lock-check pattern to get the lock far less often, but given that we're likely going to acquire it during startup, even a single access has the possibility of really gumming up the works.
This commit is contained in:
parent
faf6b5a4e4
commit
5ed6407ea3
1 changed files with 16 additions and 17 deletions
|
@ -37,8 +37,9 @@ public final class LiveRecipientCache {
|
|||
private final Map<RecipientId, LiveRecipient> recipients;
|
||||
private final LiveRecipient unknown;
|
||||
|
||||
private RecipientId localRecipientId;
|
||||
private boolean warmedUp;
|
||||
private volatile RecipientId localRecipientId;
|
||||
|
||||
private boolean warmedUp;
|
||||
|
||||
@SuppressLint("UseSparseArrays")
|
||||
public LiveRecipientCache(@NonNull Context context) {
|
||||
|
@ -110,22 +111,20 @@ public final class LiveRecipientCache {
|
|||
}
|
||||
|
||||
@NonNull Recipient getSelf() {
|
||||
try (SignalSessionLock.Lock unused = DatabaseSessionLock.INSTANCE.acquire()) {
|
||||
if (localRecipientId == null) {
|
||||
UUID localUuid = TextSecurePreferences.getLocalUuid(context);
|
||||
String localE164 = TextSecurePreferences.getLocalNumber(context);
|
||||
|
||||
if (localUuid != null) {
|
||||
localRecipientId = recipientDatabase.getByUuid(localUuid).or(recipientDatabase.getByE164(localE164)).orNull();
|
||||
} else if (localE164 != null) {
|
||||
localRecipientId = recipientDatabase.getByE164(localE164).orNull();
|
||||
} else {
|
||||
throw new IllegalStateException("Tried to call getSelf() before local data was set!");
|
||||
}
|
||||
|
||||
if (localRecipientId == null) {
|
||||
UUID localUuid = TextSecurePreferences.getLocalUuid(context);
|
||||
String localE164 = TextSecurePreferences.getLocalNumber(context);
|
||||
|
||||
if (localUuid != null) {
|
||||
localRecipientId = recipientDatabase.getByUuid(localUuid).or(recipientDatabase.getByE164(localE164)).orNull();
|
||||
} else if (localE164 != null) {
|
||||
localRecipientId = recipientDatabase.getByE164(localE164).orNull();
|
||||
} else {
|
||||
throw new IllegalStateException("Tried to call getSelf() before local data was set!");
|
||||
}
|
||||
|
||||
if (localRecipientId == null) {
|
||||
throw new MissingRecipientException(localRecipientId);
|
||||
}
|
||||
throw new MissingRecipientException(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue