Merge pull request #167 from monich/nomutex

Removed mutex from ChatModel
This commit is contained in:
Sebastian Wolf 2020-11-17 20:48:51 +01:00 committed by GitHub
commit 2a88328e57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 13 deletions

View file

@ -22,6 +22,7 @@
#include <QListIterator> #include <QListIterator>
#include <QByteArray> #include <QByteArray>
#include <QBitArray> #include <QBitArray>
#include <QDebug>
#define LOG(x) qDebug() << "[ChatModel]" << x #define LOG(x) qDebug() << "[ChatModel]" << x
@ -178,7 +179,6 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo
} }
} else { } else {
if (this->isMostRecentMessageLoaded() || this->inIncrementalUpdate) { if (this->isMostRecentMessageLoaded() || this->inIncrementalUpdate) {
this->messagesMutex.lock();
this->messagesToBeAdded.clear(); this->messagesToBeAdded.clear();
QListIterator<QVariant> messagesIterator(messages); QListIterator<QVariant> messagesIterator(messages);
while (messagesIterator.hasNext()) { while (messagesIterator.hasNext()) {
@ -194,7 +194,6 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo
if (!this->messagesToBeAdded.isEmpty()) { if (!this->messagesToBeAdded.isEmpty()) {
this->insertMessages(); this->insertMessages();
} }
this->messagesMutex.unlock();
// First call only returns a few messages, we need to get a little more than that... // First call only returns a few messages, we need to get a little more than that...
if (!this->messagesToBeAdded.isEmpty() && (this->messagesToBeAdded.size() + this->messages.size()) < 10 && !this->inReload) { if (!this->messagesToBeAdded.isEmpty() && (this->messagesToBeAdded.size() + this->messages.size()) < 10 && !this->inReload) {
@ -228,13 +227,11 @@ void ChatModel::handleNewMessageReceived(const QString &id, const QVariantMap &m
if (id.toLongLong() == chatId && !this->messageIndexMap.contains(id)) { if (id.toLongLong() == chatId && !this->messageIndexMap.contains(id)) {
if (this->isMostRecentMessageLoaded()) { if (this->isMostRecentMessageLoaded()) {
LOG("New message received for this chat"); LOG("New message received for this chat");
this->messagesMutex.lock();
this->messagesToBeAdded.clear(); this->messagesToBeAdded.clear();
this->messagesToBeAdded.append(message); this->messagesToBeAdded.append(message);
this->insertMessages(); this->insertMessages();
this->messagesMutex.unlock();
emit newMessageReceived(message); emit newMessageReceived(message);
} else { } else {
LOG("New message in this chat, but not relevant as less recent messages need to be loaded first!"); LOG("New message in this chat, but not relevant as less recent messages need to be loaded first!");
@ -267,13 +264,11 @@ void ChatModel::handleMessageSendSucceeded(const QString &messageId, const QStri
LOG("Message send succeeded, new message ID" << messageId << "old message ID" << oldMessageId << ", chat ID" << message.value(CHAT_ID).toString()); LOG("Message send succeeded, new message ID" << messageId << "old message ID" << oldMessageId << ", chat ID" << message.value(CHAT_ID).toString());
LOG("index map:" << messageIndexMap.contains(oldMessageId) << ", index count:" << messageIndexMap.size() << ", message count:" << messages.size()); LOG("index map:" << messageIndexMap.contains(oldMessageId) << ", index count:" << messageIndexMap.size() << ", message count:" << messages.size());
if (this->messageIndexMap.contains(oldMessageId)) { if (this->messageIndexMap.contains(oldMessageId)) {
this->messagesMutex.lock();
LOG("Message was successfully sent" << oldMessageId); LOG("Message was successfully sent" << oldMessageId);
int messageIndex = this->messageIndexMap.value(oldMessageId).toInt(); int messageIndex = this->messageIndexMap.value(oldMessageId).toInt();
this->messages.replace(messageIndex, message); this->messages.replace(messageIndex, message);
this->calculateMessageIndexMap(); this->calculateMessageIndexMap();
LOG("Message was replaced at index" << messageIndex); LOG("Message was replaced at index" << messageIndex);
this->messagesMutex.unlock();
emit dataChanged(index(messageIndex), index(messageIndex)); emit dataChanged(index(messageIndex), index(messageIndex));
emit lastReadSentMessageUpdated(calculateLastReadSentMessageId()); emit lastReadSentMessageUpdated(calculateLastReadSentMessageId());
} }
@ -301,7 +296,6 @@ void ChatModel::handleMessageContentUpdated(const QString &id, const QString &me
{ {
LOG("Message content updated" << id << messageId); LOG("Message content updated" << id << messageId);
if (id.toLongLong() == chatId && messageIndexMap.contains(messageId)) { if (id.toLongLong() == chatId && messageIndexMap.contains(messageId)) {
this->messagesMutex.lock();
LOG("We know the message that was updated " << messageId); LOG("We know the message that was updated " << messageId);
int messageIndex = this->messageIndexMap.value(messageId).toInt(); int messageIndex = this->messageIndexMap.value(messageId).toInt();
QVariantMap messageToBeUpdated = this->messages.at(messageIndex).toMap(); QVariantMap messageToBeUpdated = this->messages.at(messageIndex).toMap();
@ -309,7 +303,6 @@ void ChatModel::handleMessageContentUpdated(const QString &id, const QString &me
this->messages.replace(messageIndex, messageToBeUpdated); this->messages.replace(messageIndex, messageToBeUpdated);
this->calculateMessageIndexMap(); this->calculateMessageIndexMap();
LOG("Message was replaced at index" << messageIndex); LOG("Message was replaced at index" << messageIndex);
this->messagesMutex.unlock();
emit messageUpdated(messageIndex); emit messageUpdated(messageIndex);
emit dataChanged(index(messageIndex), index(messageIndex)); emit dataChanged(index(messageIndex), index(messageIndex));
} }
@ -319,7 +312,6 @@ void ChatModel::handleMessagesDeleted(const QString &id, const QVariantList &mes
{ {
LOG("Messages were deleted in a chat" << id); LOG("Messages were deleted in a chat" << id);
if (id.toLongLong() == chatId) { if (id.toLongLong() == chatId) {
this->messagesMutex.lock();
LOG("Messages in this chat were deleted..."); LOG("Messages in this chat were deleted...");
QListIterator<QVariant> messageIdIterator(messageIds); QListIterator<QVariant> messageIdIterator(messageIds);
while (messageIdIterator.hasNext()) { while (messageIdIterator.hasNext()) {
@ -333,7 +325,6 @@ void ChatModel::handleMessagesDeleted(const QString &id, const QVariantList &mes
endRemoveRows(); endRemoveRows();
} }
} }
this->messagesMutex.unlock();
emit messagesDeleted(); emit messagesDeleted();
} }
} }

View file

@ -21,8 +21,6 @@
#define CHATMODEL_H #define CHATMODEL_H
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QDebug>
#include <QMutex>
#include "tdlibwrapper.h" #include "tdlibwrapper.h"
class ChatModel : public QAbstractListModel class ChatModel : public QAbstractListModel
@ -74,7 +72,6 @@ private:
QVariantList messages; QVariantList messages;
QVariantList messagesToBeAdded; QVariantList messagesToBeAdded;
QVariantMap messageIndexMap; QVariantMap messageIndexMap;
QMutex messagesMutex;
QVariantMap chatInformation; QVariantMap chatInformation;
qlonglong chatId; qlonglong chatId;
bool inReload; bool inReload;