platform/shared/qt/rhodes/impl/RhoThreadImpl.cpp in rhodes-6.0.11 vs platform/shared/qt/rhodes/impl/RhoThreadImpl.cpp in rhodes-6.2.0

- old
+ new

@@ -28,230 +28,155 @@ #ifdef OS_WINDOWS_DESKTOP #include "common/RhoPort.h" #include "RhoThreadImpl.h" - +#include <QApplication> using namespace rho::common; IMPLEMENT_LOGCLASS(CRhoThreadImpl,"RhoThreadQT"); +#define RHOQTPREFIX ("RHOQT " + threadId + " ") -CRhoThreadImpl::CRhoThreadImpl(): m_Thread(0), m_waitThread(0) +CRhoThreadImpl::CRhoThreadImpl(): m_waitThread(new QtThread()) { -#if defined(OS_WINDOWS_DESKTOP) - LOG(INFO) + "RHOQT CRhoThreadImpl cons-before InitializeCriticalSections"; - InitializeCriticalSection(&gCS); - InitializeCriticalSection(&gCSstopwait); - LOG(TRACE) + "RHOQT CRhoThreadImpl cons-after InitializeCriticalSections"; -#endif + mutex = QSharedPointer<QMutex>(new QMutex()); + mutexWaiter = QSharedPointer<QMutex>(new QMutex()); + threadId = QString::number((quint64)this, 16).prepend("0x").toStdString(); + QObject::connect(qApp, SIGNAL(destroyed(QObject*)), m_waitThread.data(), SLOT(quit())); } CRhoThreadImpl::~CRhoThreadImpl() { - LOG(INFO) + "RHOQT CRhoThreadImpl destructor"; - + LOG(TRACE) + RHOQTPREFIX + "CRhoThreadImpl destructor"; + stopWait(); + stop(0); - if (m_Thread) - { - LOG(TRACE) + "RHOQT CRhoThreadImpl destructor before delete m_Thread"; - delete m_Thread; - LOG(TRACE) + "RHOQT CRhoThreadImpl destructor after delete m_Thread"; - } - if (m_waitThread) - { - LOG(TRACE) + "RHOQT CRhoThreadImpl destructor before delete m_waitThread "; - delete m_waitThread; - LOG(TRACE) + "RHOQT CRhoThreadImpl destructor before after m_waitThread "; - } + LOG(TRACE) + RHOQTPREFIX + "~CRhoThreadImpl - before entering mutex locker"; + QSharedPointer<QMutex> l_mutex = mutex; + QMutexLocker locker(l_mutex.data()); + LOG(TRACE) + RHOQTPREFIX + "~CRhoThreadImpl - after entering mutex locker"; + if (!m_Thread.isNull()) + { + LOG(TRACE) + RHOQTPREFIX + "CRhoThreadImpl destructor before delete m_Thread"; + m_Thread = QSharedPointer<QRhoThread>(); + LOG(TRACE) + RHOQTPREFIX + "CRhoThreadImpl destructor after delete m_Thread"; + } - -#if defined(OS_WINDOWS_DESKTOP) - LOG(TRACE) + "RHOQT CRhoThreadImpl destructor-before DeleteCriticalSection stopwait"; - DeleteCriticalSection(&gCSstopwait); - LOG(TRACE) + "RHOQT CRhoThreadImpl destructor-after DeleteCriticalSection stopwait"; - LOG(TRACE) + "RHOQT CRhoThreadImpl destructor-before DeleteCriticalSection stop"; - DeleteCriticalSection(&gCS); - LOG(TRACE) + "RHOQT CRhoThreadImpl destructor-after DeleteCriticalSection stop"; - -#endif } void CRhoThreadImpl::start(IRhoRunnable* pRunnable, IRhoRunnable::EPriority ePriority) { - LOG(TRACE) + "RHOQT start"; - if (m_Thread) + LOG(TRACE) + RHOQTPREFIX + "start"; + if (!m_Thread.isNull()) { - LOG(TRACE) + "RHOQT start-m_Thread exists before calling stop"; + LOG(TRACE) + RHOQTPREFIX + "start - m_Thread exists before calling stop"; stop(0); - LOG(TRACE) + "RHOQT start-m_Thread exists after calling stop"; + LOG(TRACE) + RHOQTPREFIX + "start - m_Thread exists after calling stop"; + }else{ + LOG(TRACE) + RHOQTPREFIX + "start - m_Thread does not exist do not call stop"; } - else - { - LOG(TRACE) + "RHOQT start-m_Thread does not exist do not call stop"; - } - LOG(TRACE) + "RHOQT start-before Qthread start"; - m_Thread = new QRhoThread(pRunnable); + LOG(TRACE) + RHOQTPREFIX + "start - before Qthread start"; + m_Thread = QSharedPointer<QRhoThread>(new QRhoThread(pRunnable)); + + QObject::connect(qApp, SIGNAL(destroyed(QObject*)), m_Thread.data(), SLOT(terminate())); + m_Thread->start(); - LOG(TRACE) + "RHOQT start-after Qthread start"; + LOG(TRACE) + RHOQTPREFIX + "start - after Qthread start"; setThreadPriority(ePriority); - LOG(TRACE) + "RHOQT start-finish aftersetThreadPriority"; + LOG(TRACE) + RHOQTPREFIX + "start - finish aftersetThreadPriority"; + } void CRhoThreadImpl::setThreadPriority(IRhoRunnable::EPriority ePriority) { QThread::Priority nPriority = QThread::NormalPriority; - if ( ePriority == IRhoRunnable::epHigh ) - nPriority = QThread::HighestPriority; - else if (ePriority == IRhoRunnable::epLow) - nPriority = QThread::LowestPriority; - m_Thread->setPriority(nPriority); + if ( ePriority == IRhoRunnable::epHigh ) nPriority = QThread::HighestPriority; + else if (ePriority == IRhoRunnable::epLow) nPriority = QThread::LowestPriority; + m_Thread.data()->setPriority(nPriority); } void CRhoThreadImpl::stop(unsigned int nTimeoutToKill) { - LOG(INFO) + "RHOQT stop"; - - - #if defined(OS_WINDOWS_DESKTOP) - LOG(TRACE) + "RHOQT stop-before enter Crticalsection"; - EnterCriticalSection(&gCS); -LOG(TRACE) + "RHOQT stop-after enter Crticalsection"; -#endif - LOG(TRACE) + "RHOQT stop-before calling stopWait"; - stopWait(); - LOG(TRACE) + "RHOQT stop-after calling stopWait"; - - if ( m_Thread ) - { - LOG(TRACE) + "RHOQT stop-m_Thread exists before m_Thread->quit()"; - m_Thread->quit(); - LOG(TRACE) + "RHOQT stop-m_Thread exists after m_Thread->quit()"; - //m_Thread->wait(nTimeoutToKill); - if (!m_Thread->isRunning()) - { - LOG(TRACE) + "RHOQT stop-m_Thread not running before m_Thread->terminate();"; - m_Thread->terminate(); - LOG(TRACE) + "RHOQT stop-m_Thread not running after m_Thread->terminate()"; - LOG(TRACE) + "RHOQT stop-m_Thread not running before m_Thread->wait()"; - m_Thread->wait(); - LOG(TRACE) + "RHOQT stop-m_Thread not running after m_Thread->wait()"; + LOG(TRACE) + RHOQTPREFIX + "stop"; + + LOG(TRACE) + RHOQTPREFIX + "stop - before enter entering mutex locker"; + QSharedPointer<QMutex> l_mutex = mutex; + QMutexLocker locker(l_mutex.data()); + LOG(TRACE) + RHOQTPREFIX + "stop - after enter entering mutex locker"; + + + LOG(TRACE) + RHOQTPREFIX + "stop - before calling stopWait"; + stopWait(); + LOG(TRACE) + RHOQTPREFIX + "stop - after calling stopWait"; + + if ( !m_Thread.isNull() ) { + LOG(TRACE) + RHOQTPREFIX + "stop - m_Thread exists before m_Thread->quit()"; + m_Thread.data()->quit(); + LOG(TRACE) + RHOQTPREFIX + "stop - m_Thread exists after m_Thread->quit()"; + if (!m_Thread.data()->isRunning()){ + LOG(TRACE) + RHOQTPREFIX + "stop - m_Thread not running before m_Thread->terminate();"; + m_Thread.data()->terminate(); + LOG(TRACE) + RHOQTPREFIX + "stop - m_Thread not running after m_Thread->terminate()"; } - else - { - LOG(TRACE) + "RHOQT stop-m_Thread running-before m_Thread->wait()"; - m_Thread->wait(); - LOG(TRACE) + "RHOQT stop-m_Thread running-after m_Thread->wait()"; + if (nTimeoutToKill){ + LOG(TRACE) + RHOQTPREFIX + "stop - m_Thread before m_Thread->wait()"; + m_Thread.data()->wait(nTimeoutToKill); + LOG(TRACE) + RHOQTPREFIX + "stop - m_Thread after m_Thread->wait()"; } - LOG(TRACE) + "RHOQT stop-before delete m_Thread"; - delete m_Thread; - LOG(TRACE) + "RHOQT stop-after delete m_Thread"; - m_Thread = 0; - + LOG(TRACE) + RHOQTPREFIX + "stop - before delete m_Thread"; + m_Thread = QSharedPointer<QRhoThread>(); + LOG(TRACE) + RHOQTPREFIX + "stop - after delete m_Thread"; } - -#if defined(OS_WINDOWS_DESKTOP) - LOG(TRACE) + "RHOQT stop-before LeaveCriticalSection"; - LeaveCriticalSection(&gCS); - LOG(TRACE) + "RHOQT stop-after LeaveCriticalSection"; -#endif -LOG(TRACE) + "RHOQT stop-finish"; + + LOG(TRACE) + "RHOQT stop - finish"; } int CRhoThreadImpl::wait(unsigned int nTimeoutMs) { - LOG(INFO) + "RHOQT wait"; - bool isVeyBigTimeoutvalue = false; - - if ((nTimeoutMs == 4294966296)||(nTimeoutMs == 4294967295)){ - isVeyBigTimeoutvalue = true; - } + LOG(TRACE) + RHOQTPREFIX + "wait"; + bool isVeyBigTimeoutvalue = false; + if ((nTimeoutMs == 4294966296)||(nTimeoutMs == 4294967295)){isVeyBigTimeoutvalue = true;} - if (m_waitThread){ - LOG(INFO) + "RHOQT wait-m_waitThread exists before calling stopWait"; - stopWait(); - LOG(TRACE) + "RHOQT wait-m_waitThread exists after calling stopWait"; - }else{ - LOG(TRACE) + "RHOQT wait-m_waitThread does not existdo not call stopWait"; - } + LOG(TRACE) + RHOQTPREFIX + "wait - m_waitThread exists before calling stopWait"; + stopWait(); + LOG(TRACE) + RHOQTPREFIX + "wait - m_waitThread exists after calling stopWait"; - LOG(TRACE) + "RHOQT wait-before m_waitThread->start"; - m_waitThread = new QThread(); - m_waitThread->start(); - LOG(INFO) + "RHOQT wait-after m_waitThread->start"; - + QSharedPointer<QMutex> l_mutexWaiter = mutexWaiter; + QMutexLocker waitLocker(l_mutexWaiter.data()); + + QSharedPointer<QtThread> l_waitThread = m_waitThread; + LOG(TRACE) + RHOQTPREFIX + "wait - before m_waitThread->start"; + l_waitThread.data()->start(); + LOG(TRACE) + RHOQTPREFIX + "wait - after m_waitThread->start"; bool result; - if(isVeyBigTimeoutvalue) { - LOG(INFO) + "RHOQT wait-before wait for a long time nTimeoutMs:- "+nTimeoutMs; - result = m_waitThread->wait(1000UL*nTimeoutMs) ? 0 : 1; - LOG(INFO) + "RHOQT wait-after wait for a long time Result:- "+result; + LOG(TRACE) + RHOQTPREFIX + "wait - before wait for a long time nTimeoutMs:- "+nTimeoutMs; + waitLocker.unlock(); + result = l_waitThread.data()->wait(1000UL*nTimeoutMs) ? 0 : 1; + LOG(TRACE) + RHOQTPREFIX + "wait - after wait for a long time Result:- "+result; }else{ - LOG(INFO) + "RHOQT wait-before wait for a short time nTimeoutMs:- "+nTimeoutMs; - result = m_waitThread->wait(1UL*nTimeoutMs) ? 0 : 1; - LOG(INFO) + "RHOQT wait-after wait for a short time Result:- "+result; + LOG(TRACE) + RHOQTPREFIX + "wait - before wait for a short time nTimeoutMs:- "+nTimeoutMs; + waitLocker.unlock(); + result = l_waitThread.data()->wait(1UL*nTimeoutMs) ? 0 : 1; + LOG(TRACE) + RHOQTPREFIX + "wait - after wait for a short time Result:- "+result; } - - - if(m_waitThread){ - LOG(INFO) + "RHOQT wait-m_waitThread exists again-before EnterCriticalSection"; - #if defined(OS_WINDOWS_DESKTOP) - EnterCriticalSection(&gCSstopwait); - LOG(TRACE) + "RHOQT wait-m_waitThread exists again-after EnterCriticalSection"; - #endif - if(m_waitThread){ - LOG(INFO) + "RHOQT wait:-m_waitThread exists again 2-before Delete m_waitThread"; - m_waitThread->terminate(); - m_waitThread->deleteLater(); - LOG(INFO) + "RHOQT wait:-m_waitThread exists again 2-after Delete m_waitThread"; - m_waitThread = 0; - }else{ - LOG(INFO) + "RHOQT wait:-m_waitThread does not exist again 2-do not Delete m_waitThread"; - } -#if defined(OS_WINDOWS_DESKTOP) - LOG(TRACE) + "RHOQT wait-m_waitThread exists again-before LeaveCriticalSection"; - LeaveCriticalSection(&gCSstopwait); - LOG(TRACE) + "RHOQT wait-m_waitThread exists again-after LeaveCriticalSection"; -#endif - } - LOG(TRACE) + "RHOQT wait-finish before return "; + LOG(TRACE) + RHOQTPREFIX + "wait - finish before return"; return result; } void CRhoThreadImpl::stopWait() { - - LOG(INFO) + "RHOQT stopWait"; - LOG(INFO) + "RHOQT stopWait- before 100 ms sleep"; - QRhoThread::msleep(100); - LOG(TRACE) + "RHOQT stopWait- after 100 ms sleep"; - - if (m_waitThread) { - #if defined(OS_WINDOWS_DESKTOP) - LOG(INFO) + "RHOQT stopWait-m_waitThread exists -before EnterCriticalSection"; - EnterCriticalSection(&gCSstopwait); - LOG(TRACE) + "RHOQT stopWait-m_waitThread exists -after EnterCriticalSection"; - #endif - if (m_waitThread) { - LOG(INFO) + "RHOQT stopWait-m_waitThread exists again- before Terminate waitthread"; - m_waitThread->terminate(); - LOG(TRACE) + "RHOQT stopWait-m_waitThread exists again- after Terminate waitthread"; - }else{ - LOG(INFO) + "RHOQT stopWait-m_waitThread does not exist now- do not Terminate waitthread"; - } - - #if defined(OS_WINDOWS_DESKTOP) - LOG(TRACE) + "RHOQT stopWait-m_waitThread exists-before LeaveCriticalSection"; - LeaveCriticalSection(&gCSstopwait); - LOG(TRACE) + "RHOQT stopWait-m_waitThread exists-after LeaveCriticalSection"; - #endif - - }else{ - LOG(TRACE) + "RHOQT stopWait-m_waitThread does not exist-do not try to terminate"; - } - + LOG(TRACE) + RHOQTPREFIX + "stopWait - before mutex"; + QSharedPointer<QMutex> l_mutexWaiter = mutexWaiter; + QMutexLocker waitLocker(l_mutexWaiter.data()); + LOG(TRACE) + RHOQTPREFIX + "stopWait - after mutex and before msleep"; + QRhoThread::msleep(2); + LOG(TRACE) + RHOQTPREFIX + "stopWait - before quit()"; + m_waitThread.data()->quit(); + LOG(TRACE) + RHOQTPREFIX + "stopWait - after quit()"; } void CRhoThreadImpl::sleep(unsigned int nTimeout) { QRhoThread::sleep(nTimeout);