Levlam race condition still persists
This commit is contained in:
parent
97dcf5576a
commit
e87ddb4d20
src/main/java/it/tdlight/common
@ -1,5 +1,7 @@
|
|||||||
package it.tdlight.common;
|
package it.tdlight.common;
|
||||||
|
|
||||||
|
import it.tdlight.jni.TdApi;
|
||||||
|
|
||||||
public abstract class CommonClientManager {
|
public abstract class CommonClientManager {
|
||||||
|
|
||||||
private static InternalClientManager getClientManager(String implementationName) {
|
private static InternalClientManager getClientManager(String implementationName) {
|
||||||
@ -32,6 +34,8 @@ public abstract class CommonClientManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static TelegramClient create(InternalClient internalClient) {
|
private static TelegramClient create(InternalClient internalClient) {
|
||||||
|
// Send a dummy request because @levlam is too lazy to fix race conditions in a better way
|
||||||
|
internalClient.send(new TdApi.GetAuthorizationState(), null, null);
|
||||||
return internalClient;
|
return internalClient;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,11 @@ 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;
|
||||||
@ -24,6 +26,8 @@ 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;
|
||||||
@ -31,6 +35,9 @@ 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,
|
||||||
|
@ -49,9 +49,6 @@ public class InternalClientManager implements AutoCloseable {
|
|||||||
if (replaced) {
|
if (replaced) {
|
||||||
throw new IllegalStateException("Client " + clientId + " already registered");
|
throw new IllegalStateException("Client " + clientId + " already registered");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a dummy request because @levlam is too lazy to fix race conditions in a better way
|
|
||||||
internalClient.send(new TdApi.GetAuthorizationState(), null, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getImplementationName() {
|
public String getImplementationName() {
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
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;
|
||||||
@ -52,7 +54,12 @@ public class ResponseReceiver extends Thread implements AutoCloseable {
|
|||||||
try {
|
try {
|
||||||
while(!closeRequested || !registeredClients.isEmpty()) {
|
while(!closeRequested || !registeredClients.isEmpty()) {
|
||||||
int resultsCount;
|
int resultsCount;
|
||||||
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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user