From 88b23611f9f74cde7a0b9de034ee83273e182590 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Sun, 23 Jun 2013 19:16:23 +0200 Subject: [PATCH] Fix DEBUG_THREADS on FreeBSD There was a typo in an #include, furthermore FreeBSD has no pthread_getname_np equivalent. I added Sys_GetThreadName() so the code is a bit cleaner. --- neo/idlib/sys/posix/posix_thread.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/neo/idlib/sys/posix/posix_thread.cpp b/neo/idlib/sys/posix/posix_thread.cpp index a0bd8fca..d51a93f0 100644 --- a/neo/idlib/sys/posix/posix_thread.cpp +++ b/neo/idlib/sys/posix/posix_thread.cpp @@ -31,7 +31,7 @@ If you have questions concerning this license or the applicable additional terms #include "../../precompiled.h" #ifdef __FreeBSD__ -#include // for pthread_set_name_np +#include // for pthread_set_name_np #endif // DG: Note: On Linux you need at least (e)glibc 2.12 to be able to set the threadname @@ -63,18 +63,15 @@ static int Sys_SetThreadName( pthread_t handle, const char* name ) ret = pthread_setname_np( handle, name ); if( ret != 0 ) idLib::common->Printf( "Setting threadname \"%s\" failed, reason: %s (%i)\n", name, strerror( errno ), errno ); - // pthread_getname_np(pthread_t, char*, size_t) #elif defined(__FreeBSD__) // according to http://www.freebsd.org/cgi/man.cgi?query=pthread_set_name_np&sektion=3 // the interface is void pthread_set_name_np(pthread_t tid, const char *name); pthread_set_name_np( handle, name ); // doesn't return anything - // seems like there is no get_name equivalent #endif /* TODO: OSX: // according to http://stackoverflow.com/a/7989973 // this needs to be called in the thread to be named! ret = pthread_setname_np(name); - // int pthread_getname_np(pthread_t, char*, size_t); // so we'd have to wrap the xthread_t function in Sys_CreateThread and set the name in the wrapping function... */ @@ -82,7 +79,24 @@ static int Sys_SetThreadName( pthread_t handle, const char* name ) return ret; } -// TODO: Sys_GetThreadName() ? +static int Sys_GetThreadName( pthread_t handle, char* namebuf, size_t buflen ) +{ + int ret = 0; +#ifdef __linux__ + ret = pthread_getname_np( handle, namebuf, buflen ); + if( ret != 0 ) + idLib::common->Printf( "Getting threadname failed, reason: %s (%i)\n", strerror( errno ), errno ); +#elif defined(__FreeBSD__) + // seems like there is no pthread_getname_np equivalent on FreeBSD + idStr::snPrintf( namebuf, buflen, "Can't read threadname on this platform!" ); +#endif + /* TODO: OSX: + // int pthread_getname_np(pthread_t, char*, size_t); + */ + + return ret; +} + #endif // DEBUG_THREADS @@ -228,7 +242,7 @@ void Sys_DestroyThread( uintptr_t threadHandle ) name[0] = '\0'; #if defined(DEBUG_THREADS) - pthread_getname_np( threadHandle, name, sizeof( name ) ); + Sys_GetThreadName( ( pthread_t )threadHandle, name, sizeof( name ) ); #endif #if 0 //!defined(__ANDROID__)