-
-
Notifications
You must be signed in to change notification settings - Fork 6k
/
GroupsV2ProcessingLock.java
54 lines (44 loc) · 1.78 KB
/
GroupsV2ProcessingLock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package org.thoughtcrime.securesms.groups;
import androidx.annotation.WorkerThread;
import org.signal.core.util.ThreadUtil;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.crypto.ReentrantSessionLock;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.util.FeatureFlags;
import java.io.Closeable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public final class GroupsV2ProcessingLock {
private static final String TAG = Log.tag(GroupsV2ProcessingLock.class);
private GroupsV2ProcessingLock() {
}
private static final ReentrantLock lock = new ReentrantLock();
@WorkerThread
public static Closeable acquireGroupProcessingLock() throws GroupChangeBusyException {
if (FeatureFlags.internalUser()) {
if (!lock.isHeldByCurrentThread()) {
if (SignalDatabase.inTransaction()) {
throw new AssertionError("Tried to acquire the group lock inside of a database transaction!");
}
if (ReentrantSessionLock.INSTANCE.isHeldByCurrentThread()) {
throw new AssertionError("Tried to acquire the group lock inside of the ReentrantSessionLock!!");
}
}
}
return acquireGroupProcessingLock(5000);
}
@WorkerThread
public static Closeable acquireGroupProcessingLock(long timeoutMs) throws GroupChangeBusyException {
ThreadUtil.assertNotMainThread();
try {
if (!lock.tryLock(timeoutMs, TimeUnit.MILLISECONDS)) {
throw new GroupChangeBusyException("Failed to get a lock on the group processing in the timeout period");
}
return lock::unlock;
} catch (InterruptedException e) {
Log.w(TAG, e);
throw new GroupChangeBusyException(e);
}
}
}