Kaydet (Commit) 8303540b authored tarafından Jan-Marek Glogowski's avatar Jan-Marek Glogowski

Use the system TID as the thread identifier

This uses the system TID as the LO thread identifier for Linux,
IOS and macOS, just as the Windows backend already does. While at
it use pthread functions on Linux, FreeBSD and MacOS to set the
thread name.

We already depend on MacOS 10.6 for dispatch support and Linux
supports pthread_setname_np since glibc 2.12, which is included in
our baseline. SYS_gettid is available since Linux 2.4.11.

I just copied the FreeBSD info from stackoverflow, while at it.

Change-Id: I39cdd09e952c0a2286d39f938c64b2d2d2f1ef91
Reviewed-on: https://gerrit.libreoffice.org/42071Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
Tested-by: 's avatarJenkins <ci@libreoffice.org>
üst 7282b25f
......@@ -42,10 +42,7 @@
#endif
#if defined LINUX && ! defined __FreeBSD_kernel__
#include <sys/prctl.h>
#ifndef PR_SET_NAME
#define PR_SET_NAME 15
#endif
#include <sys/syscall.h>
#endif
/****************************************************************************
......@@ -80,7 +77,7 @@
typedef struct osl_thread_impl_st
{
pthread_t m_hThread;
sal_uInt16 m_Ident; /* @@@ see TODO @@@ */
oslThreadIdentifier m_Ident; /* @@@ see TODO @@@ */
short m_Flags;
oslWorkerFunction m_WorkerFunction;
void* m_pData;
......@@ -135,9 +132,9 @@ static oslThread osl_thread_create_Impl (
oslWorkerFunction pWorker, void * pThreadData, short nFlags);
/* @@@ see TODO @@@ */
static sal_uInt16 insertThreadId (pthread_t hThread);
static sal_uInt16 lookupThreadId (pthread_t hThread);
static void removeThreadId (pthread_t hThread);
static oslThreadIdentifier insertThreadId (pthread_t hThread);
static oslThreadIdentifier lookupThreadId (pthread_t hThread);
static void removeThreadId (pthread_t hThread);
static void osl_thread_init_Impl()
{
......@@ -532,12 +529,25 @@ void SAL_CALL osl_yieldThread()
sched_yield();
}
void SAL_CALL osl_setThreadName(char const * name) {
void SAL_CALL osl_setThreadName(char const * name)
{
assert( name );
#if defined LINUX && ! defined __FreeBSD_kernel__
if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name), 0, 0, 0) != 0) {
int e = errno;
SAL_WARN("sal.osl", "prctl(PR_SET_NAME) failed with errno " << e);
}
const int LINUX_THREAD_NAME_MAXLEN = 15;
if ( strlen( name ) > LINUX_THREAD_NAME_MAXLEN )
SAL_INFO( "sal.osl", "osl_setThreadName truncated thread name to "
<< LINUX_THREAD_NAME_MAXLEN << " chars from name '"
<< name << "'" );
char shortname[ LINUX_THREAD_NAME_MAXLEN + 1 ];
shortname[ LINUX_THREAD_NAME_MAXLEN ] = '\0';
strncpy( shortname, name, LINUX_THREAD_NAME_MAXLEN );
int err = pthread_setname_np( pthread_self(), shortname );
if ( 0 != err )
SAL_WARN("sal.osl", "pthread_setname_np failed with errno " << err);
#elif defined __FreeBSD_kernel__
pthread_setname_np( pthread_self(), name );
#elif defined MACOSX || defined IOS
pthread_setname_np( name );
#else
(void) name;
#endif
......@@ -547,9 +557,9 @@ void SAL_CALL osl_setThreadName(char const * name) {
struct HashEntry
{
pthread_t Handle;
sal_uInt16 Ident;
HashEntry * Next;
pthread_t Handle;
oslThreadIdentifier Ident;
HashEntry * Next;
};
static HashEntry* HashTable[31];
......@@ -557,7 +567,9 @@ static int HashSize = SAL_N_ELEMENTS(HashTable);
static pthread_mutex_t HashLock = PTHREAD_MUTEX_INITIALIZER;
static sal_uInt16 LastIdent = 0;
#if ! (defined LINUX || defined MACOSX || defined IOS)
static oslThreadIdentifier LastIdent = 0;
#endif
namespace {
......@@ -566,7 +578,7 @@ std::size_t HASHID(pthread_t x)
}
static sal_uInt16 lookupThreadId (pthread_t hThread)
static oslThreadIdentifier lookupThreadId (pthread_t hThread)
{
HashEntry *pEntry;
......@@ -588,7 +600,7 @@ static sal_uInt16 lookupThreadId (pthread_t hThread)
return 0;
}
static sal_uInt16 insertThreadId (pthread_t hThread)
static oslThreadIdentifier insertThreadId (pthread_t hThread)
{
HashEntry *pEntry, *pInsert = nullptr;
......@@ -611,12 +623,28 @@ static sal_uInt16 insertThreadId (pthread_t hThread)
pEntry->Handle = hThread;
++ LastIdent;
if ( LastIdent == 0 )
#if defined LINUX && ! defined __FreeBSD_kernel__
long lin_tid = syscall(SYS_gettid);
if (lin_tid <= 0 || lin_tid > SAL_MAX_UINT32)
std::abort();
pEntry->Ident = static_cast<pid_t>(lin_tid);
#elif defined MACOSX || defined IOS
// currently the value of pthread_threadid_np is the same then
// syscall(SYS_thread_selfid), which returns an int as the TID.
// may change, as the syscall interface was deprecated.
uint64_t mac_tid;
pthread_threadid_np(nullptr, &mac_tid);
if (mac_tid > SAL_MAX_UINT32)
std::abort();
pEntry->Ident = mac_tid;
#else
++LastIdent;
if (0 == LastIdent)
LastIdent = 1;
pEntry->Ident = LastIdent;
pEntry->Ident = LastIdent;
#endif
if (0 == pEntry->Ident)
std::abort();
if (pInsert)
pInsert->Next = pEntry;
......@@ -661,7 +689,7 @@ static void removeThreadId (pthread_t hThread)
oslThreadIdentifier SAL_CALL osl_getThreadIdentifier(oslThread Thread)
{
Thread_Impl* pImpl= static_cast<Thread_Impl*>(Thread);
sal_uInt16 Ident;
oslThreadIdentifier Ident;
if (pImpl)
Ident = pImpl->m_Ident;
......@@ -676,7 +704,7 @@ oslThreadIdentifier SAL_CALL osl_getThreadIdentifier(oslThread Thread)
Ident = insertThreadId (current);
}
return (oslThreadIdentifier)Ident;
return Ident;
}
/*****************************************************************************
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment