Remove again initialization lock

This commit is contained in:
Andrea Cavalli 2020-11-15 23:38:30 +01:00
parent 8bec749d64
commit d1766ed0e8
2 changed files with 16 additions and 36 deletions

View File

@ -7,11 +7,9 @@ import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class InternalClient implements ClientEventsHandler, TelegramClient {
static final ReentrantReadWriteLock clientInitializationLock = new ReentrantReadWriteLock(true);
private final ConcurrentHashMap<Long, Handler> handlers = new ConcurrentHashMap<Long, Handler>();
private final int clientId;
@ -23,39 +21,29 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
private final AtomicBoolean isClosed = new AtomicBoolean();
public InternalClient(InternalClientManager clientManager,
ResultHandler updateHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
clientInitializationLock.writeLock().lock();
try {
this.updateHandler = new Handler(updateHandler, updateExceptionHandler);
this.updatesHandler = null;
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientManager = clientManager;
this.clientId = NativeClientAccess.create();
ResultHandler updateHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
this.updateHandler = new Handler(updateHandler, updateExceptionHandler);
this.updatesHandler = null;
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientManager = clientManager;
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
} finally {
clientInitializationLock.writeLock().unlock();
}
clientManager.registerClient(clientId, this);
}
public InternalClient(InternalClientManager clientManager,
UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
clientInitializationLock.writeLock().lock();
try {
this.updateHandler = null;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.clientManager = clientManager;
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientId = NativeClientAccess.create();
this.updateHandler = null;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.clientManager = clientManager;
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
} finally {
clientInitializationLock.writeLock().unlock();
}
clientManager.registerClient(clientId, this);
}
@Override

View File

@ -1,7 +1,5 @@
package it.tdlight.common;
import static it.tdlight.common.InternalClient.clientInitializationLock;
import it.tdlight.common.utils.IntSwapper;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.Object;
@ -53,13 +51,7 @@ public class ResponseReceiver extends Thread implements AutoCloseable {
int[] sortIndex;
try {
while(!closeRequested || !registeredClients.isEmpty()) {
int resultsCount;
clientInitializationLock.readLock().lock();
try {
resultsCount = NativeClientAccess.receive(clientIds, eventIds, events, 2.0 /*seconds*/);
} finally {
clientInitializationLock.readLock().unlock();
};
int resultsCount = NativeClientAccess.receive(clientIds, eventIds, events, 2.0 /*seconds*/);
if (resultsCount <= 0)
continue;