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.
This commit is contained in:
Daniel Gibson 2013-06-23 19:16:23 +02:00
parent 86c2ea4d61
commit 88b23611f9

View file

@ -31,7 +31,7 @@ If you have questions concerning this license or the applicable additional terms
#include "../../precompiled.h" #include "../../precompiled.h"
#ifdef __FreeBSD__ #ifdef __FreeBSD__
#include <pthread_ng.h> // for pthread_set_name_np #include <pthread_np.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 // 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 ); ret = pthread_setname_np( handle, name );
if( ret != 0 ) if( ret != 0 )
idLib::common->Printf( "Setting threadname \"%s\" failed, reason: %s (%i)\n", name, strerror( errno ), errno ); 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__) #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
// the interface is void pthread_set_name_np(pthread_t tid, const char *name); // the interface is void pthread_set_name_np(pthread_t tid, const char *name);
pthread_set_name_np( handle, name ); // doesn't return anything pthread_set_name_np( handle, name ); // doesn't return anything
// seems like there is no get_name equivalent
#endif #endif
/* TODO: OSX: /* TODO: OSX:
// according to http://stackoverflow.com/a/7989973 // according to http://stackoverflow.com/a/7989973
// this needs to be called in the thread to be named! // this needs to be called in the thread to be named!
ret = pthread_setname_np(name); 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... // 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; 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 #endif // DEBUG_THREADS
@ -228,7 +242,7 @@ void Sys_DestroyThread( uintptr_t threadHandle )
name[0] = '\0'; name[0] = '\0';
#if defined(DEBUG_THREADS) #if defined(DEBUG_THREADS)
pthread_getname_np( threadHandle, name, sizeof( name ) ); Sys_GetThreadName( ( pthread_t )threadHandle, name, sizeof( name ) );
#endif #endif
#if 0 //!defined(__ANDROID__) #if 0 //!defined(__ANDROID__)