aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarat Dukhan <maratek@gmail.com>2017-03-05 17:59:07 -0500
committerMarat Dukhan <maratek@gmail.com>2017-03-05 17:59:07 -0500
commiteef99d488f3750b2bff3caa27aad6277ace002ba (patch)
tree2bf9b1d3fd60ad4f8f7490e64449bd07509e781d /src
parent630dfb6638927df1f3181eee339662a86b3cea0b (diff)
downloadplatform_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.c48
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);
+ }
}