Add locks to prevent reading updates when destroying the client

This commit is contained in:
Andrea Cavalli 2020-09-07 15:21:57 +02:00
parent 4d909e986e
commit e92c04098f
1 changed files with 91 additions and 49 deletions

View File

@ -22,6 +22,8 @@ public class Client extends NativeClient implements TelegramClient {
private ClientState state = ClientState.of(false, 0, false, false, false);
private final ReentrantReadWriteLock stateLock = new ReentrantReadWriteLock();
private final ReentrantReadWriteLock updatesLock = new ReentrantReadWriteLock();
private final ReentrantReadWriteLock responsesLock = new ReentrantReadWriteLock();
/**
* Creates a new TDLib client.
@ -53,6 +55,10 @@ public class Client extends NativeClient implements TelegramClient {
@Override
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;
stateLock.readLock().lock();
try {
@ -72,6 +78,12 @@ public class Client extends NativeClient implements TelegramClient {
}
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) {
@ -87,6 +99,10 @@ public class Client extends NativeClient implements TelegramClient {
@Override
public Response receive(double timeout, boolean receiveResponses, boolean receiveUpdates) {
responsesLock.readLock().lock();
try {
updatesLock.readLock().lock();
try {
long clientId;
stateLock.readLock().lock();
try {
@ -112,6 +128,12 @@ public class Client extends NativeClient implements TelegramClient {
}
return null;
} finally {
updatesLock.readLock().unlock();
}
} finally {
responsesLock.readLock().unlock();
}
}
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
public void destroyClient() {
responsesLock.writeLock().lock();
try {
updatesLock.writeLock().lock();
try {
stateLock.writeLock().lock();
try {
if (state.isInitialized() && state.hasClientId()) {
@ -199,10 +225,20 @@ public class Client extends NativeClient implements TelegramClient {
} finally {
stateLock.writeLock().unlock();
}
} finally {
updatesLock.writeLock().unlock();
}
} finally {
responsesLock.writeLock().unlock();
}
}
@Override
public void initializeClient() {
responsesLock.writeLock().lock();
try {
updatesLock.writeLock().lock();
try {
stateLock.writeLock().lock();
try {
if (!state.isInitialized() && !state.hasClientId()) {
@ -212,6 +248,12 @@ public class Client extends NativeClient implements TelegramClient {
} finally {
stateLock.writeLock().unlock();
}
} finally {
updatesLock.writeLock().unlock();
}
} finally {
responsesLock.writeLock().unlock();
}
}
private void requireInitialized() {