Add a method to set the running thread's name

This commit is contained in:
Chris Robinson 2013-10-26 12:39:19 -07:00
parent 6617985a4e
commit ff5277f4d7
4 changed files with 56 additions and 2 deletions

View File

@ -269,6 +269,44 @@ void RestoreFPUMode(const FPUCtl *ctl)
}
void SetThreadName(const char *name)
{
#if defined(HAVE_PTHREAD_SETNAME_NP)
#if defined(__GNUC__)
if(pthread_setname_np(pthread_self(), name) != 0)
#elif defined(__APPLE__)
if(pthread_setname_np(name) != 0)
#endif
ERR("Failed to set thread name to \"%s\": %s\n", name, strerror(errno));
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
pthread_set_name_np(pthread_self(), name);
#elif defined(_MSC_VER)
#define MS_VC_EXCEPTION 0x406D1388
struct {
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} info;
info.dwType = 0x1000;
info.szName = name;
info.dwThreadID = -1;
info.dwFlags = 0;
__try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (DWORD*)&info);
}
__except(EXCEPTION_CONTINUE_EXECUTION)
{
}
#undef MS_VC_EXCEPTION
#else
WARN("Can't set thread name to \"%s\"\n", name);
#endif
}
#ifdef _WIN32
void pthread_once(pthread_once_t *once, void (*callback)(void))
{

View File

@ -418,21 +418,29 @@ IF(NOT HAVE_WINDOWS_H)
# Some systems need pthread_np.h to get recursive mutexes
CHECK_INCLUDE_FILES("pthread.h;pthread_np.h" HAVE_PTHREAD_NP_H)
CHECK_SYMBOL_EXISTS(pthread_setschedparam pthread.h HAVE_PTHREAD_SETSCHEDPARAM)
CHECK_C_COMPILER_FLAG(-pthread HAVE_PTHREAD)
IF(HAVE_PTHREAD)
ADD_DEFINITIONS(-pthread)
SET(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -pthread")
SET(EXTRA_LIBS ${EXTRA_LIBS} -pthread)
ENDIF()
# _GNU_SOURCE is needed on some systems for extra attributes
ADD_DEFINITIONS(-D_GNU_SOURCE=1)
SET(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -D_GNU_SOURCE=1")
CHECK_LIBRARY_EXISTS(pthread pthread_create "" HAVE_LIBPTHREAD)
IF(HAVE_LIBPTHREAD)
SET(EXTRA_LIBS pthread ${EXTRA_LIBS})
ENDIF()
CHECK_SYMBOL_EXISTS(pthread_setschedparam pthread.h HAVE_PTHREAD_SETSCHEDPARAM)
CHECK_SYMBOL_EXISTS(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
IF(NOT HAVE_PTHREAD_SETNAME_NP)
CHECK_SYMBOL_EXISTS(pthread_set_name_np pthread.h HAVE_PTHREAD_SET_NAME_NP)
ENDIF()
CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_LIBRT)
IF(HAVE_LIBRT)
SET(EXTRA_LIBS rt ${EXTRA_LIBS})

View File

@ -813,6 +813,8 @@ void RestoreFPUMode(const FPUCtl *ctl);
ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
ALuint StopThread(ALvoid *thread);
void SetThreadName(const char *name);
typedef struct RingBuffer RingBuffer;
RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
void DestroyRingBuffer(RingBuffer *ring);

View File

@ -139,3 +139,9 @@
/* Define if we have pthread_setschedparam() */
#cmakedefine HAVE_PTHREAD_SETSCHEDPARAM
/* Define if we have pthread_setname_np() */
#cmakedefine HAVE_PTHREAD_SETNAME_NP
/* Define if we have pthread_set_name_np() */
#cmakedefine HAVE_PTHREAD_SET_NAME_NP