void mmSynchronize::mmReadWriteLockCSWindows::LockForWrite(void) { SendLogMessage(mmLog::debug,mmString(L"Start LockForWrite")); bool v_bIsLocked = false; while(!v_bIsLocked) { m_psExclusiveLock->Lock(); if(m_iReadWriteCount == 0) { m_iReadWriteCount = -1; v_bIsLocked = true; } else { if( (m_iReadWriteCount > 0) && (!m_bWaitForWriteLock) ) { m_bWaitForWriteLock = true; }; }; m_psExclusiveLock->Unlock(); if(!v_bIsLocked) { Sleep(1); }; }; SendLogMessage(mmLog::debug,mmString(L"End LockForWrite LockCount=") + mmStringUtilities::MMIntToString(m_iReadWriteCount)); }
void mmSynchronize::mmReadWriteLockCSWindows::UnlockFromWrite(void) { SendLogMessage(mmLog::debug,mmString(L"Start UnlockFromWrite")); bool v_bIsReadWriteLockDeSynchronized = false; m_psExclusiveLock->Lock(); if(m_iReadWriteCount == -1) { m_iReadWriteCount = 0; m_bWaitForWriteLock = false; } else { v_bIsReadWriteLockDeSynchronized = true; }; m_psExclusiveLock->Unlock(); if(v_bIsReadWriteLockDeSynchronized) { SendLogMessage(mmLog::critical,mmString(L"UnlockFromWrite TooManyUnlocksFromWrite")); throw mmError(mmeReadWriteLockWriteUnlocked); }; SendLogMessage(mmLog::debug,mmString(L"End UnlockFromWrite LockCount=") + mmStringUtilities::MMIntToString(m_iReadWriteCount)); }
void mmThread::mmThreadWindows::ThreadExecute(void) { SendLogMessage(mmLog::debug,mmString(L"Start ThreadExecute")); CoInitializeEx(NULL,COINIT_MULTITHREADED); bool v_bIsCalculated = m_psThreadExecutionClass->Execute(); CoUninitialize(); m_psExclusiveLock->Lock(); if(v_bIsCalculated) { m_eThreadStatus = finished; SendLogMessage(mmLog::debug,mmString(L"ThreadExecute Status=finished")); } else { m_eThreadStatus = stopped; SendLogMessage(mmLog::debug,mmString(L"ThreadExecute Status=stopped")); }; // to wywo³anie jest w sekcji krytycznej dlatego, ¿e czêsto po tej metodzie // wywo³uje siê destruktora i wtedy to wywo³anie jest ju¿ w nie istniej¹cym // obiekcie SendLogMessage(mmLog::debug,mmString(L"End ThreadExecute")); m_psExclusiveLock->Unlock(); }
//--------------------------------------------------------------------------- void mmImages::mmCalcMethod::StopExecution(void) { SendLogMessage(mmLog::debug,mmString(L"Start StopExecution")); m_bStopExecution = true; SendLogMessage(mmLog::debug,mmString(L"End StopExecution")); }
//--------------------------------------------------------------------------- bool mmImages::mmCalcMethod::IsExecuting(void) { SendLogMessage(mmLog::debug,mmString(L"Start IsExecuting")); SendLogMessage(mmLog::debug,mmString(L"End IsExecuting")); return m_bIsExecuting; }
void mmSynchronize::mmExclusiveLockCSWindows::Unlock(void) { SendLogMessage(mmLog::debug,mmString(L"Start Unlock")); ::LeaveCriticalSection(&m_sCriticalSection); SendLogMessage(mmLog::debug,mmString(L"End Unlock")); }
mmThread::mmThreadI::eStatus mmThread::mmThreadWindows::GetStatus(void) { // SendLogMessage(mmLog::debug,mmString(L"Start GetStatus")); eStatus v_eTempStat; m_psExclusiveLock->Lock(); v_eTempStat = m_eThreadStatus; m_psExclusiveLock->Unlock(); mmString v_sStatusString; switch(v_eTempStat) { case created: { v_sStatusString = L"created"; }; break; case initialized: { v_sStatusString = L"initialized"; }; break; case running: { v_sStatusString = L"running"; }; break; case stopping: { v_sStatusString = L"stopping"; }; break; case stopped: { v_sStatusString = L"stopped"; }; break; case finished: { v_sStatusString = L"finished"; }; break; default: { SendLogMessage(mmLog::critical,mmString(L"GetStatus UnknownThreadState")); throw mmError(mmeThreadUnknownThreadState); }; }; SendLogMessage(mmLog::debug,mmString(L"End GetStatus Status=")+ v_sStatusString); return v_eTempStat; }
//////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //// mmSynchronize::mmExclusiveLockCSWindows //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// mmSynchronize::mmExclusiveLockCSWindows::mmExclusiveLockCSWindows(mmLog::mmLogReceiverI *p_psLogReceiver): mmLog::mmLogSender(L"mmSynchronize::mmExclusiveLockCSWindows",p_psLogReceiver) { SendLogMessage(mmLog::debug,mmString(L"Start Constructor")); ::InitializeCriticalSectionAndSpinCount(&m_sCriticalSection, 300); SendLogMessage(mmLog::debug,mmString(L"End Constructor")); }
//--------------------------------------------------------------------------- bool mmImages::mmCalcMethod::Execute(void) { SendLogMessage(mmLog::debug,mmString(L"Start Execute")); bool v_bResult = Calculate(); SendLogMessage(mmLog::debug,mmString(L"End Execute")); return v_bResult; }
mmImages::mmImagesCalculationMethodI::sCalculationMethodParams mmImages::mmCalcMethod::GetCalculationMethodInfo(void) { SendLogMessage(mmLog::debug,mmString(L"Start GetCalculationMethodInfo")); // serialize parameters SerializeParameters(); SendLogMessage(mmLog::debug,mmString(L"End GetCalculationMethodInfo")); return m_sCMParams; }
//--------------------------------------------------------------------------- mmImages::mmCalcMethod::~mmCalcMethod() { SendLogMessage(mmLog::debug,mmString(L"Start Destructor")); for(std::list<mmGenericParamI*>::iterator v_sParam = m_sInputParams.begin(); v_sParam != m_sInputParams.end(); ++v_sParam) delete *v_sParam; for(std::list<mmGenericParamI*>::iterator v_sParam = m_sOutputParams.begin(); v_sParam != m_sOutputParams.end(); ++v_sParam) delete *v_sParam; SendLogMessage(mmLog::debug,mmString(L"End Destructor")); }
mmSynchronize::mmExclusiveLockCSWindows::~mmExclusiveLockCSWindows() { SendLogMessage(mmLog::debug,mmString(L"Start Destructor")); if(m_sCriticalSection.LockCount > -1) { SendLogMessage(mmLog::critical,mmString(L"Destructor Locked")); throw mmError(mmeExclusiveLockLocked); }; ::DeleteCriticalSection(&m_sCriticalSection); SendLogMessage(mmLog::debug,mmString(L"End Destructor")); }
bool mmSynchronize::mmExclusiveLockCSWindows::TryLock(void) { bool v_bIsLocked = true; SendLogMessage(mmLog::debug,mmString(L"Start TryLock")); if(::TryEnterCriticalSection(&m_sCriticalSection) == 0) { v_bIsLocked = false; }; SendLogMessage(mmLog::debug,mmString(L"End TryLock Locked=") + mmStringUtilities::BoolToString(v_bIsLocked)); return v_bIsLocked; }
void CApiLog::LogMessage(CString SourceFile, int nSourceLine, void *pInstance, int nMessageType, LPCTSTR pMsgFormat, ...) const { ASSERT(nMessageType>=4 && nMessageType<=8); ASSERT(m_hTargetWnd || m_pApiLogParent); ASSERT(nSourceLine>0); int pos=SourceFile.ReverseFind(_MPT('\\')); if (pos!=-1) SourceFile=SourceFile.Mid(pos+1); va_list ap; va_start(ap, pMsgFormat); CString text; text.FormatV(pMsgFormat, ap); va_end(ap); #ifdef MPEXT if (nMessageType>=FZ_LOG_DEBUG) return; #endif CString msg; msg.Format(_T("%s(%d): %s caller=0x%08x"), (LPCTSTR)SourceFile, nSourceLine, (LPCTSTR)text, (int)this); SendLogMessage(nMessageType, msg); }
mmReal mmThread::mmThreadWindows::GetProgress(void) { mmReal v_rProgress = -1.0; SendLogMessage(mmLog::debug,mmString(L"Start GetProgress")); m_psExclusiveLock->Lock(); if(m_eThreadStatus == running) { v_rProgress = m_psThreadExecutionClass->GetProgress(); }; m_psExclusiveLock->Unlock(); SendLogMessage(mmLog::debug,mmString(L"End GetProgress")); return v_rProgress; }
//////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //// mmThreads::mmThreadWindows //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// mmThread::mmThreadWindows::mmThreadWindows(mmLog::mmLogReceiverI *p_psLogReceiver): mmLog::mmLogSender(L"mmThread::mmThreadWindows",p_psLogReceiver) { mmInt v_iTemp; SendLogMessage(mmLog::debug,mmString(L"Start Constructor")); try { //m_psExclusiveLock = mmInterfaceInitializers::CreateExclusiveLock(p_psLogReceiver); m_psExclusiveLock = mmInterfaceInitializers::CreateExclusiveLock(NULL); } catch(mmError &v_sErr) { if(v_sErr.GetErrorCode() == mmeBadAlloc) { SendLogMessage(mmLog::critical,mmString(L"Constructor ErrorAllocationMemory")); }; throw v_sErr; }; m_hWinThreadHandle = CreateThread(NULL,0,ThreadRedirectFunction,this,CREATE_SUSPENDED,reinterpret_cast<unsigned long*>(&v_iTemp)); //SetThreadPriority(m_hWinThreadHandle,THREAD_PRIORITY_HIGHEST); // jeœli utworzenie nowego w¹tka siê nie powiedzie if(m_hWinThreadHandle == NULL) { delete m_psExclusiveLock; SendLogMessage(mmLog::critical,mmString(L"Constructor CreationThreadError")); throw mmError(mmeThreadCreateThreadError); }; m_eThreadStatus = created; SendLogMessage(mmLog::debug,mmString(L"Constructor Status=created")); m_psThreadExecutionClass = NULL; SendLogMessage(mmLog::debug,mmString(L"End Constructor PtrToExclusiveLock=") + mmStringUtilities::PointerToString(m_psExclusiveLock) + mmString(L"; ThreadID=") + mmStringUtilities::MMIntToString(v_iTemp)); }
mmThread::mmThreadWindows::~mmThreadWindows() { SendLogMessage(mmLog::debug,mmString(L"Start Destructor")); // zatrzymujê w¹tek this->Stop(); if(WaitForSingleObject(m_hWinThreadHandle,INFINITE) != WAIT_OBJECT_0) { SendLogMessage(mmLog::critical,mmString(L"Destructor BadStateOfThread")); }; CloseHandle(m_hWinThreadHandle); delete m_psExclusiveLock; SendLogMessage(mmLog::debug,mmString(L"End Destructor")); }
void CApiLog::LogMessageRaw(int nMessageType, LPCTSTR pMsg) const { DebugAssert(nMessageType>=FZ_LOG_STATUS && nMessageType<=FZ_LOG_DEBUG); if (!LoggingMessageType(nMessageType)) return; if (nMessageType>=FZ_LOG_DEBUG) return; SendLogMessage(nMessageType, pMsg); }
mmInt mmSynchronize::mmReadWriteLockCSWindows::GetLockState(bool* p_pbWaitForWrite) { SendLogMessage(mmLog::debug,mmString(L"Start GetLockState")); mmInt v_iLockState = m_iReadWriteCount; *p_pbWaitForWrite = m_bWaitForWriteLock; this->SendLogMessage(mmLog::debug,mmString(L"End GetLockState")); return v_iLockState; }
void mmSynchronize::mmReadWriteLockCSWindows::LockForRead(void) { SendLogMessage(mmLog::debug,mmString(L"Start LockForRead")); bool v_bIsLocked = false; while(!v_bIsLocked) { if(TryLockForRead()) { v_bIsLocked = true; } else { Sleep(1); }; }; SendLogMessage(mmLog::debug,mmString(L"End LockForRead LockCount=") + mmStringUtilities::MMIntToString(m_iReadWriteCount)); }
bool mmSynchronize::mmReadWriteLockCSWindows::TryLockForWrite(void) { SendLogMessage(mmLog::debug,mmString(L"Start TryLockForWrite")); bool v_bIsLocked = false; m_psExclusiveLock->Lock(); if(m_iReadWriteCount == 0) { m_iReadWriteCount = -1; v_bIsLocked = true; }; m_psExclusiveLock->Unlock(); SendLogMessage(mmLog::debug,mmString(L"End TryLockForWrite LockCount=") + mmStringUtilities::MMIntToString(m_iReadWriteCount) + mmString(L" Locked=") + mmStringUtilities::BoolToString(v_bIsLocked)); return v_bIsLocked; }
void mmThread::mmThreadWindows::Run(void) { SendLogMessage(mmLog::debug,mmString(L"Start Run")); m_psExclusiveLock->Lock(); try { // tylko kiedy w¹tek jest w stanie initialized mo¿e byæ wywo³ywana metoda Run if(this->m_eThreadStatus == initialized) { if(ResumeThread(m_hWinThreadHandle) == 1) { m_eThreadStatus = running; SendLogMessage(mmLog::debug,mmString(L"Run Status=running")); } else { SendLogMessage(mmLog::critical,mmString(L"Run ResumeThreadError")); throw mmError(mmeThreadResumeError); }; } else { SendLogMessage(mmLog::critical,mmString(L"Run BadThreadStateError")); throw mmError(mmeThreadBadThreadState); }; } catch(mmError &v_sErr) { m_psExclusiveLock->Unlock(); throw v_sErr; }; m_psExclusiveLock->Unlock(); SendLogMessage(mmLog::debug,mmString(L"End Run")); }
void mmThread::mmThreadWindows::Stop(void) { SendLogMessage(mmLog::debug,mmString(L"Start Stop")); bool bIsCreatedInitializedStoppedOrFinished = false; while(!bIsCreatedInitializedStoppedOrFinished) { m_psExclusiveLock->Lock(); // w przypadku: kiedy jest uruchomiony ale nie wiadomo czy ju¿ liczy if(m_eThreadStatus == running) { if(m_psThreadExecutionClass->IsExecuting()) { m_eThreadStatus = stopping; SendLogMessage(mmLog::debug,mmString(L"Stop Status=stopping")); // informujê o zatrzymaniu w¹tka klasê licz¹c¹ m_psThreadExecutionClass->StopExecution(); }; } else { // w innych if(m_eThreadStatus != stopping) { bIsCreatedInitializedStoppedOrFinished = true; }; }; m_psExclusiveLock->Unlock(); if(!bIsCreatedInitializedStoppedOrFinished) { Sleep(1); }; }; SendLogMessage(mmLog::debug,mmString(L"End Stop")); }
void CApiLog::LogMessageRaw(int nMessageType, LPCTSTR pMsg) const { ASSERT(nMessageType>=0 && nMessageType<=8); ASSERT(m_hTargetWnd || m_pApiLogParent); if (nMessageType>=FZ_LOG_APIERROR && (nMessageType-FZ_LOG_APIERROR)>=m_pApiLogParent->m_nDebugLevel) return; #ifdef MPEXT if (nMessageType>=FZ_LOG_DEBUG) return; #endif SendLogMessage(nMessageType, pMsg); }
mmImages::mmCalcMethod::mmCalcMethod(mmLog::mmLogReceiverI *p_psLogReceiver, mmString p_sClassName) : mmLog::mmLogSender(p_sClassName,p_psLogReceiver) { SendLogMessage(mmLog::debug,mmString(L"Start Constructor")); swprintf_s(m_sCMParams.sShortName, L"Generic calculation method"); swprintf_s(m_sCMParams.sIDName, L"{C13390E5-EBA6-404d-8706-B07FFE01C52F}"); swprintf_s(m_sCMParams.sDescription, L"Generic calculation method"); m_sCMParams.bIsMultithreaded = true; m_psThreadSynchEL.reset(mmInterfaceInitializers::CreateExclusiveLock(NULL)); m_rProgress = 0.0; m_bIsExecuting = false; m_bStopExecution = false; m_psImageStructure = NULL; m_iRowsCountInBlock = 100; SendLogMessage(mmLog::debug,mmString(L"End Constructor")); }
void mmImages::mmCalcMethod::SetCalculationMethodParameters(mmImages::mmImageStructureI* p_psImageStructure, mmImages::mmImagesCalculationMethodI::sCalculationAutomationParams* p_psAutomationParams) { SendLogMessage(mmLog::debug,mmString(L"Start SetCalculationMethodParameters")); m_psImageStructure = p_psImageStructure; m_rProgress = 0.0; //m_iThreadsCount = 0; m_bFinishImage = false; // deserialize parameters DeserializeParameters(*p_psAutomationParams); // prepare map with first available row for each image m_mNextRows.clear(); std::vector<mmID> v_sImageIDs = GetImageIDs(); for (mmUInt i = 0; i < v_sImageIDs.size(); ++i) { m_mNextRows[v_sImageIDs[i]] = 0; } SendLogMessage(mmLog::debug,mmString(L"End SetCalculationMethodParameters")); }
void CApiLog::LogMessageRaw(int nMessageType, LPCTSTR pMsg) const { ASSERT(nMessageType>=FZ_LOG_STATUS && nMessageType<=FZ_LOG_DEBUG); ASSERT(m_hTargetWnd || m_pApiLogParent); if (!LoggingMessageType(nMessageType)) return; #ifdef MPEXT if (nMessageType>=FZ_LOG_DEBUG) return; #endif SendLogMessage(nMessageType, pMsg); }
mmSynchronize::mmReadWriteLockCSWindows::~mmReadWriteLockCSWindows() { SendLogMessage(mmLog::debug,mmString(L"Start Destructor")); delete m_psExclusiveLock; if(m_iReadWriteCount != 0) { SendLogMessage(mmLog::critical,mmString(L"Destructor BadEndConditions RWCount=") + mmStringUtilities::MMIntToString(m_iReadWriteCount)); throw mmError(mmeReadWriteLockBadEndLockCount); }; if(m_bWaitForWriteLock) { SendLogMessage(mmLog::critical,mmString(L"Destructor BadEndConditions WaitForWrite=") + mmStringUtilities::BoolToString(m_bWaitForWriteLock)); throw mmError(mmeReadWriteLockWaitForWrite); }; SendLogMessage(mmLog::debug,mmString(L"End Destructor")); }
void mmThread::mmThreadWindows::RegisterThreadExecutionClass(mmThreadExecutionI* p_psThreadExecutionClass) { SendLogMessage(mmLog::debug,mmString(L"Start RegisterThreadExecutionClass")); m_psExclusiveLock->Lock(); try { if( (m_eThreadStatus == created) || (m_eThreadStatus == initialized) ) { m_psThreadExecutionClass = p_psThreadExecutionClass; if(m_eThreadStatus != initialized) { m_eThreadStatus = initialized; SendLogMessage(mmLog::debug,mmString(L"RegisterThreadExecutionClass Status=initialized")); }; SendLogMessage(mmLog::debug,mmString(L"RegisterThreadExecutionClass Registered PtrToExecutionObject=") + mmStringUtilities::PointerToString(m_psThreadExecutionClass)); } else { SendLogMessage(mmLog::critical,mmString(L"RegisterThreadExecutionClass CantRegister")); throw mmError(mmeThreadCantRegisterExecutionClass); }; } catch(mmError &v_sErr) { m_psExclusiveLock->Unlock(); throw v_sErr; }; m_psExclusiveLock->Unlock(); SendLogMessage(mmLog::debug,mmString(L"End RegisterThreadExecutionClass")); }
//////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //// mmSynchronize::mmReadWriteLockCSWindows //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// mmSynchronize::mmReadWriteLockCSWindows::mmReadWriteLockCSWindows(mmLog::mmLogReceiverI *p_psLogReceiver): mmLog::mmLogSender(L"mmSynchronize::mmReadWriteLockCSWindows",p_psLogReceiver) { SendLogMessage(mmLog::debug,mmString(L"Start Constructor")); try { m_psExclusiveLock = mmInterfaceInitializers::CreateExclusiveLock(p_psLogReceiver); } catch(mmError &v_sErr) { if(v_sErr.GetErrorCode() == mmeBadAlloc) { SendLogMessage(mmLog::critical,mmString(L"Constructor ErrorAllocationMemory")); }; throw; }; m_iReadWriteCount = 0; m_bWaitForWriteLock = false; SendLogMessage(mmLog::debug,mmString(L"End Constructor PtrToExclusiveLock=") + mmStringUtilities::PointerToString(m_psExclusiveLock)); }