Index: src/ch/FileInfo.h =================================================================== diff -u -r22bbc4a87fa0b249e1e02ba385f28da9d77a4aa1 -rb13e3052f13b2f0a8c53c37a7b9246418b88bebd --- src/ch/FileInfo.h (.../FileInfo.h) (revision 22bbc4a87fa0b249e1e02ba385f28da9d77a4aa1) +++ src/ch/FileInfo.h (.../FileInfo.h) (revision b13e3052f13b2f0a8c53c37a7b9246418b88bebd) @@ -209,10 +209,10 @@ void SetClipboard(CClipboardArray *pClipboard) { m_pClipboard=pClipboard; }; CString GetDestinationPath(CString strPath, unsigned char ucCopyNumber, int iFlags); - void SetSrcIndex(int iIndex) { m_stSrcIndex=iIndex; }; + void SetSrcIndex(size_t stIndex) { m_stSrcIndex = stIndex; }; size_t GetSrcIndex() const { return m_stSrcIndex; }; - bool GetMove() { if (m_stSrcIndex != -1) return m_pClipboard->GetAt(m_stSrcIndex)->GetMove(); else return true; }; + bool GetMove() { if (m_stSrcIndex != std::numeric_limits::max()) return m_pClipboard->GetAt(m_stSrcIndex)->GetMove(); else return true; }; int GetBufferIndex() const; Index: src/ch/task.cpp =================================================================== diff -u -rb55cf150df571c279536a2b4e33bcecedef17305 -rb13e3052f13b2f0a8c53c37a7b9246418b88bebd --- src/ch/task.cpp (.../task.cpp) (revision b55cf150df571c279536a2b4e33bcecedef17305) +++ src/ch/task.cpp (.../task.cpp) (revision b13e3052f13b2f0a8c53c37a7b9246418b88bebd) @@ -61,9 +61,277 @@ m_strErrorDesc = pszDesc; } +//////////////////////////////////////////////////////////////////////////////// +// TTasksGlobalStats members + +TTasksGlobalStats::TTasksGlobalStats() : + m_ullGlobalTasksSize(0), + m_ullGlobalTasksPosition(0), + m_stRunningTasks(0) +{ +} + +TTasksGlobalStats::~TTasksGlobalStats() +{ +} + +void TTasksGlobalStats::IncreaseGlobalTasksSize(unsigned long long ullModify) +{ + m_lock.lock(); + m_ullGlobalTasksSize += ullModify; + m_lock.unlock(); +} + +void TTasksGlobalStats::DecreaseGlobalTasksSize(unsigned long long ullModify) +{ + m_lock.lock(); + m_ullGlobalTasksSize -= ullModify; + m_lock.unlock(); +} + +unsigned long long TTasksGlobalStats::GetGlobalTasksSize() const +{ + boost::shared_lock lock(m_lock); + return m_ullGlobalTasksSize; +} + +void TTasksGlobalStats::IncreaseGlobalTasksPosition(unsigned long long ullModify) +{ + m_lock.lock(); + m_ullGlobalTasksPosition += ullModify; + m_lock.unlock(); +} + +void TTasksGlobalStats::DecreaseGlobalTasksPosition(unsigned long long ullModify) +{ + m_lock.lock(); + m_ullGlobalTasksPosition -= ullModify; + m_lock.unlock(); +} + +unsigned long long TTasksGlobalStats::GetGlobalTasksPosition() const +{ + boost::shared_lock lock(m_lock); + return m_ullGlobalTasksPosition; +} + +void TTasksGlobalStats::IncreaseGlobalProgressData(unsigned long long ullTasksPosition, unsigned long long ullTasksSize) +{ + m_lock.lock(); + m_ullGlobalTasksSize += ullTasksSize; + m_ullGlobalTasksPosition += ullTasksPosition; + m_lock.unlock(); + +} + +void TTasksGlobalStats::DecreaseGlobalProgressData(unsigned long long ullTasksPosition, unsigned long long ullTasksSize) +{ + m_lock.lock(); + m_ullGlobalTasksSize -= ullTasksSize; + m_ullGlobalTasksPosition -= ullTasksPosition; + m_lock.unlock(); +} + +int TTasksGlobalStats::GetProgressPercents() const +{ + unsigned long long llPercent = 0; + + boost::shared_lock lock(m_lock); + + if(m_ullGlobalTasksSize != 0) + llPercent = m_ullGlobalTasksPosition * 100 / m_ullGlobalTasksSize; + + return boost::numeric_cast(llPercent); +} + +void TTasksGlobalStats::IncreaseRunningTasks() +{ + m_lock.lock(); + ++m_stRunningTasks; + m_lock.unlock(); +} + +void TTasksGlobalStats::DecreaseRunningTasks() +{ + m_lock.lock(); + --m_stRunningTasks; + m_lock.unlock(); +} + +size_t TTasksGlobalStats::GetRunningTasksCount() const +{ + boost::shared_lock lock(m_lock); + return m_stRunningTasks; +} + +//////////////////////////////////////////////////////////////////////////////// +// TTasksGlobalStats members +TTaskLocalStats::TTaskLocalStats() : + m_prtGlobalStats(NULL), + m_ullProcessedSize(0), + m_ullTotalSize(0), + m_bTaskIsRunning(false) +{ +} + +TTaskLocalStats::~TTaskLocalStats() +{ + DisconnectGlobalStats(); +} + +void TTaskLocalStats::ConnectGlobalStats(TTasksGlobalStats& rtGlobalStats) +{ + DisconnectGlobalStats(); + + boost::unique_lock lock(m_lock); + + m_prtGlobalStats = &rtGlobalStats; + m_prtGlobalStats->IncreaseGlobalProgressData(m_ullProcessedSize, m_ullTotalSize); + if(m_bTaskIsRunning) + m_prtGlobalStats->IncreaseRunningTasks(); +} + +void TTaskLocalStats::DisconnectGlobalStats() +{ + boost::unique_lock lock(m_lock); + if(m_prtGlobalStats) + { + m_prtGlobalStats->DecreaseGlobalProgressData(m_ullProcessedSize, m_ullTotalSize); + m_prtGlobalStats = NULL; + if(m_bTaskIsRunning) + m_prtGlobalStats->DecreaseRunningTasks(); + } +} + +void TTaskLocalStats::IncreaseProcessedSize(unsigned long long ullAdd) +{ + boost::unique_lock lock(m_lock); + + if(m_prtGlobalStats) + m_prtGlobalStats->IncreaseGlobalTasksPosition(ullAdd); + + m_ullProcessedSize += ullAdd; +} + +void TTaskLocalStats::DecreaseProcessedSize(unsigned long long ullSub) +{ + boost::unique_lock lock(m_lock); + if(m_prtGlobalStats) + m_prtGlobalStats->DecreaseGlobalTasksPosition(ullSub); + + m_ullProcessedSize -= ullSub; +} + +void TTaskLocalStats::SetProcessedSize(unsigned long long ullSet) +{ + boost::unique_lock lock(m_lock); + + if(m_prtGlobalStats) + { + if(ullSet < m_ullProcessedSize) + m_prtGlobalStats->DecreaseGlobalTasksPosition(m_ullProcessedSize - ullSet); + else + m_prtGlobalStats->IncreaseGlobalTasksPosition(ullSet - m_ullProcessedSize); + } + + m_ullProcessedSize = ullSet; +} + +unsigned long long TTaskLocalStats::GetProcessedSize() const +{ + boost::shared_lock lock(m_lock); + return m_ullProcessedSize; +} + +unsigned long long TTaskLocalStats::GetUnProcessedSize() const +{ + boost::shared_lock lock(m_lock); + return m_ullTotalSize - m_ullProcessedSize; +} + +void TTaskLocalStats::IncreaseTotalSize(unsigned long long ullAdd) +{ + boost::unique_lock lock(m_lock); + + if(m_prtGlobalStats) + m_prtGlobalStats->IncreaseGlobalTasksSize(ullAdd); + m_ullTotalSize += ullAdd; +} + +void TTaskLocalStats::DecreaseTotalSize(unsigned long long ullSub) +{ + boost::unique_lock lock(m_lock); + + if(m_prtGlobalStats) + m_prtGlobalStats->DecreaseGlobalTasksSize(ullSub); + + m_ullTotalSize -= ullSub; +} + +void TTaskLocalStats::SetTotalSize(unsigned long long ullSet) +{ + boost::unique_lock lock(m_lock); + + if(m_prtGlobalStats) + { + if(ullSet < m_ullTotalSize) + m_prtGlobalStats->DecreaseGlobalTasksPosition(m_ullTotalSize - ullSet); + else + m_prtGlobalStats->IncreaseGlobalTasksPosition(ullSet - m_ullTotalSize); + } + + m_ullTotalSize = ullSet; +} + +unsigned long long TTaskLocalStats::GetTotalSize() const +{ + boost::shared_lock lock(m_lock); + return m_ullTotalSize; +} + +int TTaskLocalStats::GetProgressInPercent() const +{ + boost::shared_lock lock(m_lock); + + unsigned long long ullPercent = 0; + + if(m_ullTotalSize != 0) + ullPercent = m_ullProcessedSize * 100 / m_ullTotalSize; + + return boost::numeric_cast(ullPercent); +} + +void TTaskLocalStats::MarkTaskAsRunning() +{ + boost::unique_lock lock(m_lock); + if(!m_bTaskIsRunning) + { + if(m_prtGlobalStats) + m_prtGlobalStats->IncreaseRunningTasks(); + m_bTaskIsRunning = true; + } +} + +void TTaskLocalStats::MarkTaskAsNotRunning() +{ + boost::unique_lock lock(m_lock); + if(m_bTaskIsRunning) + { + if(m_prtGlobalStats) + m_prtGlobalStats->DecreaseRunningTasks(); + m_bTaskIsRunning = false; + } +} + +bool TTaskLocalStats::IsRunning() const +{ + boost::shared_lock lock(m_lock); + return m_bTaskIsRunning; +} + //////////////////////////////////////////////////////////////////////////// // CTask members -CTask::CTask(chcore::IFeedbackHandler* piFeedbackHandler, size_t stSessionUniqueID, TTasksGlobalStats& tGlobalStats) : +CTask::CTask(chcore::IFeedbackHandler* piFeedbackHandler, size_t stSessionUniqueID) : m_log(), m_piFeedbackHandler(piFeedbackHandler), m_files(m_clipboard), @@ -72,13 +340,10 @@ m_nStatus(ST_NULL_STATUS), m_pThread(NULL), m_nPriority(THREAD_PRIORITY_NORMAL), - m_nProcessed(0), - m_nAll(0), m_bKill(false), m_bKilled(true), m_lTimeElapsed(0), m_lLastTime(-1), - m_bQueued(false), m_ucCopies(1), m_ucCurrentCopy(0), m_uiResumeInterval(0), @@ -87,9 +352,8 @@ m_bSaved(false), m_lOsError(0), m_stSessionUniqueID(stSessionUniqueID), - m_rtGlobalStats(tGlobalStats), + m_localStats(), m_bRegisteredAsRunning(false) - { BOOST_ASSERT(piFeedbackHandler); @@ -108,9 +372,18 @@ KillThread(); if(m_piFeedbackHandler) m_piFeedbackHandler->Delete(); - UnregisterTaskAsRunningNL(); } +void CTask::OnRegisterTask(TTasksGlobalStats& rtGlobalStats) +{ + m_localStats.ConnectGlobalStats(rtGlobalStats); +} + +void CTask::OnUnregisterTask() +{ + m_localStats.DisconnectGlobalStats(); +} + // m_clipboard void CTask::AddClipboardData(const CClipboardEntryPtr& spEntry) { @@ -292,7 +565,7 @@ boost::shared_lock lock(m_lock); size_t stSize = m_files.GetSize(); - if(stSize > 0 && m_stCurrentIndex != -1) + if(stSize > 0 && m_stCurrentIndex != std::numeric_limits::max()) rv = m_bsSizes.m_bOnlyDefault ? 0 : m_files.GetAt((m_stCurrentIndex < stSize) ? m_stCurrentIndex : 0)->GetBufferIndex(); return rv; @@ -321,108 +594,41 @@ m_bSaved = false; } -// m_nProcessed -void CTask::IncreaseProcessedSize(__int64 nSize) +void CTask::CalculateTotalSize() { - boost::unique_lock lock(m_lock); - m_nProcessed += nSize; -} + unsigned long long ullTotalSize = 0; -void CTask::SetProcessedSize(__int64 nSize) -{ - boost::unique_lock lock(m_lock); - m_nProcessed = nSize; -} - -__int64 CTask::GetProcessedSize() -{ - boost::shared_lock lock(m_lock); - return m_nProcessed; -} - -// m_nAll -void CTask::SetAllSize(__int64 nSize) -{ - boost::unique_lock lock(m_lock); - m_nAll=nSize; -} - -__int64 CTask::GetAllSize() -{ boost::shared_lock lock(m_lock); - return m_nAll; -} - -void CTask::CalcAllSize() -{ - boost::unique_lock lock(m_lock); - m_nAll=0; - size_t nSize = m_files.GetSize(); - for (size_t i = 0; i < nSize; i++) + for(size_t i = 0; i < nSize; i++) { - m_nAll += m_files.GetAt(i)->GetLength64(); + ullTotalSize += m_files.GetAt(i)->GetLength64(); } - m_nAll *= m_ucCopies; + ullTotalSize *= m_ucCopies; + + m_localStats.SetTotalSize(ullTotalSize); } -void CTask::CalcProcessedSize() +void CTask::CalculateProcessedSize() { - boost::unique_lock lock(m_lock); - m_nProcessed = 0; + unsigned long long ullProcessedSize = 0; // count all from previous passes + boost::shared_lock lock(m_lock); if(m_ucCopies) - m_nProcessed += m_ucCurrentCopy*(m_nAll/m_ucCopies); + ullProcessedSize += m_ucCurrentCopy * (m_localStats.GetTotalSize() / m_ucCopies); else - m_nProcessed += m_ucCurrentCopy*m_nAll; + ullProcessedSize += m_ucCurrentCopy * m_localStats.GetTotalSize(); for(size_t stIndex = 0; stIndex < m_stCurrentIndex; ++stIndex) { - m_nProcessed += m_files.GetAt(stIndex)->GetLength64(); + ullProcessedSize += m_files.GetAt(stIndex)->GetLength64(); } - m_rtGlobalStats.IncreaseGlobalTasksPosition(m_nProcessed); -} -void CTask::RegisterTaskAsRunning() -{ - boost::unique_lock lock(m_lock); - if(!m_bRegisteredAsRunning) - { - m_bRegisteredAsRunning = true; - m_rtGlobalStats.IncreaseRunningTasks(); - } + m_localStats.SetProcessedSize(ullProcessedSize); } -void CTask::RegisterTaskAsRunningNL() -{ - if(!m_bRegisteredAsRunning) - { - m_bRegisteredAsRunning = true; - m_rtGlobalStats.IncreaseRunningTasks(); - } -} - -void CTask::UnregisterTaskAsRunning() -{ - boost::unique_lock lock(m_lock); - if(m_bRegisteredAsRunning) - { - m_bRegisteredAsRunning = false; - m_rtGlobalStats.DecreaseRunningTasks(); - } -} - -void CTask::UnregisterTaskAsRunningNL() -{ - if(m_bRegisteredAsRunning) - { - m_bRegisteredAsRunning = false; - m_rtGlobalStats.DecreaseRunningTasks(); - } -} - // m_bKill void CTask::SetKillFlag(bool bKill) { @@ -490,10 +696,19 @@ ar >> m_bsSizes; ar >> m_nPriority; - ar >> m_nAll; - ar >> m_lTimeElapsed; - ar >> m_nProcessed; + // this info could be calculated on load (low cost) + unsigned long long ullTotalSize = 0; + ar >> ullTotalSize; + m_localStats.SetTotalSize(ullTotalSize); + + ar >> m_lTimeElapsed; + + // this info could be calculated on load (low cost) + unsigned long long ullProcessedSize = 0; + ar >> ullProcessedSize; + m_localStats.SetProcessedSize(ullProcessedSize); + ar >> m_ucCurrentCopy; m_clipboard.Load(ar, 0, bData); @@ -511,15 +726,11 @@ THROW(_t("Missing task path."), 0, 0, 0); if(!bData && m_bSaved) - { - TRACE("Saving locked - file not saved\n"); return; - } if(!bData && !m_bSaved && ( (m_nStatus & ST_STEP_MASK) == ST_FINISHED || (m_nStatus & ST_STEP_MASK) == ST_CANCELLED || (m_nStatus & ST_WORKING_MASK) == ST_PAUSED )) { - TRACE("Last save - saving blocked\n"); m_bSaved = true; } @@ -557,10 +768,15 @@ ar << m_bsSizes; ar << m_nPriority; - ar << m_nAll; + + unsigned long long ullTotalSize = m_localStats.GetTotalSize(); + ar << ullTotalSize; + ar << m_lTimeElapsed; - ar << m_nProcessed; + unsigned long long ullProcessedSize = m_localStats.GetProcessedSize(); + ar << ullProcessedSize; + ar << m_ucCurrentCopy; m_clipboard.Store(ar, 0, bData); @@ -613,7 +829,7 @@ } } -bool CTask::RetryProcessing(bool bOnlyErrors/*=false*/, UINT uiInterval) +bool CTask::RetryProcessing(bool bOnlyErrors, UINT uiInterval) { // retry used to auto-resume, after loading if( (GetStatus(ST_WORKING_MASK) == ST_ERROR || (!bOnlyErrors && GetStatus(ST_WORKING_MASK) != ST_PAUSED)) @@ -680,37 +896,27 @@ { pData->m_spFileInfo = m_files.GetAt(0); pData->m_spFileInfo->SetFilePath(pData->m_spFileInfo->GetFullFilePath()); - pData->m_spFileInfo->SetSrcIndex(-1); + pData->m_spFileInfo->SetSrcIndex(std::numeric_limits::max()); } else { if(m_clipboard.GetSize() > 0) { pData->m_spFileInfo->SetFilePath(m_clipboard.GetAt(0)->GetPath()); - pData->m_spFileInfo->SetSrcIndex(-1); + pData->m_spFileInfo->SetSrcIndex(std::numeric_limits::max()); } else { pData->m_spFileInfo->SetFilePath(GetResManager().LoadString(IDS_NONEINPUTFILE_STRING)); - pData->m_spFileInfo->SetSrcIndex(-1); + pData->m_spFileInfo->SetSrcIndex(std::numeric_limits::max()); } } } pData->m_uiStatus=m_nStatus; // percents - size_t stSize = m_files.GetSize() * m_ucCopies; - size_t stIndex = m_stCurrentIndex + m_ucCurrentCopy * m_files.GetSize(); - if(m_nAll != 0 && !((m_nStatus & ST_SPECIAL_MASK) & ST_IGNORE_CONTENT)) - pData->m_nPercent=static_cast( (static_cast(m_nProcessed)*100.0)/static_cast(m_nAll) ); - else - { - if(stSize != 0) - pData->m_nPercent=static_cast( static_cast(stIndex)*100.0/static_cast(stSize) ); - else - pData->m_nPercent=0; - } + pData->m_nPercent = m_localStats.GetProgressInPercent(); } void CTask::GetSnapshot(TASK_DISPLAY_DATA *pData) @@ -725,19 +931,19 @@ { pData->m_spFileInfo = m_files.GetAt(0); pData->m_spFileInfo->SetFilePath(pData->m_spFileInfo->GetFullFilePath()); - pData->m_spFileInfo->SetSrcIndex(-1); + pData->m_spFileInfo->SetSrcIndex(std::numeric_limits::max()); } else { if(m_clipboard.GetSize() > 0) { pData->m_spFileInfo->SetFilePath(m_clipboard.GetAt(0)->GetPath()); - pData->m_spFileInfo->SetSrcIndex(-1); + pData->m_spFileInfo->SetSrcIndex(std::numeric_limits::max()); } else { pData->m_spFileInfo->SetFilePath(GetResManager().LoadString(IDS_NONEINPUTFILE_STRING)); - pData->m_spFileInfo->SetSrcIndex(-1); + pData->m_spFileInfo->SetSrcIndex(std::numeric_limits::max()); } } } @@ -750,26 +956,20 @@ pData->m_strErrorDesc=m_strErrorDesc; pData->m_uiStatus=m_nStatus; pData->m_stIndex=m_stCurrentIndex+m_ucCurrentCopy*m_files.GetSize(); - pData->m_ullProcessedSize=m_nProcessed; + pData->m_ullProcessedSize = m_localStats.GetProcessedSize(); pData->m_stSize=m_files.GetSize()*m_ucCopies; - pData->m_ullSizeAll=m_nAll; + pData->m_ullSizeAll = m_localStats.GetTotalSize(); pData->m_ucCurrentCopy=static_cast(m_ucCurrentCopy+1); // visual aspect pData->m_ucCopies=m_ucCopies; pData->m_pstrUniqueName=&m_strUniqueName; - if(m_files.GetSize() > 0 && m_stCurrentIndex != -1) + if(m_files.GetSize() > 0 && m_stCurrentIndex != std::numeric_limits::max()) pData->m_iCurrentBufferIndex=m_bsSizes.m_bOnlyDefault ? 0 : m_files.GetAt((m_stCurrentIndex < m_files.GetSize()) ? m_stCurrentIndex : 0)->GetBufferIndex(); else pData->m_iCurrentBufferIndex=0; // percents - if(m_nAll != 0 && !((m_nStatus & ST_SPECIAL_MASK) & ST_IGNORE_CONTENT)) - pData->m_nPercent=static_cast( (static_cast(m_nProcessed)*100.0)/static_cast(m_nAll) ); - else - if(pData->m_stSize != 0) - pData->m_nPercent=static_cast( static_cast(pData->m_stIndex)*100.0/static_cast(pData->m_stSize) ); - else - pData->m_nPercent=0; + pData->m_nPercent = m_localStats.GetProgressInPercent(); // status string // first @@ -850,7 +1050,7 @@ pData->m_lTimeElapsed=m_lTimeElapsed; } -/*inline*/ void CTask::CleanupAfterKill() +void CTask::CleanupAfterKill() { boost::unique_lock lock(m_lock); @@ -903,7 +1103,7 @@ if(GetContinueFlagNL() || GetForceFlagNL()) { - RegisterTaskAsRunningNL(); + m_localStats.MarkTaskAsRunning(); SetForceFlagNL(false); SetContinueFlagNL(false); } @@ -951,7 +1151,7 @@ bool CTask::GetRequiredFreeSpace(ull_t *pullNeeded, ull_t *pullAvailable) { - *pullNeeded=GetAllSize()-GetProcessedSize(); // it'd be nice to round up to take cluster size into consideration, + *pullNeeded = m_localStats.GetUnProcessedSize(); // it'd be nice to round up to take cluster size into consideration, // but GetDiskFreeSpace returns flase values // get free space @@ -1074,7 +1274,7 @@ int rv = 0; size_t stSize = m_files.GetSize(); - if(stSize > 0 && m_stCurrentIndex != -1) + if(stSize > 0 && m_stCurrentIndex != std::numeric_limits::max()) rv = m_bsSizes.m_bOnlyDefault ? 0 : m_files.GetAt((m_stCurrentIndex < stSize) ? m_stCurrentIndex : 0)->GetBufferIndex(); return rv; @@ -1100,46 +1300,21 @@ m_bSaved = false; } -// m_nProcessed -void CTask::IncreaseProcessedSizeNL(__int64 nSize) +void CTask::CalculateTotalSizeNL() { - m_nProcessed += nSize; -} + unsigned long long ullTotalSize = 0; -void CTask::SetProcessedSizeNL(__int64 nSize) -{ - m_nProcessed = nSize; -} + size_t nSize = m_files.GetSize(); + for(size_t i = 0; i < nSize; i++) + { + ullTotalSize += m_files.GetAt(i)->GetLength64(); + } -__int64 CTask::GetProcessedSizeNL() -{ - return m_nProcessed; -} + ullTotalSize *= m_ucCopies; -// m_nAll -void CTask::SetAllSizeNL(__int64 nSize) -{ - m_nAll = nSize; + m_localStats.SetTotalSize(ullTotalSize); } -__int64 CTask::GetAllSizeNL() -{ - return m_nAll; -} - -void CTask::CalcAllSizeNL() -{ - m_nAll = 0; - - size_t nSize = m_files.GetSize(); - for (size_t i = 0; i < nSize; i++) - { - m_nAll += m_files.GetAt(i)->GetLength64(); - } - - m_nAll *= m_ucCopies; -} - void CTask::SetKillFlagNL(bool bKill) { m_bKill = bKill; @@ -1164,6 +1339,7 @@ { m_pThread = NULL; UpdateTimeNL(); + m_lLastTime = -1; } void CTask::UpdateTimeNL() @@ -1351,10 +1527,10 @@ } // calc size of all files - CalcAllSize(); + CalculateTotalSize(); // update *m_pnTasksAll; - m_rtGlobalStats.IncreaseGlobalTasksSize(GetAllSize()); +// m_rtGlobalStats.IncreaseGlobalTasksSize(GetAllSize()); // change state to ST_COPYING - finished searching for files SetStatus(ST_COPYING, ST_STEP_MASK); @@ -1507,8 +1683,7 @@ } case CFeedbackHandler::eResult_Skip: { - IncreaseProcessedSize(pData->spSrcFile->GetLength64()); - m_rtGlobalStats.IncreaseGlobalTasksPosition(pData->spSrcFile->GetLength64()); + m_localStats.IncreaseProcessedSize(pData->spSrcFile->GetLength64()); pData->bProcessed = false; return; } @@ -1569,8 +1744,7 @@ switch (frResult) { case CFeedbackHandler::eResult_Skip: - IncreaseProcessedSize(pData->spSrcFile->GetLength64()); - m_rtGlobalStats.IncreaseGlobalTasksPosition(pData->spSrcFile->GetLength64()); + m_localStats.IncreaseProcessedSize(pData->spSrcFile->GetLength64()); pData->bProcessed = false; return; break; @@ -1640,8 +1814,7 @@ throw new CProcessingException(E_CANCEL); break; case CFeedbackHandler::eResult_Skip: - IncreaseProcessedSize(pData->spSrcFile->GetLength64()); - m_rtGlobalStats.IncreaseGlobalTasksPosition(pData->spSrcFile->GetLength64()); + m_localStats.IncreaseProcessedSize(pData->spSrcFile->GetLength64()); pData->bProcessed = false; return; break; @@ -1700,8 +1873,7 @@ break; case CFeedbackHandler::eResult_Skip: bRetry = false; - IncreaseProcessedSize(pData->spSrcFile->GetLength64()); - m_rtGlobalStats.IncreaseGlobalTasksPosition(pData->spSrcFile->GetLength64()); + m_localStats.IncreaseProcessedSize(pData->spSrcFile->GetLength64()); pData->bProcessed = false; return; default: @@ -1714,10 +1886,7 @@ bRetry = false; // file pointers moved - so we have skipped some work - update positions if(bFirstPass) - { - IncreaseProcessedSize(ullMove); - m_rtGlobalStats.IncreaseGlobalTasksPosition(ullMove); - } + m_localStats.IncreaseProcessedSize(ullMove); } } } @@ -1831,8 +2000,7 @@ case CFeedbackHandler::eResult_Skip: bRetry = false; // TODO: correct the skip length handling - IncreaseProcessedSize(pData->spSrcFile->GetLength64()); - m_rtGlobalStats.IncreaseGlobalTasksPosition(pData->spSrcFile->GetLength64()); + m_localStats.IncreaseProcessedSize(pData->spSrcFile->GetLength64()); pData->bProcessed = false; return; default: @@ -1885,8 +2053,7 @@ case CFeedbackHandler::eResult_Skip: bRetry = false; // TODO: correct the skip length handling - IncreaseProcessedSize(pData->spSrcFile->GetLength64()); - m_rtGlobalStats.IncreaseGlobalTasksPosition(pData->spSrcFile->GetLength64()); + m_localStats.IncreaseProcessedSize(pData->spSrcFile->GetLength64()); pData->bProcessed = false; return; default: @@ -1896,17 +2063,14 @@ } // increase count of processed data - IncreaseProcessedSize(ulRead); - m_rtGlobalStats.IncreaseGlobalTasksPosition(ulRead); - // TRACE("Read: %d, Written: %d\n", rd, ulWritten); + m_localStats.IncreaseProcessedSize(ulRead); } while(ulRead != 0); } else { // we don't copy contents, but need to increase processed size - IncreaseProcessedSize(pData->spSrcFile->GetLength64()); - m_rtGlobalStats.IncreaseGlobalTasksPosition(pData->spSrcFile->GetLength64()); + m_localStats.IncreaseProcessedSize(pData->spSrcFile->GetLength64()); } // close files @@ -1935,7 +2099,7 @@ m_log.logi(_T("Processing files/folders (ProcessFiles)")); // count how much has been done (updates also a member in CTaskArray) - CalcProcessedSize(); + CalculateProcessedSize(); // create a buffer of size m_nBufferSize CUSTOM_COPY_PARAMS ccp; @@ -2071,8 +2235,7 @@ } } - IncreaseProcessedSize(spFileInfo->GetLength64()); - m_rtGlobalStats.IncreaseGlobalTasksPosition(spFileInfo->GetLength64()); + m_localStats.IncreaseProcessedSize(spFileInfo->GetLength64()); spFileInfo->SetFlags(FIF_PROCESSED, FIF_PROCESSED); } else @@ -2160,8 +2323,6 @@ UINT CTask::ThrdProc(LPVOID pParam) { - TRACE("\n\nENTERING ThrdProc (new task started)...\n"); - CTask* pTask = static_cast(pParam); chcore::IFeedbackHandler* piFeedbackHandler = pTask->GetFeedbackHandler(); @@ -2203,9 +2364,8 @@ || pTask->GetStatus(ST_STEP_MASK) == ST_SEARCHING)) { // get rid of info about processed sizes - pTask->m_rtGlobalStats.DecreaseGlobalProgressData(pTask->GetProcessedSize(), pTask->GetAllSize()); - pTask->SetProcessedSize(0); - pTask->SetAllSize(0); + pTask->m_localStats.SetProcessedSize(0); + pTask->m_localStats.SetTotalSize(0); // start searching pTask->RecurseDirectories(); @@ -2270,7 +2430,7 @@ if(pTask->GetStatus(ST_STEP_MASK) == ST_COPYING) { // decrease processed in ctaskarray - the rest will be done in ProcessFiles - pTask->m_rtGlobalStats.DecreaseGlobalTasksPosition(pTask->GetProcessedSize()); + //pTask->m_rtGlobalStats.DecreaseGlobalTasksPosition(pTask->GetProcessedSize()); pTask->ProcessFiles(); } @@ -2285,7 +2445,7 @@ pTask->Store(false); // we are ending - pTask->UnregisterTaskAsRunning(); + pTask->m_localStats.MarkTaskAsNotRunning(); // play sound piFeedbackHandler->RequestFeedback(CFeedbackHandler::eFT_OperationFinished, NULL); @@ -2355,7 +2515,7 @@ if(pTask->GetStatus(ST_WAITING_MASK) & ST_WAITING) pTask->SetStatus(0, ST_WAITING); - pTask->UnregisterTaskAsRunning(); + pTask->m_localStats.MarkTaskAsNotRunning(); pTask->SetContinueFlag(false); pTask->SetForceFlag(false); pTask->SetKilledFlag(); @@ -2366,114 +2526,10 @@ return 0xffffffff; // almost like -1 } - TRACE("TASK FINISHED - exiting ThrdProc.\n"); return 0; } //////////////////////////////////////////////////////////////////////////////// -// TTasksGlobalStats members - -TTasksGlobalStats::TTasksGlobalStats() : - m_ullGlobalTasksSize(0), - m_ullGlobalTasksPosition(0), - m_stRunningTasks(0) -{ -} - -TTasksGlobalStats::~TTasksGlobalStats() -{ -} - -void TTasksGlobalStats::IncreaseGlobalTasksSize(unsigned long long ullModify) -{ - m_lock.lock(); - m_ullGlobalTasksSize += ullModify; - m_lock.unlock(); -} - -void TTasksGlobalStats::DecreaseGlobalTasksSize(unsigned long long ullModify) -{ - m_lock.lock(); - m_ullGlobalTasksSize -= ullModify; - m_lock.unlock(); -} - -unsigned long long TTasksGlobalStats::GetGlobalTasksSize() const -{ - boost::shared_lock lock(m_lock); - return m_ullGlobalTasksSize; -} - -void TTasksGlobalStats::IncreaseGlobalTasksPosition(unsigned long long ullModify) -{ - m_lock.lock(); - m_ullGlobalTasksPosition += ullModify; - m_lock.unlock(); -} - -void TTasksGlobalStats::DecreaseGlobalTasksPosition(unsigned long long ullModify) -{ - m_lock.lock(); - m_ullGlobalTasksPosition -= ullModify; - m_lock.unlock(); -} - -unsigned long long TTasksGlobalStats::GetGlobalTasksPosition() const -{ - boost::shared_lock lock(m_lock); - return m_ullGlobalTasksPosition; -} - -void TTasksGlobalStats::IncreaseGlobalProgressData(unsigned long long ullTasksPosition, unsigned long long ullTasksSize) -{ - m_lock.lock(); - m_ullGlobalTasksSize += ullTasksSize; - m_ullGlobalTasksPosition += ullTasksPosition; - m_lock.unlock(); - -} - -void TTasksGlobalStats::DecreaseGlobalProgressData(unsigned long long ullTasksPosition, unsigned long long ullTasksSize) -{ - m_lock.lock(); - m_ullGlobalTasksSize += ullTasksSize; - m_ullGlobalTasksPosition += ullTasksPosition; - m_lock.unlock(); -} - -int TTasksGlobalStats::GetProgressPercents() const -{ - unsigned long long llPercent = 0; - - boost::shared_lock lock(m_lock); - - if(m_ullGlobalTasksSize != 0) - llPercent = m_ullGlobalTasksPosition * 100 / m_ullGlobalTasksSize; - - return boost::numeric_cast(llPercent); -} - -void TTasksGlobalStats::IncreaseRunningTasks() -{ - m_lock.lock(); - ++m_stRunningTasks; - m_lock.unlock(); -} - -void TTasksGlobalStats::DecreaseRunningTasks() -{ - m_lock.lock(); - --m_stRunningTasks; - m_lock.unlock(); -} - -size_t TTasksGlobalStats::GetRunningTasksCount() const -{ - boost::shared_lock lock(m_lock); - return m_stRunningTasks; -} - -//////////////////////////////////////////////////////////////////////////////// // CTaskArray members CTaskArray::CTaskArray() : m_piFeedbackFactory(NULL), @@ -2503,7 +2559,7 @@ if(!piHandler) return CTaskPtr(); - CTaskPtr spTask(new CTask(piHandler, m_stNextSessionUniqueID++, m_globalStats)); + CTaskPtr spTask(new CTask(piHandler, m_stNextSessionUniqueID++)); return spTask; } @@ -2551,9 +2607,9 @@ spNewTask->SetTaskPath(m_strTasksDir.c_str()); m_vTasks.push_back(spNewTask); + + spNewTask->OnRegisterTask(m_globalStats); - m_globalStats.IncreaseGlobalProgressData(spNewTask->GetProcessedSize(), spNewTask->GetAllSize()); - return m_vTasks.size() - 1; } @@ -2572,7 +2628,7 @@ // kill task if needed spTask->KillThread(); - m_globalStats.DecreaseGlobalProgressData(spTask->GetProcessedSize(), spTask->GetAllSize()); + spTask->OnUnregisterTask(); } // remove elements from array @@ -2603,7 +2659,7 @@ if((spTask->GetStatus(ST_STEP_MASK) == ST_FINISHED || spTask->GetStatus(ST_STEP_MASK) == ST_CANCELLED) && spTask->GetKilledFlag()) { - m_globalStats.DecreaseGlobalProgressData(spTask->GetProcessedSize(), spTask->GetAllSize()); + spTask->OnUnregisterTask(); vTasksToRemove.push_back(spTask); m_vTasks.erase(m_vTasks.begin() + stIndex); @@ -2633,7 +2689,7 @@ // kill task if needed spTask->KillThread(); - m_globalStats.DecreaseGlobalProgressData(spTask->GetProcessedSize(), spTask->GetAllSize()); + spTask->OnUnregisterTask(); // delete associated files spTask->DeleteProgress(m_strTasksDir.c_str()); @@ -2663,7 +2719,7 @@ // turn on some thread - find something with wait state if(spTask->GetStatus(ST_WAITING_MASK) & ST_WAITING && (stMaxRunningTasks == 0 || m_globalStats.GetRunningTasksCount() < stMaxRunningTasks)) { - spTask->RegisterTaskAsRunning(); + spTask->m_localStats.MarkTaskAsRunning(); spTask->SetContinueFlagNL(true); } } @@ -2761,7 +2817,7 @@ } } -bool CTaskArray::TasksRetryProcessing(bool bOnlyErrors/*=false*/, UINT uiInterval) +bool CTaskArray::TasksRetryProcessing(bool bOnlyErrors, UINT uiInterval) { boost::shared_lock lock(m_lock); bool bChanged=false; Index: src/ch/task.h =================================================================== diff -u -rb55cf150df571c279536a2b4e33bcecedef17305 -rb13e3052f13b2f0a8c53c37a7b9246418b88bebd --- src/ch/task.h (.../task.h) (revision b55cf150df571c279536a2b4e33bcecedef17305) +++ src/ch/task.h (.../task.h) (revision b13e3052f13b2f0a8c53c37a7b9246418b88bebd) @@ -211,14 +211,89 @@ HANDLE m_hHandle; ///< System handle }; -class TTasksGlobalStats; +/////////////////////////////////////////////////////////////////////////// +// TTasksGlobalStats +class TTasksGlobalStats +{ +public: + TTasksGlobalStats(); + ~TTasksGlobalStats(); + void IncreaseGlobalTasksSize(unsigned long long ullModify); + void DecreaseGlobalTasksSize(unsigned long long ullModify); + unsigned long long GetGlobalTasksSize() const; + + void IncreaseGlobalTasksPosition(unsigned long long ullModify); + void DecreaseGlobalTasksPosition(unsigned long long ullModify); + unsigned long long GetGlobalTasksPosition() const; + + void IncreaseGlobalProgressData(unsigned long long ullTasksPosition, unsigned long long ullTasksSize); + void DecreaseGlobalProgressData(unsigned long long ullTasksPosition, unsigned long long ullTasksSize); + + int GetProgressPercents() const; + + void IncreaseRunningTasks(); + void DecreaseRunningTasks(); + size_t GetRunningTasksCount() const; + +private: + volatile unsigned long long m_ullGlobalTasksSize; + volatile unsigned long long m_ullGlobalTasksPosition; + + volatile size_t m_stRunningTasks; // count of current operations + mutable boost::shared_mutex m_lock; +}; + +/////////////////////////////////////////////////////////////////////////// +// TTaskLocalStats +class TTaskLocalStats +{ +public: + TTaskLocalStats(); + ~TTaskLocalStats(); + + void ConnectGlobalStats(TTasksGlobalStats& rtGlobalStats); + void DisconnectGlobalStats(); + + void IncreaseProcessedSize(unsigned long long ullAdd); + void DecreaseProcessedSize(unsigned long long ullSub); + void SetProcessedSize(unsigned long long ullSet); + unsigned long long GetProcessedSize() const; + unsigned long long GetUnProcessedSize() const; + + void IncreaseTotalSize(unsigned long long ullAdd); + void DecreaseTotalSize(unsigned long long ullSub); + void SetTotalSize(unsigned long long ullSet); + unsigned long long GetTotalSize() const; + + int GetProgressInPercent() const; + + void MarkTaskAsRunning(); + void MarkTaskAsNotRunning(); + bool IsRunning() const; + +private: + volatile unsigned long long m_ullProcessedSize; + volatile unsigned long long m_ullTotalSize; + + volatile bool m_bTaskIsRunning; + + mutable boost::shared_mutex m_lock; + TTasksGlobalStats* m_prtGlobalStats; +}; + +/////////////////////////////////////////////////////////////////////////// +// CTask class CTask { public: - CTask(chcore::IFeedbackHandler* piFeedbackHandler, size_t stSessionUniqueID, TTasksGlobalStats& tGlobalStats); + CTask(chcore::IFeedbackHandler* piFeedbackHandler, size_t stSessionUniqueID); ~CTask(); + // methods are called when task is being added or removed from the global task array + void OnRegisterTask(TTasksGlobalStats& rtGlobalStats); + void OnUnregisterTask(); + // m_clipboard void AddClipboardData(const CClipboardEntryPtr& spEntry); CClipboardEntryPtr GetClipboardData(size_t stIndex); @@ -257,16 +332,8 @@ int GetPriority(); void SetPriority(int nPriority); - // m_nProcessed - void IncreaseProcessedSize(__int64 nSize); - void SetProcessedSize(__int64 nSize); - __int64 GetProcessedSize(); + void CalculateTotalSize(); - // m_nAll - void SetAllSize(__int64 nSize); - __int64 GetAllSize(); - void CalcAllSize(); - // m_bKill void SetKillFlag(bool bKill = true); bool GetKillFlag(); @@ -298,11 +365,8 @@ void DeleteProgress(LPCTSTR lpszDirectory); void SetOsErrorCode(DWORD dwError, LPCTSTR lpszErrDesc); - void CalcProcessedSize(); + void CalculateProcessedSize(); - void RegisterTaskAsRunning(); - void UnregisterTaskAsRunning(); - bool CanBegin(); void UpdateTime(); @@ -365,16 +429,8 @@ int GetPriorityNL(); void SetPriorityNL(int nPriority); - // m_nProcessed - void IncreaseProcessedSizeNL(__int64 nSize); - void SetProcessedSizeNL(__int64 nSize); - __int64 GetProcessedSizeNL(); + void CalculateTotalSizeNL(); - // m_nAll - void SetAllSizeNL(__int64 nSize); - __int64 GetAllSizeNL(); - void CalcAllSizeNL(); - void SetKillFlagNL(bool bKill = true); bool GetKillFlagNL(); @@ -391,9 +447,6 @@ void SetContinueFlagNL(bool bFlag = true); bool GetContinueFlagNL(); - void RegisterTaskAsRunningNL(); - void UnregisterTaskAsRunningNL(); - private: icpf::log_file m_log; mutable boost::shared_mutex m_lock; // protection for this class @@ -413,9 +466,11 @@ CClipboardArray m_clipboard; CFileInfoArray m_files; - volatile size_t m_stCurrentIndex; - size_t m_stLastProcessedIndex; + volatile size_t m_stCurrentIndex; + + size_t m_stLastProcessedIndex; + CDestPath m_dpDestPath; volatile UINT m_nStatus; @@ -430,11 +485,6 @@ CWinThread *m_pThread; int m_nPriority; - __int64 m_nProcessed; - __int64 m_nAll; - - bool m_bQueued; // has operations pending for this task been increased ? - volatile bool m_bKill; volatile bool m_bKilled; @@ -454,7 +504,7 @@ size_t m_stSessionUniqueID; bool m_bRegisteredAsRunning; - TTasksGlobalStats& m_rtGlobalStats; + TTaskLocalStats m_localStats; friend class CTaskArray; }; @@ -480,39 +530,6 @@ }; /////////////////////////////////////////////////////////////////////////// -// TTasksGlobalStats -class TTasksGlobalStats -{ -public: - TTasksGlobalStats(); - ~TTasksGlobalStats(); - - void IncreaseGlobalTasksSize(unsigned long long ullModify); - void DecreaseGlobalTasksSize(unsigned long long ullModify); - unsigned long long GetGlobalTasksSize() const; - - void IncreaseGlobalTasksPosition(unsigned long long ullModify); - void DecreaseGlobalTasksPosition(unsigned long long ullModify); - unsigned long long GetGlobalTasksPosition() const; - - void IncreaseGlobalProgressData(unsigned long long ullTasksPosition, unsigned long long ullTasksSize); - void DecreaseGlobalProgressData(unsigned long long ullTasksPosition, unsigned long long ullTasksSize); - - int GetProgressPercents() const; - - void IncreaseRunningTasks(); - void DecreaseRunningTasks(); - size_t GetRunningTasksCount() const; - -private: - volatile unsigned long long m_ullGlobalTasksSize; - volatile unsigned long long m_ullGlobalTasksPosition; - - volatile size_t m_stRunningTasks; // count of current operations - mutable boost::shared_mutex m_lock; -}; - -/////////////////////////////////////////////////////////////////////////// // CTaskArray class CTaskArray