Fixed race condition on game end: if the network thread quit between the lines:

if(m_thread_id)
   pthread_cancel(*m_thread_id)
a crash in pthread_cancel happened. Changing m_thread_id is now protected
by a mutex.


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@8564 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2011-05-13 06:49:12 +00:00
parent c52fa8ceff
commit 6ccd28d7b2
2 changed files with 18 additions and 13 deletions

View File

@ -63,6 +63,7 @@ NetworkHttp *network_http=NULL;
* ending up with an corrupted file).
*/
NetworkHttp::NetworkHttp() : m_abort(false),
m_thread_id(NULL),
m_all_requests(std::vector<Request*>())
{
// Don't even start the network threads if networking is disabled.
@ -96,13 +97,15 @@ void NetworkHttp::startNetworkThread()
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
m_thread_id = new pthread_t();
int error = pthread_create(m_thread_id, &attr,
m_thread_id.set(new pthread_t());
int error = pthread_create(m_thread_id.getData(), &attr,
&NetworkHttp::mainLoop, this);
if(error)
{
delete m_thread_id;
m_thread_id = 0;
m_thread_id.lock();
delete m_thread_id.getData();
m_thread_id.set(0);
m_thread_id.unlock();
printf("[addons] Warning: could not create thread, error=%d.\n", errno);
}
pthread_attr_destroy(&attr);
@ -175,7 +178,7 @@ void *NetworkHttp::mainLoop(void *obj)
if(UserConfigParams::logAddons())
printf("[addons] Network thread terminating.\n");
me->m_thread_id = 0;
me->m_thread_id.set(0);
return NULL;
} // mainLoop
@ -213,13 +216,15 @@ NetworkHttp::~NetworkHttp()
if(UserConfigParams::logAddons())
printf("[addons] Command mutex unlocked.\n");
if(m_thread_id)
m_thread_id.lock();
if(m_thread_id.getData())
{
printf("[addons] Cancelling network thread.\n");
int e = pthread_cancel(*m_thread_id);
int e = pthread_cancel(*m_thread_id.getData());
printf("[addons] Cancelled network thread. Return code: %d\n", e);
delete m_thread_id;
delete m_thread_id.getData();
}
m_thread_id.unlock();
pthread_cond_destroy(&m_cond_request);

View File

@ -51,19 +51,19 @@ public:
private:
/** The list of pointes to all requests. */
mutable Synchronised< std::vector<Request*> > m_all_requests;
Synchronised< std::vector<Request*> > m_all_requests;
/** A conditional variable to wake up the main loop. */
pthread_cond_t m_cond_request;
pthread_cond_t m_cond_request;
/** Signal an abort in case that a download is still happening. */
Synchronised<bool> m_abort;
Synchronised<bool> m_abort;
/** Thread id of the thread running in this object. */
pthread_t *m_thread_id;
Synchronised<pthread_t *> m_thread_id;
/** The curl session. */
CURL *m_curl_session;
CURL *m_curl_session;
static void *mainLoop(void *obj);
int init();