blob: 128071922b7b722462645aa129577fbb2c3aeda0 (
plain)
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
|
/*
* copyright (c) 2006 Boudewijn Rempt
*
* This program is free software; you can distribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kis_thread_pool.h"
#include <tdeglobal.h>
#include <tdeconfig.h>
#include <kdebug.h>
KisThreadPool * KisThreadPool::m_singleton = 0;
KisThreadPool::KisThreadPool()
{
Q_ASSERT(KisThreadPool::m_singleton == 0);
KisThreadPool::m_singleton = this;
TDEConfig * cfg = TDEGlobal::config();
cfg->setGroup("");
m_maxThreads = cfg->readNumEntry("maxthreads", 10);
m_numberOfRunningThreads = 0;
m_numberOfQueuedThreads = 0;
m_wait = 200;
start();
}
KisThreadPool::~KisThreadPool()
{
m_poolMutex.lock();
m_canceled = true;
m_runningThreads.setAutoDelete(true);
m_threads.setAutoDelete(true);
m_oldThreads.setAutoDelete(true);
KisThread * t;
for ( t = m_threads.first(); t; t = m_threads.next()) {
if (t) {
t->cancel();
t->wait();
m_threads.remove(t);
}
}
for ( t = m_runningThreads.first(); t; t = m_runningThreads.next()) {
if (t) {
t->cancel();
t->wait();
m_runningThreads.remove(t);
}
}
for ( t = m_oldThreads.first(); t; t = m_oldThreads.next()) {
if (t) {
t->cancel();
t->wait();
m_runningThreads.remove(t);
}
}
KisThreadPool::m_singleton = 0;
m_poolMutex.unlock();
}
KisThreadPool * KisThreadPool::instance()
{
if(KisThreadPool::m_singleton == 0)
{
KisThreadPool::m_singleton = new KisThreadPool();
}
else {
if (KisThreadPool::m_singleton->finished()) {
delete KisThreadPool::m_singleton;
KisThreadPool::m_singleton = 0;
KisThreadPool::m_singleton = new KisThreadPool();
}
}
return KisThreadPool::m_singleton;
}
void KisThreadPool::enqueue(KisThread * thread)
{
m_poolMutex.lock();
m_threads.append(thread);
m_numberOfQueuedThreads++;
m_poolMutex.unlock();
m_wait = 200;
}
void KisThreadPool::dequeue(KisThread * thread)
{
KisThread * t = 0;
m_poolMutex.lock();
int i = m_threads.findRef(thread);
if (i >= 0) {
t = m_threads.take(i);
m_numberOfQueuedThreads--;
} else {
i = m_runningThreads.findRef(thread);
if (i >= 0) {
t = m_runningThreads.take(i);
m_numberOfRunningThreads--;
}
else {
i = m_oldThreads.findRef(thread);
if (i >= 0) {
t = m_oldThreads.take(i);
}
}
}
m_poolMutex.unlock();
if (t) {
t->cancel();
t->wait();
delete t;
}
}
void KisThreadPool::run()
{
int sleeps = 10;
while(!m_canceled) {
if (m_numberOfQueuedThreads > 0 && m_numberOfRunningThreads < m_maxThreads) {
KisThread * thread = 0;
m_poolMutex.lock();
if (m_threads.count() > 0) {
thread = m_threads.take();
m_numberOfQueuedThreads--;
}
if (thread) {
thread->start();
m_runningThreads.append(thread);
m_numberOfRunningThreads++;
}
m_poolMutex.unlock();
}
else {
msleep(m_wait);
m_poolMutex.lock();
for ( KisThread * t = m_runningThreads.first(); t; t = m_runningThreads.next()) {
if (t) {
if (t->finished()) {
m_runningThreads.remove(t);
m_numberOfRunningThreads--;
m_oldThreads.append(t);
}
}
}
m_poolMutex.unlock();
m_poolMutex.lock();
if (m_numberOfQueuedThreads == 0 && m_numberOfRunningThreads == 0) {
sleeps--;
if (sleeps == 0) {
m_poolMutex.unlock();
return;
}
m_poolMutex.unlock();
}
m_poolMutex.unlock();
}
}
}
|