diff --git a/include/OpenThreads/Thread b/include/OpenThreads/Thread index 4d0d71562..b644fee1c 100644 --- a/include/OpenThreads/Thread +++ b/include/OpenThreads/Thread @@ -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 diff --git a/include/osg/GraphicsContext b/include/osg/GraphicsContext index 33d7b0b12..fc2371f9d 100644 --- a/include/osg/GraphicsContext +++ b/include/osg/GraphicsContext @@ -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 _operationsBlock; diff --git a/src/OpenThreads/pthreads/PThread.cpp b/src/OpenThreads/pthreads/PThread.cpp index 06762af22..7cac33085 100644 --- a/src/OpenThreads/pthreads/PThread.cpp +++ b/src/OpenThreads/pthreads/PThread.cpp @@ -48,6 +48,11 @@ #endif #endif +#if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__ANDROID__) +# include +# include +#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(data); - PThreadPrivateData *pd = - static_cast(thread->_prvData); + PThreadPrivateData *pd = static_cast(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 (_prvData); diff --git a/src/OpenThreads/pthreads/PThreadPrivateData.h b/src/OpenThreads/pthreads/PThreadPrivateData.h index bbbe535cb..731a0a404 100644 --- a/src/OpenThreads/pthreads/PThreadPrivateData.h +++ b/src/OpenThreads/pthreads/PThreadPrivateData.h @@ -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; }; diff --git a/src/OpenThreads/win32/Win32Thread.cpp b/src/OpenThreads/win32/Win32Thread.cpp index ce1db0b73..6b06ff564 100644 --- a/src/OpenThreads/win32/Win32Thread.cpp +++ b/src/OpenThreads/win32/Win32Thread.cpp @@ -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 (_prvData); return pd->uniqueId; } @@ -352,7 +359,7 @@ int Thread::start() { pd->tid.set( (void*)_beginthreadex(NULL,static_cast(pd->stackSize),ThreadPrivateActions::StartThread,static_cast(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; diff --git a/src/OpenThreads/win32/Win32ThreadPrivateData.h b/src/OpenThreads/win32/Win32ThreadPrivateData.h index f7d4497cc..282af54b9 100644 --- a/src/OpenThreads/win32/Win32ThreadPrivateData.h +++ b/src/OpenThreads/win32/Win32ThreadPrivateData.h @@ -54,7 +54,7 @@ private: HandleHolder tid; - int uniqueId; + size_t uniqueId; Affinity affinity; diff --git a/src/osg/GraphicsContext.cpp b/src/osg/GraphicsContext.cpp index 1a35497d0..0d14aaf8e 100644 --- a/src/osg/GraphicsContext.cpp +++ b/src/osg/GraphicsContext.cpp @@ -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); } diff --git a/src/osgPlugins/curl/ReaderWriterCURL.h b/src/osgPlugins/curl/ReaderWriterCURL.h index 783954646..b06f8ca2c 100644 --- a/src/osgPlugins/curl/ReaderWriterCURL.h +++ b/src/osgPlugins/curl/ReaderWriterCURL.h @@ -165,7 +165,7 @@ class ReaderWriterCURL : public osgDB::ReaderWriter { OpenThreads::ScopedLock lock(_threadCurlMapMutex); - osg::ref_ptr& ec = _threadCurlMap[OpenThreads::Thread::CurrentThread()]; + osg::ref_ptr& 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 > ThreadCurlMap; + typedef std::map< size_t, osg::ref_ptr > ThreadCurlMap; mutable OpenThreads::Mutex _threadCurlMapMutex; mutable ThreadCurlMap _threadCurlMap; diff --git a/src/osgPlugins/zip/ZipArchive.cpp b/src/osgPlugins/zip/ZipArchive.cpp index 70d15a31d..02b2417dc 100644 --- a/src/osgPlugins/zip/ZipArchive.cpp +++ b/src/osgPlugins/zip/ZipArchive.cpp @@ -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 ); diff --git a/src/osgPlugins/zip/ZipArchive.h b/src/osgPlugins/zip/ZipArchive.h index c1d6e1e78..958695a83 100644 --- a/src/osgPlugins/zip/ZipArchive.h +++ b/src/osgPlugins/zip/ZipArchive.h @@ -92,7 +92,7 @@ class ZipArchive : public osgDB::Archive HZIP _zipHandle; }; - typedef std::map PerThreadDataMap; + typedef std::map PerThreadDataMap; PerThreadDataMap _perThreadData; const PerThreadData& getData() const; diff --git a/src/osgViewer/GraphicsWindowX11.cpp b/src/osgViewer/GraphicsWindowX11.cpp index 7df251323..c0d0b0e80 100644 --- a/src/osgViewer/GraphicsWindowX11.cpp +++ b/src/osgViewer/GraphicsWindowX11.cpp @@ -279,7 +279,7 @@ Display* GraphicsWindowX11::getDisplayToUse() const return _display; } - if (OpenThreads::Thread::CurrentThread()==_threadOfLastMakeCurrent) + if (OpenThreads::Thread::CurrentThreadId()==_threadOfLastMakeCurrent) { return _display; }