Client initialization lock

This commit is contained in:
Andrea Cavalli 2020-10-13 18:47:54 +02:00
parent 167bac99e7
commit 9dd625a1d7
2 changed files with 15 additions and 5 deletions

View File

@ -7,10 +7,11 @@ import it.tdlight.jni.TdApi.Update;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class InternalClient implements ClientEventsHandler, TelegramClient {
static final java.lang.Object CLIENT_CREATION_LOCK = new java.lang.Object();
static final ReentrantReadWriteLock clientInitializationLock = new ReentrantReadWriteLock(true);
private final ConcurrentHashMap<Long, Handler> handlers = new ConcurrentHashMap<Long, Handler>();
private final int clientId;
@ -25,7 +26,8 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
ResultHandler updateHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
synchronized (CLIENT_CREATION_LOCK) {
clientInitializationLock.writeLock().lock();
try {
this.updateHandler = new Handler(updateHandler, updateExceptionHandler);
this.updatesHandler = null;
this.defaultExceptionHandler = defaultExceptionHandler;
@ -33,6 +35,8 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
} finally {
clientInitializationLock.writeLock().unlock();
}
}
@ -40,7 +44,8 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
synchronized (CLIENT_CREATION_LOCK) {
clientInitializationLock.writeLock().lock();
try {
this.updateHandler = null;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.clientManager = clientManager;
@ -48,6 +53,8 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
} finally {
clientInitializationLock.writeLock().unlock();
}
}

View File

@ -1,6 +1,6 @@
package it.tdlight.common;
import static it.tdlight.common.InternalClient.CLIENT_CREATION_LOCK;
import static it.tdlight.common.InternalClient.clientInitializationLock;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.Object;
@ -49,8 +49,11 @@ public class ResponseReceiver extends Thread implements AutoCloseable {
try {
while(true) {
int resultsCount;
synchronized (CLIENT_CREATION_LOCK) {
clientInitializationLock.readLock().lock();
try {
resultsCount = NativeClientAccess.receive(clientIds, eventIds, events, 2.0 /*seconds*/);
} finally {
clientInitializationLock.readLock().unlock();
}
if (resultsCount <= 0)