Change isRunning variable to an Atomic to address possible race condition asscoiated with reading and writing to the variable from different threads.
git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14464 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
@@ -83,7 +83,7 @@ struct ThreadCleanupStruct
|
||||
{
|
||||
|
||||
OpenThreads::Thread *thread;
|
||||
volatile bool *runflag;
|
||||
Atomic *runflag;
|
||||
|
||||
};
|
||||
|
||||
@@ -97,7 +97,7 @@ void thread_cleanup_handler(void *arg)
|
||||
ThreadCleanupStruct *tcs = static_cast<ThreadCleanupStruct *>(arg);
|
||||
|
||||
tcs->thread->cancelCleanup();
|
||||
*(tcs->runflag) = false;
|
||||
(*(tcs->runflag)).exchange(0);
|
||||
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ private:
|
||||
|
||||
ThreadCleanupStruct tcs;
|
||||
tcs.thread = thread;
|
||||
tcs.runflag = &pd->isRunning;
|
||||
tcs.runflag = &pd->_isRunning;
|
||||
|
||||
// Set local storage so that Thread::CurrentThread() can return the right thing
|
||||
int status = pthread_setspecific(PThreadPrivateData::s_tls_key, thread);
|
||||
@@ -196,14 +196,14 @@ private:
|
||||
|
||||
#endif // ] ALLOW_PRIORITY_SCHEDULING
|
||||
|
||||
pd->isRunning = true;
|
||||
pd->setRunning(true);
|
||||
|
||||
// release the thread that created this thread.
|
||||
pd->threadStartedBlock.release();
|
||||
|
||||
thread->run();
|
||||
|
||||
pd->isRunning = false;
|
||||
pd->setRunning(false);
|
||||
|
||||
pthread_cleanup_pop(0);
|
||||
|
||||
@@ -426,7 +426,7 @@ Thread::Thread()
|
||||
pd->stackSize = 0;
|
||||
pd->stackSizeLocked = false;
|
||||
pd->idSet = false;
|
||||
pd->isRunning = false;
|
||||
pd->setRunning(false);
|
||||
pd->isCanceled = false;
|
||||
pd->uniqueId = pd->nextId;
|
||||
pd->nextId++;
|
||||
@@ -448,7 +448,7 @@ Thread::~Thread()
|
||||
{
|
||||
PThreadPrivateData *pd = static_cast<PThreadPrivateData *>(_prvData);
|
||||
|
||||
if(pd->isRunning)
|
||||
if(pd->isRunning())
|
||||
{
|
||||
std::cout<<"Error: Thread "<<this<<" still running in destructor"<<std::endl;
|
||||
|
||||
@@ -594,7 +594,7 @@ int Thread::setProcessorAffinity(unsigned int cpunum)
|
||||
|
||||
#elif defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
|
||||
|
||||
if (pd->isRunning && Thread::CurrentThread()==this)
|
||||
if (pd->isRunning() && Thread::CurrentThread()==this)
|
||||
{
|
||||
cpu_set_t cpumask;
|
||||
CPU_ZERO( &cpumask );
|
||||
@@ -624,7 +624,7 @@ int Thread::setProcessorAffinity(unsigned int cpunum)
|
||||
bool Thread::isRunning()
|
||||
{
|
||||
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
|
||||
return pd->isRunning;
|
||||
return pd->isRunning();
|
||||
|
||||
}
|
||||
|
||||
@@ -783,7 +783,7 @@ int Thread::cancel()
|
||||
{
|
||||
#if defined(HAVE_PTHREAD_CANCEL)
|
||||
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
|
||||
if (pd->isRunning)
|
||||
if (pd->isRunning())
|
||||
{
|
||||
pd->isCanceled = true;
|
||||
int status = pthread_cancel(pd->tid);
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <pthread.h>
|
||||
#include <OpenThreads/Thread>
|
||||
#include <OpenThreads/Block>
|
||||
#include <OpenThreads/Atomic>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
@@ -33,7 +34,7 @@ class PThreadPrivateData {
|
||||
friend class Thread;
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// We're friendly to PThreadPrivateActions, so it can get at some
|
||||
// We're friendly to PThreadPrivateActions, so it can get at some
|
||||
// variables.
|
||||
//
|
||||
friend class ThreadPrivateActions;
|
||||
@@ -48,7 +49,10 @@ private:
|
||||
|
||||
volatile bool stackSizeLocked;
|
||||
|
||||
volatile bool isRunning;
|
||||
void setRunning(bool flag) { _isRunning.exchange(flag); }
|
||||
bool isRunning() const { return _isRunning!=0; }
|
||||
|
||||
Atomic _isRunning;
|
||||
|
||||
Block threadStartedBlock;
|
||||
|
||||
@@ -57,7 +61,7 @@ private:
|
||||
volatile bool idSet;
|
||||
|
||||
volatile Thread::ThreadPriority threadPriority;
|
||||
|
||||
|
||||
volatile Thread::ThreadPolicy threadPolicy;
|
||||
|
||||
pthread_t tid;
|
||||
@@ -65,7 +69,7 @@ private:
|
||||
volatile int uniqueId;
|
||||
|
||||
volatile int cpunum;
|
||||
|
||||
|
||||
|
||||
static int nextId;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user