From 66589cb8a0f462333afade1eac13283a11b8baed Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Mon, 27 Apr 2020 15:51:38 +0200 Subject: [PATCH] Update Client.java, td, and tdjni.so --- src/main/java/it/ernytech/tdlib/Client.java | 27 +++++++++++++++------ src/main/jni/td | 2 +- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/it/ernytech/tdlib/Client.java b/src/main/java/it/ernytech/tdlib/Client.java index 934fefb..0f2c8e3 100644 --- a/src/main/java/it/ernytech/tdlib/Client.java +++ b/src/main/java/it/ernytech/tdlib/Client.java @@ -23,14 +23,16 @@ import it.ernytech.tdlib.utils.Init; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.locks.StampedLock; /** * Interface for interaction with TDLib. */ public class Client { - private final long clientId; + private long clientId; private final ReentrantLock receiveLock = new ReentrantLock(); - private final ReentrantLock executionLock = new ReentrantLock(); + private final StampedLock executionLock = new StampedLock(); + private volatile Long stampedLockValue = 1L; private static native long createNativeClient(); private static native void nativeClientSend(long nativeClientId, long eventId, TdApi.Function function); private static native int nativeClientReceive(long nativeClientId, long[] eventIds, TdApi.Object[] events, double timeout); @@ -55,7 +57,7 @@ public class Client { * @param request Request to TDLib. */ public void send(Request request) { - if (this.executionLock.isLocked()) { + if (this.executionLock.isWriteLocked()) { throw new IllegalStateException("ClientActor is destroyed"); } @@ -69,7 +71,7 @@ public class Client { * @return An incoming update or request response list. The object returned in the response may be an empty list if the timeout expires. */ public List receive(double timeout, int eventSize) { - if (this.executionLock.isLocked()) { + if (this.executionLock.isWriteLocked()) { throw new IllegalStateException("ClientActor is destroyed"); } @@ -98,7 +100,7 @@ public class Client { * @return An incoming update or request response. The object returned in the response may be a nullptr if the timeout expires. */ public Response receive(double timeout) { - if (this.executionLock.isLocked()) { + if (this.executionLock.isWriteLocked()) { throw new IllegalStateException("ClientActor is destroyed"); } @@ -117,7 +119,7 @@ public class Client { * @return The request response. */ public Response execute(Request request) { - if (this.executionLock.isLocked()) { + if (this.executionLock.isWriteLocked()) { throw new IllegalStateException("ClientActor is destroyed"); } @@ -129,11 +131,20 @@ public class Client { * Destroys the client and TDLib instance. */ public void destroyClient() { - this.executionLock.lock(); + stampedLockValue = this.executionLock.writeLock(); destroyNativeClient(this.clientId); } + /** + * Destroys the client and TDLib instance. + */ + public void initializeClient() { + this.executionLock.tryUnlockWrite(); + stampedLockValue = null; + this.clientId = createNativeClient(); + } + public boolean isDestroyed() { - return this.executionLock.isLocked(); + return this.executionLock.isWriteLocked(); } } diff --git a/src/main/jni/td b/src/main/jni/td index 4f8b29c..077d237 160000 --- a/src/main/jni/td +++ b/src/main/jni/td @@ -1 +1 @@ -Subproject commit 4f8b29cfbdfe774ba8d3f93a385e67e21c30e5f8 +Subproject commit 077d237eaebb4b4d4f8522b12fdb420c8c323434