(svn r27670) -Add: [FS#6471] Assign descriptive names to (GNU pthread) threads. (JGR)

This commit is contained in:
frosch 2016-10-30 17:29:33 +00:00
parent f8f2c9d3a5
commit 3f75b8bca2
13 changed files with 30 additions and 19 deletions

View File

@ -331,7 +331,7 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti
_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();

View File

@ -61,7 +61,7 @@ void LinkGraphJob::EraseFlows(NodeID from)
*/
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

View File

@ -35,7 +35,7 @@ TCPConnecter::TCPConnecter(const NetworkAddress &address) :
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();
}
}

View File

@ -109,7 +109,7 @@ static void NetworkUDPQueryServerThread(void *pntr)
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 blocking)
/* 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);
}
}

View File

@ -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!. */
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);

View File

@ -2585,7 +2585,7 @@ static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
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);

View File

@ -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);
};
/**

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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) {