Skip to content

Commit

Permalink
#5555: Avoid double-entering the think() method from external trigger…
Browse files Browse the repository at this point in the history
…s (wxTimer).

The lock cannot be placed within the GameConnection::think() body, since think is recursively calling itself by design.
  • Loading branch information
codereader committed May 22, 2021
1 parent b75c4f2 commit 66c3df0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 14 additions & 0 deletions plugins/dm.gameconnection/GameConnection.cpp
Expand Up @@ -16,6 +16,7 @@

#include "scene/Traverse.h"
#include "wxutil/Bitmap.h"
#include "util/ScopedBoolLock.h"

#include <sigc++/signal.h>
#include <sigc++/connection.h>
Expand Down Expand Up @@ -51,6 +52,10 @@ namespace
#endif
}

GameConnection::GameConnection() :
_timerInProgress(false)
{}

std::size_t GameConnection::generateNewSequenceNumber() {
return ++_seqno;
}
Expand Down Expand Up @@ -103,6 +108,15 @@ void GameConnection::think() {
}
}

void GameConnection::onTimerEvent(wxTimerEvent& ev)
{
if (_timerInProgress) return; // avoid double-entering

util::ScopedBoolLock guard(_timerInProgress);

think();
}

void GameConnection::waitAction() {
while (_seqnoInProgress)
think();
Expand Down
4 changes: 3 additions & 1 deletion plugins/dm.gameconnection/GameConnection.h
Expand Up @@ -25,6 +25,7 @@ class GameConnection :
public RegisterableModule
{
public:
GameConnection();
~GameConnection();

//connect to TDM instance if not connected yet
Expand Down Expand Up @@ -103,8 +104,9 @@ class GameConnection :
std::unique_ptr<MessageTcp> _connection;
//when connected, this timer calls Think periodically
std::unique_ptr<wxTimer> _thinkTimer;
bool _timerInProgress;

void onTimerEvent(wxTimerEvent& ev) { think(); }
void onTimerEvent(wxTimerEvent& ev);

//signal listener for when map is saved, loaded, unloaded, etc.
sigc::connection _mapEventListener;
Expand Down

0 comments on commit 66c3df0

Please sign in to comment.