Added Thread::CurrentThreadId() method to wrap up thread id functionality in a more platform appropriate way.

This commit is contained in:
Robert Osfield
2020-01-06 18:39:51 +00:00
parent 8551f25da0
commit d1ff16614c
11 changed files with 59 additions and 30 deletions

View File

@@ -48,6 +48,11 @@
#endif
#endif
#if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__ANDROID__)
# include <unistd.h>
# include <sys/syscall.h>
#endif
#if defined(__ANDROID__)
#ifndef PAGE_SIZE
#define PAGE_SIZE 0x400
@@ -68,11 +73,6 @@ using namespace OpenThreads;
#endif
//-----------------------------------------------------------------------------
// Initialize the static unique ids.
//
int PThreadPrivateData::nextId = 0;
//-----------------------------------------------------------------------------
// Initialize thread master priority level
//
@@ -184,8 +184,7 @@ private:
Thread *thread = static_cast<Thread *>(data);
PThreadPrivateData *pd =
static_cast<PThreadPrivateData *>(thread->_prvData);
PThreadPrivateData *pd = static_cast<PThreadPrivateData *>(thread->_prvData);
// set up processor affinity
setAffinity( pd->affinity );
@@ -212,6 +211,8 @@ private:
#endif // ] ALLOW_PRIORITY_SCHEDULING
pd->uniqueId = Thread::CurrentThreadId();
pd->setRunning(true);
// release the thread that created this thread.
@@ -463,6 +464,25 @@ Thread *Thread::CurrentThread()
}
size_t Thread::CurrentThreadId()
{
#if defined(__APPLE__)
return (size_t)::syscall(SYS_thread_selfid);
#elif defined(__ANDROID__)
return (size_t)gettid();
#elif defined(__linux__)
return (size_t)::syscall(SYS_gettid);
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
long tid;
syscall(SYS_thr_self, &tid);
return (size_t)tid;
#else
return (size_t)pthread_self();
#endif
}
//-----------------------------------------------------------------------------
//
// Description: Initialize Threading
@@ -528,7 +548,7 @@ void Thread::Init()
//
// Use: public
//
int Thread::getThreadId()
size_t Thread::getThreadId()
{
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);

View File

@@ -49,8 +49,7 @@ private:
setRunning(false);
isCanceled = false;
tid = 0;
uniqueId = nextId;
nextId++;
uniqueId = 0;
threadPriority = Thread::THREAD_PRIORITY_DEFAULT;
threadPolicy = Thread::THREAD_SCHEDULE_DEFAULT;
};
@@ -78,12 +77,10 @@ private:
pthread_t tid;
volatile int uniqueId;
size_t uniqueId;
Affinity affinity;
static int nextId;
static pthread_key_t s_tls_key;
};

View File

@@ -103,6 +103,8 @@ namespace OpenThreads {
pd->isRunning = true;
pd->uniqueId = Thread::CurrentThreadId();
// release the thread that created this thread.
pd->threadStartedBlock.release();
@@ -210,6 +212,11 @@ Thread* Thread::CurrentThread()
return (Thread* )TlsGetValue(ID);
}
size_t Thread::CurrentThreadId()
{
return (size_t)::GetCurrentThreadId();
}
//----------------------------------------------------------------------------
//
// Description: Set the concurrency level (no-op)
@@ -300,7 +307,7 @@ void Thread::Init() {
//
// Use: public
//
int Thread::getThreadId() {
size_t Thread::getThreadId() {
Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData);
return pd->uniqueId;
}
@@ -352,7 +359,7 @@ int Thread::start() {
pd->tid.set( (void*)_beginthreadex(NULL,static_cast<unsigned>(pd->stackSize),ThreadPrivateActions::StartThread,static_cast<void *>(this),CREATE_SUSPENDED,&ID));
ResumeThread(pd->tid.get());
pd->uniqueId = (int)ID;
pd->uniqueId = (size_t)ID;
if(!pd->tid) {
return -1;
@@ -442,9 +449,9 @@ int Thread::testCancel()
if(pd->cancelMode == 2)
return 0;
DWORD curr = GetCurrentThreadId();
size_t curr = Thread::CurrentThreadId();
if( pd->uniqueId != (int)curr )
if( pd->uniqueId != curr )
return -1;
// pd->isRunning = false;

View File

@@ -54,7 +54,7 @@ private:
HandleHolder tid;
int uniqueId;
size_t uniqueId;
Affinity affinity;