QThreadPool: fix race at time of thread expiry.
authorDavid Faure <david.faure@kdab.com>
Fri, 1 Nov 2013 09:49:30 +0000 (10:49 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 15 Nov 2013 11:42:27 +0000 (12:42 +0100)
commit92a4fcff80cf7a2e3a71c0ff8a2a378866b79739
tree489cea2b8b4bbfd0a7a2cf35e4a5ba48ff435438
parent605647f2b8a6c8d4f700732a7fcdb1dec3459231
QThreadPool: fix race at time of thread expiry.

The current synchronization mechanism was racy: decrementing waitingThreads
and then hoping that the wakeOne will wake a thread before its expiry
timeout happens. In other words, on timeout, a just-assigned task would
never run. And then no other task would run, if maxThreadCount is reached.

Fixed by using a queue of waiting threads (rather than just a count), and by
moving the wait condition into the thread itself, so we know precisely
which one we're waking up, and we can remove it from the set of waiting threads
before waking it up, and therefore it can determine on wakeup whether it
has work to do (caller removed it from the queue) or it expired (it's still
in the queue). This is reliable, whereas the return value from QWaitCondition::wait
isn't reliable, when the main thread has already decided that this thread
has work to do.

Task-number: QTBUG-3786
Backport from qtbase/a9b6a78e54670a70b96c122b10ad7bd64d166514

Change-Id: Ic766ff67dea7a8bb8f1bc893943060ee5428d782
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/concurrent/qthreadpool.cpp
src/corelib/concurrent/qthreadpool_p.h
tests/auto/qthreadpool/tst_qthreadpool.cpp