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