Refactor setting threadname on POSIX systems

* setting threadname is now done in a seperate function
* if setting the threadname fails, it just prints a warning now
  instead of terminating the game with a FatalError
This commit is contained in:
Daniel Gibson 2013-03-25 23:34:26 +01:00
parent fe18a49303
commit da9ab07e9c

View file

@ -34,6 +34,7 @@ If you have questions concerning this license or the applicable additional terms
#include <pthread_ng.h> // for pthread_set_name_np #include <pthread_ng.h> // for pthread_set_name_np
#endif #endif
// DG: Note: On Linux you need at least (e)glibc 2.12 to be able to set the threadname
//#define DEBUG_THREADS //#define DEBUG_THREADS
typedef void* ( *pthread_function_t )( void* ); typedef void* ( *pthread_function_t )( void* );
@ -49,11 +50,19 @@ caedes: This should be seen as a helper-function for Sys_CreateThread() only.
======================== ========================
*/ */
#ifdef DEBUG_THREADS
static int Sys_SetThreadName( pthread_t handle, const char* name ) static int Sys_SetThreadName( pthread_t handle, const char* name )
{ {
int ret = 0; int ret = 0;
#ifdef __linux__ #ifdef __linux__
// NOTE: linux only supports threadnames up to 16chars *including* terminating NULL
// http://man7.org/linux/man-pages/man3/pthread_setname_np.3.html
// on my machine a longer name (eg "JobListProcessor_0") caused an ENOENT error (instead of ERANGE)
assert( strlen( name ) < 16 );
ret = pthread_setname_np( handle, 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) // pthread_getname_np(pthread_t, char*, size_t)
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
// according to http://www.freebsd.org/cgi/man.cgi?query=pthread_set_name_np&sektion=3 // according to http://www.freebsd.org/cgi/man.cgi?query=pthread_set_name_np&sektion=3
@ -69,10 +78,14 @@ static int Sys_SetThreadName( pthread_t handle, const char* name )
// so we'd have to wrap the xthread_t function in Sys_CreateThread and set the name in the wrapping function... // so we'd have to wrap the xthread_t function in Sys_CreateThread and set the name in the wrapping function...
*/ */
return ret; return ret;
} }
// TODO: Sys_GetThreadName() ? // TODO: Sys_GetThreadName() ?
#endif // DEBUG_THREADS
/* /*
======================== ========================
@ -103,7 +116,7 @@ uintptr_t Sys_CreateThread( xthread_t function, void* parms, xthreadPriority pri
#if defined(DEBUG_THREADS) #if defined(DEBUG_THREADS)
if( Sys_SetThreadName( handle, name ) != 0 ) if( Sys_SetThreadName( handle, name ) != 0 )
{ {
idLib::common->FatalError( "ERROR: pthread_setname_np %s failed\n", name ); idLib::common->Warning( "Warning: pthread_setname_np %s failed\n", name );
return ( uintptr_t )0; return ( uintptr_t )0;
} }
#endif #endif