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
Shinya Kitaoka 120a6e
TThreadMessageDispatcher
Shinya Kitaoka 120a6e
    *Dispatcher;  // MUST BE CREATED  in the main thread!!!!!!
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TThread::isMainThread() { return MainThread == QThread::currentThread(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThreadMessageDispatcher::TThreadMessageDispatcher() {
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(signaled(TThread::Message *)), this,
Shinya Kitaoka 120a6e
          SLOT(onSignal(TThread::Message *)));
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(blockingSignaled(TThread::Message *)), this,
Shinya Kitaoka 120a6e
          SLOT(onSignal(TThread::Message *)), Qt::BlockingQueuedConnection);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThreadMessageDispatcher::init() {
Shinya Kitaoka 120a6e
  if (!TThread::isMainThread()) return;
Shinya Kitaoka 120a6e
  if (Dispatcher == 0) Dispatcher = new TThreadMessageDispatcher();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThreadMessageDispatcher *TThreadMessageDispatcher::instance() {
Shinya Kitaoka 120a6e
  assert(Dispatcher);
Shinya Kitaoka 120a6e
  return Dispatcher;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThreadMessageDispatcher::emitSignaled(TThread::Message *msg) {
Shinya Kitaoka 120a6e
  Q_EMIT signaled(msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThreadMessageDispatcher::emitBlockingSignaled(TThread::Message *msg) {
Shinya Kitaoka 120a6e
  Q_EMIT blockingSignaled(msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThreadMessageDispatcher::onSignal(TThread::Message *msg) {
Shinya Kitaoka 120a6e
  msg->onDeliver();
Shinya Kitaoka 120a6e
  delete msg;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThread::Message::Message() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
void TThread::Message::send() {
Shinya Kitaoka 120a6e
  if (isMainThread())
Shinya Kitaoka 120a6e
    onDeliver();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    TThreadMessageDispatcher::instance()->emitSignaled(clone());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThread::Message::sendBlocking() {
Shinya Kitaoka 120a6e
  if (isMainThread())
Shinya Kitaoka 120a6e
    onDeliver();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    TThreadMessageDispatcher::instance()->emitBlockingSignaled(clone());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------