From 2a7456998ae8389deb473aa3beb9b834575d32cf Mon Sep 17 00:00:00 2001 From: lotodore Date: Wed, 12 Jan 2011 23:35:45 +0000 Subject: [PATCH] Improve thread safety. --- src/engine/handinterface.h | 16 ++++++++++++---- src/engine/local_engine/localhand.h | 8 ++++---- src/engine/network_engine/clientbero.cpp | 10 ++++++---- src/engine/network_engine/clientboard.cpp | 9 +-------- src/engine/network_engine/clientboard.h | 11 ++++------- src/engine/network_engine/clienthand.cpp | 12 ++++++++++-- src/engine/network_engine/clienthand.h | 11 ++++++----- src/engine/network_engine/clientplayer.cpp | 8 ++++---- src/net/common/clientstate.cpp | 3 +++ src/net/common/clientthread.cpp | 2 +- 10 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/engine/handinterface.h b/src/engine/handinterface.h index cb2b7cce..b83110a0 100644 --- a/src/engine/handinterface.h +++ b/src/engine/handinterface.h @@ -36,10 +36,6 @@ public: virtual PlayerList getActivePlayerList() const =0; virtual PlayerList getRunningPlayerList() const =0; - virtual PlayerListIterator getSeatIt(unsigned) const =0; - virtual PlayerListIterator getActivePlayerIt(unsigned) const =0; - virtual PlayerListIterator getRunningPlayerIt(unsigned) const =0; - virtual boost::shared_ptr getBoard() const =0; virtual boost::shared_ptr getPreflop() const =0; virtual boost::shared_ptr getFlop() const =0; @@ -83,6 +79,18 @@ public: virtual void switchRounds() =0; +protected: + virtual PlayerListIterator getSeatIt(unsigned) const =0; + virtual PlayerListIterator getActivePlayerIt(unsigned) const =0; + virtual PlayerListIterator getRunningPlayerIt(unsigned) const =0; + +friend class Game; +friend class LocalBeRo; +friend class LocalBeRoPreflop; +friend class LocalBeRoFlop; +friend class LocalBeRoTurn; +friend class LocalBeRoRiver; +friend class LocalBeRoPostRiver; }; #endif diff --git a/src/engine/local_engine/localhand.h b/src/engine/local_engine/localhand.h index 48f78c31..4734ca44 100755 --- a/src/engine/local_engine/localhand.h +++ b/src/engine/local_engine/localhand.h @@ -42,10 +42,6 @@ public: PlayerList getActivePlayerList() const {return activePlayerList;} PlayerList getRunningPlayerList() const {return runningPlayerList;} - PlayerListIterator getSeatIt(unsigned) const; - PlayerListIterator getActivePlayerIt(unsigned) const; - PlayerListIterator getRunningPlayerIt(unsigned) const; - boost::shared_ptr getBoard() const { return myBoard; } boost::shared_ptr getPreflop() const { return myBeRo[GAME_STATE_PREFLOP]; } boost::shared_ptr getFlop() const { return myBeRo[GAME_STATE_FLOP]; } @@ -91,6 +87,10 @@ public: void switchRounds(); +protected: + PlayerListIterator getSeatIt(unsigned) const; + PlayerListIterator getActivePlayerIt(unsigned) const; + PlayerListIterator getRunningPlayerIt(unsigned) const; private: diff --git a/src/engine/network_engine/clientbero.cpp b/src/engine/network_engine/clientbero.cpp index dbaa9d64..78ed3988 100644 --- a/src/engine/network_engine/clientbero.cpp +++ b/src/engine/network_engine/clientbero.cpp @@ -103,6 +103,7 @@ ClientBeRo::setSmallBlindPositionId(unsigned theValue) unsigned ClientBeRo::getSmallBlindPositionId() const { + boost::recursive_mutex::scoped_lock lock(m_syncMutex); return smallBlindPositionId; } @@ -116,6 +117,7 @@ ClientBeRo::setBigBlindPositionId(unsigned theValue) unsigned ClientBeRo::getBigBlindPositionId() const { + boost::recursive_mutex::scoped_lock lock(m_syncMutex); return bigBlindPositionId; } @@ -220,15 +222,15 @@ ClientBeRo::getMinimumRaise() const void ClientBeRo::setFullBetRule ( bool theValue ) { - boost::recursive_mutex::scoped_lock lock(m_syncMutex); - fullBetRule = theValue; + boost::recursive_mutex::scoped_lock lock(m_syncMutex); + fullBetRule = theValue; } bool ClientBeRo::getFullBetRule() const { - boost::recursive_mutex::scoped_lock lock(m_syncMutex); - return fullBetRule; + boost::recursive_mutex::scoped_lock lock(m_syncMutex); + return fullBetRule; } void diff --git a/src/engine/network_engine/clientboard.cpp b/src/engine/network_engine/clientboard.cpp index 36a24616..e1f61692 100644 --- a/src/engine/network_engine/clientboard.cpp +++ b/src/engine/network_engine/clientboard.cpp @@ -24,7 +24,7 @@ using namespace std; ClientBoard::ClientBoard(unsigned dp) -: currentHand(0), pot(0), sets(0), dealerPosition(dp) +: pot(0), sets(0), dealerPosition(dp) { myCards[0] = myCards[1] = myCards[2] = myCards[3] = myCards[4] = 0; } @@ -43,13 +43,6 @@ ClientBoard::setPlayerLists(PlayerList sl, PlayerList apl, PlayerList rpl) runningPlayerList = rpl; } -void -ClientBoard::setHand(HandInterface* br) -{ - boost::recursive_mutex::scoped_lock lock(m_syncMutex); - currentHand = br; -} - void ClientBoard::setMyCards(int* theValue) { diff --git a/src/engine/network_engine/clientboard.h b/src/engine/network_engine/clientboard.h index c3aec2bd..fbb19c6d 100644 --- a/src/engine/network_engine/clientboard.h +++ b/src/engine/network_engine/clientboard.h @@ -35,7 +35,6 @@ public: ~ClientBoard(); void setPlayerLists(PlayerList, PlayerList, PlayerList); - void setHand(HandInterface*); void setMyCards(int* theValue); void getMyCards(int* theValue); @@ -52,13 +51,13 @@ public: void collectPot(); void distributePot(); - void determinePlayerNeedToShowCards(); + void determinePlayerNeedToShowCards(); std::list getWinners() const; void setWinners(const std::list &winners); - std::list getPlayerNeedToShowCards() const; - void setPlayerNeedToShowCards(const std::list &playerNeedToShowCards); + std::list getPlayerNeedToShowCards() const; + void setPlayerNeedToShowCards(const std::list &playerNeedToShowCards); private: mutable boost::recursive_mutex m_syncMutex; @@ -67,10 +66,8 @@ private: PlayerList activePlayerList; PlayerList runningPlayerList; - HandInterface *currentHand; - std::list winners; - std::list playerNeedToShowCards; + std::list playerNeedToShowCards; int myCards[5]; int pot; diff --git a/src/engine/network_engine/clienthand.cpp b/src/engine/network_engine/clienthand.cpp index 0036dff4..4a8b8081 100644 --- a/src/engine/network_engine/clienthand.cpp +++ b/src/engine/network_engine/clienthand.cpp @@ -68,24 +68,28 @@ ClientHand::start() PlayerList ClientHand::getSeatsList() const { + boost::recursive_mutex::scoped_lock lock(m_syncMutex); return seatsList; } PlayerList ClientHand::getActivePlayerList() const { + boost::recursive_mutex::scoped_lock lock(m_syncMutex); return activePlayerList; } PlayerList ClientHand::getRunningPlayerList() const { + boost::recursive_mutex::scoped_lock lock(m_syncMutex); return runningPlayerList; } PlayerListIterator ClientHand::getSeatIt(unsigned uniqueId) const { + boost::recursive_mutex::scoped_lock lock(m_syncMutex); PlayerListIterator it; for(it=seatsList->begin(); it!=seatsList->end(); it++) { @@ -100,6 +104,7 @@ ClientHand::getSeatIt(unsigned uniqueId) const PlayerListIterator ClientHand::getActivePlayerIt(unsigned uniqueId) const { + boost::recursive_mutex::scoped_lock lock(m_syncMutex); PlayerListIterator it; for(it=activePlayerList->begin(); it!=activePlayerList->end(); it++) { @@ -114,6 +119,7 @@ ClientHand::getActivePlayerIt(unsigned uniqueId) const PlayerListIterator ClientHand::getRunningPlayerIt(unsigned uniqueId) const { + boost::recursive_mutex::scoped_lock lock(m_syncMutex); PlayerListIterator it; for(it=runningPlayerList->begin(); it!=runningPlayerList->end(); it++) { @@ -297,13 +303,15 @@ ClientHand::getLastPlayersTurn() const void ClientHand::setLastActionPlayer (unsigned theValue) { - lastActionPlayer = theValue; + boost::recursive_mutex::scoped_lock lock(m_syncMutex); + lastActionPlayer = theValue; } unsigned ClientHand::getLastActionPlayer() const { - return lastActionPlayer; + boost::recursive_mutex::scoped_lock lock(m_syncMutex); + return lastActionPlayer; } void diff --git a/src/engine/network_engine/clienthand.h b/src/engine/network_engine/clienthand.h index cbc5e867..d94f73c6 100644 --- a/src/engine/network_engine/clienthand.h +++ b/src/engine/network_engine/clienthand.h @@ -42,10 +42,6 @@ class ClientHand : public HandInterface PlayerList getActivePlayerList() const; PlayerList getRunningPlayerList() const; - PlayerListIterator getSeatIt(unsigned) const; - PlayerListIterator getActivePlayerIt(unsigned) const; - PlayerListIterator getRunningPlayerIt(unsigned) const; - boost::shared_ptr getBoard() const; boost::shared_ptr getPreflop() const; boost::shared_ptr getFlop() const; @@ -92,8 +88,13 @@ class ClientHand : public HandInterface void switchRounds(); +protected: + PlayerListIterator getSeatIt(unsigned) const; + PlayerListIterator getActivePlayerIt(unsigned) const; + PlayerListIterator getRunningPlayerIt(unsigned) const; - private: + +private: mutable boost::recursive_mutex m_syncMutex; boost::shared_ptr myFactory; diff --git a/src/engine/network_engine/clientplayer.cpp b/src/engine/network_engine/clientplayer.cpp index fb7f0a9d..cbfe4ed8 100644 --- a/src/engine/network_engine/clientplayer.cpp +++ b/src/engine/network_engine/clientplayer.cpp @@ -217,15 +217,15 @@ ClientPlayer::getMyActiveStatus() const void ClientPlayer::setMyStayOnTableStatus(bool theValue) { - boost::recursive_mutex::scoped_lock lock(m_syncMutex); - myStayOnTableStatus = theValue; + boost::recursive_mutex::scoped_lock lock(m_syncMutex); + myStayOnTableStatus = theValue; } bool ClientPlayer::getMyStayOnTableStatus() const { - boost::recursive_mutex::scoped_lock lock(m_syncMutex); - return myStayOnTableStatus; + boost::recursive_mutex::scoped_lock lock(m_syncMutex); + return myStayOnTableStatus; } void diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index 197f4984..6986ef7d 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -1732,6 +1732,9 @@ ClientStateWaitHand::InternalHandlePacket(boost::shared_ptr client cardDataStream >> myCards[0]; cardDataStream >> myCards[1]; } + // Basic synchronisation before a new hand is started. + client->GetGui().waitForGuiUpdateDone(); + // Start new hand. client->GetGame()->getSeatsList()->front()->setMyCards(myCards); client->GetGame()->initHand(); client->GetGame()->getCurrentHand()->setSmallBlind(netHandStart->smallBlind); diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index 7b24ea3a..cea5ef8e 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -1219,7 +1219,7 @@ void ClientThread::RemoveDisconnectedPlayers() { // This should only be called between hands. - if (m_game.get()) + if (m_game) { PlayerListIterator it; for (it = m_game->getSeatsList()->begin(); it != m_game->getSeatsList()->end(); it++)