diff options
| author | Michele Calgaro <michele.calgaro@yahoo.it> | 2025-03-22 17:47:07 +0900 |
|---|---|---|
| committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2025-03-23 20:36:29 +0900 |
| commit | cc42c3a8ea0022b8777ed95c1f73b2a5845c5587 (patch) | |
| tree | 48a1ba0dc3871583d4e05b959f9cc72ef0214dc0 /src/kernel/qthread_unix.cpp | |
| parent | 67cb0f6762768ee0d32adef1d7307ff7bb985407 (diff) | |
| download | tqt-cc42c3a8ea0022b8777ed95c1f73b2a5845c5587.tar.gz tqt-cc42c3a8ea0022b8777ed95c1f73b2a5845c5587.zip | |
Fix usage of condition variable in TQThread which could lead to a thread being run multiple times in parallel.
Although highly unlikely, the following situation was possible:
- a thread is restarted while still running. The call to the second 'start()' blocks because the thread is still running
- the OS decides to randomly wake up the thread that called 'start()'
- a second thread is started and runs in parallel to the first thread, with obvious undefined behavior
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
(cherry picked from commit 3143d2d40ab5af1053596cd9b46c7034f22c4624)
Diffstat (limited to 'src/kernel/qthread_unix.cpp')
| -rw-r--r-- | src/kernel/qthread_unix.cpp | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/src/kernel/qthread_unix.cpp b/src/kernel/qthread_unix.cpp index b68b5cd7a..723ca8bcf 100644 --- a/src/kernel/qthread_unix.cpp +++ b/src/kernel/qthread_unix.cpp @@ -107,7 +107,7 @@ void TQThreadInstance::init(unsigned int stackSize) orphan = FALSE; disableThreadPostedEvents = FALSE; - pthread_cond_init(&thread_done, NULL); + pthread_cond_init(&thread_done, nullptr); thread_id = 0; eventLoop = 0; @@ -326,9 +326,11 @@ void TQThread::usleep( unsigned long usecs ) void TQThread::start(Priority priority) { TQMutexLocker locker( d->mutex() ); + while ( d->running ) + { + pthread_cond_wait(&d->thread_done, &locker.mutex()->d->handle); + } - if ( d->running ) - pthread_cond_wait(&d->thread_done, &locker.mutex()->d->handle); d->running = TRUE; d->finished = FALSE; |
