Improved indentation to make it more readable and consistent with OSG.

This commit is contained in:
Robert Osfield
2010-12-21 10:23:31 +00:00
parent 7507242891
commit 56a8df7ba7

View File

@@ -71,7 +71,8 @@ Thread::ThreadPriority Thread::s_masterThreadPriority =
bool Thread::s_isInitialized = false;
pthread_key_t PThreadPrivateData::s_tls_key;
struct ThreadCleanupStruct {
struct ThreadCleanupStruct
{
OpenThreads::Thread *thread;
volatile bool *runflag;
@@ -82,7 +83,8 @@ struct ThreadCleanupStruct {
// This cleanup handler is necessary to ensure that the thread will cleanup
// and set its isRunning flag properly.
//
void thread_cleanup_handler(void *arg) {
void thread_cleanup_handler(void *arg)
{
ThreadCleanupStruct *tcs = static_cast<ThreadCleanupStruct *>(arg);
@@ -96,9 +98,11 @@ void thread_cleanup_handler(void *arg) {
// correctly.
//
namespace OpenThreads {
namespace OpenThreads
{
class ThreadPrivateActions {
class ThreadPrivateActions
{
//-------------------------------------------------------------------------
// We're friendly to Thread, so it can issue the methods.
@@ -110,13 +114,14 @@ private:
//-------------------------------------------------------------------------
// pthreads standard start routine.
//
static void *StartThread(void *data) {
static void *StartThread(void *data)
{
Thread *thread = static_cast<Thread *>(data);
Thread *thread = static_cast<Thread *>(data);
PThreadPrivateData *pd =
PThreadPrivateData *pd =
static_cast<PThreadPrivateData *>(thread->_prvData);
if (pd->cpunum>=0)
{
@@ -136,27 +141,27 @@ private:
#endif
#endif
}
ThreadCleanupStruct tcs;
tcs.thread = thread;
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);
if (status)
ThreadCleanupStruct tcs;
tcs.thread = thread;
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);
if (status)
{
printf("Error: pthread_setspecific(,) returned error status, status = %d\n",status);
}
pthread_cleanup_push(thread_cleanup_handler, &tcs);
pthread_cleanup_push(thread_cleanup_handler, &tcs);
#ifdef ALLOW_PRIORITY_SCHEDULING
//---------------------------------------------------------------------
// Set the proper scheduling priorities
//
SetThreadSchedulingParams(thread);
//---------------------------------------------------------------------
// Set the proper scheduling priorities
//
SetThreadSchedulingParams(thread);
#endif // ] ALLOW_PRIORITY_SCHEDULING
@@ -169,7 +174,7 @@ private:
pd->isRunning = false;
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return 0;
@@ -182,46 +187,49 @@ private:
#ifdef ALLOW_PRIORITY_SCHEDULING // [
if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING)) {
if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING))
{
int status, my_policy, min_priority, max_priority;
struct sched_param my_param;
int status, my_policy, min_priority, max_priority;
struct sched_param my_param;
status = pthread_getschedparam(thread->getProcessId(),
&my_policy,
&my_param);
status = pthread_getschedparam(thread->getProcessId(),
&my_policy,
&my_param);
if(status != 0) {
printf("THREAD INFO (%d) : Get sched: %s\n",
thread->getProcessId(),
strerror(status));
} else {
printf(
"THREAD INFO (%d) : Thread running at %s / Priority: %d\n",
thread->getProcessId(),
(my_policy == SCHED_FIFO ? "SCHEDULE_FIFO"
: (my_policy == SCHED_RR ? "SCHEDULE_ROUND_ROBIN"
: (my_policy == SCHED_OTHER ? "SCHEDULE_OTHER"
: "UNKNOWN"))),
my_param.sched_priority);
if(status != 0) {
printf("THREAD INFO (%d) : Get sched: %s\n",
thread->getProcessId(),
strerror(status));
} else {
printf(
"THREAD INFO (%d) : Thread running at %s / Priority: %d\n",
thread->getProcessId(),
(my_policy == SCHED_FIFO ? "SCHEDULE_FIFO"
: (my_policy == SCHED_RR ? "SCHEDULE_ROUND_ROBIN"
: (my_policy == SCHED_OTHER ? "SCHEDULE_OTHER"
: "UNKNOWN"))),
my_param.sched_priority);
max_priority = sched_get_priority_max(my_policy);
min_priority = sched_get_priority_min(my_policy);
max_priority = sched_get_priority_max(my_policy);
min_priority = sched_get_priority_min(my_policy);
printf(
"THREAD INFO (%d) : Max priority: %d, Min priority: %d\n",
thread->getProcessId(),
max_priority, min_priority);
printf(
"THREAD INFO (%d) : Max priority: %d, Min priority: %d\n",
thread->getProcessId(),
max_priority, min_priority);
}
}
else
{
printf(
"THREAD INFO (%d) POSIX Priority scheduling not available\n",
thread->getProcessId());
}
} else {
printf(
"THREAD INFO (%d) POSIX Priority scheduling not available\n",
thread->getProcessId());
}
fflush(stdout);
fflush(stdout);
#endif // ] ALLOW_PRIORITY_SCHEDULING
@@ -231,101 +239,105 @@ private:
// Set thread scheduling parameters. Unfortunately on Linux, there's no
// good way to set this, as pthread_setschedparam is mostly a no-op.
//
static int SetThreadSchedulingParams(Thread *thread) {
static int SetThreadSchedulingParams(Thread *thread)
{
int status = 0;
int status = 0;
#ifdef ALLOW_PRIORITY_SCHEDULING // [
if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING)) {
if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING))
{
int th_policy;
int max_priority, nominal_priority, min_priority;
sched_param th_param;
pthread_getschedparam(thread->getProcessId(),
&th_policy, &th_param);
int th_policy;
int max_priority, nominal_priority, min_priority;
sched_param th_param;
pthread_getschedparam(thread->getProcessId(),
&th_policy, &th_param);
#ifndef __linux__
switch(thread->getSchedulePolicy()) {
switch(thread->getSchedulePolicy())
{
case Thread::THREAD_SCHEDULE_FIFO:
th_policy = SCHED_FIFO;
break;
case Thread::THREAD_SCHEDULE_FIFO:
th_policy = SCHED_FIFO;
break;
case Thread::THREAD_SCHEDULE_ROUND_ROBIN:
th_policy = SCHED_RR;
break;
case Thread::THREAD_SCHEDULE_ROUND_ROBIN:
th_policy = SCHED_RR;
break;
case Thread::THREAD_SCHEDULE_TIME_SHARE:
th_policy = SCHED_OTHER;
break;
case Thread::THREAD_SCHEDULE_TIME_SHARE:
th_policy = SCHED_OTHER;
break;
default:
default:
#ifdef __sgi
th_policy = SCHED_RR;
th_policy = SCHED_RR;
#else
th_policy = SCHED_FIFO;
th_policy = SCHED_FIFO;
#endif
break;
};
break;
};
#else
th_policy = SCHED_OTHER; // Must protect linux from realtime.
th_policy = SCHED_OTHER; // Must protect linux from realtime.
#endif
#ifdef __linux__
max_priority = 0;
min_priority = 20;
nominal_priority = (max_priority + min_priority)/2;
max_priority = 0;
min_priority = 20;
nominal_priority = (max_priority + min_priority)/2;
#else
max_priority = sched_get_priority_max(th_policy);
min_priority = sched_get_priority_min(th_policy);
nominal_priority = (max_priority + min_priority)/2;
max_priority = sched_get_priority_max(th_policy);
min_priority = sched_get_priority_min(th_policy);
nominal_priority = (max_priority + min_priority)/2;
#endif
switch(thread->getSchedulePriority()) {
switch(thread->getSchedulePriority())
{
case Thread::THREAD_PRIORITY_MAX:
th_param.sched_priority = max_priority;
break;
case Thread::THREAD_PRIORITY_MAX:
th_param.sched_priority = max_priority;
break;
case Thread::THREAD_PRIORITY_HIGH:
th_param.sched_priority = (max_priority + nominal_priority)/2;
break;
case Thread::THREAD_PRIORITY_HIGH:
th_param.sched_priority = (max_priority + nominal_priority)/2;
break;
case Thread::THREAD_PRIORITY_NOMINAL:
th_param.sched_priority = nominal_priority;
break;
case Thread::THREAD_PRIORITY_NOMINAL:
th_param.sched_priority = nominal_priority;
break;
case Thread::THREAD_PRIORITY_LOW:
th_param.sched_priority = (min_priority + nominal_priority)/2;
break;
case Thread::THREAD_PRIORITY_LOW:
th_param.sched_priority = (min_priority + nominal_priority)/2;
break;
case Thread::THREAD_PRIORITY_MIN:
th_param.sched_priority = min_priority;
break;
case Thread::THREAD_PRIORITY_MIN:
th_param.sched_priority = min_priority;
break;
default:
th_param.sched_priority = max_priority;
break;
default:
th_param.sched_priority = max_priority;
break;
}
status = pthread_setschedparam(thread->getProcessId(),
th_policy,
&th_param);
if(getenv("OUTPUT_THREADLIB_SCHEDULING_INFO") != 0)
PrintThreadSchedulingInfo(thread);
}
status = pthread_setschedparam(thread->getProcessId(),
th_policy,
&th_param);
if(getenv("OUTPUT_THREADLIB_SCHEDULING_INFO") != 0)
PrintThreadSchedulingInfo(thread);
}
#endif // ] ALLOW_PRIORITY_SCHEDULING
return status;
@@ -340,7 +352,8 @@ private:
//
// Use static public
//
int Thread::SetConcurrency(int concurrencyLevel) {
int Thread::SetConcurrency(int concurrencyLevel)
{
#if defined (HAVE_PTHREAD_SETCONCURRENCY)
return pthread_setconcurrency(concurrencyLevel);
@@ -356,7 +369,8 @@ int Thread::SetConcurrency(int concurrencyLevel) {
//
// Use static public
//
int Thread::GetConcurrency() {
int Thread::GetConcurrency()
{
#if defined (HAVE_PTHREAD_GETCONCURRENCY)
return pthread_getconcurrency();
@@ -372,7 +386,8 @@ int Thread::GetConcurrency() {
//
// Use: public.
//
Thread::Thread() {
Thread::Thread()
{
if(!s_isInitialized) Init();
@@ -406,10 +421,10 @@ Thread::~Thread()
{
std::cout<<"Error: Thread "<<this<<" still running in destructor"<<std::endl;
//---------------------------------------------------------------------
// Kill the thread when it is destructed
//
cancel();
//---------------------------------------------------------------------
// Kill the thread when it is destructed
//
cancel();
}
delete pd;
@@ -434,7 +449,8 @@ Thread *Thread::CurrentThread()
//
// Use: public.
//
void Thread::Init() {
void Thread::Init()
{
if(s_isInitialized) return;
@@ -450,30 +466,33 @@ void Thread::Init() {
//--------------------------------------------------------------------------
// If we've got priority scheduling, set things to nominal.
//
if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING)) {
if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING))
{
int max_priority, nominal_priority, min_priority;
int max_priority, nominal_priority, min_priority;
int th_policy;
sched_param th_param;
pthread_getschedparam(pthread_self(),
&th_policy, &th_param);
int th_policy;
sched_param th_param;
pthread_getschedparam(pthread_self(),
&th_policy, &th_param);
max_priority = sched_get_priority_max(th_policy);
min_priority = sched_get_priority_min(th_policy);
nominal_priority = (max_priority + min_priority)/2;
max_priority = sched_get_priority_max(th_policy);
min_priority = sched_get_priority_min(th_policy);
nominal_priority = (max_priority + min_priority)/2;
th_param.sched_priority = nominal_priority;
th_param.sched_priority = nominal_priority;
pthread_setschedparam(pthread_self(),
th_policy,
&th_param);
pthread_setschedparam(pthread_self(),
th_policy,
&th_param);
s_masterThreadPriority = Thread::THREAD_PRIORITY_NOMINAL;
s_masterThreadPriority = Thread::THREAD_PRIORITY_NOMINAL;
} else {
}
else
{
s_masterThreadPriority = Thread::THREAD_PRIORITY_DEFAULT;
s_masterThreadPriority = Thread::THREAD_PRIORITY_DEFAULT;
}
@@ -489,7 +508,8 @@ void Thread::Init() {
//
// Use: public
//
int Thread::getThreadId() {
int Thread::getThreadId()
{
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
return pd->uniqueId;
@@ -501,7 +521,8 @@ int Thread::getThreadId() {
//
// Use: public
//
size_t Thread::getProcessId() {
size_t Thread::getProcessId()
{
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
@@ -528,9 +549,10 @@ int Thread::setProcessorAffinity(unsigned int cpunum)
pthread_attr_t thread_attr;
status = pthread_attr_init( &thread_attr );
if(status != 0) {
if(status != 0)
{
return status;
}
}
status = pthread_attr_setscope( &thread_attr, PTHREAD_SCOPE_BOUND_NP );
return status;
@@ -564,8 +586,8 @@ int Thread::setProcessorAffinity(unsigned int cpunum)
//
// Use: public
//
bool Thread::isRunning() {
bool Thread::isRunning()
{
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
return pd->isRunning;
@@ -583,8 +605,9 @@ int Thread::start() {
pthread_attr_t thread_attr;
status = pthread_attr_init( &thread_attr );
if(status != 0) {
return status;
if(status != 0)
{
return status;
}
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
@@ -593,13 +616,15 @@ int Thread::start() {
// Set the stack size if requested, but not less than a platform reasonable
// value.
//
if(pd->stackSize) {
if(pd->stackSize)
{
#ifdef PTHREAD_STACK_MIN
if(pd->stackSize < PTHREAD_STACK_MIN)
pd->stackSize = PTHREAD_STACK_MIN;
#endif
pthread_attr_setstacksize( &thread_attr, pd->stackSize);
if(status != 0) {
if(status != 0)
{
return status;
}
}
@@ -609,8 +634,9 @@ int Thread::start() {
//
size_t size;
pthread_attr_getstacksize( &thread_attr, &size);
if(status != 0) {
return status;
if(status != 0)
{
return status;
}
pd->stackSize = size;
@@ -628,8 +654,9 @@ int Thread::start() {
#endif // ] ALLOW_PRIORITY_SCHEDULING
if(status != 0) {
return status;
if(status != 0)
{
return status;
}
pd->threadStartedBlock.reset();
@@ -638,7 +665,8 @@ int Thread::start() {
ThreadPrivateActions::StartThread,
static_cast<void *>(this));
if(status == 0) {
if(status == 0)
{
// wait till the thread has actually started.
pd->threadStartedBlock.block();
@@ -662,15 +690,14 @@ int Thread::startThread()
//-----------------------------------------------------------------------------
//
// Description: Join the thread.
// Description: detach the thread.
//
// Use: public
//
int Thread::detach() {
int Thread::detach()
{
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
return pthread_detach(pd->tid);
}
//-----------------------------------------------------------------------------
@@ -679,7 +706,8 @@ int Thread::detach() {
//
// Use: public
//
int Thread::join() {
int Thread::join()
{
void *threadResult = 0; // Dummy var.
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
@@ -693,7 +721,8 @@ int Thread::join() {
//
// Use: public
//
int Thread::testCancel() {
int Thread::testCancel()
{
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
@@ -713,7 +742,8 @@ int Thread::testCancel() {
//
// Use: public
//
int Thread::cancel() {
int Thread::cancel()
{
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
if (pd->isRunning)
@@ -731,7 +761,8 @@ int Thread::cancel() {
//
// Use: public
//
int Thread::setCancelModeDisable() {
int Thread::setCancelModeDisable()
{
return pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, 0 );
@@ -781,9 +812,9 @@ int Thread::setSchedulePriority(ThreadPriority priority) {
pd->threadPriority = priority;
if(pd->isRunning)
return ThreadPrivateActions::SetThreadSchedulingParams(this);
return ThreadPrivateActions::SetThreadSchedulingParams(this);
else
return 0;
return 0;
#else
return -1;
@@ -811,7 +842,8 @@ int Thread::getSchedulePriority() {
//
// Use: public
//
int Thread::setSchedulePolicy(ThreadPolicy policy) {
int Thread::setSchedulePolicy(ThreadPolicy policy)
{
#ifdef ALLOW_PRIORITY_SCHEDULING
@@ -835,7 +867,8 @@ int Thread::setSchedulePolicy(ThreadPolicy policy) {
//
// Use: public
//
int Thread::getSchedulePolicy() {
int Thread::getSchedulePolicy()
{
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
@@ -868,7 +901,8 @@ int Thread::setStackSize(size_t stackSize) {
//
// Use: public
//
size_t Thread::getStackSize() {
size_t Thread::getStackSize()
{
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
@@ -882,7 +916,8 @@ size_t Thread::getStackSize() {
//
// Use: public
//
void Thread::printSchedulingInfo() {
void Thread::printSchedulingInfo()
{
ThreadPrivateActions::PrintThreadSchedulingInfo(this);