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)
|
if (!room)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
QMutexLocker roomGamesLocker(&room->gamesMutex);
|
QReadLocker roomGamesLocker(&room->gamesLock);
|
||||||
Server_Game *game = room->getGames().value(userGamesIterator.key());
|
Server_Game *game = room->getGames().value(userGamesIterator.key());
|
||||||
if (!game)
|
if (!game)
|
||||||
continue;
|
continue;
|
||||||
|
@ -399,7 +399,7 @@ void Server::externalGameCommandContainerReceived(const CommandContainer &cont,
|
||||||
throw Response::RespNotInRoom;
|
throw Response::RespNotInRoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
QMutexLocker roomGamesLocker(&room->gamesMutex);
|
QReadLocker roomGamesLocker(&room->gamesLock);
|
||||||
Server_Game *game = room->getGames().value(cont.game_id());
|
Server_Game *game = room->getGames().value(cont.game_id());
|
||||||
if (!game) {
|
if (!game) {
|
||||||
qDebug() << "externalGameCommandContainerReceived: game id=" << cont.game_id() << "not found";
|
qDebug() << "externalGameCommandContainerReceived: game id=" << cont.game_id() << "not found";
|
||||||
|
@ -509,7 +509,7 @@ int Server::getGamesCount() const
|
||||||
QMapIterator<int, Server_Room *> roomIterator(rooms);
|
QMapIterator<int, Server_Room *> roomIterator(rooms);
|
||||||
while (roomIterator.hasNext()) {
|
while (roomIterator.hasNext()) {
|
||||||
Server_Room *room = roomIterator.next().value();
|
Server_Room *room = roomIterator.next().value();
|
||||||
QMutexLocker roomLocker(&room->gamesMutex);
|
QReadLocker roomLocker(&room->gamesLock);
|
||||||
result += room->getGames().size();
|
result += room->getGames().size();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -78,7 +78,7 @@ void Server_AbstractUserInterface::joinPersistentGames(ResponseContainer &rc)
|
||||||
Server_Room *room = server->getRooms().value(pr.getRoomId());
|
Server_Room *room = server->getRooms().value(pr.getRoomId());
|
||||||
if (!room)
|
if (!room)
|
||||||
continue;
|
continue;
|
||||||
QMutexLocker roomGamesLocker(&room->gamesMutex);
|
QReadLocker roomGamesLocker(&room->gamesLock);
|
||||||
|
|
||||||
Server_Game *game = room->getGames().value(pr.getGameId());
|
Server_Game *game = room->getGames().value(pr.getGameId());
|
||||||
if (!game)
|
if (!game)
|
||||||
|
|
|
@ -91,7 +91,7 @@ Server_Game::Server_Game(const ServerInfo_User &_creatorInfo, int _gameId, const
|
||||||
|
|
||||||
Server_Game::~Server_Game()
|
Server_Game::~Server_Game()
|
||||||
{
|
{
|
||||||
room->gamesMutex.lock();
|
room->gamesLock.lockForWrite();
|
||||||
gameMutex.lock();
|
gameMutex.lock();
|
||||||
|
|
||||||
gameClosed = true;
|
gameClosed = true;
|
||||||
|
@ -107,7 +107,7 @@ Server_Game::~Server_Game()
|
||||||
creatorInfo = 0;
|
creatorInfo = 0;
|
||||||
|
|
||||||
gameMutex.unlock();
|
gameMutex.unlock();
|
||||||
room->gamesMutex.unlock();
|
room->gamesLock.unlock();
|
||||||
|
|
||||||
currentReplay->set_duration_seconds(secondsElapsed - startTimeOfThisGame);
|
currentReplay->set_duration_seconds(secondsElapsed - startTimeOfThisGame);
|
||||||
replayList.append(currentReplay);
|
replayList.append(currentReplay);
|
||||||
|
|
|
@ -60,24 +60,24 @@ void Server_ProtocolHandler::prepareDestroy()
|
||||||
Server_Room *r = server->getRooms().value(gameIterator.value().first);
|
Server_Room *r = server->getRooms().value(gameIterator.value().first);
|
||||||
if (!r)
|
if (!r)
|
||||||
continue;
|
continue;
|
||||||
r->gamesMutex.lock();
|
r->gamesLock.lockForRead();
|
||||||
Server_Game *g = r->getGames().value(gameIterator.key());
|
Server_Game *g = r->getGames().value(gameIterator.key());
|
||||||
if (!g) {
|
if (!g) {
|
||||||
r->gamesMutex.unlock();
|
r->gamesLock.unlock();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
g->gameMutex.lock();
|
g->gameMutex.lock();
|
||||||
Server_Player *p = g->getPlayers().value(gameIterator.value().second);
|
Server_Player *p = g->getPlayers().value(gameIterator.value().second);
|
||||||
if (!p) {
|
if (!p) {
|
||||||
g->gameMutex.unlock();
|
g->gameMutex.unlock();
|
||||||
r->gamesMutex.unlock();
|
r->gamesLock.unlock();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
p->disconnectClient();
|
p->disconnectClient();
|
||||||
|
|
||||||
g->gameMutex.unlock();
|
g->gameMutex.unlock();
|
||||||
r->gamesMutex.unlock();
|
r->gamesLock.unlock();
|
||||||
}
|
}
|
||||||
server->roomsLock.unlock();
|
server->roomsLock.unlock();
|
||||||
|
|
||||||
|
@ -197,7 +197,7 @@ Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const
|
||||||
if (!room)
|
if (!room)
|
||||||
return Response::RespNotInRoom;
|
return Response::RespNotInRoom;
|
||||||
|
|
||||||
QMutexLocker roomGamesLocker(&room->gamesMutex);
|
QReadLocker roomGamesLocker(&room->gamesLock);
|
||||||
Server_Game *game = room->getGames().value(cont.game_id());
|
Server_Game *game = room->getGames().value(cont.game_id());
|
||||||
if (!game) {
|
if (!game) {
|
||||||
if (room->getExternalGames().contains(cont.game_id())) {
|
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());
|
QMapIterator<int, Server_Room *> roomIterator(server->getRooms());
|
||||||
while (roomIterator.hasNext()) {
|
while (roomIterator.hasNext()) {
|
||||||
Server_Room *room = roomIterator.next().value();
|
Server_Room *room = roomIterator.next().value();
|
||||||
room->gamesMutex.lock();
|
room->gamesLock.lockForRead();
|
||||||
room->getInfo(*re->add_room_list(), false, true);
|
room->getInfo(*re->add_room_list(), false, true);
|
||||||
QListIterator<ServerInfo_Game> gameIterator(room->getGamesOfUser(QString::fromStdString(cmd.user_name())));
|
QListIterator<ServerInfo_Game> gameIterator(room->getGamesOfUser(QString::fromStdString(cmd.user_name())));
|
||||||
while (gameIterator.hasNext())
|
while (gameIterator.hasNext())
|
||||||
re->add_game_list()->CopyFrom(gameIterator.next());
|
re->add_game_list()->CopyFrom(gameIterator.next());
|
||||||
room->gamesMutex.unlock();
|
room->gamesLock.unlock();
|
||||||
}
|
}
|
||||||
server->roomsLock.unlock();
|
server->roomsLock.unlock();
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
#include <google/protobuf/descriptor.h>
|
#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)
|
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);
|
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");
|
qDebug("Server_Room destructor");
|
||||||
|
|
||||||
gamesMutex.lock();
|
gamesLock.lockForWrite();
|
||||||
const QList<Server_Game *> gameList = games.values();
|
const QList<Server_Game *> gameList = games.values();
|
||||||
for (int i = 0; i < gameList.size(); ++i)
|
for (int i = 0; i < gameList.size(); ++i)
|
||||||
delete gameList[i];
|
delete gameList[i];
|
||||||
games.clear();
|
games.clear();
|
||||||
gamesMutex.unlock();
|
gamesLock.unlock();
|
||||||
|
|
||||||
usersLock.lockForWrite();
|
usersLock.lockForWrite();
|
||||||
users.clear();
|
users.clear();
|
||||||
|
@ -49,7 +49,7 @@ const ServerInfo_Room &Server_Room::getInfo(ServerInfo_Room &result, bool comple
|
||||||
result.set_auto_join(autoJoin);
|
result.set_auto_join(autoJoin);
|
||||||
}
|
}
|
||||||
|
|
||||||
gamesMutex.lock();
|
gamesLock.lockForRead();
|
||||||
result.set_game_count(games.size() + externalGames.size());
|
result.set_game_count(games.size() + externalGames.size());
|
||||||
if (complete) {
|
if (complete) {
|
||||||
QMapIterator<int, Server_Game *> gameIterator(games);
|
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());
|
result.add_game_list()->CopyFrom(externalGameIterator.next().value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gamesMutex.unlock();
|
gamesLock.unlock();
|
||||||
|
|
||||||
usersLock.lockForRead();
|
usersLock.lockForRead();
|
||||||
result.set_player_count(users.size() + externalUsers.size());
|
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)
|
void Server_Room::updateExternalGameList(const ServerInfo_Game &gameInfo)
|
||||||
{
|
{
|
||||||
// This function is always called from the Server thread with server->roomsMutex locked.
|
// 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()))
|
if (!gameInfo.has_player_count() && externalGames.contains(gameInfo.game_id()))
|
||||||
externalGames.remove(gameInfo.game_id());
|
externalGames.remove(gameInfo.game_id());
|
||||||
else
|
else
|
||||||
externalGames.insert(gameInfo.game_id(), gameInfo);
|
externalGames.insert(gameInfo.game_id(), gameInfo);
|
||||||
gamesMutex.unlock();
|
gamesLock.unlock();
|
||||||
|
|
||||||
broadcastGameListUpdate(gameInfo, false);
|
broadcastGameListUpdate(gameInfo, false);
|
||||||
ServerInfo_Room roomInfo;
|
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.
|
// This function is called from the Server thread and from the S_PH thread.
|
||||||
// server->roomsMutex is always locked.
|
// server->roomsMutex is always locked.
|
||||||
|
|
||||||
QMutexLocker roomGamesLocker(&gamesMutex);
|
QReadLocker roomGamesLocker(&gamesLock);
|
||||||
Server_Game *g = games.value(cmd.game_id());
|
Server_Game *g = games.value(cmd.game_id());
|
||||||
if (!g) {
|
if (!g) {
|
||||||
if (externalGames.contains(cmd.game_id())) {
|
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)
|
void Server_Room::addGame(Server_Game *game)
|
||||||
{
|
{
|
||||||
gamesMutex.lock();
|
gamesLock.lockForWrite();
|
||||||
connect(game, SIGNAL(gameInfoChanged(ServerInfo_Game)), this, SLOT(broadcastGameListUpdate(ServerInfo_Game)));
|
connect(game, SIGNAL(gameInfoChanged(ServerInfo_Game)), this, SLOT(broadcastGameListUpdate(ServerInfo_Game)));
|
||||||
|
|
||||||
game->gameMutex.lock();
|
game->gameMutex.lock();
|
||||||
|
@ -241,7 +241,7 @@ void Server_Room::addGame(Server_Game *game)
|
||||||
ServerInfo_Game gameInfo;
|
ServerInfo_Game gameInfo;
|
||||||
game->getInfo(gameInfo);
|
game->getInfo(gameInfo);
|
||||||
game->gameMutex.unlock();
|
game->gameMutex.unlock();
|
||||||
gamesMutex.unlock();
|
gamesLock.unlock();
|
||||||
|
|
||||||
emit gameListChanged(gameInfo);
|
emit gameListChanged(gameInfo);
|
||||||
ServerInfo_Room roomInfo;
|
ServerInfo_Room roomInfo;
|
||||||
|
@ -250,7 +250,7 @@ void Server_Room::addGame(Server_Game *game)
|
||||||
|
|
||||||
void Server_Room::removeGame(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.
|
// called from ~Server_Game, which locks both mutexes anyway beforehand.
|
||||||
|
|
||||||
disconnect(game, 0, this, 0);
|
disconnect(game, 0, this, 0);
|
||||||
|
@ -267,7 +267,7 @@ void Server_Room::removeGame(Server_Game *game)
|
||||||
|
|
||||||
int Server_Room::getGamesCreatedByUser(const QString &userName) const
|
int Server_Room::getGamesCreatedByUser(const QString &userName) const
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&gamesMutex);
|
QReadLocker locker(&gamesLock);
|
||||||
|
|
||||||
QMapIterator<int, Server_Game *> gamesIterator(games);
|
QMapIterator<int, Server_Game *> gamesIterator(games);
|
||||||
int result = 0;
|
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
|
QList<ServerInfo_Game> Server_Room::getGamesOfUser(const QString &userName) const
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&gamesMutex);
|
QReadLocker locker(&gamesLock);
|
||||||
|
|
||||||
QList<ServerInfo_Game> result;
|
QList<ServerInfo_Game> result;
|
||||||
QMapIterator<int, Server_Game *> gamesIterator(games);
|
QMapIterator<int, Server_Game *> gamesIterator(games);
|
||||||
|
|
|
@ -43,7 +43,7 @@ private slots:
|
||||||
void broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool sendToIsl = true);
|
void broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool sendToIsl = true);
|
||||||
public:
|
public:
|
||||||
mutable QReadWriteLock usersLock;
|
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 _id, const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage, const QStringList &_gameTypes, Server *parent);
|
||||||
~Server_Room();
|
~Server_Room();
|
||||||
int getId() const { return id; }
|
int getId() const { return id; }
|
||||||
|
|
|
@ -128,7 +128,7 @@ void IslInterface::initServer()
|
||||||
while (roomIterator.hasNext()) {
|
while (roomIterator.hasNext()) {
|
||||||
Server_Room *room = roomIterator.next().value();
|
Server_Room *room = roomIterator.next().value();
|
||||||
room->usersLock.lockForRead();
|
room->usersLock.lockForRead();
|
||||||
room->gamesMutex.lock();
|
room->gamesLock.lockForRead();
|
||||||
room->getInfo(*event.add_room_list(), true, true, false, false);
|
room->getInfo(*event.add_room_list(), true, true, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ void IslInterface::initServer()
|
||||||
roomIterator.toFront();
|
roomIterator.toFront();
|
||||||
while (roomIterator.hasNext()) {
|
while (roomIterator.hasNext()) {
|
||||||
roomIterator.next();
|
roomIterator.next();
|
||||||
roomIterator.value()->gamesMutex.unlock();
|
roomIterator.value()->gamesLock.unlock();
|
||||||
roomIterator.value()->usersLock.unlock();
|
roomIterator.value()->usersLock.unlock();
|
||||||
}
|
}
|
||||||
server->roomsLock.unlock();
|
server->roomsLock.unlock();
|
||||||
|
|
Loading…
Reference in a new issue