From 5ed6407ea3bc8c6aaf8fc5331349a4bc803c6f07 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Mon, 8 Mar 2021 09:48:28 -0500 Subject: [PATCH] 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. --- .../recipients/LiveRecipientCache.java | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/recipients/LiveRecipientCache.java b/app/src/main/java/org/thoughtcrime/securesms/recipients/LiveRecipientCache.java index de3c95253c..1fb4a873d7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recipients/LiveRecipientCache.java +++ b/app/src/main/java/org/thoughtcrime/securesms/recipients/LiveRecipientCache.java @@ -37,8 +37,9 @@ public final class LiveRecipientCache { private final Map 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); } }