19#include "kis_thread_pool.h"
26KisThreadPool::KisThreadPool()
28 Q_ASSERT(KisThreadPool::m_singleton == 0);
30 KisThreadPool::m_singleton =
this;
32 TDEConfig * cfg = TDEGlobal::config();
34 m_maxThreads = cfg->readNumEntry(
"maxthreads", 10);
35 m_numberOfRunningThreads = 0;
36 m_numberOfQueuedThreads = 0;
43KisThreadPool::~KisThreadPool()
49 m_runningThreads.setAutoDelete(
true);
50 m_threads.setAutoDelete(
true);
51 m_oldThreads.setAutoDelete(
true);
55 for ( t = m_threads.first(); t; t = m_threads.next()) {
63 for ( t = m_runningThreads.first(); t; t = m_runningThreads.next()) {
67 m_runningThreads.remove(t);
71 for ( t = m_oldThreads.first(); t; t = m_oldThreads.next()) {
75 m_runningThreads.remove(t);
78 KisThreadPool::m_singleton = 0;
86 if(KisThreadPool::m_singleton == 0)
88 KisThreadPool::m_singleton =
new KisThreadPool();
92 if (KisThreadPool::m_singleton->finished()) {
93 delete KisThreadPool::m_singleton;
94 KisThreadPool::m_singleton = 0;
95 KisThreadPool::m_singleton =
new KisThreadPool();
99 return KisThreadPool::m_singleton;
102void KisThreadPool::enqueue(
KisThread * thread)
105 m_threads.append(thread);
106 m_numberOfQueuedThreads++;
107 m_poolMutex.unlock();
112void KisThreadPool::dequeue(
KisThread * thread)
118 int i = m_threads.findRef(thread);
120 t = m_threads.take(i);
121 m_numberOfQueuedThreads--;
123 i = m_runningThreads.findRef(thread);
125 t = m_runningThreads.take(i);
126 m_numberOfRunningThreads--;
129 i = m_oldThreads.findRef(thread);
131 t = m_oldThreads.take(i);
136 m_poolMutex.unlock();
146void KisThreadPool::run()
151 if (m_numberOfQueuedThreads > 0 && m_numberOfRunningThreads < m_maxThreads) {
154 if (m_threads.count() > 0) {
155 thread = m_threads.take();
156 m_numberOfQueuedThreads--;
160 m_runningThreads.append(thread);
161 m_numberOfRunningThreads++;
163 m_poolMutex.unlock();
168 for (
KisThread * t = m_runningThreads.first(); t; t = m_runningThreads.next()) {
171 m_runningThreads.remove(t);
172 m_numberOfRunningThreads--;
173 m_oldThreads.append(t);
177 m_poolMutex.unlock();
179 if (m_numberOfQueuedThreads == 0 && m_numberOfRunningThreads == 0) {
182 m_poolMutex.unlock();
185 m_poolMutex.unlock();
188 m_poolMutex.unlock();
A thread pool starts executing threads some time after they are added, running a maximum number of th...
A KisThread is a TQThread that can be set in the canceled state.
KisThread()
Create a new KisThread with the canceled state set to false.
virtual void cancel()
Request the thread to cancel at the first opportunity.