Qt 4.8
Public Functions | Public Variables | Friends | List of all members
QThreadPoolPrivate Class Reference

#include <qthreadpool_p.h>

Inheritance diagram for QThreadPoolPrivate:
QObjectPrivate QObjectData

Public Functions

int activeThreadCount () const
 
void enqueueTask (QRunnable *task, int priority=0)
 
 QThreadPoolPrivate ()
 
void reset ()
 Makes all threads exit, waits for each thread to exit and deletes it. More...
 
bool startFrontRunnable ()
 Pulls a runnable from the front queue and runs it in the current thread. More...
 
void startThread (QRunnable *runnable=0)
 
void stealRunnable (QRunnable *)
 Searches for runnable in the queue, removes it from the queue and runs it if found. More...
 
bool tooManyThreadsActive () const
 
bool tryStart (QRunnable *task)
 
void tryToStartMoreThreads ()
 
bool waitForDone (int msecs=-1)
 
- Public Functions inherited from QObjectPrivate
void _q_reregisterTimers (void *pointer)
 
void addConnection (int signal, Connection *c)
 
void cleanConnectionLists ()
 
void connectNotify (const char *signal)
 
void deleteChildren ()
 
void disconnectNotify (const char *signal)
 
bool isSender (const QObject *receiver, const char *signal) const
 
bool isSignalConnected (uint signalIdx) const
 Returns true if the signal with index signal_index from object sender is connected. More...
 
void moveToThread_helper ()
 
 QObjectPrivate (int version=QObjectPrivateVersion)
 
QObjectList receiverList (const char *signal) const
 
QObjectList senderList () const
 
void setParent_helper (QObject *)
 
void setThreadData_helper (QThreadData *currentData, QThreadData *targetData)
 
int signalIndex (const char *signalName) const
 Returns the signal index used in the internal connectionLists vector. More...
 
virtual ~QObjectPrivate ()
 
- Public Functions inherited from QObjectData
virtual ~QObjectData ()=0
 

Public Variables

int activeThreads
 
QSet< QThreadPoolThread * > allThreads
 
QQueue< QThreadPoolThread * > expiredThreads
 
int expiryTimeout
 
bool isExiting
 
int maxThreadCount
 
QMutex mutex
 
QWaitCondition noActiveThreads
 
QList< QPair< QRunnable *, int > > queue
 
int reservedThreads
 
QQueue< QThreadPoolThread * > waitingThreads
 
- Public Variables inherited from QObjectPrivate
union {
   QObject *   currentChildBeingDeleted
 
   QAbstractDeclarativeData *   declarativeData
 
}; 
 
quint32 connectedSignals [2]
 
QObjectConnectionListVectorconnectionLists
 
SendercurrentSender
 
QList< QPointer< QObject > > eventFilters
 
ExtraDataextraData
 
QString objectName
 
Connectionsenders
 
QAtomicPointer< QtSharedPointer::ExternalRefCountData > sharedRefcount
 
QThreadDatathreadData
 
void * unused
 
- Public Variables inherited from QObjectData
uint blockSig: 1
 
QObjectList children
 
uint hasGuards: 1
 
uint inEventHandler: 1
 
uint inThreadChangeEvent: 1
 
uint isWidget: 1
 
QMetaObjectmetaObject
 
uint ownObjectName: 1
 
QObjectparent
 
uint pendTimer: 1
 
int postedEvents
 
QObjectq_ptr
 
uint receiveChildEvents: 1
 
uint sendChildEvents: 1
 
uint unused: 22
 
uint wasDeleted: 1
 

Friends

class QThreadPoolThread
 

Additional Inherited Members

- Public Types inherited from QObjectPrivate
typedef void(* StaticMetaCallFunction) (QObject *, QMetaObject::Call, int, void **)
 
- Static Public Functions inherited from QObjectPrivate
static void clearGuards (QObject *)
 
static QObjectPrivateget (QObject *o)
 
static void resetCurrentSender (QObject *receiver, Sender *currentSender, Sender *previousSender)
 
static SendersetCurrentSender (QObject *receiver, Sender *sender)
 
static void signalSignature (const QMetaMethod &signal, QVarLengthArray< char > *result)
 

Detailed Description

Definition at line 68 of file qthreadpool_p.h.

Constructors and Destructors

◆ QThreadPoolPrivate()

QThreadPoolPrivate::QThreadPoolPrivate ( )

Definition at line 165 of file qthreadpool.cpp.

166  : isExiting(false),
167  expiryTimeout(30000),
169  reservedThreads(0),
170  activeThreads(0)
171 { }
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
static int idealThreadCount()
Returns the ideal number of threads that can be run on the system.

Functions

◆ activeThreadCount()

int QThreadPoolPrivate::activeThreadCount ( ) const

Definition at line 222 of file qthreadpool.cpp.

Referenced by tooManyThreadsActive(), and tryStart().

223 {
224  return (allThreads.count()
227  + reservedThreads);
228 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QQueue< QThreadPoolThread * > expiredThreads
Definition: qthreadpool_p.h:92
int count() const
Definition: qset.h:178
QSet< QThreadPoolThread * > allThreads
Definition: qthreadpool_p.h:90
QQueue< QThreadPoolThread * > waitingThreads
Definition: qthreadpool_p.h:91

◆ enqueueTask()

void QThreadPoolPrivate::enqueueTask ( QRunnable task,
int  priority = 0 
)

Definition at line 211 of file qthreadpool.cpp.

Referenced by tryStart().

212 {
213  if (runnable->autoDelete())
214  ++runnable->ref;
215 
216  // put it on the queue
217  QList<QPair<QRunnable *, int> >::iterator at =
218  qUpperBound(queue.begin(), queue.end(), priority);
219  queue.insert(at, qMakePair(runnable, priority));
220 }
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qUpperBound(RandomAccessIterator begin, RandomAccessIterator end, const T &value)
Definition: qalgorithms.h:262
#define at(className, varName)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
void insert(int i, const T &t)
Inserts value at index position i in the list.
Definition: qlist.h:575
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
Definition: qpair.h:102
QList< QPair< QRunnable *, int > > queue
Definition: qthreadpool_p.h:93
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ reset()

void QThreadPoolPrivate::reset ( )

Makes all threads exit, waits for each thread to exit and deletes it.

Warning
This function is not part of the public interface.

Definition at line 266 of file qthreadpool.cpp.

267 {
268  QMutexLocker locker(&mutex);
269  isExiting = true;
270 
271  do {
272  // make a copy of the set so that we can iterate without the lock
273  QSet<QThreadPoolThread *> allThreadsCopy = allThreads;
274  allThreads.clear();
275  locker.unlock();
276 
277  foreach (QThreadPoolThread *thread, allThreadsCopy) {
278  thread->runnableReady.wakeAll();
279  thread->wait();
280  delete thread;
281  }
282 
283  locker.relock();
284  // repeat until all newly arrived threads have also completed
285  } while (!allThreads.isEmpty());
286 
289 
290  isExiting = false;
291 }
bool isEmpty() const
Definition: qset.h:77
QQueue< QThreadPoolThread * > expiredThreads
Definition: qthreadpool_p.h:92
void clear()
Definition: qset.h:87
void clear()
Removes all items from the list.
Definition: qlist.h:764
QSet< QThreadPoolThread * > allThreads
Definition: qthreadpool_p.h:90
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
bool wait(unsigned long time=ULONG_MAX)
Blocks the thread until either of these conditions is met:
QWaitCondition runnableReady
Definition: qthreadpool.cpp:71
QQueue< QThreadPoolThread * > waitingThreads
Definition: qthreadpool_p.h:91

◆ startFrontRunnable()

bool QThreadPoolPrivate::startFrontRunnable ( )

Pulls a runnable from the front queue and runs it in the current thread.

Warning
This function is not part of the public interface. Blocks until the runnable has completed. Returns true if a runnable was found.

Definition at line 317 of file qthreadpool.cpp.

318 {
319  QMutexLocker locker(&mutex);
320  if (queue.isEmpty())
321  return false;
322 
323  QRunnable *runnable = queue.takeFirst().first;
324  const bool autoDelete = runnable->autoDelete();
325  bool del = autoDelete && !--runnable->ref;
326 
327  locker.unlock();
328  runnable->run();
329  locker.relock();
330 
331  if (del) {
332  delete runnable;
333  }
334 
335  return true;
336 }
virtual void run()=0
Implement this pure virtual function in your subclass.
The QRunnable class is the base class for all runnable objects.
Definition: qrunnable.h:52
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
int ref
Definition: qrunnable.h:54
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
bool autoDelete() const
Returns true is auto-deletion is enabled; false otherwise.
Definition: qrunnable.h:66
QList< QPair< QRunnable *, int > > queue
Definition: qthreadpool_p.h:93

◆ startThread()

void QThreadPoolPrivate::startThread ( QRunnable runnable = 0)
Warning
This function is not part of the public interface.

Definition at line 246 of file qthreadpool.cpp.

Referenced by tryStart().

247 {
249  thread->setObjectName(QLatin1String("Thread (pooled)"));
250  allThreads.insert(thread.data());
251  ++activeThreads;
252 
253  if (runnable->autoDelete())
254  ++runnable->ref;
255  thread->runnable = runnable;
256  thread.take()->start();
257 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
friend class QThreadPoolThread
Definition: qthreadpool_p.h:71
const_iterator insert(const T &value)
Definition: qset.h:179
int ref
Definition: qrunnable.h:54
QSet< QThreadPoolThread * > allThreads
Definition: qthreadpool_p.h:90
bool autoDelete() const
Returns true is auto-deletion is enabled; false otherwise.
Definition: qrunnable.h:66

◆ stealRunnable()

void QThreadPoolPrivate::stealRunnable ( QRunnable runnable)

Searches for runnable in the queue, removes it from the queue and runs it if found.

Warning
This function is not part of the public interface. This function does not return until the runnable has completed.

Definition at line 347 of file qthreadpool.cpp.

348 {
349  if (runnable == 0 || queue.isEmpty())
350  return;
351  bool found = false;
352  {
353  QMutexLocker locker(&mutex);
355  QList<QPair<QRunnable *, int> >::iterator end = queue.end();
356 
357  while (it != end) {
358  if (it->first == runnable) {
359  found = true;
360  queue.erase(it);
361  break;
362  }
363  ++it;
364  }
365  }
366 
367  if (!found)
368  return;
369 
370  const bool autoDelete = runnable->autoDelete();
371  bool del = autoDelete && !--runnable->ref;
372 
373  runnable->run();
374 
375  if (del) {
376  delete runnable;
377  }
378 }
#define it(className, varName)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
virtual void run()=0
Implement this pure virtual function in your subclass.
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
int ref
Definition: qrunnable.h:54
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
iterator erase(iterator pos)
Removes the item associated with the iterator pos from the list, and returns an iterator to the next ...
Definition: qlist.h:464
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
bool autoDelete() const
Returns true is auto-deletion is enabled; false otherwise.
Definition: qrunnable.h:66
static const KeyPair *const end
QList< QPair< QRunnable *, int > > queue
Definition: qthreadpool_p.h:93
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ tooManyThreadsActive()

bool QThreadPoolPrivate::tooManyThreadsActive ( ) const

Definition at line 237 of file qthreadpool.cpp.

Referenced by QThreadPoolThread::run().

238 {
239  const int activeThreadCount = this->activeThreadCount();
240  return activeThreadCount > maxThreadCount && (activeThreadCount - reservedThreads) > 1;
241 }
int activeThreadCount() const

◆ tryStart()

bool QThreadPoolPrivate::tryStart ( QRunnable task)

Definition at line 173 of file qthreadpool.cpp.

Referenced by tryToStartMoreThreads().

174 {
175  if (allThreads.isEmpty()) {
176  // always create at least one thread
177  startThread(task);
178  return true;
179  }
180 
181  // can't do anything if we're over the limit
183  return false;
184 
185  if (waitingThreads.count() > 0) {
186  // recycle an available thread
187  enqueueTask(task);
188  waitingThreads.takeFirst()->runnableReady.wakeOne();
189  return true;
190  }
191 
192  if (!expiredThreads.isEmpty()) {
193  // restart an expired thread
195  Q_ASSERT(thread->runnable == 0);
196 
197  ++activeThreads;
198 
199  if (task->autoDelete())
200  ++task->ref;
201  thread->runnable = task;
202  thread->start();
203  return true;
204  }
205 
206  // start a new thread
207  startThread(task);
208  return true;
209 }
QRunnable * runnable
Definition: qthreadpool.cpp:73
bool isEmpty() const
Definition: qset.h:77
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
QQueue< QThreadPoolThread * > expiredThreads
Definition: qthreadpool_p.h:92
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
int ref
Definition: qrunnable.h:54
QSet< QThreadPoolThread * > allThreads
Definition: qthreadpool_p.h:90
void start(Priority=InheritPriority)
Begins execution of the thread by calling run().
bool autoDelete() const
Returns true is auto-deletion is enabled; false otherwise.
Definition: qrunnable.h:66
int activeThreadCount() const
void enqueueTask(QRunnable *task, int priority=0)
T dequeue()
Removes the head item in the queue and returns it.
Definition: qqueue.h:61
void startThread(QRunnable *runnable=0)
QQueue< QThreadPoolThread * > waitingThreads
Definition: qthreadpool_p.h:91

◆ tryToStartMoreThreads()

void QThreadPoolPrivate::tryToStartMoreThreads ( )

Definition at line 230 of file qthreadpool.cpp.

231 {
232  // try to push tasks on the queue to any available threads
233  while (!queue.isEmpty() && tryStart(queue.first().first))
234  queue.removeFirst();
235 }
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void removeFirst()
Removes the first item in the list.
Definition: qlist.h:286
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
QList< QPair< QRunnable *, int > > queue
Definition: qthreadpool_p.h:93
bool tryStart(QRunnable *task)

◆ waitForDone()

bool QThreadPoolPrivate::waitForDone ( int  msecs = -1)

Definition at line 293 of file qthreadpool.cpp.

294 {
295  QMutexLocker locker(&mutex);
296  if (msecs < 0) {
297  while (!(queue.isEmpty() && activeThreads == 0))
298  noActiveThreads.wait(locker.mutex());
299  } else {
301  timer.start();
302  int t;
303  while (!(queue.isEmpty() && activeThreads == 0) &&
304  ((t = msecs - timer.elapsed()) > 0))
305  noActiveThreads.wait(locker.mutex(), t);
306  }
307  return queue.isEmpty() && activeThreads == 0;
308 }
EventLoopTimerRef timer
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
bool wait(QMutex *mutex, unsigned long time=ULONG_MAX)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
QWaitCondition noActiveThreads
Definition: qthreadpool_p.h:94
void start()
Starts this timer.
QList< QPair< QRunnable *, int > > queue
Definition: qthreadpool_p.h:93

Friends and Related Functions

◆ QThreadPoolThread

friend class QThreadPoolThread
friend

Definition at line 71 of file qthreadpool_p.h.

Referenced by startThread().

Properties

◆ activeThreads

int QThreadPoolPrivate::activeThreads

◆ allThreads

QSet<QThreadPoolThread *> QThreadPoolPrivate::allThreads

Definition at line 90 of file qthreadpool_p.h.

Referenced by activeThreadCount(), reset(), startThread(), and tryStart().

◆ expiredThreads

QQueue<QThreadPoolThread *> QThreadPoolPrivate::expiredThreads

Definition at line 92 of file qthreadpool_p.h.

Referenced by activeThreadCount(), reset(), QThreadPoolThread::run(), and tryStart().

◆ expiryTimeout

int QThreadPoolPrivate::expiryTimeout

Definition at line 97 of file qthreadpool_p.h.

Referenced by QThreadPoolThread::run().

◆ isExiting

bool QThreadPoolPrivate::isExiting

Definition at line 96 of file qthreadpool_p.h.

Referenced by reset(), and QThreadPoolThread::run().

◆ maxThreadCount

int QThreadPoolPrivate::maxThreadCount

Definition at line 98 of file qthreadpool_p.h.

Referenced by tooManyThreadsActive(), and tryStart().

◆ mutex

QMutex QThreadPoolPrivate::mutex
mutable

◆ noActiveThreads

QWaitCondition QThreadPoolPrivate::noActiveThreads

Definition at line 94 of file qthreadpool_p.h.

Referenced by QThreadPoolThread::registerThreadInactive(), and waitForDone().

◆ queue

QList<QPair<QRunnable *, int> > QThreadPoolPrivate::queue

◆ reservedThreads

int QThreadPoolPrivate::reservedThreads

Definition at line 99 of file qthreadpool_p.h.

Referenced by activeThreadCount(), and tooManyThreadsActive().

◆ waitingThreads

QQueue<QThreadPoolThread *> QThreadPoolPrivate::waitingThreads

Definition at line 91 of file qthreadpool_p.h.

Referenced by activeThreadCount(), reset(), QThreadPoolThread::run(), and tryStart().


The documentation for this class was generated from the following files: