platform/shared/qt/rhodes/impl/RhoThreadImpl.h in rhodes-6.2.0 vs platform/shared/qt/rhodes/impl/RhoThreadImpl.h in rhodes-7.1.17

- old
+ new

@@ -25,17 +25,26 @@ *------------------------------------------------------------------------*/ #ifndef RHOTHREADIMPL_H #define RHOTHREADIMPL_H -#include <QThread> #include "common/IRhoThreadImpl.h" #include "logging/RhoLog.h" + +#ifndef RHODES_VERSION_LIBRARY +#include <QThread> #include <QSharedPointer> #include <QMutex> #include <QMutexLocker> #include <QTimer> +#else +#include <thread> +#include <mutex> +#include <memory> +#include <sstream> +#include <condition_variable> +#endif namespace rho{ namespace common{ class CRhoThreadImpl : public IRhoThreadImpl @@ -52,10 +61,13 @@ virtual void sleep(unsigned int nTimeout); private: void setThreadPriority(IRhoRunnable::EPriority ePriority); rho::String threadId; private: + + +#ifndef RHODES_VERSION_LIBRARY class QtThread: public QThread { public: QtThread():QThread(){} virtual ~QtThread(){ @@ -66,11 +78,10 @@ terminate(); } } }; - class QRhoThread: public QtThread { public: QRhoThread(IRhoRunnable* pRunnable): QtThread(), m_Runnable(pRunnable) {} void run() { m_Runnable->runObject(); } @@ -81,12 +92,78 @@ QSharedPointer<QRhoThread> m_Thread; QSharedPointer<QtThread> m_waitThread; QSharedPointer<QMutex> mutex; QSharedPointer<QMutex> mutexWaiter; +#else + class QRhoThread + { + friend CRhoThreadImpl; + bool runnig = false; + std::mutex waiter; + std::condition_variable wait_cond; + public: + QRhoThread(IRhoRunnable* pRunnable): runnig(false), m_Runnable(pRunnable) {} + + ~QRhoThread() + { + if (isRunning()){ + TerminateThread(_thr.native_handle(), 0); + } + } + + void run() { + runnig = true; + m_Runnable->runObject(); + runnig = false; + } + + bool wait(unsigned int nTimeout) + { + try + { + std::unique_lock<std::mutex> lock(waiter); + std::chrono::duration<uint64_t, std::milli> _timeout (static_cast<uint64_t>(nTimeout)); + wait_cond.wait_for(lock, _timeout); + return true; + } + catch(std::system_error& err) + { + LOG(ERROR) + err.what(); + return false; + } + } + + void stopWait() + { + wait_cond.notify_one(); + } + + void start() + { + _thr = std::thread(&QRhoThread::run, this); + } + + static void sleep(unsigned long timeout) { + std::chrono::duration<uint64_t, std::milli> _timeout (static_cast<uint64_t>(timeout)); + std::this_thread::sleep_for(_timeout); + } + bool isRunning() + { + return runnig; + } + private: + IRhoRunnable* m_Runnable; + std::thread _thr; + }; + std::shared_ptr<QRhoThread> m_Thread; + std::mutex mutex; + +#endif }; } } + #endif // RHOTHREADIMPL_H