Repository navigation
Expand file tree
/
Copy pathIOThread.cpp
More file actions
141 lines (124 loc) · 3.6 KB
/
Copy pathIOThread.cpp
File metadata and controls
141 lines (124 loc) · 3.6 KB
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
#include "IOThread.h"
#include "IOWorker.h"
#include <limits>
#include "multiresolutionimageinterface/MultiResolutionImage.h"
#include "interfaces/interfaces.h"
#include "WSITileGraphicsItem.h"
using namespace pathology;
IOThread::IOThread(QObject *parent, unsigned int nrThreads) :
QObject(parent),
_abort(false),
_threadsWaiting(0)
{
for (int i = 0; i < nrThreads; ++i) {
IOWorker* worker = new IOWorker(this);
worker->start(QThread::HighPriority);
_workers.push_back(worker);
}
}
IOThread::~IOThread()
{
shutdown();
}
void IOThread::shutdown() {
_abort = true;
for (std::vector<IOWorker*>::iterator it = _workers.begin(); it != _workers.end(); ++it) {
(*it)->abort();
while ((*it)->isRunning()) {
_condition.wakeOne();
}
delete (*it);
}
_workers.clear();
}
void IOThread::onBackgroundChannelChanged(int channel) {
_jobListMutex.lock();
for (unsigned int i = 0; i < _workers.size(); ++i) {
_workers[i]->setBackgroundChannel(channel);
}
_jobListMutex.unlock();
}
void IOThread::onForegroundChannelChanged(int channel) {
_jobListMutex.lock();
for (unsigned int i = 0; i < _workers.size(); ++i) {
_workers[i]->setForegroundChannel(channel);
}
_jobListMutex.unlock();
}
void IOThread::onLUTChanged(const pathology::LUT& LUT) {
_jobListMutex.lock();
for (unsigned int i = 0; i < _workers.size(); ++i) {
_workers[i]->setLUT(LUT);
}
_jobListMutex.unlock();
}
std::vector<IOWorker*> IOThread::getWorkers() {
return _workers;
}
void IOThread::addJob(const unsigned int tileSize, const long long imgPosX, const long long imgPosY, const unsigned int level, ImageSource* foregroundTile)
{
ThreadJob* job = NULL;
if (foregroundTile) {
job = new RenderJob(tileSize, imgPosX, imgPosY, level, foregroundTile);
}
else {
job = new IOJob(tileSize, imgPosX, imgPosY, level);
}
QMutexLocker locker(&_jobListMutex);
_jobList.push_front(job);
_condition.wakeOne();
}
void IOThread::setForegroundImage(std::weak_ptr<MultiResolutionImage> for_img, float scale) {
QMutexLocker locker(&_jobListMutex);
_for_img = for_img;
for (unsigned int i = 0; i < _workers.size(); ++i) {
_workers[i]->setForegroundImage(for_img, scale);
}
}
void IOThread::setBackgroundImage(std::weak_ptr<MultiResolutionImage> bck_img) {
QMutexLocker locker(&_jobListMutex);
_bck_img = bck_img;
for (unsigned int i = 0; i < _workers.size(); ++i) {
_workers[i]->setBackgroundImage(_bck_img);
}
}
unsigned int IOThread::getWaitingThreads() {
return _threadsWaiting;
}
ThreadJob* IOThread::getJob() {
_jobListMutex.lock();
while (_jobList.empty() && !_abort) {
_threadsWaiting++;
_condition.wait(&_jobListMutex);
_threadsWaiting--;
}
if (_abort) {
_jobListMutex.unlock();
return NULL;
}
ThreadJob* job = _jobList.front();
_jobList.pop_front();
_jobListMutex.unlock();
return job;
}
void IOThread::clearJobs() {
QMutexLocker locker(&_jobListMutex);
for (auto job : _jobList) {
if (_workers.size() > 0) {
if (dynamic_cast<IOJob*>(job)) {
emit _workers[0]->tileLoaded(nullptr, job->_imgPosX, job->_imgPosY, job->_tileSize, 0, job->_level, nullptr, nullptr);
}
else {
emit _workers[0]->foregroundTileRendered(nullptr, job->_imgPosX, job->_imgPosY, job->_level);
}
}
delete job;
}
_jobList.clear();
}
unsigned int IOThread::numberOfJobs() {
_jobListMutex.lock();
unsigned int nrJobs = _jobList.size();
_jobListMutex.unlock();
return nrJobs;
}