diff options
| author | Marat Dukhan <maratek@gmail.com> | 2017-03-05 17:59:07 -0500 |
|---|---|---|
| committer | Marat Dukhan <maratek@gmail.com> | 2017-03-05 17:59:07 -0500 |
| commit | eef99d488f3750b2bff3caa27aad6277ace002ba (patch) | |
| tree | 2bf9b1d3fd60ad4f8f7490e64449bd07509e781d /src | |
| parent | 630dfb6638927df1f3181eee339662a86b3cea0b (diff) | |
| download | platform_external_pthreadpool-eef99d488f3750b2bff3caa27aad6277ace002ba.tar.gz platform_external_pthreadpool-eef99d488f3750b2bff3caa27aad6277ace002ba.tar.bz2 platform_external_pthreadpool-eef99d488f3750b2bff3caa27aad6277ace002ba.zip | |
Allow NULL threadpool in pthreadpool_destroy
Diffstat (limited to 'src')
| -rw-r--r-- | src/pthreadpool.c | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/src/pthreadpool.c b/src/pthreadpool.c index a62bd44..e97a56c 100644 --- a/src/pthreadpool.c +++ b/src/pthreadpool.c @@ -452,33 +452,35 @@ void pthreadpool_compute_2d_tiled( } void pthreadpool_destroy(struct pthreadpool* threadpool) { - /* Lock the state variables to ensure that threads don't start processing before they observe complete state */ - pthread_mutex_lock(&threadpool->state_mutex); + if (threadpool != NULL) { + /* Lock the state variables to ensure that threads don't start processing before they observe complete state */ + pthread_mutex_lock(&threadpool->state_mutex); - /* Locking of barrier_mutex not needed: readers are sleeping on state_condvar */ - threadpool->checkedin_threads = 0; + /* Locking of barrier_mutex not needed: readers are sleeping on state_condvar */ + threadpool->checkedin_threads = 0; - /* Update threads' states */ - for (size_t tid = 0; tid < threadpool->threads_count; tid++) { - threadpool->threads[tid].state = thread_state_shutdown; - } + /* Update threads' states */ + for (size_t tid = 0; tid < threadpool->threads_count; tid++) { + threadpool->threads[tid].state = thread_state_shutdown; + } - /* Wake up worker threads */ - pthread_cond_broadcast(&threadpool->state_condvar); + /* Wake up worker threads */ + pthread_cond_broadcast(&threadpool->state_condvar); - /* Commit the state changes and let workers start processing */ - pthread_mutex_unlock(&threadpool->state_mutex); + /* Commit the state changes and let workers start processing */ + pthread_mutex_unlock(&threadpool->state_mutex); - /* Wait until all threads return */ - for (size_t tid = 0; tid < threadpool->threads_count; tid++) { - pthread_join(threadpool->threads[tid].thread_object, NULL); - } + /* Wait until all threads return */ + for (size_t tid = 0; tid < threadpool->threads_count; tid++) { + pthread_join(threadpool->threads[tid].thread_object, NULL); + } - /* Release resources */ - pthread_mutex_destroy(&threadpool->execution_mutex); - pthread_mutex_destroy(&threadpool->barrier_mutex); - pthread_cond_destroy(&threadpool->barrier_condvar); - pthread_mutex_destroy(&threadpool->state_mutex); - pthread_cond_destroy(&threadpool->state_condvar); - free(threadpool); + /* Release resources */ + pthread_mutex_destroy(&threadpool->execution_mutex); + pthread_mutex_destroy(&threadpool->barrier_mutex); + pthread_cond_destroy(&threadpool->barrier_condvar); + pthread_mutex_destroy(&threadpool->state_mutex); + pthread_cond_destroy(&threadpool->state_condvar); + free(threadpool); + } } |
