Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TTHREADMESSAGE_H
Toshihiro Shimizu 890ddd
#define TTHREADMESSAGE_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! HOW TO USE: subclass TThread::Message (MyMessage, in example) defining what to execute in onDeliver method and than, to send a message to be executed in 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
Toshihiro Shimizu 890ddd
namespace TThread
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool DVAPI isMainThread();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//This class is used for communication between different threads. Calling the 'send' method in a thread, the user defined method 'onDeliver' is executed in the mainThread.
Toshihiro Shimizu 890ddd
//using 'sendblocking', the calling thread will block until the main thread has executed the onDeliver function.
Toshihiro Shimizu 890ddd
//WARNING!! the sendblocking method will cause a deadlock if used in a executable without main loop! (such as composer, cleanupper, etc.)
Toshihiro Shimizu 890ddd
//NOTE: if the 'send' is called in the main thread, ther onDeliver is executed immediately.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI Message
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Message();
Toshihiro Shimizu 890ddd
	virtual ~Message(){};
Toshihiro Shimizu 890ddd
	virtual Message *clone() const = 0;
Toshihiro Shimizu 890ddd
	virtual void onDeliver() = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void send();
Toshihiro Shimizu 890ddd
	void sendBlocking();
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI Mutex
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	HANDLE m_mutex;
Toshihiro Shimizu 890ddd
	Mutex() { m_mutex = CreateMutex(NULL, FALSE, NULL); }
Toshihiro Shimizu 890ddd
	~Mutex() { CloseHandle(m_mutex); }
Toshihiro Shimizu 890ddd
	void lock() { WaitForSingleObject(
Toshihiro Shimizu 890ddd
		m_mutex, // handle to mutex
Toshihiro Shimizu 890ddd
		INFINITE); }
Toshihiro Shimizu 890ddd
	void unlock() { ReleaseMutex(m_mutex); }
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// not implemented
Toshihiro Shimizu 890ddd
	Mutex(const Mutex &);
Toshihiro Shimizu 890ddd
	Mutex &operator=(const Mutex &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI MutexLocker
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	HANDLE m_mutex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	MutexLocker(Mutex *mutex) : m_mutex(mutex->m_mutex) { WaitForSingleObject(m_mutex, INFINITE); }
Toshihiro Shimizu 890ddd
	~MutexLocker() { ReleaseMutex(m_mutex); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI Mutex : public QMutex
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Mutex() : QMutex(QMutex::Recursive) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// not implemented
Toshihiro Shimizu 890ddd
	Mutex(const Mutex &);
Toshihiro Shimizu 890ddd
	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
Toshihiro Shimizu 890ddd
} //namespace  TThread
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif