All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Member Functions | Static Public Member Functions
QGpCoreTools::Thread Class Reference

Thread with an internal stack to record trace points. More...

#include <Thread.h>

Inheritance diagram for QGpCoreTools::Thread:
Acquisition ArrayCore::KmaxSolver DinverCore::Generator FileReader GeopsyCore::AsciiSignalFormatReader GeopsyCore::CityScanner InversionThread Process QGpCoreTools::ColumnTextParser QGpCoreTools::LoopTask RealTimeArrayProcess RealTimeArrayProcess ResultsThread SciFigs::LayerPainterThread

List of all members.

Public Member Functions

QStack< TraceBug * > * bugStack ()
QVector< TraceLog * > * logVector ()
void setSleeping (bool s)
bool sleeping ()
void start (QThread::Priority priority=QThread::InheritPriority)
void startNoThread ()
 Thread (QObject *parent=0)
 ~Thread ()

Static Public Member Functions

static QList< QStack< TraceBug * > * > bugStacks ()
static ThreadcurrentThread ()
static int idealThreadCount ()
static void lockThreads ()
static QList< QVector
< TraceLog * > * > 
logVectors ()
static void msleep (unsigned long msecs)
static void sleep (unsigned long secs)
static QStringList threadNames ()
static void unlockThreads ()
static void usleep (unsigned long usecs)

Detailed Description

Thread with an internal stack to record trace points.


Constructor & Destructor Documentation

QGpCoreTools::Thread::Thread ( QObject *  parent = 0)
   : QThread(parent)
{
  QMutexLocker ml(&_threadsMutex);
  _threads.append(this);
  _sleeping=true;
}

References QGpCoreTools::CoreApplication::instance(), and QGpCoreTools::CoreApplicationPrivate::threadDeleted().

{
  QMutexLocker ml(&_threadsMutex);
  int index=_threads.indexOf(this);
  if(index>=0) {
    _threads.removeAt(index);
    CoreApplication::instance()->threadDeleted(this);
  }
  qDeleteAll(_logVector);
}

Member Function Documentation

QStack<TraceBug *>* QGpCoreTools::Thread::bugStack ( ) [inline]

Referenced by QGpCoreTools::TraceBug::TraceBug().

{return &_bugStack;}
QList< QStack< TraceBug * > * > QGpCoreTools::Thread::bugStacks ( ) [static]

References QGpCoreTools::TraceBug::mainStack().

Referenced by QGpCoreTools::CoreApplicationPrivate::backTraceBug().

{
  QList< QStack<TraceBug *> * > list;
  list.append(TraceBug::mainStack());
  for(QList<Thread *>::iterator it=_threads.begin(); it!=_threads.end(); it++ ) {
    list.append((*it)->bugStack());
  }
  return list;
}
static Thread* QGpCoreTools::Thread::currentThread ( ) [inline, static]

Referenced by msleep(), sleep(), and usleep().

{return static_cast<Thread *>(QThread::currentThread());}

An implementation that tries to take already running threads into account. The return number is always less or equal to the number of physical cores. Never returns a number less than 1.

References QGpCoreTools::CoreApplication::instance(), and QGpCoreTools::CoreApplicationPrivate::maximumThreadCount().

Referenced by QGpCoreTools::ParallelLoop::start().

{
  int nThreads=CoreApplication::instance()->maximumThreadCount();
  if(nThreads<2) return 1; // Exit as soon as possible for single processor machines
  QSet<Thread *>::iterator it;
  QMutexLocker ml(&_threadsMutex);
  for(it=_runningThreads.begin(); it!=_runningThreads.end(); it++) {
    if(!(*it)->sleeping()) nThreads--;
  }
  return nThreads>0 ? nThreads : 1;
}
static void QGpCoreTools::Thread::lockThreads ( ) [inline, static]

Referenced by QGpCoreTools::TraceLog::TraceLog().

{return &_logVector;}
QList< QVector< TraceLog * > * > QGpCoreTools::Thread::logVectors ( ) [static]

References QGpCoreTools::TraceLog::mainVector().

Referenced by QGpCoreTools::CoreApplicationPrivate::backTraceBug().

{
  QList< QVector<TraceLog *> * > list;
  list.append(TraceLog::mainVector());
  for(QList<Thread *>::iterator it=_threads.begin(); it!=_threads.end(); it++ ) {
    list.append((*it)->logVector());
  }
  return list;
}
void QGpCoreTools::Thread::msleep ( unsigned long  msecs) [static]

References currentThread(), and setSleeping().

Referenced by Process::run().

{
  Thread * t=currentThread();
  _threadsMutex.lock();
  if(msecs>10 && _runningThreads.contains(t)) {
    _threadsMutex.unlock();
    t->setSleeping(true);
    QThread::msleep(msecs);
    t->setSleeping(false);
  } else {
    _threadsMutex.unlock();
    QThread::msleep(msecs);
  }
}
void QGpCoreTools::Thread::setSleeping ( bool  s) [inline]

Referenced by msleep(), SciFigs::LayerPainterThread::run(), sleep(), and usleep().

{_sleeping.fetchAndStoreOrdered(s);}
void QGpCoreTools::Thread::sleep ( unsigned long  secs) [static]

References currentThread(), and setSleeping().

Referenced by Acquisition::stop().

{
  Thread * t = currentThread();
  _threadsMutex.lock();
  if (_runningThreads.contains( t ) ) {
    _threadsMutex.unlock();
    t->setSleeping(true);
    QThread::sleep(secs);
    t->setSleeping(false);
  } else {
    _threadsMutex.unlock();
    QThread::sleep( secs );
  }
}
bool QGpCoreTools::Thread::sleeping ( ) [inline]
{return _sleeping.testAndSetOrdered(true, true);}
void QGpCoreTools::Thread::start ( QThread::Priority  priority = QThread::InheritPriority)

Referenced by SciFigs::LayerPainter::LayerPainter(), SciFigs::CurveBrowser::loadMultiColumns(), main(), RealTimeArrayManager::RealTimeArrayManager(), GeopsyCore::AsciiSignalFormatReader::setFileName(), GeopsyCore::AsciiSignalFormatReader::setFormat(), GeopsyCore::CityScanner::setPath(), QGpCoreTools::ParallelLoop::start(), and QGpCoreTools::ColumnTextParser::startUpdates().

{
  _threadsMutex.lock();
  _runningThreads.insert(this);
  _threadsMutex.unlock();
  _sleeping=false;
  QThread::start(priority);
  _sleeping=true;
  _threadsMutex.lock();
  _runningThreads.remove(this);
  _threadsMutex.unlock();
}

Referenced by QGpCoreTools::ParallelLoop::start().

{
  run();
  emit QThread::finished();
}
QStringList QGpCoreTools::Thread::threadNames ( ) [static]

Referenced by QGpCoreTools::CoreApplicationPrivate::backTrace(), QGpCoreTools::CoreApplicationPrivate::backTraceBug(), and QGpCoreTools::CoreApplicationPrivate::bugInfo().

{
  QMutexLocker ml(&_threadsMutex);
  QStringList list;
  list.append("main");
  for(QList<Thread *>::iterator it=_threads.begin(); it!=_threads.end(); it++ ) {
    list.append((*it)->objectName());
  }
  return list;
}
static void QGpCoreTools::Thread::unlockThreads ( ) [inline, static]
void QGpCoreTools::Thread::usleep ( unsigned long  usecs) [static]

References currentThread(), and setSleeping().

{
  Thread * t = currentThread();
  _threadsMutex.lock();
  if (usecs>10000 && _runningThreads.contains(t)) {
    _threadsMutex.unlock();
    t->setSleeping(true);
    QThread::usleep(usecs);
    t->setSleeping(false);
  } else {
    _threadsMutex.unlock();
    QThread::usleep(usecs);
  }
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines