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 it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class InternalClient implements ClientEventsHandler, TelegramClient { 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 ConcurrentHashMap<Long, Handler> handlers = new ConcurrentHashMap<Long, Handler>();
private final int clientId; private final int clientId;
@ -26,8 +24,6 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
ResultHandler updateHandler, ResultHandler updateHandler,
ExceptionHandler updateExceptionHandler, ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) { ExceptionHandler defaultExceptionHandler) {
clientInitializationLock.writeLock().lock();
try {
this.updateHandler = new Handler(updateHandler, updateExceptionHandler); this.updateHandler = new Handler(updateHandler, updateExceptionHandler);
this.updatesHandler = null; this.updatesHandler = null;
this.defaultExceptionHandler = defaultExceptionHandler; this.defaultExceptionHandler = defaultExceptionHandler;
@ -35,17 +31,12 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
this.clientId = NativeClientAccess.create(); this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this); clientManager.registerClient(clientId, this);
} finally {
clientInitializationLock.writeLock().unlock();
}
} }
public InternalClient(InternalClientManager clientManager, public InternalClient(InternalClientManager clientManager,
UpdatesHandler updatesHandler, UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler, ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) { ExceptionHandler defaultExceptionHandler) {
clientInitializationLock.writeLock().lock();
try {
this.updateHandler = null; this.updateHandler = null;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler); this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.clientManager = clientManager; this.clientManager = clientManager;
@ -53,9 +44,6 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
this.clientId = NativeClientAccess.create(); this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this); clientManager.registerClient(clientId, this);
} finally {
clientInitializationLock.writeLock().unlock();
}
} }
@Override @Override

View File

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