forked from Tulon/scantailor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkerThread.cpp
322 lines (260 loc) · 7.27 KB
/
WorkerThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) Joseph Artsimovich <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WorkerThread.h"
#include "WorkerThread.h.moc"
#include "ThreadPriority.h"
#include "OutOfMemoryHandler.h"
#include <QCoreApplication>
#include <QThread>
#include <QEvent>
#include <QSettings>
#include <new>
#include <assert.h>
#if defined(Q_OS_LINUX) // For Linux updatePriority()
#include <unistd.h>
#include <errno.h>
#include <sys/resource.h>
#endif
class WorkerThread::Dispatcher : public QObject
{
public:
enum UpdatePriorityResult {
PriorityUpdated,
PriorityUpdateFailed,
ThreadRestartRequired
};
Dispatcher(Impl& owner);
UpdatePriorityResult updateThreadPriority(BackgroundTask const& task);
void maybeProcessQueuedTask();
protected:
virtual void customEvent(QEvent* event);
private:
void processTask(BackgroundTaskPtr const& task);
Impl& m_rOwner;
/**
* This one will be set if we decide we need to restart
* the background thread before processing a given task.
*/
BackgroundTaskPtr m_ptrQueuedTask;
};
class WorkerThread::Impl : public QThread
{
public:
enum { NormalExit = 0, ExitForRestart };
Impl(WorkerThread& owner);
~Impl();
void performTask(BackgroundTaskPtr const& task);
protected:
virtual void run();
virtual void customEvent(QEvent* event);
protected:
void resetPriority();
private:
static QEvent::Type const ThreadRestartEvent = (QEvent::Type)(QEvent::User + 1);
WorkerThread& m_rOwner;
Dispatcher m_dispatcher;
bool m_threadStarted;
};
class WorkerThread::PerformTaskEvent : public QEvent
{
public:
PerformTaskEvent(BackgroundTaskPtr const& task);
BackgroundTaskPtr const& task() const { return m_ptrTask; }
private:
BackgroundTaskPtr m_ptrTask;
};
class WorkerThread::TaskResultEvent : public QEvent
{
public:
TaskResultEvent(BackgroundTaskPtr const& task, FilterResultPtr const& result);
BackgroundTaskPtr const& task() const { return m_ptrTask; }
FilterResultPtr const& result() const { return m_ptrResult; }
private:
BackgroundTaskPtr m_ptrTask;
FilterResultPtr m_ptrResult;
};
/*=============================== WorkerThread ==============================*/
WorkerThread::WorkerThread(QObject* parent)
: QObject(parent),
m_ptrImpl(new Impl(*this))
{
}
WorkerThread::~WorkerThread()
{
}
void
WorkerThread::shutdown()
{
m_ptrImpl.reset();
}
void
WorkerThread::performTask(BackgroundTaskPtr const& task)
{
if (m_ptrImpl.get()) {
m_ptrImpl->performTask(task);
}
}
void
WorkerThread::emitTaskResult(
BackgroundTaskPtr const& task, FilterResultPtr const& result)
{
emit taskResult(task, result);
}
/*======================== WorkerThread::Dispatcher ========================*/
WorkerThread::Dispatcher::Dispatcher(Impl& owner)
: m_rOwner(owner)
{
}
WorkerThread::Dispatcher::UpdatePriorityResult
WorkerThread::Dispatcher::updateThreadPriority(BackgroundTask const& task)
{
assert(QCoreApplication::instance()->thread() != QThread::currentThread());
ThreadPriority prio(
ThreadPriority::load(
"settings/batch_processing_priority", ThreadPriority::Normal
)
);
if (task.type() == task.INTERACTIVE) {
prio.setValue(ThreadPriority::Normal);
}
#if defined(Q_OS_LINUX)
/*
On Linux, there is no good way of adjusting priority of an individual
thread of a process. In particular, pthread_setschedparam() only works
for realtime threads.
Fortunately, the following quote from pthread(7) provides a workaround
that's ugly but better than nothing:
----------------------------------------------------
NPTL still has a few non-conformances with POSIX.1:
...
Threads do not share a common nice value.
----------------------------------------------------
*/
if (setpriority(PRIO_PROCESS, 0, prio.toPosixNiceLevel()) != 0) {
if (errno == EACCES) {
// Unless the executable has special permissions, lowering
// the process nice value (even if it doesn't go below zero)
// will fail with this error code. In this case, we restart
// the thread.
return ThreadRestartRequired;
} else {
return PriorityUpdateFailed;
}
}
#else
// QThread::setPriority() doesn't do anything on Linux BTW.
m_rOwner.setPriority(prio.toQThreadPriority());
#endif
return PriorityUpdated;
}
void
WorkerThread::Dispatcher::maybeProcessQueuedTask()
{
if (m_ptrQueuedTask.get()) {
BackgroundTaskPtr task;
m_ptrQueuedTask.swap(task);
// In this case we must not restart the thread if updateThreadPriority()
// tells us we should. After all, we've just did that, and doing it again
// would probably lead to infinite loop.
updateThreadPriority(*task);
processTask(task);
}
}
void
WorkerThread::Dispatcher::customEvent(QEvent* event)
{
PerformTaskEvent* evt = dynamic_cast<PerformTaskEvent*>(event);
assert(evt);
BackgroundTaskPtr const& task = evt->task();
if (updateThreadPriority(*task) == ThreadRestartRequired) {
m_ptrQueuedTask = task;
m_rOwner.exit(Impl::ExitForRestart);
return;
}
processTask(task);
}
void
WorkerThread::Dispatcher::processTask(BackgroundTaskPtr const& task)
{
if (task->isCancelled()) {
return;
}
try {
FilterResultPtr const result((*task)());
if (result) {
QCoreApplication::postEvent(
&m_rOwner, new TaskResultEvent(task, result)
);
}
} catch (std::bad_alloc const&) {
OutOfMemoryHandler::instance().handleOutOfMemorySituation();
}
}
/*========================== WorkerThread::Impl ============================*/
WorkerThread::Impl::Impl(WorkerThread& owner)
: m_rOwner(owner),
m_dispatcher(*this),
m_threadStarted(false)
{
m_dispatcher.moveToThread(this);
}
WorkerThread::Impl::~Impl()
{
exit(NormalExit);
wait();
}
void
WorkerThread::Impl::performTask(BackgroundTaskPtr const& task)
{
QCoreApplication::postEvent(&m_dispatcher, new PerformTaskEvent(task));
if (!m_threadStarted) {
start();
m_threadStarted = true;
}
}
void
WorkerThread::Impl::run()
{
m_dispatcher.maybeProcessQueuedTask();
if (exec() == ExitForRestart) {
QCoreApplication::postEvent(this, new QEvent(ThreadRestartEvent));
}
}
void
WorkerThread::Impl::customEvent(QEvent* event)
{
if (event->type() == ThreadRestartEvent) {
start();
return;
}
if (TaskResultEvent* evt = dynamic_cast<TaskResultEvent*>(event)) {
m_rOwner.emitTaskResult(evt->task(), evt->result());
}
}
/*====================== WorkerThread::PerformTaskEvent ====================*/
WorkerThread::PerformTaskEvent::PerformTaskEvent(
BackgroundTaskPtr const& task)
: QEvent(User),
m_ptrTask(task)
{
}
/*====================== WorkerThread::TaskResultEvent =====================*/
WorkerThread::TaskResultEvent::TaskResultEvent(
BackgroundTaskPtr const& task, FilterResultPtr const& result)
: QEvent(User),
m_ptrTask(task),
m_ptrResult(result)
{
}