From 231a822e0e602444e4533f2579e33d8d08376d09 Mon Sep 17 00:00:00 2001 From: Takashi Sawanaka Date: Sun, 9 Jun 2019 14:28:36 +0900 Subject: [PATCH] DiffThread: refactor --- Src/DiffThread.cpp | 46 ++++------------------------------------------ Src/DiffThread.h | 9 +++++---- Src/DirDoc.cpp | 31 ++++++++++++++++++++++++++----- Src/DirScan.cpp | 3 ++- 4 files changed, 37 insertions(+), 52 deletions(-) diff --git a/Src/DiffThread.cpp b/Src/DiffThread.cpp index c1bba012b..b464fdfc0 100644 --- a/Src/DiffThread.cpp +++ b/Src/DiffThread.cpp @@ -27,10 +27,6 @@ #include #include #include "UnicodeString.h" -#include "DiffContext.h" -#include "DirScan.h" -#include "DiffItemList.h" -#include "PathContext.h" #include "CompareStats.h" #include "IAbortable.h" #include "DebugNew.h" @@ -63,8 +59,8 @@ CDiffThread::CDiffThread() , m_bAborting(false) , m_bPaused(false) , m_pDiffParm(new DiffFuncStruct) +, m_pAbortgate(new DiffThreadAbortable(this)) { - m_pAbortgate.reset(new DiffThreadAbortable(this)); } /** @@ -104,7 +100,6 @@ unsigned CDiffThread::CompareDirectories() m_pDiffParm->context = m_pDiffContext; m_pDiffParm->m_pAbortgate = m_pAbortgate.get(); - m_pDiffParm->bOnlyRequested = m_bOnlyRequested; m_bAborting = false; m_bPaused = false; @@ -115,31 +110,13 @@ unsigned CDiffThread::CompareDirectories() m_pDiffParm->context->m_pCompareStats->SetCompareState(CompareStats::STATE_START); - if (!m_bOnlyRequested) - m_threads[0].start(DiffThreadCollect, m_pDiffParm.get()); - else - { - int nItems = DirScan_UpdateMarkedItems(m_pDiffParm.get(), nullptr); - // Send message to UI to update - int event = CDiffThread::EVENT_COLLECT_COMPLETED; - m_pDiffParm->m_listeners.notify(m_pDiffParm.get(), event); - m_pDiffParm->context->m_pCompareStats->IncreaseTotalItems(nItems - m_pDiffParm->context->m_pCompareStats->GetTotalItems()); - } + m_threads[0].start(DiffThreadCollect, m_pDiffParm.get()); m_threads[1].start(DiffThreadCompare, m_pDiffParm.get()); return 1; } /** - * @brief Selects to compare all or only selected items. - * @param [in] bSelected If true only selected items are compared. - */ -void CDiffThread::SetCompareSelected(bool bSelected /*=false*/) -{ - m_bOnlyRequested = bSelected; -} - -/** * @brief Returns thread's current state */ unsigned CDiffThread::GetThreadState() const @@ -157,24 +134,12 @@ unsigned CDiffThread::GetThreadState() const */ static void DiffThreadCollect(void *pParam) { - PathContext paths; DiffFuncStruct *myStruct = static_cast(pParam); - assert(!myStruct->bOnlyRequested); - // Stash abortable interface into context myStruct->context->SetAbortable(myStruct->m_pAbortgate); - bool casesensitive = false; - int depth = myStruct->context->m_bRecursive ? -1 : 0; - - paths = myStruct->context->GetNormalizedPaths(); - - String subdir[3] = {_T(""), _T(""), _T("")}; // blank to start at roots specified in diff context - - // Build results list (except delaying file comparisons until below) - DirScan_GetItems(paths, subdir, myStruct, - casesensitive, depth, nullptr, myStruct->context->m_bWalkUniques); + myStruct->m_fncCollect(myStruct); // Release Semaphore() once again to signal that collect phase is ready myStruct->pSemaphore->set(); @@ -202,10 +167,7 @@ static void DiffThreadCompare(void *pParam) myStruct->context->m_pCompareStats->SetCompareState(CompareStats::STATE_COMPARE); // Now do all pending file comparisons - if (myStruct->bOnlyRequested) - DirScan_CompareRequestedItems(myStruct, nullptr); - else - DirScan_CompareItems(myStruct, nullptr); + myStruct->m_fncCompare(myStruct); myStruct->context->m_pCompareStats->SetCompareState(CompareStats::STATE_IDLE); diff --git a/Src/DiffThread.h b/Src/DiffThread.h index 388292332..62ea77df1 100644 --- a/Src/DiffThread.h +++ b/Src/DiffThread.h @@ -22,6 +22,7 @@ #pragma once #include +#include #define POCO_NO_UNWINDOWS 1 #include #include @@ -45,14 +46,14 @@ struct DiffFuncStruct Poco::BasicEvent m_listeners; /**< Event listeners */ int nThreadState; /**< Thread state. */ DiffThreadAbortable * m_pAbortgate; /**< Interface for aborting compare. */ - bool bOnlyRequested; /**< Compare only requested items? */ Poco::Semaphore *pSemaphore; /**< Semaphore for synchronizing threads. */ + std::function m_fncCollect; + std::function m_fncCompare; DiffFuncStruct() : context(nullptr) , nThreadState(0/*CDiffThread::THREAD_NOTSTARTED*/) , m_pAbortgate(nullptr) - , bOnlyRequested(false) , pSemaphore(nullptr) {} }; @@ -95,7 +96,8 @@ public: void RemoveListener(T *pObj, void (T::*pMethod)(int& state)) { m_pDiffParm->m_listeners -= Poco::delegate(pObj, pMethod); } - void SetCompareSelected(bool bSelected = false); + void SetCollectFunction(std::function func) { m_pDiffParm->m_fncCollect = func; } + void SetCompareFunction(std::function func) { m_pDiffParm->m_fncCompare = func; } // runtime interface for main thread, called on main thread unsigned GetThreadState() const; @@ -115,5 +117,4 @@ private: std::unique_ptr m_pAbortgate; bool m_bAborting; /**< Is compare aborting? */ bool m_bPaused; /**< Is compare paused? */ - bool m_bOnlyRequested; /**< Are we comparing only requested items (Update?) */ }; diff --git a/Src/DirDoc.cpp b/Src/DirDoc.cpp index 1fee51ed9..89970d15d 100644 --- a/Src/DirDoc.cpp +++ b/Src/DirDoc.cpp @@ -47,6 +47,7 @@ #include "FileFilterHelper.h" #include "unicoder.h" #include "DirActions.h" +#include "DirScan.h" #include "MessageBoxDialog.h" #ifdef _DEBUG @@ -260,10 +261,6 @@ void CDirDoc::Rescan() m_pCtxt->m_bIgnoreCodepage = GetOptionsMgr()->GetBool(OPT_CMP_IGNORE_CODEPAGE); m_pCtxt->m_pCompareStats = m_pCompareStats.get(); - // Set total items count since we don't collect items - if (m_bMarkedRescan) - m_pCompareStats->IncreaseTotalItems(m_pDirView->GetSelectedCount()); - pf->GetHeaderInterface()->SetPaneCount(m_nDirs); pf->GetHeaderInterface()->SetOnSetFocusCallback([&](int pane) { m_pDirView->SetActivePane(pane); @@ -293,7 +290,31 @@ void CDirDoc::Rescan() m_diffThread.SetContext(m_pCtxt.get()); m_diffThread.RemoveListener(this, &CDirDoc::DiffThreadCallback); m_diffThread.AddListener(this, &CDirDoc::DiffThreadCallback); - m_diffThread.SetCompareSelected(m_bMarkedRescan); + if (m_bMarkedRescan) + { + m_diffThread.SetCollectFunction([](DiffFuncStruct* myStruct) { + int nItems = DirScan_UpdateMarkedItems(myStruct, nullptr); + myStruct->context->m_pCompareStats->IncreaseTotalItems(nItems); + }); + m_diffThread.SetCompareFunction([](DiffFuncStruct* myStruct) { + DirScan_CompareRequestedItems(myStruct, nullptr); + }); + } + else + { + m_diffThread.SetCollectFunction([](DiffFuncStruct* myStruct) { + bool casesensitive = false; + int depth = myStruct->context->m_bRecursive ? -1 : 0; + PathContext paths = myStruct->context->GetNormalizedPaths(); + String subdir[3] = {_T(""), _T(""), _T("")}; // blank to start at roots specified in diff context + // Build results list (except delaying file comparisons until below) + DirScan_GetItems(paths, subdir, myStruct, + casesensitive, depth, nullptr, myStruct->context->m_bWalkUniques); + }); + m_diffThread.SetCompareFunction([](DiffFuncStruct* myStruct) { + DirScan_CompareItems(myStruct, nullptr); + }); + } m_diffThread.CompareDirectories(); m_bMarkedRescan = false; } diff --git a/Src/DirScan.cpp b/Src/DirScan.cpp index ffff1e118..19b4c2d71 100644 --- a/Src/DirScan.cpp +++ b/Src/DirScan.cpp @@ -610,8 +610,9 @@ static int CompareRequestedItems(DiffFuncStruct *myStruct, DIFFITEM *parentdiffp CDiffContext *pCtxt = myStruct->context; int res = 0; bool bCompareFailure = false; + if (parentdiffpos == nullptr) + myStruct->pSemaphore->wait(); DIFFITEM *pos = pCtxt->GetFirstChildDiffPosition(parentdiffpos); - while (pos != nullptr) { if (pCtxt->ShouldAbort()) -- 2.11.0