Prevent deadlock while shutting down thread pool.
authorTobias Brunner <tobias@strongswan.org>
Wed, 8 Jun 2011 08:52:05 +0000 (10:52 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 8 Jun 2011 09:44:42 +0000 (11:44 +0200)
During destruction the main thread locks the mutex in processor_t and
waits on a condvar for threads to have terminated.  Because the mutex
has also to be locked to decrement the thread count the condvar cannot
be signaled before doing that as otherwise the main thread might already
be waiting to join the threads while locking the mutex and thus causing
a deadlock.

src/libstrongswan/processing/processor.c

index 31df580..be33fcd 100644 (file)
@@ -181,10 +181,10 @@ static void process_jobs(private_processor_t *this)
                                this->working_threads[i]++;
                                this->mutex->unlock(this->mutex);
                                this->priority->set(this->priority, (void*)(intptr_t)i);
-                               thread_cleanup_push((thread_cleanup_t)decrement_working_threads,
-                                                                       this);
                                /* terminated threads are restarted to get a constant pool */
                                thread_cleanup_push((thread_cleanup_t)restart, this);
+                               thread_cleanup_push((thread_cleanup_t)decrement_working_threads,
+                                                                       this);
                                job->execute(job);
                                thread_cleanup_pop(FALSE);
                                thread_cleanup_pop(FALSE);