Skip to content

Commit

Permalink
Fix thread naming on Linux
Browse files Browse the repository at this point in the history
Added some years ago, #define got lost in
autoconf -> cmake conversion.
  • Loading branch information
pkerling committed Mar 2, 2018
1 parent 685c383 commit 9a1ba6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
6 changes: 6 additions & 0 deletions cmake/scripts/linux/ArchSetup.cmake
Expand Up @@ -81,10 +81,16 @@ endif()
include(CheckSymbolExists)
set(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
check_symbol_exists("mkostemp" "stdlib.h" HAVE_MKOSTEMP)
set(CMAKE_REQUIRED_LIBRARIES "-pthread")
check_symbol_exists("pthread_setname_np" "pthread.h" HAVE_PTHREAD_SETNAME_NP)
set(CMAKE_REQUIRED_DEFINITIONS "")
set(CMAKE_REQUIRED_LIBRARIES "")
if(HAVE_MKOSTEMP)
list(APPEND ARCH_DEFINES "-DHAVE_MKOSTEMP=1" "-D_GNU_SOURCE")
endif()
if(HAVE_PTHREAD_SETNAME_NP)
list(APPEND ARCH_DEFINES "-DHAVE_PTHREAD_SETNAME_NP=1" "-D_GNU_SOURCE")
endif()

# Additional SYSTEM_DEFINES
list(APPEND SYSTEM_DEFINES -DHAS_LINUX_NETWORK)
Expand Down
25 changes: 13 additions & 12 deletions xbmc/threads/platform/pthreads/ThreadImpl.cpp
Expand Up @@ -92,16 +92,17 @@ void CThread::SetThreadInfo()
m_ThreadOpaque.LwpId = syscall(SYS_gettid);
#endif

#if defined(HAVE_PTHREAD_SETNAME_NP)
#ifdef TARGET_DARWIN
#if defined(TARGET_DARWIN)
pthread_setname_np(m_ThreadName.c_str());
#else
pthread_setname_np(m_ThreadId, m_ThreadName.c_str());
#elif defined(HAVE_PTHREAD_SETNAME_NP)
// Add prefix so Kodi-owned threads are distinguishable from other libraries
std::string pthreadName("k/");
pthreadName += m_ThreadName;
// Limit on Linux is 15 chars + NULL
pthreadName.resize(15);
pthread_setname_np(m_ThreadId, pthreadName.c_str());
#endif
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
pthread_set_name_np(m_ThreadId, m_ThreadName.c_str());
#endif


#ifdef RLIMIT_NICE
// get user max prio
struct rlimit limit;
Expand Down Expand Up @@ -164,7 +165,7 @@ bool CThread::SetPriority(const int iPriority)

// wait until thread is running, it needs to get its lwp id
m_StartEvent.Wait();

CSingleLock lock(m_CriticalSection);

// get min prio for SCHED_RR
Expand Down Expand Up @@ -223,7 +224,7 @@ int CThread::GetPriority()
m_StartEvent.Wait();

CSingleLock lock(m_CriticalSection);

int appNice = getpriority(PRIO_PROCESS, getpid());
int prio = getpriority(PRIO_PROCESS, m_ThreadOpaque.LwpId);
iReturn = appNice - prio;
Expand All @@ -241,10 +242,10 @@ bool CThread::WaitForThreadExit(unsigned int milliseconds)
int64_t CThread::GetAbsoluteUsage()
{
CSingleLock lock(m_CriticalSection);

if (!m_ThreadId)
return 0;

int64_t time = 0;
#ifdef TARGET_DARWIN
thread_basic_info threadInfo;
Expand Down

0 comments on commit 9a1ba6f

Please sign in to comment.