1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * @brief Code for DiffThread class
9 #include "DiffThread.h"
12 #include <Poco/Thread.h>
13 #include <Poco/Semaphore.h>
14 #include "UnicodeString.h"
15 #include "CompareStats.h"
16 #include "IAbortable.h"
21 using Poco::Semaphore;
24 static void DiffThreadCollect(void *lpParam);
25 static void DiffThreadCompare(void *lpParam);
27 /** @brief abort handler for CDiffThread -- just a gateway to CDiffThread */
28 class DiffThreadAbortable : public IAbortable
30 // Implement DirScan's IAbortable
32 virtual bool ShouldAbort() const override { return m_diffthread->ShouldAbort(); }
34 // All this object does is forward ShouldAbort calls to its containing CDiffThread
36 explicit DiffThreadAbortable(CDiffThread * diffthread) : m_diffthread(diffthread) { }
37 CDiffThread * m_diffthread;
41 * @brief Default constructor.
43 CDiffThread::CDiffThread()
44 : m_pDiffContext(nullptr)
47 , m_pDiffParm(new DiffFuncStruct)
48 , m_pAbortgate(new DiffThreadAbortable(this))
53 * @brief Destructor, release resources.
55 CDiffThread::~CDiffThread()
57 delete m_pDiffParm->pSemaphore;
61 * @brief runtime interface for child thread, called on child thread
63 bool CDiffThread::ShouldAbort() const
65 while (m_bPaused && !m_bAborting)
66 Poco::Thread::sleep(100);
71 * @brief Start and run directory compare thread.
72 * @return Success (1) or error for thread. Currently always 1.
74 unsigned CDiffThread::CompareDirectories()
76 assert(m_pDiffParm->nThreadState != THREAD_COMPARING);
78 m_pDiffParm->context = m_pDiffContext;
79 m_pDiffParm->m_pAbortgate = m_pAbortgate.get();
83 m_pDiffParm->nThreadState = THREAD_COMPARING;
85 delete m_pDiffParm->pSemaphore;
86 m_pDiffParm->pSemaphore = new Semaphore(0, LONG_MAX);
88 m_pDiffParm->context->m_pCompareStats->SetCompareState(CompareStats::STATE_START);
90 m_threads[0].start(DiffThreadCollect, m_pDiffParm.get());
91 m_threads[1].start(DiffThreadCompare, m_pDiffParm.get());
97 * @brief Item collection thread function.
99 * This thread is responsible for finding and collecting all items to compare
101 * @param [in] lpParam Pointer to parameter structure.
102 * @return Thread's return value.
104 static void DiffThreadCollect(void *pParam)
106 DiffFuncStruct *myStruct = static_cast<DiffFuncStruct *>(pParam);
108 // Stash abortable interface into context
109 myStruct->context->SetAbortable(myStruct->m_pAbortgate);
111 if (myStruct->m_fncCollect)
112 myStruct->m_fncCollect(myStruct);
114 // Release Semaphore() once again to signal that collect phase is ready
115 myStruct->pSemaphore->set();
117 // Send message to UI to update
118 int event = CDiffThread::EVENT_COLLECT_COMPLETED;
119 myStruct->m_listeners.notify(myStruct, event);
123 * @brief Folder compare thread function.
125 * Compares items in item list. After compare is ready
126 * sends message to UI so UI can update itself.
127 * @param [in] lpParam Pointer to parameter structure.
128 * @return Thread's return value.
130 static void DiffThreadCompare(void *pParam)
132 DiffFuncStruct *myStruct = static_cast<DiffFuncStruct *>(pParam);
133 CAssureScriptsForThread scriptsForRescan;
135 // Stash abortable interface into context
136 myStruct->context->SetAbortable(myStruct->m_pAbortgate);
138 myStruct->context->m_pCompareStats->SetCompareState(CompareStats::STATE_COMPARE);
140 // Now do all pending file comparisons
141 myStruct->m_fncCompare(myStruct);
143 myStruct->context->m_pCompareStats->SetCompareState(CompareStats::STATE_IDLE);
145 // Send message to UI to update
146 myStruct->nThreadState = CDiffThread::THREAD_COMPLETED;
147 int event = CDiffThread::EVENT_COMPARE_COMPLETED;
148 myStruct->m_listeners.notify(myStruct, event);