Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TTHREADMESSAGE_H
Toshihiro Shimizu 890ddd
#define TTHREADMESSAGE_H
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
//! HOW TO USE: subclass TThread::Message (MyMessage, in example) defining what
Shinya Kitaoka 120a6e
//! to execute in onDeliver method and than, to send a message to be executed in
Shinya Kitaoka 120a6e
//! the main thread:
Toshihiro Shimizu 890ddd
//  MyMessage().send();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
#include <qmutex></qmutex>
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TNZCORE_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace TThread {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool DVAPI isMainThread();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// This class is used for communication between different threads. Calling the
Shinya Kitaoka 120a6e
// 'send' method in a thread, the user defined method 'onDeliver' is executed in
Shinya Kitaoka 120a6e
// the mainThread.
Shinya Kitaoka 120a6e
// using 'sendblocking', the calling thread will block until the main thread has
Shinya Kitaoka 120a6e
// executed the onDeliver function.
Shinya Kitaoka 120a6e
// WARNING!! the sendblocking method will cause a deadlock if used in a
Shinya Kitaoka 120a6e
// executable without main loop! (such as composer, cleanupper, etc.)
luz paz 6454c4
// NOTE: if the 'send' is called in the main thread, then onDeliver is executed
Shinya Kitaoka 120a6e
// immediately.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
class DVAPI Message {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Message();
Shinya Kitaoka 120a6e
  virtual ~Message(){};
Shinya Kitaoka 120a6e
  virtual Message *clone() const = 0;
Shinya Kitaoka 120a6e
  virtual void onDeliver()       = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void send();
Shinya Kitaoka 120a6e
  void sendBlocking();
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI Mutex {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  HANDLE m_mutex;
Shinya Kitaoka 120a6e
  Mutex() { m_mutex = CreateMutex(NULL, FALSE, NULL); }
Shinya Kitaoka 120a6e
  ~Mutex() { CloseHandle(m_mutex); }
Shinya Kitaoka 120a6e
  void lock() {
Shinya Kitaoka 120a6e
    WaitForSingleObject(m_mutex,  // handle to mutex
Shinya Kitaoka 120a6e
                        INFINITE);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void unlock() { ReleaseMutex(m_mutex); }
Shinya Kitaoka 120a6e
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // not implemented
Shinya Kitaoka 120a6e
  Mutex(const Mutex &);
Shinya Kitaoka 120a6e
  Mutex &operator=(const Mutex &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI MutexLocker {
Shinya Kitaoka 120a6e
  HANDLE m_mutex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  MutexLocker(Mutex *mutex) : m_mutex(mutex->m_mutex) {
Shinya Kitaoka 120a6e
    WaitForSingleObject(m_mutex, INFINITE);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ~MutexLocker() { ReleaseMutex(m_mutex); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class DVAPI Mutex final : public QMutex {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Mutex() : QMutex(QMutex::Recursive) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // not implemented
Shinya Kitaoka 120a6e
  Mutex(const Mutex &);
Shinya Kitaoka 120a6e
  Mutex &operator=(const Mutex &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef QMutexLocker MutexLocker;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace  TThread
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif