Add locks to prevent reading updates when destroying the client
This commit is contained in:
parent
4d909e986e
commit
e92c04098f
|
@ -22,6 +22,8 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
|
|
||||||
private ClientState state = ClientState.of(false, 0, false, false, false);
|
private ClientState state = ClientState.of(false, 0, false, false, false);
|
||||||
private final ReentrantReadWriteLock stateLock = new ReentrantReadWriteLock();
|
private final ReentrantReadWriteLock stateLock = new ReentrantReadWriteLock();
|
||||||
|
private final ReentrantReadWriteLock updatesLock = new ReentrantReadWriteLock();
|
||||||
|
private final ReentrantReadWriteLock responsesLock = new ReentrantReadWriteLock();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new TDLib client.
|
* Creates a new TDLib client.
|
||||||
|
@ -53,6 +55,10 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Response> receive(double timeout, int eventsSize, boolean receiveResponses, boolean receiveUpdates) {
|
public List<Response> receive(double timeout, int eventsSize, boolean receiveResponses, boolean receiveUpdates) {
|
||||||
|
if (receiveResponses) responsesLock.readLock().lock();
|
||||||
|
try {
|
||||||
|
if (receiveUpdates) updatesLock.readLock().lock();
|
||||||
|
try {
|
||||||
long clientId;
|
long clientId;
|
||||||
stateLock.readLock().lock();
|
stateLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
|
@ -72,6 +78,12 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
return Arrays.asList(this.internalReceive(clientId, timeout, eventsSize, receiveResponses, receiveUpdates));
|
return Arrays.asList(this.internalReceive(clientId, timeout, eventsSize, receiveResponses, receiveUpdates));
|
||||||
|
} finally {
|
||||||
|
if (receiveUpdates) updatesLock.readLock().unlock();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (receiveResponses) responsesLock.readLock().unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sleep(double timeout) {
|
private void sleep(double timeout) {
|
||||||
|
@ -87,6 +99,10 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response receive(double timeout, boolean receiveResponses, boolean receiveUpdates) {
|
public Response receive(double timeout, boolean receiveResponses, boolean receiveUpdates) {
|
||||||
|
responsesLock.readLock().lock();
|
||||||
|
try {
|
||||||
|
updatesLock.readLock().lock();
|
||||||
|
try {
|
||||||
long clientId;
|
long clientId;
|
||||||
stateLock.readLock().lock();
|
stateLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
|
@ -112,6 +128,12 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
} finally {
|
||||||
|
updatesLock.readLock().unlock();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
responsesLock.readLock().unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Response[] internalReceive(long clientId, double timeout, int eventsSize, boolean receiveResponses, boolean receiveUpdates) {
|
private Response[] internalReceive(long clientId, double timeout, int eventsSize, boolean receiveResponses, boolean receiveUpdates) {
|
||||||
|
@ -187,6 +209,10 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroyClient() {
|
public void destroyClient() {
|
||||||
|
responsesLock.writeLock().lock();
|
||||||
|
try {
|
||||||
|
updatesLock.writeLock().lock();
|
||||||
|
try {
|
||||||
stateLock.writeLock().lock();
|
stateLock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
if (state.isInitialized() && state.hasClientId()) {
|
if (state.isInitialized() && state.hasClientId()) {
|
||||||
|
@ -199,10 +225,20 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
} finally {
|
} finally {
|
||||||
stateLock.writeLock().unlock();
|
stateLock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
updatesLock.writeLock().unlock();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
responsesLock.writeLock().unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initializeClient() {
|
public void initializeClient() {
|
||||||
|
responsesLock.writeLock().lock();
|
||||||
|
try {
|
||||||
|
updatesLock.writeLock().lock();
|
||||||
|
try {
|
||||||
stateLock.writeLock().lock();
|
stateLock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
if (!state.isInitialized() && !state.hasClientId()) {
|
if (!state.isInitialized() && !state.hasClientId()) {
|
||||||
|
@ -212,6 +248,12 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
} finally {
|
} finally {
|
||||||
stateLock.writeLock().unlock();
|
stateLock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
updatesLock.writeLock().unlock();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
responsesLock.writeLock().unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void requireInitialized() {
|
private void requireInitialized() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user