diff --git a/common/server.cpp b/common/server.cpp index 5c960273..49ed6232 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -273,7 +273,7 @@ void Server::externalUserLeft(const QString &userName) if (!room) continue; - QMutexLocker roomGamesLocker(&room->gamesMutex); + QReadLocker roomGamesLocker(&room->gamesLock); Server_Game *game = room->getGames().value(userGamesIterator.key()); if (!game) continue; @@ -399,7 +399,7 @@ void Server::externalGameCommandContainerReceived(const CommandContainer &cont, throw Response::RespNotInRoom; } - QMutexLocker roomGamesLocker(&room->gamesMutex); + QReadLocker roomGamesLocker(&room->gamesLock); Server_Game *game = room->getGames().value(cont.game_id()); if (!game) { qDebug() << "externalGameCommandContainerReceived: game id=" << cont.game_id() << "not found"; @@ -509,7 +509,7 @@ int Server::getGamesCount() const QMapIterator roomIterator(rooms); while (roomIterator.hasNext()) { Server_Room *room = roomIterator.next().value(); - QMutexLocker roomLocker(&room->gamesMutex); + QReadLocker roomLocker(&room->gamesLock); result += room->getGames().size(); } return result; diff --git a/common/server_abstractuserinterface.cpp b/common/server_abstractuserinterface.cpp index 6ecc1c46..ea4b0f6b 100644 --- a/common/server_abstractuserinterface.cpp +++ b/common/server_abstractuserinterface.cpp @@ -78,7 +78,7 @@ void Server_AbstractUserInterface::joinPersistentGames(ResponseContainer &rc) Server_Room *room = server->getRooms().value(pr.getRoomId()); if (!room) continue; - QMutexLocker roomGamesLocker(&room->gamesMutex); + QReadLocker roomGamesLocker(&room->gamesLock); Server_Game *game = room->getGames().value(pr.getGameId()); if (!game) diff --git a/common/server_game.cpp b/common/server_game.cpp index e2694d23..94eb03ae 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -91,7 +91,7 @@ Server_Game::Server_Game(const ServerInfo_User &_creatorInfo, int _gameId, const Server_Game::~Server_Game() { - room->gamesMutex.lock(); + room->gamesLock.lockForWrite(); gameMutex.lock(); gameClosed = true; @@ -107,7 +107,7 @@ Server_Game::~Server_Game() creatorInfo = 0; gameMutex.unlock(); - room->gamesMutex.unlock(); + room->gamesLock.unlock(); currentReplay->set_duration_seconds(secondsElapsed - startTimeOfThisGame); replayList.append(currentReplay); diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index c33a5545..00cb05b1 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -60,24 +60,24 @@ void Server_ProtocolHandler::prepareDestroy() Server_Room *r = server->getRooms().value(gameIterator.value().first); if (!r) continue; - r->gamesMutex.lock(); + r->gamesLock.lockForRead(); Server_Game *g = r->getGames().value(gameIterator.key()); if (!g) { - r->gamesMutex.unlock(); + r->gamesLock.unlock(); continue; } g->gameMutex.lock(); Server_Player *p = g->getPlayers().value(gameIterator.value().second); if (!p) { g->gameMutex.unlock(); - r->gamesMutex.unlock(); + r->gamesLock.unlock(); continue; } p->disconnectClient(); g->gameMutex.unlock(); - r->gamesMutex.unlock(); + r->gamesLock.unlock(); } server->roomsLock.unlock(); @@ -197,7 +197,7 @@ Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const if (!room) return Response::RespNotInRoom; - QMutexLocker roomGamesLocker(&room->gamesMutex); + QReadLocker roomGamesLocker(&room->gamesLock); Server_Game *game = room->getGames().value(cont.game_id()); if (!game) { if (room->getExternalGames().contains(cont.game_id())) { @@ -410,12 +410,12 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_G QMapIterator roomIterator(server->getRooms()); while (roomIterator.hasNext()) { Server_Room *room = roomIterator.next().value(); - room->gamesMutex.lock(); + room->gamesLock.lockForRead(); room->getInfo(*re->add_room_list(), false, true); QListIterator gameIterator(room->getGamesOfUser(QString::fromStdString(cmd.user_name()))); while (gameIterator.hasNext()) re->add_game_list()->CopyFrom(gameIterator.next()); - room->gamesMutex.unlock(); + room->gamesLock.unlock(); } server->roomsLock.unlock(); diff --git a/common/server_room.cpp b/common/server_room.cpp index 43b1b54a..6c37e0e9 100644 --- a/common/server_room.cpp +++ b/common/server_room.cpp @@ -13,7 +13,7 @@ #include Server_Room::Server_Room(int _id, const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage, const QStringList &_gameTypes, Server *parent) - : QObject(parent), id(_id), name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage), gameTypes(_gameTypes), gamesMutex(QMutex::Recursive) + : QObject(parent), id(_id), name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage), gameTypes(_gameTypes), gamesLock(QReadWriteLock::Recursive) { connect(this, SIGNAL(gameListChanged(ServerInfo_Game)), this, SLOT(broadcastGameListUpdate(ServerInfo_Game)), Qt::QueuedConnection); } @@ -22,12 +22,12 @@ Server_Room::~Server_Room() { qDebug("Server_Room destructor"); - gamesMutex.lock(); + gamesLock.lockForWrite(); const QList gameList = games.values(); for (int i = 0; i < gameList.size(); ++i) delete gameList[i]; games.clear(); - gamesMutex.unlock(); + gamesLock.unlock(); usersLock.lockForWrite(); users.clear(); @@ -49,7 +49,7 @@ const ServerInfo_Room &Server_Room::getInfo(ServerInfo_Room &result, bool comple result.set_auto_join(autoJoin); } - gamesMutex.lock(); + gamesLock.lockForRead(); result.set_game_count(games.size() + externalGames.size()); if (complete) { QMapIterator gameIterator(games); @@ -61,7 +61,7 @@ const ServerInfo_Room &Server_Room::getInfo(ServerInfo_Room &result, bool comple result.add_game_list()->CopyFrom(externalGameIterator.next().value()); } } - gamesMutex.unlock(); + gamesLock.unlock(); usersLock.lockForRead(); result.set_player_count(users.size() + externalUsers.size()); @@ -158,12 +158,12 @@ void Server_Room::removeExternalUser(const QString &name) void Server_Room::updateExternalGameList(const ServerInfo_Game &gameInfo) { // This function is always called from the Server thread with server->roomsMutex locked. - gamesMutex.lock(); + gamesLock.lockForWrite(); if (!gameInfo.has_player_count() && externalGames.contains(gameInfo.game_id())) externalGames.remove(gameInfo.game_id()); else externalGames.insert(gameInfo.game_id(), gameInfo); - gamesMutex.unlock(); + gamesLock.unlock(); broadcastGameListUpdate(gameInfo, false); ServerInfo_Room roomInfo; @@ -175,7 +175,7 @@ Response::ResponseCode Server_Room::processJoinGameCommand(const Command_JoinGam // This function is called from the Server thread and from the S_PH thread. // server->roomsMutex is always locked. - QMutexLocker roomGamesLocker(&gamesMutex); + QReadLocker roomGamesLocker(&gamesLock); Server_Game *g = games.value(cmd.game_id()); if (!g) { if (externalGames.contains(cmd.game_id())) { @@ -233,7 +233,7 @@ void Server_Room::broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool void Server_Room::addGame(Server_Game *game) { - gamesMutex.lock(); + gamesLock.lockForWrite(); connect(game, SIGNAL(gameInfoChanged(ServerInfo_Game)), this, SLOT(broadcastGameListUpdate(ServerInfo_Game))); game->gameMutex.lock(); @@ -241,7 +241,7 @@ void Server_Room::addGame(Server_Game *game) ServerInfo_Game gameInfo; game->getInfo(gameInfo); game->gameMutex.unlock(); - gamesMutex.unlock(); + gamesLock.unlock(); emit gameListChanged(gameInfo); ServerInfo_Room roomInfo; @@ -250,7 +250,7 @@ void Server_Room::addGame(Server_Game *game) void Server_Room::removeGame(Server_Game *game) { - // No need to lock gamesMutex or gameMutex. This method is only + // No need to lock gamesLock or gameMutex. This method is only // called from ~Server_Game, which locks both mutexes anyway beforehand. disconnect(game, 0, this, 0); @@ -267,7 +267,7 @@ void Server_Room::removeGame(Server_Game *game) int Server_Room::getGamesCreatedByUser(const QString &userName) const { - QMutexLocker locker(&gamesMutex); + QReadLocker locker(&gamesLock); QMapIterator gamesIterator(games); int result = 0; @@ -279,7 +279,7 @@ int Server_Room::getGamesCreatedByUser(const QString &userName) const QList Server_Room::getGamesOfUser(const QString &userName) const { - QMutexLocker locker(&gamesMutex); + QReadLocker locker(&gamesLock); QList result; QMapIterator gamesIterator(games); diff --git a/common/server_room.h b/common/server_room.h index bd15982d..a39a8100 100644 --- a/common/server_room.h +++ b/common/server_room.h @@ -43,7 +43,7 @@ private slots: void broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool sendToIsl = true); public: mutable QReadWriteLock usersLock; - mutable QMutex gamesMutex; + mutable QReadWriteLock gamesLock; Server_Room(int _id, const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage, const QStringList &_gameTypes, Server *parent); ~Server_Room(); int getId() const { return id; } diff --git a/servatrice/src/isl_interface.cpp b/servatrice/src/isl_interface.cpp index 52968422..5cede78a 100644 --- a/servatrice/src/isl_interface.cpp +++ b/servatrice/src/isl_interface.cpp @@ -128,7 +128,7 @@ void IslInterface::initServer() while (roomIterator.hasNext()) { Server_Room *room = roomIterator.next().value(); room->usersLock.lockForRead(); - room->gamesMutex.lock(); + room->gamesLock.lockForRead(); room->getInfo(*event.add_room_list(), true, true, false, false); } @@ -150,7 +150,7 @@ void IslInterface::initServer() roomIterator.toFront(); while (roomIterator.hasNext()) { roomIterator.next(); - roomIterator.value()->gamesMutex.unlock(); + roomIterator.value()->gamesLock.unlock(); roomIterator.value()->usersLock.unlock(); } server->roomsLock.unlock();