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

@@ -113,6 +113,11 @@ public:
*/
static Thread *CurrentThread();
/**
* Return the id of the current thread
*/
static size_t CurrentThreadId();
/**
* Initialize Threading in a program. This method must be called before
@@ -147,7 +152,7 @@ public:
*
* @return a unique thread identifier
*/
int getThreadId();
size_t getThreadId();
/**
* Get the thread's process id. This is the pthread_t or pid_t value

View File

@@ -390,7 +390,7 @@ class OSG_EXPORT GraphicsContext : public Object
bool releaseContext();
/** Return true if the current thread has this OpenGL graphics context.*/
inline bool isCurrent() const { return _threadOfLastMakeCurrent == OpenThreads::Thread::CurrentThread(); }
inline bool isCurrent() const { return _threadOfLastMakeCurrent == OpenThreads::Thread::CurrentThreadId(); }
/** Bind the graphics context to associated texture.*/
inline void bindPBufferToTexture(GLenum buffer) { bindPBufferToTextureImplementation(buffer); }
@@ -547,7 +547,7 @@ class OSG_EXPORT GraphicsContext : public Object
Vec4 _clearColor;
GLbitfield _clearMask;
OpenThreads::Thread* _threadOfLastMakeCurrent;
size_t _threadOfLastMakeCurrent;
OpenThreads::Mutex _operationsMutex;
osg::ref_ptr<osg::RefBlock> _operationsBlock;

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;

View File

@@ -526,7 +526,7 @@ void GraphicsContext::close(bool callCloseImplementation)
bool GraphicsContext::makeCurrent()
{
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThreadId();
bool result = makeCurrentImplementation();
@@ -546,7 +546,7 @@ bool GraphicsContext::makeContextCurrent(GraphicsContext* readContext)
if (result)
{
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThreadId();
// initialize extension process, not only initializes on first
// call, will be a non-op on subsequent calls.
@@ -560,7 +560,7 @@ bool GraphicsContext::releaseContext()
{
bool result = releaseContextImplementation();
_threadOfLastMakeCurrent = (OpenThreads::Thread*)(-1);
_threadOfLastMakeCurrent = 0;
return result;
}
@@ -573,7 +573,7 @@ void GraphicsContext::swapBuffers()
clear();
}
else if (_graphicsThread.valid() &&
_threadOfLastMakeCurrent == _graphicsThread.get())
_threadOfLastMakeCurrent == _graphicsThread->getThreadId())
{
_graphicsThread->add(new SwapBuffersOperation);
}

View File

@@ -165,7 +165,7 @@ class ReaderWriterCURL : public osgDB::ReaderWriter
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadCurlMapMutex);
osg::ref_ptr<EasyCurl>& ec = _threadCurlMap[OpenThreads::Thread::CurrentThread()];
osg::ref_ptr<EasyCurl>& ec = _threadCurlMap[OpenThreads::Thread::CurrentThreadId()];
if (!ec) ec = new EasyCurl;
return *ec;
@@ -176,7 +176,7 @@ class ReaderWriterCURL : public osgDB::ReaderWriter
protected:
void getConnectionOptions(const osgDB::ReaderWriter::Options *options, std::string& proxyAddress, long& connectTimeout, long& timeout, long& sslVerifyPeer) const;
typedef std::map< OpenThreads::Thread*, osg::ref_ptr<EasyCurl> > ThreadCurlMap;
typedef std::map< size_t, osg::ref_ptr<EasyCurl> > ThreadCurlMap;
mutable OpenThreads::Mutex _threadCurlMapMutex;
mutable ThreadCurlMap _threadCurlMap;

View File

@@ -672,7 +672,7 @@ const ZipArchive::PerThreadData&
ZipArchive::getDataNoLock() const
{
// get/create data for the currently running thread:
OpenThreads::Thread* current = OpenThreads::Thread::CurrentThread();
size_t current = OpenThreads::Thread::CurrentThreadId();
PerThreadDataMap::const_iterator i = _perThreadData.find( current );

View File

@@ -92,7 +92,7 @@ class ZipArchive : public osgDB::Archive
HZIP _zipHandle;
};
typedef std::map<OpenThreads::Thread*, PerThreadData> PerThreadDataMap;
typedef std::map<size_t, PerThreadData> PerThreadDataMap;
PerThreadDataMap _perThreadData;
const PerThreadData& getData() const;

View File

@@ -279,7 +279,7 @@ Display* GraphicsWindowX11::getDisplayToUse() const
return _display;
}
if (OpenThreads::Thread::CurrentThread()==_threadOfLastMakeCurrent)
if (OpenThreads::Thread::CurrentThreadId()==_threadOfLastMakeCurrent)
{
return _display;
}