tahoma2d/toonz/sources/include/tthreadmessage.h

105 lines
2.3 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TTHREADMESSAGE_H
#define TTHREADMESSAGE_H
2016-06-15 18:43:10 +12:00
//! 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:
2016-03-19 06:57:51 +13:00
// MyMessage().send();
#ifndef TNZCORE_LIGHT
#include <QMutex>
#else
#include <windows.h>
#endif
#undef DVAPI
#undef DVVAR
#ifdef TNZCORE_EXPORTS
#define DVAPI DV_EXPORT_API
#define DVVAR DV_EXPORT_VAR
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
#endif
#include "tcommon.h"
2016-06-15 18:43:10 +12:00
namespace TThread {
2016-03-19 06:57:51 +13:00
bool DVAPI isMainThread();
2016-06-15 18:43:10 +12:00
// 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.
// using 'sendblocking', the calling thread will block until the main thread has
// executed the onDeliver function.
// WARNING!! the sendblocking method will cause a deadlock if used in a
// executable without main loop! (such as composer, cleanupper, etc.)
// NOTE: if the 'send' is called in the main thread, ther onDeliver is executed
// immediately.
class DVAPI Message {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
Message();
virtual ~Message(){};
virtual Message *clone() const = 0;
virtual void onDeliver() = 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void send();
void sendBlocking();
2016-03-19 06:57:51 +13:00
};
#ifdef TNZCORE_LIGHT
2016-06-15 18:43:10 +12:00
class DVAPI Mutex {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
HANDLE m_mutex;
Mutex() { m_mutex = CreateMutex(NULL, FALSE, NULL); }
~Mutex() { CloseHandle(m_mutex); }
void lock() {
WaitForSingleObject(m_mutex, // handle to mutex
INFINITE);
}
void unlock() { ReleaseMutex(m_mutex); }
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
// not implemented
Mutex(const Mutex &);
Mutex &operator=(const Mutex &);
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
class DVAPI MutexLocker {
HANDLE m_mutex;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
MutexLocker(Mutex *mutex) : m_mutex(mutex->m_mutex) {
WaitForSingleObject(m_mutex, INFINITE);
}
~MutexLocker() { ReleaseMutex(m_mutex); }
2016-03-19 06:57:51 +13:00
};
#else
class DVAPI Mutex final : public QMutex {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
Mutex() : QMutex(QMutex::Recursive) {}
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
// not implemented
Mutex(const Mutex &);
Mutex &operator=(const Mutex &);
2016-03-19 06:57:51 +13:00
};
typedef QMutexLocker MutexLocker;
#endif
2016-06-15 18:43:10 +12:00
} // namespace TThread
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#endif