Index: src/libchcore/TWorkerThreadController.cpp =================================================================== diff -u -N -r6103ac74583f2136b821dc67515ed8469abd8155 -re96806b7f8ff7ca7e9f4afbea603e6351a3dc3e3 --- src/libchcore/TWorkerThreadController.cpp (.../TWorkerThreadController.cpp) (revision 6103ac74583f2136b821dc67515ed8469abd8155) +++ src/libchcore/TWorkerThreadController.cpp (.../TWorkerThreadController.cpp) (revision e96806b7f8ff7ca7e9f4afbea603e6351a3dc3e3) @@ -25,197 +25,196 @@ #include "TCoreException.h" #include "ErrorCodes.h" -BEGIN_CHCORE_NAMESPACE - -TWorkerThreadController::TWorkerThreadController() : - m_hThread(NULL), - m_hKillThread(NULL) +namespace chcore { - m_hKillThread = CreateEvent(NULL, TRUE, FALSE, NULL); - if(!m_hKillThread) - THROW_CORE_EXCEPTION_WIN32(eErr_CannotCreateEvent, GetLastError()); -} - -TWorkerThreadController::~TWorkerThreadController() -{ - try + TWorkerThreadController::TWorkerThreadController() : + m_hThread(NULL), + m_hKillThread(NULL) { - StopThread(); - CloseHandle(m_hKillThread); + m_hKillThread = CreateEvent(NULL, TRUE, FALSE, NULL); + if (!m_hKillThread) + THROW_CORE_EXCEPTION_WIN32(eErr_CannotCreateEvent, GetLastError()); } - catch(...) + + TWorkerThreadController::~TWorkerThreadController() { + try + { + StopThread(); + CloseHandle(m_hKillThread); + } + catch (...) + { + } } -} -void TWorkerThreadController::StartThread(PTHREAD_START_ROUTINE pThreadFunction, PVOID pThreadParam, int iPriority) -{ - boost::upgrade_lock lock(m_lock); + void TWorkerThreadController::StartThread(PTHREAD_START_ROUTINE pThreadFunction, PVOID pThreadParam, int iPriority) + { + boost::upgrade_lock lock(m_lock); - RemoveZombieData(lock); + RemoveZombieData(lock); - if(m_hThread) - THROW_CORE_EXCEPTION(eErr_ThreadAlreadyStarted); + if (m_hThread) + THROW_CORE_EXCEPTION(eErr_ThreadAlreadyStarted); - // just in case reset the kill event to avoid early death of the thread to be created - if(!::ResetEvent(m_hKillThread)) - THROW_CORE_EXCEPTION_WIN32(eErr_CannotResetEvent, GetLastError()); + // just in case reset the kill event to avoid early death of the thread to be created + if (!::ResetEvent(m_hKillThread)) + THROW_CORE_EXCEPTION_WIN32(eErr_CannotResetEvent, GetLastError()); - boost::upgrade_to_unique_lock lock_upgraded(lock); + boost::upgrade_to_unique_lock lock_upgraded(lock); - m_hThread = ::CreateThread(NULL, 0, pThreadFunction, pThreadParam, CREATE_SUSPENDED, NULL); - if(!m_hThread) - THROW_CORE_EXCEPTION_WIN32(eErr_CannotCreateThread, GetLastError()); + m_hThread = ::CreateThread(NULL, 0, pThreadFunction, pThreadParam, CREATE_SUSPENDED, NULL); + if (!m_hThread) + THROW_CORE_EXCEPTION_WIN32(eErr_CannotCreateThread, GetLastError()); - if(!::SetThreadPriority(m_hThread, iPriority)) - { - DWORD dwLastError = GetLastError(); + if (!::SetThreadPriority(m_hThread, iPriority)) + { + DWORD dwLastError = GetLastError(); - CloseHandle(m_hThread); - m_hThread = NULL; + CloseHandle(m_hThread); + m_hThread = NULL; - THROW_CORE_EXCEPTION_WIN32(eErr_CannotChangeThreadPriority, dwLastError); - } + THROW_CORE_EXCEPTION_WIN32(eErr_CannotChangeThreadPriority, dwLastError); + } - if(::ResumeThread(m_hThread) == (DWORD)-1) - { - DWORD dwLastError = GetLastError(); + if (::ResumeThread(m_hThread) == (DWORD)-1) + { + DWORD dwLastError = GetLastError(); - CloseHandle(m_hThread); - m_hThread = NULL; + CloseHandle(m_hThread); + m_hThread = NULL; - THROW_CORE_EXCEPTION_WIN32(eErr_CannotResumeThread, dwLastError); + THROW_CORE_EXCEPTION_WIN32(eErr_CannotResumeThread, dwLastError); + } } -} -void TWorkerThreadController::SignalThreadToStop() -{ - boost::upgrade_lock lock(m_lock); - - SignalThreadToStop(lock); -} - -void TWorkerThreadController::WaitForThreadToExit() -{ - boost::upgrade_lock lock(m_lock); - - DWORD dwRes = WaitForSingleObject(m_hThread, INFINITE); - if(dwRes == WAIT_OBJECT_0) + void TWorkerThreadController::SignalThreadToStop() { - if(!::ResetEvent(m_hKillThread)) - THROW_CORE_EXCEPTION_WIN32(eErr_CannotResetEvent, GetLastError()); + boost::upgrade_lock lock(m_lock); - boost::upgrade_to_unique_lock lock_upgraded(lock); - - CloseHandle(m_hThread); - m_hThread = NULL; + SignalThreadToStop(lock); } - else - THROW_CORE_EXCEPTION_WIN32(eErr_WaitingFailed, GetLastError()); -} -void TWorkerThreadController::StopThread() -{ - boost::upgrade_lock lock(m_lock); + void TWorkerThreadController::WaitForThreadToExit() + { + boost::upgrade_lock lock(m_lock); - SignalThreadToStop(lock); + DWORD dwRes = WaitForSingleObject(m_hThread, INFINITE); + if (dwRes == WAIT_OBJECT_0) + { + if (!::ResetEvent(m_hKillThread)) + THROW_CORE_EXCEPTION_WIN32(eErr_CannotResetEvent, GetLastError()); - WaitForThreadToExit(lock); -} + boost::upgrade_to_unique_lock lock_upgraded(lock); -void TWorkerThreadController::ChangePriority(int iPriority) -{ - boost::upgrade_lock lock(m_lock); + CloseHandle(m_hThread); + m_hThread = NULL; + } + else + THROW_CORE_EXCEPTION_WIN32(eErr_WaitingFailed, GetLastError()); + } - RemoveZombieData(lock); + void TWorkerThreadController::StopThread() + { + boost::upgrade_lock lock(m_lock); - if(!m_hThread) - return; + SignalThreadToStop(lock); - if(m_hThread != NULL) + WaitForThreadToExit(lock); + } + + void TWorkerThreadController::ChangePriority(int iPriority) { - if(::SuspendThread(m_hThread) == (DWORD)-1) - THROW_CORE_EXCEPTION_WIN32(eErr_CannotSuspendThread, GetLastError()); + boost::upgrade_lock lock(m_lock); - if(!::SetThreadPriority(m_hThread, iPriority)) + RemoveZombieData(lock); + + if (!m_hThread) + return; + + if (m_hThread != NULL) { - DWORD dwLastError = GetLastError(); + if (::SuspendThread(m_hThread) == (DWORD)-1) + THROW_CORE_EXCEPTION_WIN32(eErr_CannotSuspendThread, GetLastError()); - // try to resume thread priority cannot be changed - DWORD dwResult = ::ResumeThread(m_hThread); - dwResult; // to avoid warnings in release builds - BOOST_ASSERT(dwResult != (DWORD)-1); + if (!::SetThreadPriority(m_hThread, iPriority)) + { + DWORD dwLastError = GetLastError(); - THROW_CORE_EXCEPTION_WIN32(eErr_CannotChangeThreadPriority, dwLastError); + // try to resume thread priority cannot be changed + DWORD dwResult = ::ResumeThread(m_hThread); + dwResult; // to avoid warnings in release builds + BOOST_ASSERT(dwResult != (DWORD)-1); + + THROW_CORE_EXCEPTION_WIN32(eErr_CannotChangeThreadPriority, dwLastError); + } + + if (::ResumeThread(m_hThread) == (DWORD)-1) + THROW_CORE_EXCEPTION_WIN32(eErr_CannotResumeThread, GetLastError()); } + } - if(::ResumeThread(m_hThread) == (DWORD)-1) - THROW_CORE_EXCEPTION_WIN32(eErr_CannotResumeThread, GetLastError()); + bool TWorkerThreadController::KillRequested(DWORD dwWaitForSignal) + { + // this method does not have any mutexes, because it should be only called from within the thread + // being controlled by this object. This implies that the thread is alive and running, + // this class must exist because it should not be possible for the thread to exist and be active + // when this object is out of scope, and so the m_hKillThread should be non-NULL, since it is being destroyed + // in destructor. + return (m_hKillThread && WaitForSingleObject(m_hKillThread, dwWaitForSignal) == WAIT_OBJECT_0); } -} -bool TWorkerThreadController::KillRequested(DWORD dwWaitForSignal) -{ - // this method does not have any mutexes, because it should be only called from within the thread - // being controlled by this object. This implies that the thread is alive and running, - // this class must exist because it should not be possible for the thread to exist and be active - // when this object is out of scope, and so the m_hKillThread should be non-NULL, since it is being destroyed - // in destructor. - return (m_hKillThread && WaitForSingleObject(m_hKillThread, dwWaitForSignal) == WAIT_OBJECT_0); -} + HANDLE TWorkerThreadController::GetKillThreadHandle() const + { + return m_hKillThread; + } -HANDLE TWorkerThreadController::GetKillThreadHandle() const -{ - return m_hKillThread; -} - -void TWorkerThreadController::RemoveZombieData(boost::upgrade_lock& rUpgradeLock) -{ - // if thread is already stopped, then there is nothing to do - if(!m_hThread) - return; - - // thread already stopped? - if(WaitForSingleObject(m_hThread, 0) == WAIT_OBJECT_0) + void TWorkerThreadController::RemoveZombieData(boost::upgrade_lock& rUpgradeLock) { - if(!::ResetEvent(m_hKillThread)) - THROW_CORE_EXCEPTION_WIN32(eErr_CannotResetEvent, GetLastError()); + // if thread is already stopped, then there is nothing to do + if (!m_hThread) + return; - boost::upgrade_to_unique_lock lock_upgraded(rUpgradeLock); + // thread already stopped? + if (WaitForSingleObject(m_hThread, 0) == WAIT_OBJECT_0) + { + if (!::ResetEvent(m_hKillThread)) + THROW_CORE_EXCEPTION_WIN32(eErr_CannotResetEvent, GetLastError()); - CloseHandle(m_hThread); - m_hThread = NULL; + boost::upgrade_to_unique_lock lock_upgraded(rUpgradeLock); + + CloseHandle(m_hThread); + m_hThread = NULL; + } } -} -void TWorkerThreadController::SignalThreadToStop(boost::upgrade_lock& rUpgradeLock) -{ - RemoveZombieData(rUpgradeLock); - if(!m_hThread) - return; + void TWorkerThreadController::SignalThreadToStop(boost::upgrade_lock& rUpgradeLock) + { + RemoveZombieData(rUpgradeLock); + if (!m_hThread) + return; - if(!::SetEvent(m_hKillThread)) - THROW_CORE_EXCEPTION_WIN32(eErr_CannotSetEvent, GetLastError()); -} + if (!::SetEvent(m_hKillThread)) + THROW_CORE_EXCEPTION_WIN32(eErr_CannotSetEvent, GetLastError()); + } -void TWorkerThreadController::WaitForThreadToExit(boost::upgrade_lock& rUpgradeLock) -{ - if(!m_hThread) - return; - - DWORD dwRes = WaitForSingleObject(m_hThread, INFINITE); - if(dwRes == WAIT_OBJECT_0) + void TWorkerThreadController::WaitForThreadToExit(boost::upgrade_lock& rUpgradeLock) { - if(!::ResetEvent(m_hKillThread)) - THROW_CORE_EXCEPTION_WIN32(eErr_CannotResetEvent, GetLastError()); + if (!m_hThread) + return; - boost::upgrade_to_unique_lock lock_upgraded(rUpgradeLock); + DWORD dwRes = WaitForSingleObject(m_hThread, INFINITE); + if (dwRes == WAIT_OBJECT_0) + { + if (!::ResetEvent(m_hKillThread)) + THROW_CORE_EXCEPTION_WIN32(eErr_CannotResetEvent, GetLastError()); - CloseHandle(m_hThread); - m_hThread = NULL; + boost::upgrade_to_unique_lock lock_upgraded(rUpgradeLock); + + CloseHandle(m_hThread); + m_hThread = NULL; + } + else + THROW_CORE_EXCEPTION_WIN32(eErr_WaitingFailed, GetLastError()); } - else - THROW_CORE_EXCEPTION_WIN32(eErr_WaitingFailed, GetLastError()); } - -END_CHCORE_NAMESPACE