/** Launch threads until we have <b>n</b>. */ static int threadpool_start_threads(threadpool_t *pool, int n) { if (n < 0) return -1; if (n > MAX_THREADS) n = MAX_THREADS; tor_mutex_acquire(&pool->lock); if (pool->n_threads < n) pool->threads = tor_reallocarray(pool->threads, sizeof(workerthread_t*), n); while (pool->n_threads < n) { void *state = pool->new_thread_state_fn(pool->new_thread_state_arg); workerthread_t *thr = workerthread_new(state, pool, pool->reply_queue); if (!thr) { tor_mutex_release(&pool->lock); return -1; } thr->index = pool->n_threads; pool->threads[pool->n_threads++] = thr; } tor_mutex_release(&pool->lock); return 0; }
/** * Process all pending replies on a reply queue. The main thread should call * this function every time the socket returned by replyqueue_get_socket() is * readable. */ void replyqueue_process(replyqueue_t *queue) { if (queue->alert.drain_fn(queue->alert.read_fd) < 0) { 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); while (!TOR_TAILQ_EMPTY(&queue->answers)) { /* lock must be held at this point.*/ workqueue_entry_t *work = TOR_TAILQ_FIRST(&queue->answers); TOR_TAILQ_REMOVE(&queue->answers, work, next_work); tor_mutex_release(&queue->lock); work->on_pool = NULL; work->reply_fn(work->arg); workqueue_entry_free(work); tor_mutex_acquire(&queue->lock); } tor_mutex_release(&queue->lock); }
/** Launch threads until we have <b>n</b>. */ static int threadpool_start_threads(threadpool_t *pool, int n) { if (BUG(n < 0)) return -1; // LCOV_EXCL_LINE if (n > MAX_THREADS) n = MAX_THREADS; tor_mutex_acquire(&pool->lock); if (pool->n_threads < n) pool->threads = tor_reallocarray(pool->threads, sizeof(workerthread_t*), n); while (pool->n_threads < n) { void *state = pool->new_thread_state_fn(pool->new_thread_state_arg); workerthread_t *thr = workerthread_new(state, pool, pool->reply_queue); if (!thr) { //LCOV_EXCL_START tor_assert_nonfatal_unreached(); pool->free_thread_state_fn(state); tor_mutex_release(&pool->lock); return -1; //LCOV_EXCL_STOP } thr->index = pool->n_threads; pool->threads[pool->n_threads++] = thr; } tor_mutex_release(&pool->lock); return 0; }
static void cv_test_thr_fn_(void *arg) { cv_testinfo_t *i = arg; int tid, r; tor_mutex_acquire(i->mutex); tid = i->n_threads++; tor_mutex_release(i->mutex); (void) tid; tor_mutex_acquire(i->mutex); while (1) { if (i->addend) { i->value += i->addend; i->addend = 0; } if (i->shutdown) { ++i->n_shutdown; i->shutdown = 0; tor_mutex_release(i->mutex); spawn_exit(); } r = tor_cond_wait(i->cond, i->mutex, i->tv); ++i->n_wakeups; if (r == 1) { ++i->n_timeouts; tor_mutex_release(i->mutex); spawn_exit(); } } }
/** * Main function for the worker thread. */ static void worker_thread_main(void *thread_) { workerthread_t *thread = thread_; threadpool_t *pool = thread->in_pool; workqueue_entry_t *work; workqueue_reply_t result; tor_mutex_acquire(&pool->lock); while (1) { /* lock must be held at this point. */ while (worker_thread_has_work(thread)) { /* lock must be held at this point. */ if (thread->in_pool->generation != thread->generation) { void *arg = thread->in_pool->update_args[thread->index]; thread->in_pool->update_args[thread->index] = NULL; workqueue_reply_t (*update_fn)(void*,void*) = thread->in_pool->update_fn; thread->generation = thread->in_pool->generation; tor_mutex_release(&pool->lock); workqueue_reply_t r = update_fn(thread->state, arg); if (r != WQ_RPL_REPLY) { return; } tor_mutex_acquire(&pool->lock); continue; } work = TOR_TAILQ_FIRST(&pool->work); TOR_TAILQ_REMOVE(&pool->work, work, next_work); work->pending = 0; tor_mutex_release(&pool->lock); /* We run the work function without holding the thread lock. This * is the main thread's first opportunity to give us more work. */ result = work->fn(thread->state, work->arg); /* Queue the reply for the main thread. */ queue_reply(thread->reply_queue, work); /* We may need to exit the thread. */ if (result != WQ_RPL_REPLY) { return; } tor_mutex_acquire(&pool->lock); } /* At this point the lock is held, and there is no work in this thread's * queue. */ /* TODO: support an idle-function */ /* Okay. Now, wait till somebody has work for us. */ if (tor_cond_wait(&pool->condition, &pool->lock, NULL) < 0) { log_warn(LD_GENERAL, "Fail tor_cond_wait."); } } }
static void test_threads_conditionvar(void *arg) { cv_testinfo_t *ti=NULL; const struct timeval msec100 = { 0, 100*1000 }; const int timeout = !strcmp(arg, "tv"); ti = cv_testinfo_new(); if (timeout) { ti->tv = &msec100; } spawn_func(cv_test_thr_fn_, ti); spawn_func(cv_test_thr_fn_, ti); spawn_func(cv_test_thr_fn_, ti); spawn_func(cv_test_thr_fn_, ti); tor_mutex_acquire(ti->mutex); ti->addend = 7; ti->shutdown = 1; tor_cond_signal_one(ti->cond); tor_mutex_release(ti->mutex); #define SPIN() \ while (1) { \ tor_mutex_acquire(ti->mutex); \ if (ti->addend == 0) { \ break; \ } \ tor_mutex_release(ti->mutex); \ } SPIN(); ti->addend = 30; ti->shutdown = 1; tor_cond_signal_all(ti->cond); tor_mutex_release(ti->mutex); SPIN(); ti->addend = 1000; if (! timeout) ti->shutdown = 1; tor_cond_signal_one(ti->cond); tor_mutex_release(ti->mutex); SPIN(); ti->addend = 300; if (! timeout) ti->shutdown = 1; tor_cond_signal_all(ti->cond); tor_mutex_release(ti->mutex); SPIN(); tor_mutex_release(ti->mutex); tt_int_op(ti->value, ==, 1337); if (!timeout) { tt_int_op(ti->n_shutdown, ==, 4); } else {
/** Log a message <b>msg</b> at <b>severity</b> in <b>domain</b>, and follow * that with a backtrace log. */ void log_backtrace(int severity, int domain, const char *msg) { int depth; char **symbols; int i; tor_mutex_acquire(&cb_buf_mutex); depth = backtrace(cb_buf, MAX_DEPTH); symbols = backtrace_symbols(cb_buf, depth); tor_log(severity, domain, "%s. Stack trace:", msg); if (!symbols) { tor_log(severity, domain, " Unable to generate backtrace."); goto done; } for (i=0; i < depth; ++i) { tor_log(severity, domain, " %s", symbols[i]); } free(symbols); done: tor_mutex_release(&cb_buf_mutex); }
/** Log a message <b>msg</b> at <b>severity</b> in <b>domain</b>, and follow * that with a backtrace log. */ void log_backtrace(int severity, int domain, const char *msg) { size_t depth; char **symbols; size_t i; tor_mutex_acquire(&cb_buf_mutex); depth = backtrace(cb_buf, MAX_DEPTH); symbols = backtrace_symbols(cb_buf, (int)depth); tor_log(severity, domain, "%s. Stack trace:", msg); if (!symbols) { /* LCOV_EXCL_START -- we can't provoke this. */ tor_log(severity, domain, " Unable to generate backtrace."); goto done; /* LCOV_EXCL_STOP */ } for (i=0; i < depth; ++i) { tor_log(severity, domain, " %s", symbols[i]); } raw_free(symbols); done: tor_mutex_release(&cb_buf_mutex); }
static void replysock_readable_cb(tor_socket_t sock, short what, void *arg) { threadpool_t *tp = arg; replyqueue_t *rq = threadpool_get_replyqueue(tp); int old_r = n_received; (void) sock; (void) what; replyqueue_process(rq); if (old_r == n_received) return; if (opt_verbose) { printf("%d / %d", n_received, n_sent); if (opt_n_cancel) printf(" (%d cancelled, %d uncancellable)", n_successful_cancel, n_failed_cancel); puts(""); } #ifdef TRACK_RESPONSES tor_mutex_acquire(&bitmap_mutex); for (i = 0; i < opt_n_items; ++i) { if (bitarray_is_set(received, i)) putc('o', stdout); else if (bitarray_is_set(handled, i)) putc('!', stdout); else putc('.', stdout); } puts(""); tor_mutex_release(&bitmap_mutex); #endif if (n_sent - (n_received+n_successful_cancel) < opt_n_lowwater) { int n_to_send = n_received + opt_n_inflight - n_sent; if (n_to_send > opt_n_items - n_sent) n_to_send = opt_n_items - n_sent; add_n_work_items(tp, n_to_send); } if (shutting_down == 0 && n_received+n_successful_cancel == n_sent && n_sent >= opt_n_items) { shutting_down = 1; threadpool_queue_update(tp, NULL, workqueue_do_shutdown, NULL, NULL); // Anything we add after starting the shutdown must not be executed. threadpool_queue_work(tp, workqueue_shutdown_error, handle_reply_shutdown, NULL); { struct timeval limit = { 2, 0 }; tor_event_base_loopexit(tor_libevent_get_base(), &limit); } } }
/** Helper function for threading unit tests: This function runs in a * subthread. It grabs its own mutex (start1 or start2) to make sure that it * should start, then it repeatedly alters _test_thread_strmap protected by * thread_test_mutex_. */ static void thread_test_func_(void* _s) { char *s = _s; int i, *count; tor_mutex_t *m; char buf[64]; char **cp; if (!strcmp(s, "thread 1")) { m = thread_test_start1_; cp = &thread1_name_; count = &t1_count; thread_fn_tid1 = tor_get_thread_id(); } else { m = thread_test_start2_; cp = &thread2_name_; count = &t2_count; thread_fn_tid2 = tor_get_thread_id(); } tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id()); *cp = tor_strdup(buf); tor_mutex_acquire(m); for (i=0; i<10000; ++i) { tor_mutex_acquire(thread_test_mutex_); strmap_set(thread_test_strmap_, "last to run", *cp); ++*count; tor_mutex_release(thread_test_mutex_); } tor_mutex_acquire(thread_test_mutex_); strmap_set(thread_test_strmap_, s, *cp); if (in_main_thread()) ++thread_fns_failed; tor_mutex_release(thread_test_mutex_); tor_mutex_release(m); spawn_exit(); }
static void mark_handled(int serial) { #ifdef TRACK_RESPONSES tor_mutex_acquire(&bitmap_mutex); tor_assert(serial < handled_len); tor_assert(! bitarray_is_set(handled, serial)); bitarray_set(handled, serial); tor_mutex_release(&bitmap_mutex); #else (void)serial; #endif }
/** Put a reply on the reply queue. The reply must not currently be on * any thread's work queue. */ static void queue_reply(replyqueue_t *queue, workqueue_entry_t *work) { int was_empty; tor_mutex_acquire(&queue->lock); was_empty = TOR_TAILQ_EMPTY(&queue->answers); TOR_TAILQ_INSERT_TAIL(&queue->answers, work, next_work); tor_mutex_release(&queue->lock); if (was_empty) { if (queue->alert.alert_fn(queue->alert.write_fd) < 0) { /* XXXX complain! */ } } }
/** * Cancel a workqueue_entry_t that has been returned from * threadpool_queue_work. * * You must not call this function on any work whose reply function has been * executed in the main thread; that will cause undefined behavior (probably, * a crash). * * If the work is cancelled, this function return the argument passed to the * work function. It is the caller's responsibility to free this storage. * * This function will have no effect if the worker thread has already executed * or begun to execute the work item. In that case, it will return NULL. */ void * workqueue_entry_cancel(workqueue_entry_t *ent) { int cancelled = 0; void *result = NULL; tor_mutex_acquire(&ent->on_pool->lock); if (ent->pending) { TOR_TAILQ_REMOVE(&ent->on_pool->work, ent, next_work); cancelled = 1; result = ent->arg; } tor_mutex_release(&ent->on_pool->lock); if (cancelled) { workqueue_entry_free(ent); } return result; }
/** * Queue a copy of a work item for every thread in a pool. This can be used, * for example, to tell the threads to update some parameter in their states. * * Arguments are as for <b>threadpool_queue_work</b>, except that the * <b>arg</b> value is passed to <b>dup_fn</b> once per each thread to * make a copy of it. * * UPDATE FUNCTIONS MUST BE IDEMPOTENT. We do not guarantee that every update * will be run. If a new update is scheduled before the old update finishes * running, then the new will replace the old in any threads that haven't run * it yet. * * Return 0 on success, -1 on failure. */ int threadpool_queue_update(threadpool_t *pool, void *(*dup_fn)(void *), int (*fn)(void *, void *), void (*free_fn)(void *), void *arg) { int i, n_threads; void (*old_args_free_fn)(void *arg); void **old_args; void **new_args; tor_mutex_acquire(&pool->lock); n_threads = pool->n_threads; old_args = pool->update_args; old_args_free_fn = pool->free_update_arg_fn; new_args = tor_calloc(n_threads, sizeof(void*)); for (i = 0; i < n_threads; ++i) { if (dup_fn) new_args[i] = dup_fn(arg); else new_args[i] = arg; } pool->update_args = new_args; pool->free_update_arg_fn = free_fn; pool->update_fn = fn; ++pool->generation; tor_mutex_release(&pool->lock); tor_cond_signal_all(&pool->condition); if (old_args) { for (i = 0; i < n_threads; ++i) { if (old_args[i] && old_args_free_fn) old_args_free_fn(old_args[i]); } tor_free(old_args); } return 0; }
/** * Queue an item of work for a thread in a thread pool. The function * <b>fn</b> will be run in a worker thread, and will receive as arguments the * thread's state object, and the provided object <b>arg</b>. It must return * one of WQ_RPL_REPLY, WQ_RPL_ERROR, or WQ_RPL_SHUTDOWN. * * Regardless of its return value, the function <b>reply_fn</b> will later be * run in the main thread when it invokes replyqueue_process(), and will * receive as its argument the same <b>arg</b> object. It's the reply * function's responsibility to free the work object. * * On success, return a workqueue_entry_t object that can be passed to * workqueue_entry_cancel(). On failure, return NULL. * * Note that because each thread has its own work queue, work items may not * be executed strictly in order. */ workqueue_entry_t * threadpool_queue_work(threadpool_t *pool, int (*fn)(void *, void *), void (*reply_fn)(void *), void *arg) { workqueue_entry_t *ent = workqueue_entry_new(fn, reply_fn, arg); ent->on_pool = pool; ent->pending = 1; tor_mutex_acquire(&pool->lock); TOR_TAILQ_INSERT_TAIL(&pool->work, ent, next_work); tor_mutex_release(&pool->lock); tor_cond_signal_one(&pool->condition); return ent; }
/** Run unit tests for threading logic. */ static void test_threads_basic(void *arg) { char *s1 = NULL, *s2 = NULL; int done = 0, timedout = 0; time_t started; #ifndef _WIN32 struct timeval tv; tv.tv_sec=0; tv.tv_usec=100*1000; #endif (void) arg; set_main_thread(); thread_test_mutex_ = tor_mutex_new(); thread_test_start1_ = tor_mutex_new(); thread_test_start2_ = tor_mutex_new(); thread_test_strmap_ = strmap_new(); s1 = tor_strdup("thread 1"); s2 = tor_strdup("thread 2"); tor_mutex_acquire(thread_test_start1_); tor_mutex_acquire(thread_test_start2_); spawn_func(thread_test_func_, s1); spawn_func(thread_test_func_, s2); tor_mutex_release(thread_test_start2_); tor_mutex_release(thread_test_start1_); started = time(NULL); while (!done) { tor_mutex_acquire(thread_test_mutex_); strmap_assert_ok(thread_test_strmap_); if (strmap_get(thread_test_strmap_, "thread 1") && strmap_get(thread_test_strmap_, "thread 2")) { done = 1; } else if (time(NULL) > started + 150) { timedout = done = 1; } tor_mutex_release(thread_test_mutex_); #ifndef _WIN32 /* Prevent the main thread from starving the worker threads. */ select(0, NULL, NULL, NULL, &tv); #endif } tor_mutex_acquire(thread_test_start1_); tor_mutex_release(thread_test_start1_); tor_mutex_acquire(thread_test_start2_); tor_mutex_release(thread_test_start2_); tor_mutex_free(thread_test_mutex_); if (timedout) { printf("\nTimed out: %d %d", t1_count, t2_count); tt_assert(strmap_get(thread_test_strmap_, "thread 1")); tt_assert(strmap_get(thread_test_strmap_, "thread 2")); tt_assert(!timedout); } /* different thread IDs. */ tt_assert(strcmp(strmap_get(thread_test_strmap_, "thread 1"), strmap_get(thread_test_strmap_, "thread 2"))); tt_assert(!strcmp(strmap_get(thread_test_strmap_, "thread 1"), strmap_get(thread_test_strmap_, "last to run")) || !strcmp(strmap_get(thread_test_strmap_, "thread 2"), strmap_get(thread_test_strmap_, "last to run"))); tt_int_op(thread_fns_failed, ==, 0); tt_int_op(thread_fn_tid1, !=, thread_fn_tid2); done: tor_free(s1); tor_free(s2); tor_free(thread1_name_); tor_free(thread2_name_); if (thread_test_strmap_) strmap_free(thread_test_strmap_, NULL); if (thread_test_start1_) tor_mutex_free(thread_test_start1_); if (thread_test_start2_) tor_mutex_free(thread_test_start2_); }