From 28c3f22ba1c61ef03da708f1971822d3e8abf747 Mon Sep 17 00:00:00 2001 From: Laurent Fasnacht Date: Mon, 16 Jun 2014 10:41:50 +0200 Subject: [PATCH] Fix possible freeze --- src/threadqueue.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/threadqueue.c b/src/threadqueue.c index 5d275188..00d50c4e 100644 --- a/src/threadqueue.c +++ b/src/threadqueue.c @@ -54,6 +54,8 @@ typedef struct { } while (0); #endif //PTHREAD_DUMP +const struct timespec time_to_wait = {1, 0}; + static void* threadqueue_worker(void* threadqueue_worker_spec_opaque) { threadqueue_worker_spec * const threadqueue_worker_spec = threadqueue_worker_spec_opaque; threadqueue_queue * const threadqueue = threadqueue_worker_spec->threadqueue; @@ -393,9 +395,6 @@ int threadqueue_finalize(threadqueue_queue * const threadqueue) { int threadqueue_flush(threadqueue_queue * const threadqueue) { int notdone = 1; - struct timespec time_to_wait; - time_to_wait.tv_sec = 1; - time_to_wait.tv_nsec = 0; //Lock the queue PTHREAD_LOCK(&threadqueue->lock); @@ -442,9 +441,17 @@ int threadqueue_waitfor(threadqueue_queue * const threadqueue, threadqueue_job * PTHREAD_UNLOCK(&job->lock); if (!job_done) { + int ret; PTHREAD_COND_BROADCAST(&(threadqueue->cond)); + PTHREAD_UNLOCK(&threadqueue->lock); SLEEP(); - PTHREAD_COND_WAIT(&threadqueue->cb_cond, &threadqueue->lock); + PTHREAD_LOCK(&threadqueue->lock); + ret = pthread_cond_timedwait(&threadqueue->cb_cond, &threadqueue->lock, &time_to_wait); + if (ret != 0 && ret != ETIMEDOUT) { + fprintf(stderr, "pthread_cond_timedwait failed!\n"); + assert(0); + return 0; + } } } while (!job_done); @@ -636,4 +643,4 @@ int threadqueue_log(threadqueue_queue * threadqueue, const CLOCK_T *start, const } return 1; } -#endif //_DEBUG \ No newline at end of file +#endif //_DEBUG