OSDN Git Service

Improve plugin system (#797) (6)
[winmerge-jp/winmerge-jp.git] / Src / DiffThread.cpp
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** 
3  * @file  DiffThread.cpp
4  *
5  * @brief Code for DiffThread class
6  */
7
8 #include "pch.h"
9 #include "DiffThread.h"
10 #include <cassert>
11 #include <climits>
12 #include <Poco/Thread.h>
13 #include <Poco/Semaphore.h>
14 #include "UnicodeString.h"
15 #include "CompareStats.h"
16 #include "IAbortable.h"
17 #include "Plugins.h"
18 #include "DebugNew.h"
19
20 using Poco::Thread;
21 using Poco::Semaphore;
22
23 // Thread functions
24 static void DiffThreadCollect(void *lpParam);
25 static void DiffThreadCompare(void *lpParam);
26
27 /** @brief abort handler for CDiffThread -- just a gateway to CDiffThread */
28 class DiffThreadAbortable : public IAbortable
29 {
30 // Implement DirScan's IAbortable
31 public:
32         virtual bool ShouldAbort() const override { return m_diffthread->ShouldAbort(); }
33
34 // All this object does is forward ShouldAbort calls to its containing CDiffThread
35
36         explicit DiffThreadAbortable(CDiffThread * diffthread) : m_diffthread(diffthread) { }
37         CDiffThread * m_diffthread;
38 };
39
40 /**
41  * @brief Default constructor.
42  */
43 CDiffThread::CDiffThread()
44 : m_pDiffContext(nullptr)
45 , m_bAborting(false)
46 , m_bPaused(false)
47 , m_pDiffParm(new DiffFuncStruct)
48 , m_pAbortgate(new DiffThreadAbortable(this))
49 {
50 }
51
52 /**
53  * @brief Destructor, release resources.
54  */
55 CDiffThread::~CDiffThread()
56 {
57         delete m_pDiffParm->pSemaphore;
58 }
59
60 /**
61  * @brief runtime interface for child thread, called on child thread
62  */
63 bool CDiffThread::ShouldAbort() const
64 {
65         while (m_bPaused && !m_bAborting)
66                 Poco::Thread::sleep(100);
67         return m_bAborting;
68 }
69
70 /**
71  * @brief Start and run directory compare thread.
72  * @return Success (1) or error for thread. Currently always 1.
73  */
74 unsigned CDiffThread::CompareDirectories()
75 {
76         assert(m_pDiffParm->nThreadState != THREAD_COMPARING);
77
78         m_pDiffParm->context = m_pDiffContext;
79         m_pDiffParm->m_pAbortgate = m_pAbortgate.get();
80         m_bAborting = false;
81         m_bPaused = false;
82
83         m_pDiffParm->nThreadState = THREAD_COMPARING;
84
85         delete m_pDiffParm->pSemaphore;
86         m_pDiffParm->pSemaphore = new Semaphore(0, LONG_MAX);
87
88         m_pDiffParm->context->m_pCompareStats->SetCompareState(CompareStats::STATE_START);
89
90         m_threads[0].start(DiffThreadCollect, m_pDiffParm.get());
91         m_threads[1].start(DiffThreadCompare, m_pDiffParm.get());
92
93         return 1;
94 }
95
96 /**
97  * @brief Item collection thread function.
98  *
99  * This thread is responsible for finding and collecting all items to compare
100  * to the item list.
101  * @param [in] lpParam Pointer to parameter structure.
102  * @return Thread's return value.
103  */
104 static void DiffThreadCollect(void *pParam)
105 {
106         DiffFuncStruct *myStruct = static_cast<DiffFuncStruct *>(pParam);
107
108         // Stash abortable interface into context
109         myStruct->context->SetAbortable(myStruct->m_pAbortgate);
110
111         if (myStruct->m_fncCollect)
112                 myStruct->m_fncCollect(myStruct);
113
114         // Release Semaphore() once again to signal that collect phase is ready
115         myStruct->pSemaphore->set();
116
117         // Send message to UI to update
118         int event = CDiffThread::EVENT_COLLECT_COMPLETED;
119         myStruct->m_listeners.notify(myStruct, event);
120 };
121
122 /**
123  * @brief Folder compare thread function.
124  *
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.
129  */
130 static void DiffThreadCompare(void *pParam)
131 {
132         DiffFuncStruct *myStruct = static_cast<DiffFuncStruct *>(pParam);
133         CAssureScriptsForThread scriptsForRescan;
134
135         // Stash abortable interface into context
136         myStruct->context->SetAbortable(myStruct->m_pAbortgate);
137
138         myStruct->context->m_pCompareStats->SetCompareState(CompareStats::STATE_COMPARE);
139
140         // Now do all pending file comparisons
141         myStruct->m_fncCompare(myStruct);
142
143         myStruct->context->m_pCompareStats->SetCompareState(CompareStats::STATE_IDLE);
144
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);
149 }