33 #include <QtCore/QObject>
34 #include <QtCore/QMutex>
35 #include <QtCore/QDebug>
50 using namespace ThreadWeaver;
55 , m_inventoryMax( qMax(4, 2 *
QThread::idealThreadCount() ) )
56 , m_mutex ( new QMutex( QMutex::Recursive ) )
57 , m_finishMutex( new QMutex )
58 , m_jobAvailableMutex ( new QMutex )
61 QMutexLocker l(m_mutex); Q_UNUSED(l);
74 Qt::QueuedConnection );
81 REQUIRE( QThread::currentThread() == thread() );
82 debug ( 3,
"WeaverImpl dtor: destroying inventory.\n" );
97 QMutexLocker l(m_mutex); Q_UNUSED(l);
101 if ( !th->isFinished() )
106 if ( th->wait( 100 ) )
break;
107 debug ( 1,
"WeaverImpl::~WeaverImpl: thread %i did not exit as expected, "
108 "retrying.\n", th->
id() );
115 delete m_finishMutex;
116 delete m_jobAvailableMutex;
117 debug ( 3,
"WeaverImpl dtor: done\n" );
126 QMutexLocker l(m_mutex); Q_UNUSED(l);
127 if ( m_state==0 || m_state->
stateId() != id )
129 m_state = m_states[id];
130 debug ( 2,
"WeaverImpl::setState: state changed to \"%s\".\n",
131 m_state->
stateName().toLatin1().constData() );
145 QMutexLocker l(m_mutex); Q_UNUSED(l);
151 Q_ASSERT_X ( cap > 0,
"Weaver Impl",
"Thread inventory size has to be larger than zero." );
152 QMutexLocker l (m_mutex);
158 QMutexLocker l(m_mutex); Q_UNUSED(l);
164 QMutexLocker l(m_mutex); Q_UNUSED(l);
186 debug ( 3,
"WeaverImpl::enqueue: queueing job %p of type %s.\n",
187 (
void*)job, job->metaObject()->className() );
188 QMutexLocker l (m_mutex); Q_UNUSED(l);
207 QMutexLocker l(m_mutex); Q_UNUSED(l);
214 for (
int i = 0; i < qMin ( reserve, numberOfNewJobs ); ++i )
217 th->moveToThread( th );
227 debug ( 2,
"WeaverImpl::adjustInventory: thread created, "
235 return new Thread(
this );
242 QMutexLocker l (m_mutex);
251 debug( 3,
"WeaverImpl::dequeue: job %p dequeued, %i jobs left.\n",
254 debug( 3,
"WeaverImpl::dequeue: job %p not found in queue.\n", (
void*)job );
267 debug( 3,
"WeaverImpl::dequeue: dequeueing all jobs.\n" );
268 QMutexLocker l (m_mutex);
295 QMutexLocker l(m_mutex); Q_UNUSED(l);
315 QMutexLocker l (m_mutex); Q_UNUSED(l);
317 debug ( 4,
"WeaverImpl::adjustActiveThreadCount: %i active threads (%i jobs"
329 QMutexLocker l(m_mutex); Q_UNUSED(l);
335 QMutexLocker l (m_mutex); Q_UNUSED(l);
367 debug ( 4,
"WeaverImpl::blockThread...: thread %i blocked.\n", th->
id());
369 QMutexLocker l( m_jobAvailableMutex );
371 debug ( 4,
"WeaverImpl::blockThread...: thread %i resumed.\n", th->
id());
376 QMutexLocker l(m_mutex); Q_UNUSED(l);
382 QMutexLocker l(m_mutex); Q_UNUSED(l);
389 const int MaxWaitMilliSeconds = 50;
391 const int MaxWaitMilliSeconds = 500;
395 debug (2,
"WeaverImpl::finish: not done, waiting.\n" );
396 QMutexLocker l( m_finishMutex );
397 if (
m_jobFinished.wait( l.mutex(), MaxWaitMilliSeconds ) ==
false ) {
398 debug ( 2,
"WeaverImpl::finish: wait timed out, %i jobs left, waking threads.\n",
403 debug (2,
"WeaverImpl::finish: done.\n\n\n" );
408 QMutexLocker l(m_mutex); Q_UNUSED(l);
416 QMutexLocker l(m_mutex); Q_UNUSED(l);
417 debug( 0,
"WeaverImpl::dumpJobs: current jobs:\n" );
418 for (
int index = 0; index <
m_assignments.size(); ++index ) {
419 debug( 0,
"--> %4i: %p %s (priority %i, can be executed: %s)\n", index, (
void*)
m_assignments.at( index ),
426 #include "WeaverImpl.moc"