Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tthread.h"
Toshihiro Shimizu 890ddd
#include <pthread.h></pthread.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//    TMutex & TMutexImp
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class TMutexImp {
Shinya Kitaoka 120a6e
  pthread_mutex_t id;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TMutexImp();
Shinya Kitaoka 120a6e
  ~TMutexImp();
Shinya Kitaoka 120a6e
  void lock();
Shinya Kitaoka 120a6e
  void unlock();
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TMutex lockForTheList;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class TThreadGroupImp {
Shinya Kitaoka 120a6e
  list<tthread *=""> threads;</tthread>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TThreadGroupImp();
Shinya Kitaoka 120a6e
  ~TThreadGroupImp();
Shinya Kitaoka 120a6e
  void add(TThread *);
Shinya Kitaoka 120a6e
  void remove(TThread *);
Shinya Kitaoka 120a6e
  void wait();
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TMutexImp::TMutexImp() { pthread_mutex_init(&id, 0); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TMutexImp::~TMutexImp() { pthread_mutex_destroy(&id); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMutexImp::lock() { pthread_mutex_lock(&id); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMutexImp::unlock() { pthread_mutex_unlock(&id); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TMutex::TMutex() : m_imp(new TMutexImp) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TMutex::~TMutex() { delete m_imp; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMutex::lock() { m_imp->lock(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMutex::unlock() { m_imp->unlock(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//    TThread & TThreadImp
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class TThreadImp {
Shinya Kitaoka 120a6e
  pthread_t threadId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TThreadImp();
Shinya Kitaoka 120a6e
  ~TThreadImp();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TThread *thread;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void start();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool setThreadPriority(TThread::TThreadPriority p);
Shinya Kitaoka 120a6e
  bool setPreferredProcessor(int processorId);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TMutex secureLock;
Shinya Kitaoka 120a6e
  bool isRunning;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  static void incNThreads() {
Shinya Kitaoka 120a6e
    mutex.lock();
Shinya Kitaoka 120a6e
    nThreads++;
Shinya Kitaoka 120a6e
    mutex.unlock();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  static void decNThreads() {
Shinya Kitaoka 120a6e
    mutex.lock();
Shinya Kitaoka 120a6e
    nThreads--;
Shinya Kitaoka 120a6e
    mutex.unlock();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // some static stuff
Shinya Kitaoka 120a6e
  static TUINT32 nThreads;
Shinya Kitaoka 120a6e
  static TMutex mutex;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  friend class TThreadGroupImp;
Shinya Kitaoka 120a6e
  void setOwner(TThreadGroupImp *_owner) { owner = _owner; }
Shinya Kitaoka 120a6e
  TThreadGroupImp *owner;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUINT32 TThreadImp::nThreads = 0;
Shinya Kitaoka 120a6e
TMutex TThreadImp::mutex     = TMutex();
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThreadImp::TThreadImp() : isRunning(false), owner(0), thread(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThreadImp::~TThreadImp() {
Shinya Kitaoka 120a6e
  // CloseHandle(threadId);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void * /*__stdcall*/ fun(void *data) {
Shinya Kitaoka 120a6e
  TThreadImp *t = (TThreadImp *)data;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  t->secureLock.lock();
Shinya Kitaoka 120a6e
  if (t->isRunning) {
Shinya Kitaoka 120a6e
    t->secureLock.unlock();
Shinya Kitaoka 120a6e
    assert(!"thread is already running");
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  t->isRunning = true;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  t->secureLock.unlock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  t->thread->run();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  t->decNThreads();
Shinya Kitaoka 120a6e
  if (t->owner) t->owner->remove(t->thread);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThreadImp::start() {
Shinya Kitaoka 120a6e
  incNThreads();
Shinya Kitaoka 120a6e
  pthread_create(&threadId, 0, fun, (void *)this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TThreadImp::setThreadPriority(TThread::TThreadPriority) {
Shinya Kitaoka 120a6e
  assert(!"not implemented");
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TThreadImp::setPreferredProcessor(int processorId) {
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
#if (OP_RELEASE == rel_2)
Shinya Kitaoka 120a6e
  assert(!"Not implemented");
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  int rc = pthread_setrunon_np(processorId);
Shinya Kitaoka 120a6e
  return (rc != -1);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  assert(0);
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThread::TThread() : m_imp(new TThreadImp()) { m_imp->thread = this; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThread::~TThread() { delete m_imp; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThread::start() { m_imp->start(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TThread::setPreferredProcessor(int processorId) {
Shinya Kitaoka 120a6e
  return m_imp->setPreferredProcessor(processorId);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TThread::setThreadPriority(TThread::TThreadPriority p) {
Shinya Kitaoka 120a6e
  return m_imp->setThreadPriority(p);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=======================
Toshihiro Shimizu 890ddd
//    TThreadGroupImp
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThreadGroupImp::TThreadGroupImp() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThreadGroupImp::~TThreadGroupImp() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThreadGroupImp::add(TThread *t) {
Shinya Kitaoka 120a6e
  lockForTheList.lock();
Shinya Kitaoka 120a6e
  threads.push_back(t);
Shinya Kitaoka 120a6e
  lockForTheList.unlock();
Shinya Kitaoka 120a6e
  t->m_imp->setOwner(this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThreadGroupImp::remove(TThread *t) {
Shinya Kitaoka 120a6e
  lockForTheList.lock();
Shinya Kitaoka 120a6e
  threads.remove(t);
Shinya Kitaoka 120a6e
  lockForTheList.unlock();
Shinya Kitaoka 120a6e
  t->m_imp->setOwner(0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void * /*__stdcall*/ mainFun(void *data) {
Shinya Kitaoka 120a6e
  // cout << "mainfun" << endl;
Shinya Kitaoka 120a6e
  list<tthread *=""> *threads = (list<tthread *=""> *)data;</tthread></tthread>
Shinya Kitaoka 120a6e
  // lockForTheList.lock();
Shinya Kitaoka 120a6e
  ULONG s = threads->size();
Shinya Kitaoka 120a6e
  // lockForTheList.unlock();
Shinya Kitaoka 120a6e
  // cout <<"ci sono " << s << "thread in ballo..." << endl;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (s != 0) {
Shinya Kitaoka 120a6e
    lockForTheList.lock();
Shinya Kitaoka 120a6e
    s = threads->size();
Shinya Kitaoka 120a6e
    lockForTheList.unlock();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Shinya Kitaoka 120a6e
void TThreadGroupImp::wait() {
Shinya Kitaoka 120a6e
  // cout << "wait()" << endl;
Shinya Kitaoka 120a6e
  lockForTheList.lock();
Shinya Kitaoka 120a6e
  ULONG count = threads.size();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (list<tthread *="">::iterator it = threads.begin(); it != threads.end();</tthread>
Shinya Kitaoka 120a6e
       it++) {
Shinya Kitaoka 120a6e
    TThread *t = *it;
Shinya Kitaoka 120a6e
    t->start();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  lockForTheList.unlock();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (count == 0) return;
Shinya Kitaoka 120a6e
  void *mainRet = 0;
Shinya Kitaoka 120a6e
  pthread_t mainThread;
Shinya Kitaoka 120a6e
  // cout << "creo il main" << endl;
Shinya Kitaoka 120a6e
  pthread_create(&mainThread, 0, mainFun, &threads);
Shinya Kitaoka 120a6e
  // cout << mainThread << endl;
Shinya Kitaoka 120a6e
  pthread_join(mainThread, &mainRet);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThreadGroup::TThreadGroup()
Shinya Kitaoka 120a6e
    : m_imp(new TThreadGroupImp())
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
{}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThreadGroup::~TThreadGroup() { delete m_imp; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThreadGroup::add(TThread *t) { m_imp->add(t); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThreadGroup::wait() { m_imp->wait(); }