From da9ab07e9c67fe574a31696ed1241ff3e6447cd6 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Mon, 25 Mar 2013 23:34:26 +0100 Subject: [PATCH] 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 --- neo/idlib/sys/posix/posix_thread.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/neo/idlib/sys/posix/posix_thread.cpp b/neo/idlib/sys/posix/posix_thread.cpp index 32c6ad0c..39ce2ae6 100644 --- a/neo/idlib/sys/posix/posix_thread.cpp +++ b/neo/idlib/sys/posix/posix_thread.cpp @@ -34,6 +34,7 @@ If you have questions concerning this license or the applicable additional terms #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 //#define DEBUG_THREADS 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 ) { int ret = 0; #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 ); + 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 @@ -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... */ + return ret; } // 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( 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; } #endif