From 3c8dabf69aa950c2df49f48aebbe02aac5b519f3 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 21 Jan 2015 12:22:41 -0500 Subject: [PATCH] Fix up some workqueue/threading issues spotted by dgoulet. --- src/common/compat_pthreads.c | 3 ++- src/common/workqueue.c | 24 +++++------------------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/common/compat_pthreads.c b/src/common/compat_pthreads.c index 188a91f68d..c217c5145b 100644 --- a/src/common/compat_pthreads.c +++ b/src/common/compat_pthreads.c @@ -212,7 +212,8 @@ tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv) struct timespec ts; struct timeval tvnow, tvsum; while (1) { - gettimeofday(&tvnow, NULL); + if (gettimeofday(&tvnow, NULL) < 0) + return -1; timeradd(tv, &tvnow, &tvsum); ts.tv_sec = tvsum.tv_sec; ts.tv_nsec = tvsum.tv_usec * 1000; diff --git a/src/common/workqueue.c b/src/common/workqueue.c index 5ba29e3b26..77a4fbc3f6 100644 --- a/src/common/workqueue.c +++ b/src/common/workqueue.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2013-2015, The Tor Project, Inc. */ +/* copyright (c) 2013-2015, The Tor Project, Inc. */ /* See LICENSE for licensing information */ #include "orconfig.h" @@ -81,13 +81,6 @@ typedef struct workerthread_s { int index; /** The pool this thread is a part of. */ struct threadpool_s *in_pool; - /** True iff this thread is currently in its loop. (Not currently used.) */ - unsigned is_running; - /** True iff this thread has crashed or is shut down for some reason. (Not - * currently used.) */ - unsigned is_shut_down; - /** True if we're waiting for more elements to get added to the queue. */ - unsigned waiting; /** User-supplied state field that we pass to the worker functions of each * work item. */ void *state; @@ -181,8 +174,6 @@ worker_thread_main(void *thread_) workqueue_entry_t *work; int result; - thread->is_running = 1; - tor_mutex_acquire(&pool->lock); while (1) { /* lock must be held at this point. */ @@ -198,8 +189,6 @@ worker_thread_main(void *thread_) int r = update_fn(thread->state, arg); if (r < 0) { - thread->is_running = 0; - thread->is_shut_down = 1; return; } @@ -220,8 +209,6 @@ worker_thread_main(void *thread_) /* We may need to exit the thread. */ if (result >= WQ_RPL_ERROR) { - thread->is_running = 0; - thread->is_shut_down = 1; return; } tor_mutex_acquire(&pool->lock); @@ -232,12 +219,9 @@ worker_thread_main(void *thread_) /* TODO: support an idle-function */ /* Okay. Now, wait till somebody has work for us. */ - /* XXXX we could just omit waiting and instead */ - thread->waiting = 1; if (tor_cond_wait(&pool->condition, &pool->lock, NULL) < 0) { - /* XXXX ERROR */ + log_warn(LD_GENERAL, "Fail tor_cond_wait."); } - thread->waiting = 0; } } @@ -482,7 +466,9 @@ void replyqueue_process(replyqueue_t *queue) { if (queue->alert.drain_fn(queue->alert.read_fd) < 0) { - /* XXXX complain! */ + static ratelim_t warn_limit = RATELIM_INIT(7200); + log_fn_ratelim(&warn_limit, LOG_WARN, LD_GENERAL, + "Failure from drain_fd"); } tor_mutex_acquire(&queue->lock);