Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tthreadmessage.h"
Toshihiro Shimizu 890ddd
#include "tthreadp.h"
Toshihiro Shimizu 890ddd
#include <qthread></qthread>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QThread *MainThread = QThread::currentThread();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThreadMessageDispatcher *Dispatcher; //MUST BE CREATED  in the main thread!!!!!!
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TThread::isMainThread()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return MainThread == QThread::currentThread();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThreadMessageDispatcher::TThreadMessageDispatcher()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(signaled(TThread::Message *)), this, SLOT(onSignal(TThread::Message *)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(blockingSignaled(TThread::Message *)), this, SLOT(onSignal(TThread::Message *)), Qt::BlockingQueuedConnection);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TThreadMessageDispatcher::init()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!TThread::isMainThread())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (Dispatcher == 0)
Toshihiro Shimizu 890ddd
		Dispatcher = new TThreadMessageDispatcher();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThreadMessageDispatcher *TThreadMessageDispatcher::instance()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(Dispatcher);
Toshihiro Shimizu 890ddd
	return Dispatcher;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TThreadMessageDispatcher::emitSignaled(TThread::Message *msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Q_EMIT signaled(msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TThreadMessageDispatcher::emitBlockingSignaled(TThread::Message *msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Q_EMIT blockingSignaled(msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TThreadMessageDispatcher::onSignal(TThread::Message *msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	msg->onDeliver();
Toshihiro Shimizu 890ddd
	delete msg;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThread::Message::Message()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
void TThread::Message::send()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (isMainThread())
Toshihiro Shimizu 890ddd
		onDeliver();
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		TThreadMessageDispatcher::instance()->emitSignaled(clone());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TThread::Message::sendBlocking()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (isMainThread())
Toshihiro Shimizu 890ddd
		onDeliver();
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		TThreadMessageDispatcher::instance()->emitBlockingSignaled(clone());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------