changed Server_Room::gamesMutex to QReadWriteLock
This commit is contained in:
parent
00029eeeb4
commit
7a30b827c8
7 changed files with 29 additions and 29 deletions
|
@ -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<int, Server_Room *> 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;
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<int, Server_Room *> 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<ServerInfo_Game> 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();
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
#include <google/protobuf/descriptor.h>
|
||||
|
||||
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<Server_Game *> 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<int, Server_Game *> 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<int, Server_Game *> gamesIterator(games);
|
||||
int result = 0;
|
||||
|
@ -279,7 +279,7 @@ int Server_Room::getGamesCreatedByUser(const QString &userName) const
|
|||
|
||||
QList<ServerInfo_Game> Server_Room::getGamesOfUser(const QString &userName) const
|
||||
{
|
||||
QMutexLocker locker(&gamesMutex);
|
||||
QReadLocker locker(&gamesLock);
|
||||
|
||||
QList<ServerInfo_Game> result;
|
||||
QMapIterator<int, Server_Game *> gamesIterator(games);
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue