Label threads with a descriptive name where supported (pthreads).

pull/6/merge
Jonathan G Rennison 9 years ago
parent 3a28be7841
commit a3768d3a29

@ -331,7 +331,7 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti
_gw.thread = NULL; _gw.thread = NULL;
} }
if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) { if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread, "ottd:genworld")) {
DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode"); DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
_gw.threaded = false; _gw.threaded = false;
_modal_progress_work_mutex->EndCritical(); _modal_progress_work_mutex->EndCritical();

@ -62,7 +62,7 @@ void LinkGraphJob::EraseFlows(NodeID from)
*/ */
void LinkGraphJob::SpawnThread() void LinkGraphJob::SpawnThread()
{ {
if (!ThreadObject::New(&(LinkGraphSchedule::Run), this, &this->thread)) { if (!ThreadObject::New(&(LinkGraphSchedule::Run), this, &this->thread, "ottd:linkgraph")) {
this->thread = NULL; this->thread = NULL;
/* Of course this will hang a bit. /* Of course this will hang a bit.
* On the other hand, if you want to play games which make this hang noticably * On the other hand, if you want to play games which make this hang noticably

@ -35,7 +35,7 @@ TCPConnecter::TCPConnecter(const NetworkAddress &address) :
address(address) address(address)
{ {
*_tcp_connecters.Append() = this; *_tcp_connecters.Append() = this;
if (!ThreadObject::New(TCPConnecter::ThreadEntry, this, &this->thread)) { if (!ThreadObject::New(TCPConnecter::ThreadEntry, this, &this->thread, "ottd:tcp")) {
this->Connect(); this->Connect();
} }
} }

@ -109,7 +109,7 @@ static void NetworkUDPQueryServerThread(void *pntr)
void NetworkUDPQueryServer(NetworkAddress address, bool manually) void NetworkUDPQueryServer(NetworkAddress address, bool manually)
{ {
NetworkUDPQueryServerInfo *info = new NetworkUDPQueryServerInfo(address, manually); NetworkUDPQueryServerInfo *info = new NetworkUDPQueryServerInfo(address, manually);
if (address.IsResolved() || !ThreadObject::New(NetworkUDPQueryServerThread, info)) { if (address.IsResolved() || !ThreadObject::New(NetworkUDPQueryServerThread, info, NULL, "ottd:udp-query")) {
NetworkUDPQueryServerThread(info); NetworkUDPQueryServerThread(info);
} }
} }
@ -565,7 +565,7 @@ void NetworkUDPRemoveAdvertise(bool blocking)
/* Check if we are advertising */ /* Check if we are advertising */
if (!_networking || !_network_server || !_network_udp_server) return; if (!_networking || !_network_server || !_network_udp_server) return;
if (blocking || !ThreadObject::New(NetworkUDPRemoveAdvertiseThread, NULL)) { if (blocking || !ThreadObject::New(NetworkUDPRemoveAdvertiseThread, NULL, NULL, "ottd:udp-advert")) {
NetworkUDPRemoveAdvertiseThread(NULL); NetworkUDPRemoveAdvertiseThread(NULL);
} }
} }
@ -648,7 +648,7 @@ void NetworkUDPAdvertise()
if (_next_advertisement < _last_advertisement) _next_advertisement = UINT32_MAX; if (_next_advertisement < _last_advertisement) _next_advertisement = UINT32_MAX;
if (_next_retry < _last_advertisement) _next_retry = UINT32_MAX; if (_next_retry < _last_advertisement) _next_retry = UINT32_MAX;
if (!ThreadObject::New(NetworkUDPAdvertiseThread, NULL)) { if (!ThreadObject::New(NetworkUDPAdvertiseThread, NULL, NULL, "ottd:udp-advert")) {
NetworkUDPAdvertiseThread(NULL); NetworkUDPAdvertiseThread(NULL);
} }
} }

@ -786,7 +786,7 @@ void ScanNewGRFFiles(NewGRFScanCallback *callback)
/* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */ /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
MarkWholeScreenDirty(); MarkWholeScreenDirty();
if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL)) { if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL, "ottd:newgrf-scan")) {
_modal_progress_work_mutex->EndCritical(); _modal_progress_work_mutex->EndCritical();
_modal_progress_paint_mutex->EndCritical(); _modal_progress_paint_mutex->EndCritical();
DoScanNewGRFFiles(callback); DoScanNewGRFFiles(callback);

@ -2681,7 +2681,7 @@ static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
SlSaveChunks(); SlSaveChunks();
SaveFileStart(); SaveFileStart();
if (!threaded || !ThreadObject::New(&SaveFileToDiskThread, NULL, &_save_thread)) { if (!threaded || !ThreadObject::New(&SaveFileToDiskThread, NULL, &_save_thread, "ottd:savegame")) {
if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode..."); if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
SaveOrLoadResult result = SaveFileToDisk(false); SaveOrLoadResult result = SaveFileToDisk(false);

@ -44,9 +44,10 @@ public:
* @param proc The procedure to call inside the thread. * @param proc The procedure to call inside the thread.
* @param param The params to give with 'proc'. * @param param The params to give with 'proc'.
* @param thread Place to store a pointer to the thread in. May be NULL. * @param thread Place to store a pointer to the thread in. May be NULL.
* @param name A name for the thread. May be NULL.
* @return True if the thread was started correctly. * @return True if the thread was started correctly.
*/ */
static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread = NULL); static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread = NULL, const char *name = NULL);
}; };
/** /**

@ -193,7 +193,7 @@ private:
} }
}; };
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) /* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
{ {
ThreadObject *to = new ThreadObject_MorphOS(proc, param, thread == NULL); ThreadObject *to = new ThreadObject_MorphOS(proc, param, thread == NULL);
if (thread != NULL) *thread = to; if (thread != NULL) *thread = to;

@ -14,7 +14,7 @@
#include "../safeguards.h" #include "../safeguards.h"
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) /* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
{ {
if (thread != NULL) *thread = NULL; if (thread != NULL) *thread = NULL;
return false; return false;

@ -83,7 +83,7 @@ private:
} }
}; };
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) /* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
{ {
ThreadObject *to = new ThreadObject_OS2(proc, param, thread == NULL); ThreadObject *to = new ThreadObject_OS2(proc, param, thread == NULL);
if (thread != NULL) *thread = to; if (thread != NULL) *thread = to;

@ -25,16 +25,18 @@ private:
OTTDThreadFunc proc; ///< External thread procedure. OTTDThreadFunc proc; ///< External thread procedure.
void *param; ///< Parameter for the external thread procedure. void *param; ///< Parameter for the external thread procedure.
bool self_destruct; ///< Free ourselves when done? bool self_destruct; ///< Free ourselves when done?
const char *name; ///< Name for the thread
public: public:
/** /**
* Create a pthread and start it, calling proc(param). * Create a pthread and start it, calling proc(param).
*/ */
ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct) : ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct, const char *name) :
thread(0), thread(0),
proc(proc), proc(proc),
param(param), param(param),
self_destruct(self_destruct) self_destruct(self_destruct),
name(name)
{ {
pthread_create(&this->thread, NULL, &stThreadProc, this); pthread_create(&this->thread, NULL, &stThreadProc, this);
} }
@ -60,7 +62,15 @@ private:
*/ */
static void *stThreadProc(void *thr) static void *stThreadProc(void *thr)
{ {
((ThreadObject_pthread *)thr)->ThreadProc(); ThreadObject_pthread *self = (ThreadObject_pthread *) thr;
#if defined(__GLIBC__)
#if __GLIBC_PREREQ(2, 12)
if (self->name) {
pthread_setname_np(pthread_self(), self->name);
}
#endif
#endif
self->ThreadProc();
pthread_exit(NULL); pthread_exit(NULL);
} }
@ -85,9 +95,9 @@ private:
} }
}; };
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) /* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
{ {
ThreadObject *to = new ThreadObject_pthread(proc, param, thread == NULL); ThreadObject *to = new ThreadObject_pthread(proc, param, thread == NULL, name);
if (thread != NULL) *thread = to; if (thread != NULL) *thread = to;
return true; return true;
} }

@ -96,7 +96,7 @@ private:
} }
}; };
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread) /* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
{ {
ThreadObject *to = new ThreadObject_Win32(proc, param, thread == NULL); ThreadObject *to = new ThreadObject_Win32(proc, param, thread == NULL);
if (thread != NULL) *thread = to; if (thread != NULL) *thread = to;

@ -687,7 +687,7 @@ void VideoDriver_SDL::MainLoop()
_draw_mutex->BeginCritical(); _draw_mutex->BeginCritical();
_draw_continue = true; _draw_continue = true;
_draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread); _draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread, "ottd:draw-sdl");
/* Free the mutex if we won't be able to use it. */ /* Free the mutex if we won't be able to use it. */
if (!_draw_threaded) { if (!_draw_threaded) {

Loading…
Cancel
Save