Lot of changes to make a cleaner startup in no-graphics mode and having a clean exit with all threads being stopped etc...

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@13669 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius 2013-09-12 15:54:32 +00:00
parent 0bddb320cd
commit 20418a82af
8 changed files with 36 additions and 11 deletions

View File

@ -1431,11 +1431,8 @@ int main(int argc, char *argv[] )
if (players[n].getName() == UserConfigParams::m_default_player.toString())
unlock_manager->setCurrentSlot(players[n].getUniqueID());
main_loop->run();
throw "salut";
}
if(!UserConfigParams::m_no_start_screen)
else if(!UserConfigParams::m_no_start_screen)
{
StateManager::get()->pushScreen(StoryModeLobbyScreen::getInstance());
#ifdef ENABLE_WIIUSE
@ -1596,6 +1593,7 @@ int main(int argc, char *argv[] )
// so we don't crash later when StateManager tries to access input devices.
StateManager::get()->resetActivePlayers();
if(input_manager) delete input_manager; // if early crash avoid delete NULL
NetworkManager::getInstance()->abort();
cleanSuperTuxKart();

View File

@ -76,6 +76,13 @@ void NetworkManager::reset()
}
}
void NetworkManager::abort()
{
m_localhost->stopListening();
reset();
ProtocolManager::getInstance()->abort();
}
//-----------------------------------------------------------------------------
bool NetworkManager::connect(TransportAddress peer)

View File

@ -40,6 +40,7 @@ class NetworkManager : public Singleton<NetworkManager>
public:
virtual void run();
virtual void reset();
virtual void abort();
// network management functions
virtual bool connect(TransportAddress peer);

View File

@ -31,7 +31,7 @@
void* protocolManagerUpdate(void* data)
{
ProtocolManager* manager = static_cast<ProtocolManager*>(data);
while(!manager->exit())
while(manager && !manager->exit())
{
manager->update();
Time::sleep(2);
@ -41,11 +41,13 @@ void* protocolManagerUpdate(void* data)
void* protocolManagerAsynchronousUpdate(void* data)
{
ProtocolManager* manager = static_cast<ProtocolManager*>(data);
while(!manager->exit())
manager->m_asynchronous_thread_running = true;
while(manager && !manager->exit())
{
manager->asynchronousUpdate();
Time::sleep(2);
}
manager->m_asynchronous_thread_running = false;
return NULL;
}
@ -73,8 +75,17 @@ ProtocolManager::ProtocolManager()
}
ProtocolManager::~ProtocolManager()
{
}
void ProtocolManager::abort()
{
pthread_mutex_unlock(&m_exit_mutex); // will stop the update function
while (m_asynchronous_thread_running) // wait the thread to finish before we delete all mutexes etc..
{
Time::sleep(2);
}
pthread_mutex_lock(&m_events_mutex);
pthread_mutex_lock(&m_protocols_mutex);
pthread_mutex_lock(&m_asynchronous_protocols_mutex);

View File

@ -105,8 +105,11 @@ typedef struct EventProcessingInfo
class ProtocolManager : public Singleton<ProtocolManager>
{
friend class Singleton<ProtocolManager>;
friend void* protocolManagerAsynchronousUpdate(void* data);
public:
/*! \brief Stops the protocol manager. */
virtual void abort();
/*!
* \brief Function that processes incoming events.
* This function is called by the network manager each time there is an
@ -323,6 +326,8 @@ class ProtocolManager : public Singleton<ProtocolManager>
pthread_t* m_update_thread;
/*! Asynchronous update thread.*/
pthread_t* m_asynchronous_update_thread;
/*! True if the thread is running. */
bool m_asynchronous_thread_running;
};

View File

@ -224,13 +224,13 @@ void ServerLobbyRoomProtocol::checkRaceFinished()
if (NetworkWorld::getInstance()->isRaceOver())
{
// calculate karts ranks :
unsigned int num_players = race_manager->getNumberOfKarts();
int num_players = race_manager->getNumberOfKarts();
std::vector<int> karts_results;
std::vector<float> karts_times;
for (int j = 0; j < num_players; j++)
{
float kart_time = race_manager->getKartRaceTime(j);
for (int i = 0; i < karts_times.size(); i++)
for (unsigned int i = 0; i < karts_times.size(); i++)
{
if (kart_time < karts_times[i])
{

View File

@ -67,10 +67,10 @@ void* waitInput2(void* data)
uint32_t id = ProtocolManager::getInstance()->requestStart(new StopServer());
while(ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED)
{
Time::sleep(1);
}
main_loop->abort();
exit(0);
return NULL;
}

View File

@ -89,7 +89,7 @@ STKHost::STKHost()
pthread_mutex_init(&m_exit_mutex, NULL);
pthread_mutex_init(&m_log_mutex, NULL);
if (UserConfigParams::m_packets_log_filename.toString() != "")
m_log_file = fopen(UserConfigParams::m_packets_log_filename.c_str(), "w");
m_log_file = fopen(UserConfigParams::m_packets_log_filename.c_str(), "w+");
if (!m_log_file)
Log::warn("STKHost", "Network packets won't be logged: no file.");
}
@ -100,7 +100,10 @@ STKHost::~STKHost()
{
stopListening();
if (m_log_file)
{
fclose(m_log_file);
Log::warn("STKHost", "Packet logging file has been closed.");
}
if (m_host)
{
enet_host_destroy(m_host);