diff --git a/src/genworld.cpp b/src/genworld.cpp --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -331,7 +331,7 @@ void GenerateWorld(GenWorldMode mode, ui _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"); _gw.threaded = false; _modal_progress_work_mutex->EndCritical(); diff --git a/src/linkgraph/linkgraphjob.cpp b/src/linkgraph/linkgraphjob.cpp --- a/src/linkgraph/linkgraphjob.cpp +++ b/src/linkgraph/linkgraphjob.cpp @@ -61,7 +61,7 @@ void LinkGraphJob::EraseFlows(NodeID fro */ void LinkGraphJob::SpawnThread() { - if (!ThreadObject::New(&(LinkGraphSchedule::Run), this, &this->thread)) { + if (!ThreadObject::New(&(LinkGraphSchedule::Run), this, &this->thread, "ottd:linkgraph")) { this->thread = NULL; /* Of course this will hang a bit. * On the other hand, if you want to play games which make this hang noticably diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp --- a/src/network/core/tcp_connect.cpp +++ b/src/network/core/tcp_connect.cpp @@ -35,7 +35,7 @@ TCPConnecter::TCPConnecter(const Network address(address) { *_tcp_connecters.Append() = this; - if (!ThreadObject::New(TCPConnecter::ThreadEntry, this, &this->thread)) { + if (!ThreadObject::New(TCPConnecter::ThreadEntry, this, &this->thread, "ottd:tcp")) { this->Connect(); } } diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp --- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -109,7 +109,7 @@ static void NetworkUDPQueryServerThread( void NetworkUDPQueryServer(NetworkAddress address, bool 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); } } @@ -565,7 +565,7 @@ void NetworkUDPRemoveAdvertise(bool bloc /* Check if we are advertising */ 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); } } @@ -648,7 +648,7 @@ void NetworkUDPAdvertise() if (_next_advertisement < _last_advertisement) _next_advertisement = 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); } } diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -786,7 +786,7 @@ void ScanNewGRFFiles(NewGRFScanCallback /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */ 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_paint_mutex->EndCritical(); DoScanNewGRFFiles(callback); diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2585,7 +2585,7 @@ static SaveOrLoadResult DoSave(SaveFilte SlSaveChunks(); 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..."); SaveOrLoadResult result = SaveFileToDisk(false); diff --git a/src/thread/thread.h b/src/thread/thread.h --- a/src/thread/thread.h +++ b/src/thread/thread.h @@ -44,9 +44,10 @@ public: * @param proc The procedure to call inside the thread. * @param param The params to give with 'proc'. * @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. */ - static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread = NULL); + static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread = NULL, const char *name = NULL); }; /** diff --git a/src/thread/thread_morphos.cpp b/src/thread/thread_morphos.cpp --- a/src/thread/thread_morphos.cpp +++ b/src/thread/thread_morphos.cpp @@ -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); if (thread != NULL) *thread = to; diff --git a/src/thread/thread_none.cpp b/src/thread/thread_none.cpp --- a/src/thread/thread_none.cpp +++ b/src/thread/thread_none.cpp @@ -14,7 +14,7 @@ #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; return false; diff --git a/src/thread/thread_os2.cpp b/src/thread/thread_os2.cpp --- a/src/thread/thread_os2.cpp +++ b/src/thread/thread_os2.cpp @@ -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); if (thread != NULL) *thread = to; diff --git a/src/thread/thread_pthread.cpp b/src/thread/thread_pthread.cpp --- a/src/thread/thread_pthread.cpp +++ b/src/thread/thread_pthread.cpp @@ -25,16 +25,18 @@ private: OTTDThreadFunc proc; ///< External thread procedure. void *param; ///< Parameter for the external thread procedure. bool self_destruct; ///< Free ourselves when done? + const char *name; ///< Name for the thread public: /** * 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), proc(proc), param(param), - self_destruct(self_destruct) + self_destruct(self_destruct), + name(name) { pthread_create(&this->thread, NULL, &stThreadProc, this); } @@ -60,7 +62,15 @@ private: */ 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); } @@ -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; return true; } diff --git a/src/thread/thread_win32.cpp b/src/thread/thread_win32.cpp --- a/src/thread/thread_win32.cpp +++ b/src/thread/thread_win32.cpp @@ -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); if (thread != NULL) *thread = to; diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -687,7 +687,7 @@ void VideoDriver_SDL::MainLoop() _draw_mutex->BeginCritical(); _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. */ if (!_draw_threaded) {