- overhaul of thread processing, especially in the menus, things now freeze much less and should be more reliable

This commit is contained in:
Mark Vejvoda 2011-01-02 06:46:48 +00:00
parent 9150701e70
commit 2b1732e27e
17 changed files with 435 additions and 507 deletions

View File

@ -238,7 +238,7 @@ Renderer::~Renderer() {
this->game = NULL;
}
void Renderer::simpleTask() {
void Renderer::simpleTask(BaseThread *callingThread) {
// This code reads pixmaps from a queue and saves them to disk
Pixmap2D *savePixMapBuffer=NULL;
string path="";

View File

@ -433,7 +433,7 @@ private:
void renderTile(const Vec2i &pos);
void renderQuad(int x, int y, int w, int h, const Texture2D *texture);
void simpleTask();
void simpleTask(BaseThread *callingThread);
//static
static Texture2D::Filter strToTextureFilter(const string &s);

View File

@ -222,7 +222,7 @@ void Program::eventMouseMove(int x, int y, const MouseState *ms) {
}
}
void Program::simpleTask() {
void Program::simpleTask(BaseThread *callingThread) {
loopWorker();
}

View File

@ -154,7 +154,7 @@ public:
void init(WindowGl *window, bool initSound=true, bool toggleFullScreen=false);
void exit();
virtual void simpleTask();
virtual void simpleTask(BaseThread *callingThread);
void mouseDownLeft(int x, int y);
void eventMouseMove(int x, int y, const MouseState *ms);

View File

@ -507,14 +507,10 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b
console.addLine(lang.get("To switch off music press")+" - \""+configKeys.getCharKey("ToggleMusic")+"\"");
//chatManager.init(&console, world.getThisTeamIndex());
chatManager.init(&console, -1,true);
GraphicComponent::applyAllCustomProperties(containerName);
MutexSafeWrapper safeMutexPtr(&publishToMasterserverThreadPtrChangeAccessor);
publishToMasterserverThreadInDeletion = false;
publishToMasterserverThread = new SimpleTaskThread(this,0,25);
publishToMasterserverThread->setUniqueID(__FILE__);
publishToMasterserverThread->start();
@ -523,34 +519,19 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b
}
MenuStateCustomGame::~MenuStateCustomGame() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(publishToMasterserverThreadInDeletion == false) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutexPtr(&publishToMasterserverThreadPtrChangeAccessor);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(publishToMasterserverThread != NULL) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
needToBroadcastServerSettings = false;
needToRepublishToMasterserver = false;
//BaseThread::shutdownAndWait(publishToMasterserverThread);
if(publishToMasterserverThread != NULL &&
publishToMasterserverThread->canShutdown(true) == true &&
publishToMasterserverThread->shutdownAndWait() == true) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
delete publishToMasterserverThread;
}
publishToMasterserverThread = NULL;
}
publishToMasterserverThreadInDeletion = false;
safeMutexPtr.ReleaseLock();
}
else {
safeMutexPtr.ReleaseLock();
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
needToBroadcastServerSettings = false;
needToRepublishToMasterserver = false;
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -561,26 +542,20 @@ MenuStateCustomGame::~MenuStateCustomGame() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
void MenuStateCustomGame::returnToParentMenu(){
void MenuStateCustomGame::returnToParentMenu() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutexPtr(&publishToMasterserverThreadPtrChangeAccessor);
publishToMasterserverThreadInDeletion = true;
needToBroadcastServerSettings = false;
needToRepublishToMasterserver = false;
bool returnToMasterServerMenu = parentMenuIsMs;
//BaseThread::shutdownAndWait(publishToMasterserverThread);
if(publishToMasterserverThread != NULL &&
publishToMasterserverThread->canShutdown() == true &&
publishToMasterserverThread->shutdownAndWait() == true) {
delete publishToMasterserverThread;
publishToMasterserverThread = NULL;
}
publishToMasterserverThreadInDeletion = false;
safeMutexPtr.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(returnToMasterServerMenu) {
@ -618,22 +593,20 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
soundRenderer.playFx(coreData.getClickSoundA());
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
needToBroadcastServerSettings = false;
needToRepublishToMasterserver = false;
safeMutex.ReleaseLock();
MutexSafeWrapper safeMutexPtr(&publishToMasterserverThreadPtrChangeAccessor);
publishToMasterserverThreadInDeletion = true;
//BaseThread::shutdownAndWait(publishToMasterserverThread);
if(publishToMasterserverThread != NULL &&
publishToMasterserverThread->canShutdown() == true &&
publishToMasterserverThread->shutdownAndWait() == true) {
delete publishToMasterserverThread;
publishToMasterserverThread = NULL;
}
publishToMasterserverThreadInDeletion = false;
safeMutexPtr.ReleaseLock();
//publishToMasterserverThreadInDeletion = false;
//safeMutexPtr.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -652,7 +625,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
else if(listBoxMap.mouseClick(x, y)){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s\n", getCurrentMapFile().c_str());
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
loadMapInfo(Map::getMapPath(getCurrentMapFile(),"",false), &mapInfo, true);
labelMapInfo.setText(mapInfo.desc);
@ -670,7 +643,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
}
}
else if (listBoxAdvanced.getSelectedItemIndex() == 1 && listBoxFogOfWar.mouseClick(x, y)) {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(listBoxPublishServer.getSelectedItemIndex() == 0) {
needToRepublishToMasterserver = true;
@ -683,7 +656,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
}
}
else if (listBoxAdvanced.getSelectedItemIndex() == 1 && listBoxAllowObservers.mouseClick(x, y)) {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(listBoxPublishServer.getSelectedItemIndex() == 0) {
needToRepublishToMasterserver = true;
@ -697,7 +670,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
}
}
else if (listBoxAdvanced.getSelectedItemIndex() == 1 && listBoxEnableObserverMode.mouseClick(x, y)) {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(listBoxPublishServer.getSelectedItemIndex() == 0) {
needToRepublishToMasterserver = true;
@ -710,7 +683,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
}
}
else if (listBoxAdvanced.getSelectedItemIndex() == 1 && listBoxPathFinderType.mouseClick(x, y)) {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(listBoxPublishServer.getSelectedItemIndex() == 0) {
needToRepublishToMasterserver = true;
@ -725,20 +698,8 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
else if (listBoxAdvanced.mouseClick(x, y)) {
//TODO
}
/*
else if (listBoxAdvanced.getSelectedItemIndex() == 1 && listBoxEnableServerControlledAI.mouseClick(x, y) && listBoxEnableServerControlledAI.getEditable()) {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
needToRepublishToMasterserver = true;
if(hasNetworkGameSettings() == true)
{
needToSetChangedGameSettings = true;
lastSetChangedGameSettings = time(NULL);
}
}
*/
else if(listBoxTileset.mouseClick(x, y)){
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(listBoxPublishServer.getSelectedItemIndex() == 0) {
needToRepublishToMasterserver = true;
@ -750,7 +711,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
}
}
else if(listBoxMapFilter.mouseClick(x, y)){
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
switchToNextMapGroup(listBoxMapFilter.getSelectedItemIndex()-oldListBoxMapfilterIndex);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s\n", getCurrentMapFile().c_str());
@ -772,7 +733,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
else if(listBoxTechTree.mouseClick(x, y)){
reloadFactions(false);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(listBoxPublishServer.getSelectedItemIndex() == 0) {
needToRepublishToMasterserver = true;
@ -785,25 +746,12 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
}
}
else if(listBoxPublishServer.mouseClick(x, y) && listBoxPublishServer.getEditable()) {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
needToRepublishToMasterserver = true;
soundRenderer.playFx(coreData.getClickSoundC());
}
/*
else if(listBoxAdvanced.getSelectedItemIndex() == 1 && listBoxNetworkFramePeriod.mouseClick(x, y)){
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
needToRepublishToMasterserver = true;
if(hasNetworkGameSettings() == true)
{
needToSetChangedGameSettings = true;
lastSetChangedGameSettings = time(NULL);
}
soundRenderer.playFx(coreData.getClickSoundC());
}
*/
else if(listBoxAdvanced.getSelectedItemIndex() == 1 && listBoxNetworkPauseGameForLaggedClients.mouseClick(x, y)){
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(listBoxPublishServer.getSelectedItemIndex() == 0) {
needToRepublishToMasterserver = true;
@ -818,7 +766,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
}
else {
for(int i=0; i<mapInfo.players; ++i) {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if (listBoxAdvanced.getSelectedItemIndex() == 1) {
// set multiplier
@ -993,7 +941,7 @@ void MenuStateCustomGame::RestoreLastGameSettings() {
ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface();
serverInterface->setGameSettings(&gameSettings,false);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(listBoxPublishServer.getSelectedItemIndex() == 0) {
needToRepublishToMasterserver = true;
@ -1007,7 +955,7 @@ void MenuStateCustomGame::RestoreLastGameSettings() {
}
void MenuStateCustomGame::PlayNow() {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
saveGameSettingsToFile("lastCustomGamSettings.mgg");
closeUnusedSlots();
@ -1145,18 +1093,13 @@ void MenuStateCustomGame::PlayNow() {
needToRepublishToMasterserver = false;
safeMutex.ReleaseLock();
MutexSafeWrapper safeMutexPtr(&publishToMasterserverThreadPtrChangeAccessor);
publishToMasterserverThreadInDeletion = true;
//BaseThread::shutdownAndWait(publishToMasterserverThread);
if(publishToMasterserverThread != NULL &&
publishToMasterserverThread->canShutdown() == true &&
publishToMasterserverThread->shutdownAndWait() == true) {
delete publishToMasterserverThread;
publishToMasterserverThread = NULL;
}
publishToMasterserverThreadInDeletion = false;
safeMutexPtr.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
assert(program != NULL);
@ -1379,11 +1322,9 @@ void MenuStateCustomGame::update() {
Chrono chrono;
chrono.start();
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
try {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(serverInitError == true) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -1633,7 +1574,6 @@ void MenuStateCustomGame::update() {
labelNetStatus[i].setText(szBuf);
}
else {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] C - ctNetwork\n",__FILE__,__FUNCTION__);
string port = intToStr(config.getInt("ServerPort"));
if(port != intToStr(GameConstants::serverPort)){
port = port + " " + lang.get("NonStandardPort") + "!)";
@ -1852,46 +1792,36 @@ void MenuStateCustomGame::publishToMasterserver()
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
void MenuStateCustomGame::simpleTask() {
void MenuStateCustomGame::simpleTask(BaseThread *callingThread) {
try {
if(publishToMasterserverThreadInDeletion == true) {
if(callingThread->getQuitStatus() == true) {
return;
}
if( publishToMasterserverThread == NULL ||
publishToMasterserverThread->getQuitStatus() == true) {
return;
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex(callingThread->getMutexThreadObjectAccessor());
bool republish = (needToRepublishToMasterserver == true && publishToServerInfo.size() != 0);
needToRepublishToMasterserver = false;
std::map<string,string> newPublishToServerInfo = publishToServerInfo;
publishToServerInfo.clear();
bool broadCastSettings = needToBroadcastServerSettings;
needToBroadcastServerSettings=false;
needToBroadcastServerSettings = false;
bool hasClientConnection = false;
if(broadCastSettings) {
if(broadCastSettings == true) {
ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface();
hasClientConnection = serverInterface->hasClientConnection();
}
bool needPing = (difftime(time(NULL),lastNetworkPing) >= GameConstants::networkPingInterval);
safeMutex.ReleaseLock();
safeMutex.ReleaseLock(true);
if( publishToMasterserverThread == NULL ||
publishToMasterserverThread->getQuitStatus() == true) {
if(callingThread->getQuitStatus() == true) {
return;
}
if(republish == true) {
safeMutex.Lock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
//string request = Config::getInstance().getString("Masterserver") + "addServerInfo.php?" + newPublishToServerInfo;
string request = Config::getInstance().getString("Masterserver") + "addServerInfo.php?";
CURL *handle = SystemFlags::initHTTP();
@ -1906,10 +1836,16 @@ void MenuStateCustomGame::simpleTask() {
//printf("the request is:\n%s\n",request.c_str());
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] the request is:\n%s\n",__FILE__,__FUNCTION__,__LINE__,request.c_str());
safeMutex.ReleaseLock(true);
std::string serverInfo = SystemFlags::getHTTP(request,handle);
SystemFlags::cleanupHTTP(&handle);
if(callingThread->getQuitStatus() == true) {
return;
}
safeMutex.Lock();
//printf("the result is:\n'%s'\n",serverInfo.c_str());
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] the result is:\n'%s'\n",__FILE__,__FUNCTION__,__LINE__,serverInfo.c_str());
@ -1918,47 +1854,39 @@ void MenuStateCustomGame::simpleTask() {
showMasterserverError=true;
masterServererErrorToShow = (serverInfo != "" ? serverInfo : "No Reply");
}
safeMutex.ReleaseLock(true);
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
if( publishToMasterserverThread == NULL ||
publishToMasterserverThread->getQuitStatus() == true) {
if(callingThread->getQuitStatus() == true) {
return;
}
if(broadCastSettings) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(broadCastSettings == true) {
safeMutex.Lock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex2(&masterServerThreadAccessor);
GameSettings gameSettings;
loadGameSettings(&gameSettings);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
serverInterface->setGameSettings(&gameSettings,false);
safeMutex2.ReleaseLock();
if(hasClientConnection == true) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex3(&masterServerThreadAccessor);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
serverInterface->broadcastGameSetup(&gameSettings);
safeMutex3.ReleaseLock();
}
safeMutex.ReleaseLock(true);
}
if( publishToMasterserverThread == NULL ||
publishToMasterserverThread->getQuitStatus() == true) {
if(callingThread->getQuitStatus() == true) {
return;
}
safeMutex.Lock();
if(needPing == true) {
lastNetworkPing = time(NULL);
@ -1966,20 +1894,22 @@ void MenuStateCustomGame::simpleTask() {
ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface();
NetworkMessagePing msg(GameConstants::networkPingInterval,time(NULL));
MutexSafeWrapper safeMutex2(&masterServerThreadAccessor);
serverInterface->broadcastPing(&msg);
safeMutex2.ReleaseLock();
}
safeMutex.ReleaseLock();
}
catch(const std::exception &ex) {
char szBuf[4096]="";
sprintf(szBuf,"In [%s::%s %d] Error detected:\n%s\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
SystemFlags::OutputDebug(SystemFlags::debugError,szBuf);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf);
if(callingThread->getQuitStatus() == false) {
//throw runtime_error(szBuf);!!!
showGeneralError=true;
generalErrorToShow = ex.what();
}
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
@ -2638,7 +2568,7 @@ void MenuStateCustomGame::keyDown(char key) {
activeInputLabel->setText(text);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(hasNetworkGameSettings() == true) {
needToSetChangedGameSettings = true;
lastSetChangedGameSettings = time(NULL);
@ -2693,7 +2623,7 @@ void MenuStateCustomGame::keyPress(char c) {
text.insert(text.end()-1, c);
activeInputLabel->setText(text);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(hasNetworkGameSettings() == true) {
needToSetChangedGameSettings = true;
lastSetChangedGameSettings = time(NULL);

View File

@ -102,9 +102,9 @@ private:
bool needToBroadcastServerSettings;
std::map<string,string> publishToServerInfo;
SimpleTaskThread *publishToMasterserverThread;
Mutex masterServerThreadAccessor;
Mutex publishToMasterserverThreadPtrChangeAccessor;
bool publishToMasterserverThreadInDeletion;
//Mutex masterServerThreadAccessor;
//Mutex publishToMasterserverThreadPtrChangeAccessor;
//bool publishToMasterserverThreadInDeletion;
bool parentMenuIsMs;
int soundConnectionCount;
@ -157,7 +157,7 @@ public:
virtual void keyUp(char key);
virtual void simpleTask();
virtual void simpleTask(BaseThread *callingThread);
virtual bool isInSpecialKeyCaptureEvent() { return chatManager.getEditEnabled(); }
private:

View File

@ -241,8 +241,6 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen
chatManager.setYPos(consoleIRC.getYPos()-20);
chatManager.setFont(CoreData::getInstance().getMenuFontNormal());
MutexSafeWrapper safeMutexPtr(&masterServerThreadPtrChangeAccessor);
masterServerThreadInDeletion = false;
needUpdateFromServer = true;
updateFromMasterserverThread = new SimpleTaskThread(this,0,100);
updateFromMasterserverThread->setUniqueID(__FILE__);
@ -300,48 +298,29 @@ void MenuStateMasterserver::IRC_CallbackEvent(IRCEventType evt, const char* orig
void MenuStateMasterserver::cleanup() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//printf("In [%s::%s Line: %d] [%p]\n",__FILE__,__FUNCTION__,__LINE__,ircClient);
if(masterServerThreadInDeletion == false) {
MutexSafeWrapper safeMutexPtr(&masterServerThreadPtrChangeAccessor);
if(updateFromMasterserverThread != NULL) {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((updateFromMasterserverThread != NULL ? updateFromMasterserverThread->getMutexThreadObjectAccessor() : NULL));
needUpdateFromServer = false;
safeMutex.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//BaseThread::shutdownAndWait(updateFromMasterserverThread);
masterServerThreadInDeletion = true;
if(updateFromMasterserverThread != NULL &&
updateFromMasterserverThread->canShutdown(true) == true &&
updateFromMasterserverThread->shutdownAndWait() == true) {
delete updateFromMasterserverThread;
}
updateFromMasterserverThread = NULL;
masterServerThreadInDeletion = false;
safeMutexPtr.ReleaseLock();
}
else {
safeMutexPtr.ReleaseLock();
}
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
clearServerLines();
clearUserButtons();
//printf("Exiting master server menu [%p]\n",ircClient);
MutexSafeWrapper safeMutexIRCPtr(&mutexIRCClient);
if(ircClient != NULL) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
ircClient->setCallbackObj(NULL);
ircClient->signalQuit();
//if(ircClient->shutdownAndWait() == true) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//delete ircClient;
//}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
ircClient = NULL;
}
@ -351,14 +330,12 @@ void MenuStateMasterserver::cleanup() {
MenuStateMasterserver::~MenuStateMasterserver() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//printf("In [%s::%s Line: %d] [%p]\n",__FILE__,__FUNCTION__,__LINE__,ircClient);
cleanup();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] END\n",__FILE__,__FUNCTION__,__LINE__);
}
void MenuStateMasterserver::clearServerLines() {
while(!serverLines.empty()){
while(!serverLines.empty()) {
delete serverLines.back();
serverLines.pop_back();
}
@ -401,7 +378,7 @@ void MenuStateMasterserver::mouseClick(int x, int y, MouseButton mouseButton){
else if(buttonRefresh.mouseClick(x, y)){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((updateFromMasterserverThread != NULL ? updateFromMasterserverThread->getMutexThreadObjectAccessor() : NULL));
soundRenderer.playFx(coreData.getClickSoundB());
needUpdateFromServer = true;
@ -410,22 +387,8 @@ void MenuStateMasterserver::mouseClick(int x, int y, MouseButton mouseButton){
else if(buttonReturn.mouseClick(x, y)){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
soundRenderer.playFx(coreData.getClickSoundB());
//BaseThread::shutdownAndWait(updateFromMasterserverThread);
MutexSafeWrapper safeMutexPtr(&masterServerThreadPtrChangeAccessor);
masterServerThreadInDeletion = true;
if(updateFromMasterserverThread != NULL &&
updateFromMasterserverThread->shutdownAndWait() == true) {
delete updateFromMasterserverThread;
}
updateFromMasterserverThread = NULL;
masterServerThreadInDeletion = false;
safeMutexPtr.ReleaseLock();
safeMutex.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
cleanup();
@ -439,26 +402,13 @@ void MenuStateMasterserver::mouseClick(int x, int y, MouseButton mouseButton){
else if(buttonCreateGame.mouseClick(x, y)){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((updateFromMasterserverThread != NULL ? updateFromMasterserverThread->getMutexThreadObjectAccessor() : NULL));
soundRenderer.playFx(coreData.getClickSoundB());
needUpdateFromServer = false;
safeMutex.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//BaseThread::shutdownAndWait(updateFromMasterserverThread);
MutexSafeWrapper safeMutexPtr(&masterServerThreadPtrChangeAccessor);
masterServerThreadInDeletion = true;
if(updateFromMasterserverThread != NULL &&
updateFromMasterserverThread->shutdownAndWait() == true) {
delete updateFromMasterserverThread;
}
updateFromMasterserverThread = NULL;
masterServerThreadInDeletion = false;
safeMutexPtr.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
cleanup();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -467,12 +417,12 @@ void MenuStateMasterserver::mouseClick(int x, int y, MouseButton mouseButton){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
else if(listBoxAutoRefresh.mouseClick(x, y)){
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((updateFromMasterserverThread != NULL ? updateFromMasterserverThread->getMutexThreadObjectAccessor() : NULL));
soundRenderer.playFx(coreData.getClickSoundA());
autoRefreshTime=10*listBoxAutoRefresh.getSelectedItemIndex();
}
else {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((updateFromMasterserverThread != NULL ? updateFromMasterserverThread->getMutexThreadObjectAccessor() : NULL));
bool clicked=false;
if(!clicked && serverScrollBar.getElementCount()!=0){
for(int i = serverScrollBar.getVisibleStart(); i <= serverScrollBar.getVisibleEnd(); ++i) {
@ -486,15 +436,7 @@ void MenuStateMasterserver::mouseClick(int x, int y, MouseButton mouseButton){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
safeMutex.ReleaseLock();
MutexSafeWrapper safeMutexPtr(&masterServerThreadPtrChangeAccessor);
masterServerThreadInDeletion = true;
if(updateFromMasterserverThread != NULL &&
updateFromMasterserverThread->shutdownAndWait() == true) {
delete updateFromMasterserverThread;
}
updateFromMasterserverThread = NULL;
masterServerThreadInDeletion = false;
safeMutexPtr.ReleaseLock();
cleanup();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
mainMenu->setState(new MenuStateConnectedGame(program, mainMenu,jmMasterserver));
@ -523,7 +465,7 @@ void MenuStateMasterserver::mouseClick(int x, int y, MouseButton mouseButton){
}
void MenuStateMasterserver::mouseMove(int x, int y, const MouseState *ms){
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((updateFromMasterserverThread != NULL ? updateFromMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if (mainMessageBox.getEnabled()) {
mainMessageBox.mouseMove(x, y);
@ -556,7 +498,7 @@ void MenuStateMasterserver::mouseMove(int x, int y, const MouseState *ms){
void MenuStateMasterserver::render(){
Renderer &renderer= Renderer::getInstance();
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((updateFromMasterserverThread != NULL ? updateFromMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(mainMessageBox.getEnabled()) {
renderer.renderMessageBox(&mainMessageBox);
}
@ -632,7 +574,7 @@ void MenuStateMasterserver::render(){
}
void MenuStateMasterserver::update() {
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex((updateFromMasterserverThread != NULL ? updateFromMasterserverThread->getMutexThreadObjectAccessor() : NULL));
if(autoRefreshTime!=0 && difftime(time(NULL),lastRefreshTimer) >= autoRefreshTime ) {
needUpdateFromServer = true;
lastRefreshTimer= time(NULL);
@ -727,47 +669,33 @@ void MenuStateMasterserver::update() {
}
}
void MenuStateMasterserver::simpleTask() {
if(masterServerThreadInDeletion == true) {
void MenuStateMasterserver::simpleTask(BaseThread *callingThread) {
if(callingThread->getQuitStatus() == true) {
return;
}
if( updateFromMasterserverThread == NULL ||
updateFromMasterserverThread->getQuitStatus() == true) {
return;
}
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
MutexSafeWrapper safeMutex(callingThread->getMutexThreadObjectAccessor());
bool needUpdate = needUpdateFromServer;
safeMutex.ReleaseLock();
if(needUpdate == true) {
updateServerInfo();
}
}
void MenuStateMasterserver::updateServerInfo() {
try {
if(masterServerThreadInDeletion == true) {
if(callingThread->getQuitStatus() == true) {
return;
}
MutexSafeWrapper safeMutexPtr(&masterServerThreadPtrChangeAccessor);
if( updateFromMasterserverThread == NULL ||
updateFromMasterserverThread->getQuitStatus() == true) {
safeMutexPtr.ReleaseLock();
return;
}
safeMutexPtr.ReleaseLock(true);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor);
needUpdateFromServer = false;
safeMutex.ReleaseLock(true);
if(announcementLoaded == false) {
string announcementURL = Config::getInstance().getString("AnnouncementURL","http://master.megaglest.org/files/announcement.txt");
if(announcementURL != "") {
safeMutex.ReleaseLock(true);
std::string announcementTxt = SystemFlags::getHTTP(announcementURL);
if(callingThread->getQuitStatus() == true) {
return;
}
safeMutex.Lock();
if(StartsWith(announcementTxt,"Announcement from Masterserver:") == true) {
int newlineCount=0;
size_t lastIndex=0;
@ -775,19 +703,17 @@ void MenuStateMasterserver::updateServerInfo() {
//announcementLabel.setText(announcementTxt);
consoleIRC.addLine(announcementTxt);
while(true){
while(true) {
lastIndex=announcementTxt.find("\n",lastIndex+1);
if(lastIndex==string::npos)
{
if(lastIndex==string::npos) {
break;
}
else
{
else {
newlineCount++;
}
}
newlineCount--;// remove my own line
for( int i=0; i< newlineCount;++i ){
for( int i=0; i< newlineCount;++i ) {
consoleIRC.addLine("");
}
}
@ -796,7 +722,13 @@ void MenuStateMasterserver::updateServerInfo() {
string versionURL = Config::getInstance().getString("VersionURL","http://master.megaglest.org/files/versions/")+glestVersionString+".txt";
//printf("\nversionURL=%s\n",versionURL.c_str());
if(versionURL != "") {
safeMutex.ReleaseLock(true);
std::string versionTxt = SystemFlags::getHTTP(versionURL);
if(callingThread->getQuitStatus() == true) {
return;
}
safeMutex.Lock();
if(StartsWith(versionTxt,"Version info:") == true) {
int newlineCount=0;
size_t lastIndex=0;
@ -804,19 +736,17 @@ void MenuStateMasterserver::updateServerInfo() {
//versionInfoLabel.setText(versionTxt);
consoleIRC.addLine(versionTxt);
while(true){
while(true) {
lastIndex=versionTxt.find("\n",lastIndex+1);
if(lastIndex==string::npos)
{
if(lastIndex==string::npos) {
break;
}
else
{
else {
newlineCount++;
}
}
newlineCount--;// remove my own line
for( int i=0; i< newlineCount;++i ){
for( int i=0; i< newlineCount;++i ) {
consoleIRC.addLine("");
}
}
@ -833,7 +763,13 @@ void MenuStateMasterserver::updateServerInfo() {
try {
if(Config::getInstance().getString("Masterserver","") != "") {
safeMutex.ReleaseLock(true);
std::string localServerInfoString = SystemFlags::getHTTP(Config::getInstance().getString("Masterserver") + "showServersForGlest.php");
if(callingThread->getQuitStatus() == true) {
return;
}
safeMutex.Lock();
serverInfoString=localServerInfoString;
}
}
@ -841,25 +777,16 @@ void MenuStateMasterserver::updateServerInfo() {
serverInfoString=ex.what();
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line %d] error during Internet game status update: [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
}
safeMutexPtr.Lock();
if( updateFromMasterserverThread == NULL ||
updateFromMasterserverThread->getQuitStatus() == true) {
safeMutexPtr.ReleaseLock();
return;
}
safeMutexPtr.ReleaseLock();
}
catch(const exception &e){
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what());
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d, error [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what());
threadedErrorMsg = e.what();
}
}
}
void MenuStateMasterserver::rebuildServerLines(const string &serverInfo)
{
void MenuStateMasterserver::rebuildServerLines(const string &serverInfo) {
int numberOfOldServerLines=serverLines.size();
clearServerLines();
Lang &lang= Lang::getInstance();

View File

@ -97,9 +97,6 @@ private:
static DisplayMessageFunction pCB_DisplayMessage;
std::string threadedErrorMsg;
Mutex masterServerThreadAccessor;
Mutex masterServerThreadPtrChangeAccessor;
bool masterServerThreadInDeletion;
std::vector<string> ircArgs;
Mutex mutexIRCClient;
@ -122,7 +119,7 @@ public:
virtual void keyPress(char c);
virtual void keyUp(char key);
virtual void simpleTask();
virtual void simpleTask(BaseThread *callingThread);
static void setDisplayMessageFunction(DisplayMessageFunction pDisplayMessage) { pCB_DisplayMessage = pDisplayMessage; }
@ -133,7 +130,6 @@ private:
void setButtonLinePosition(int pos);
void clearServerLines();
void clearUserButtons();
void updateServerInfo();
void rebuildServerLines(const string &serverInfo);
void cleanup();
virtual void IRC_CallbackEvent(IRCEventType evt, const char* origin, const char **params, unsigned int count);

View File

@ -146,7 +146,7 @@ ServerInterface::~ServerInterface() {
// This triggers a gameOver message to be sent to the masterserver
lastMasterserverHeartbeatTime = 0;
if(needToRepublishToMasterserver == true) {
simpleTask();
simpleTask(NULL);
}
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -1523,7 +1523,7 @@ std::map<string,string> ServerInterface::publishToMasterserver() {
return publishToServerInfo;
}
void ServerInterface::simpleTask() {
void ServerInterface::simpleTask(BaseThread *callingThread) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex(&masterServerThreadAccessor,intToStr(__LINE__));

View File

@ -121,7 +121,7 @@ public:
Mutex * getServerSynchAccessor() { return &serverSynchAccessor; }
virtual void simpleTask();
virtual void simpleTask(BaseThread *callingThread);
void addClientToServerIPAddress(uint32 clientIp,uint32 ServerIp);
private:

View File

@ -58,7 +58,7 @@ public:
static SoundRenderer &getInstance();
bool init(Window *window);
void update();
virtual void simpleTask() { update(); }
virtual void simpleTask(BaseThread *callingThread) { update(); }
SoundPlayer *getSoundPlayer() const {return soundPlayer;}
//music

View File

@ -31,13 +31,21 @@ protected:
Mutex mutexRunning;
Mutex mutexQuit;
Mutex mutexBeginExecution;
Mutex mutexDeleteSelfOnExecutionDone;
Mutex mutexThreadObjectAccessor;
Mutex mutexExecutingTask;
bool executingTask;
bool quit;
bool running;
string uniqueID;
bool hasBeginExecution;
bool deleteSelfOnExecutionDone;
virtual void setQuitStatus(bool value);
void deleteSelfIfRequired();
public:
BaseThread();
@ -47,17 +55,26 @@ public:
virtual void signalQuit();
virtual bool getQuitStatus();
virtual bool getRunningStatus();
virtual bool getHasBeginExecution();
virtual void setHasBeginExecution(bool value);
static bool shutdownAndWait(BaseThread *ppThread);
virtual bool shutdownAndWait();
virtual bool canShutdown() { return true; }
virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);
virtual bool getDeleteSelfOnExecutionDone();
virtual void setDeleteSelfOnExecutionDone(bool value);
void setUniqueID(string value) { uniqueID = value; }
string getUniqueID() { return uniqueID; }
virtual void setRunningStatus(bool value);
void setExecutingTask(bool value);
bool getExecutingTask();
Mutex * getMutexThreadObjectAccessor() { return &mutexThreadObjectAccessor; }
};
class RunningStatusSafeWrapper {
@ -85,6 +102,32 @@ public:
}
};
class ExecutingTaskSafeWrapper {
protected:
BaseThread *thread;
public:
ExecutingTaskSafeWrapper(BaseThread *thread) {
this->thread = thread;
Enable();
}
~ExecutingTaskSafeWrapper() {
Disable();
}
void Enable() {
if(this->thread != NULL) {
this->thread->setExecutingTask(true);
}
}
void Disable() {
if(this->thread != NULL) {
this->thread->setExecutingTask(false);
}
}
};
}}//end namespace
#endif

View File

@ -48,7 +48,7 @@ public:
//
class SimpleTaskCallbackInterface {
public:
virtual void simpleTask() = 0;
virtual void simpleTask(BaseThread *callingThread) = 0;
};
class SimpleTaskThread : public BaseThread
@ -63,9 +63,6 @@ protected:
bool taskSignalled;
bool needTaskSignal;
Mutex mutexExecutingTask;
bool executingTask;
public:
SimpleTaskThread();
SimpleTaskThread(SimpleTaskCallbackInterface *simpleTaskInterface,
@ -73,12 +70,10 @@ public:
unsigned int millisecsBetweenExecutions=0,
bool needTaskSignal = false);
virtual void execute();
virtual bool canShutdown();
virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);
void setTaskSignalled(bool value);
bool getTaskSignalled();
void setExecutingTask(bool value);
bool getExecutingTask();
};
// =====================================================

View File

@ -121,7 +121,7 @@ public:
Socket();
virtual ~Socket();
virtual void simpleTask();
virtual void simpleTask(BaseThread *callingThread);
static int getBroadCastPort() { return broadcast_portno; }
static void setBroadCastPort(int value) { broadcast_portno = value; }

View File

@ -27,6 +27,8 @@ BaseThread::BaseThread() : Thread() {
setQuitStatus(false);
setRunningStatus(false);
setHasBeginExecution(false);
setExecutingTask(false);
setDeleteSelfOnExecutionDone(false);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
@ -117,6 +119,46 @@ void BaseThread::setRunningStatus(bool value) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str());
}
void BaseThread::setExecutingTask(bool value) {
MutexSafeWrapper safeMutex(&mutexExecutingTask);
executingTask = value;
safeMutex.ReleaseLock();
}
bool BaseThread::getExecutingTask() {
bool retval = false;
MutexSafeWrapper safeMutex(&mutexExecutingTask);
retval = executingTask;
safeMutex.ReleaseLock();
return retval;
}
bool BaseThread::getDeleteSelfOnExecutionDone() {
bool retval = false;
MutexSafeWrapper safeMutex(&mutexDeleteSelfOnExecutionDone);
retval = deleteSelfOnExecutionDone;
safeMutex.ReleaseLock();
return retval;
}
void BaseThread::setDeleteSelfOnExecutionDone(bool value) {
MutexSafeWrapper safeMutex(&mutexDeleteSelfOnExecutionDone);
deleteSelfOnExecutionDone = value;
}
void BaseThread::deleteSelfIfRequired() {
if(getDeleteSelfOnExecutionDone() == true) {
delete this;
return;
}
}
bool BaseThread::canShutdown(bool deleteSelfIfShutdownDelayed) {
return true;
}
bool BaseThread::shutdownAndWait(BaseThread *pThread) {
bool ret = false;
if(pThread != NULL) {

View File

@ -26,10 +26,12 @@ FileCRCPreCacheThread::FileCRCPreCacheThread() : BaseThread() {
}
void FileCRCPreCacheThread::execute() {
{
RunningStatusSafeWrapper runningStatus(this);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(getQuitStatus() == true) {
deleteSelfIfRequired();
return;
}
@ -56,7 +58,7 @@ void FileCRCPreCacheThread::execute() {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
break;
}
sleep( 100 );
sleep( 50 );
}
}
}
@ -70,6 +72,8 @@ void FileCRCPreCacheThread::execute() {
}
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] FILE CRC PreCache thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
}
deleteSelfIfRequired();
}
SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInterface,
@ -81,14 +85,20 @@ SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInter
this->millisecsBetweenExecutions = millisecsBetweenExecutions;
this->needTaskSignal = needTaskSignal;
setTaskSignalled(false);
setExecutingTask(false);
}
bool SimpleTaskThread::canShutdown() {
return (getExecutingTask() == false);
bool SimpleTaskThread::canShutdown(bool deleteSelfIfShutdownDelayed) {
bool ret = (getExecutingTask() == false);
if(deleteSelfIfShutdownDelayed == true) {
setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed);
signalQuit();
}
return ret;
}
void SimpleTaskThread::execute() {
{
RunningStatusSafeWrapper runningStatus(this);
try {
if(getQuitStatus() == true) {
@ -114,15 +124,8 @@ void SimpleTaskThread::execute() {
else if(runTask == true) {
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str());
if(getQuitStatus() == false) {
try {
setExecutingTask(true);
this->simpleTaskInterface->simpleTask();
setExecutingTask(false);
}
catch(const exception &ex) {
setExecutingTask(false);
throw runtime_error(ex.what());
}
ExecutingTaskSafeWrapper safeExecutingTaskMutex(this);
this->simpleTaskInterface->simpleTask(this);
}
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str());
}
@ -151,7 +154,10 @@ void SimpleTaskThread::execute() {
throw runtime_error(ex.what());
}
}
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s] END\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str());
deleteSelfIfRequired();
}
void SimpleTaskThread::setTaskSignalled(bool value) {
@ -177,21 +183,6 @@ bool SimpleTaskThread::getTaskSignalled() {
return retval;
}
void SimpleTaskThread::setExecutingTask(bool value) {
MutexSafeWrapper safeMutex(&mutexExecutingTask);
executingTask = value;
safeMutex.ReleaseLock();
}
bool SimpleTaskThread::getExecutingTask() {
bool retval = false;
MutexSafeWrapper safeMutex(&mutexExecutingTask);
retval = executingTask;
safeMutex.ReleaseLock();
return retval;
}
// -------------------------------------------------
LogFileThread::LogFileThread() : BaseThread() {
@ -223,10 +214,12 @@ bool LogFileThread::checkSaveCurrentLogBufferToDisk() {
}
void LogFileThread::execute() {
{
RunningStatusSafeWrapper runningStatus(this);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(getQuitStatus() == true) {
deleteSelfIfRequired();
return;
}
@ -258,6 +251,8 @@ void LogFileThread::execute() {
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] LogFile thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] LogFile thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
}
deleteSelfIfRequired();
}
std::size_t LogFileThread::getLogEntryBufferCount() {

View File

@ -787,7 +787,7 @@ float Socket::getThreadedPingMS(std::string host) {
return result;
}
void Socket::simpleTask() {
void Socket::simpleTask(BaseThread *callingThread) {
// update ping times every x seconds
const int pingFrequencySeconds = 2;
if(difftime(time(NULL),lastThreadedPing) < pingFrequencySeconds) {