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;
@ -23,39 +21,29 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
private final AtomicBoolean isClosed = new AtomicBoolean(); private final AtomicBoolean isClosed = new AtomicBoolean();
public InternalClient(InternalClientManager clientManager, public InternalClient(InternalClientManager clientManager,
ResultHandler updateHandler, ResultHandler updateHandler,
ExceptionHandler updateExceptionHandler, ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) { ExceptionHandler defaultExceptionHandler) {
clientInitializationLock.writeLock().lock(); this.updateHandler = new Handler(updateHandler, updateExceptionHandler);
try { this.updatesHandler = null;
this.updateHandler = new Handler(updateHandler, updateExceptionHandler); this.defaultExceptionHandler = defaultExceptionHandler;
this.updatesHandler = null; this.clientManager = clientManager;
this.defaultExceptionHandler = defaultExceptionHandler; this.clientId = NativeClientAccess.create();
this.clientManager = clientManager;
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(); this.updateHandler = null;
try { this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.updateHandler = null; this.clientManager = clientManager;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler); this.defaultExceptionHandler = defaultExceptionHandler;
this.clientManager = clientManager; this.clientId = NativeClientAccess.create();
this.defaultExceptionHandler = defaultExceptionHandler;
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;