tahoma2d/toonz/sources/common/tcore/tthread_x.cpp

280 lines
6.9 KiB
C++
Raw Normal View History

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