From 471431c3d75eb4df127f22f1e1175bc43cd66549 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 22 Jun 2016 21:26:21 -0700 Subject: Store worker_count on choices_t --- src/choices.c | 14 +++++++------- src/choices.h | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/choices.c b/src/choices.c index 02c551c..70fc679 100644 --- a/src/choices.c +++ b/src/choices.c @@ -104,6 +104,8 @@ void choices_init(choices_t *c) { c->capacity = c->size = 0; choices_resize(c, INITIAL_CHOICE_CAPACITY); + c->worker_count = 8; + choices_reset_search(c); } @@ -149,8 +151,8 @@ static void *choices_search_worker(void *data) { struct worker *w = (struct worker *)data; const choices_t *c = w->choices; - size_t start = (w->worker_num) * c->size / w->worker_count; - size_t end = (w->worker_num + 1) * c->size / w->worker_count; + size_t start = (w->worker_num) * c->size / c->worker_count; + size_t end = (w->worker_num + 1) * c->size / c->worker_count; for(size_t i = start; i < end; i++) { if (has_match(w->search, c->strings[i])) { @@ -173,12 +175,10 @@ void choices_search(choices_t *c, const char *search) { abort(); } - int worker_count = 8; - struct worker *workers = calloc(worker_count, sizeof(struct worker)); - for (int i = 0; i < worker_count; i++) { + struct worker *workers = calloc(c->worker_count, sizeof(struct worker)); + for (unsigned int i = 0; i < c->worker_count; i++) { workers[i].choices = c; workers[i].search = search; - workers[i].worker_count = worker_count; workers[i].worker_num = i; workers[i].results = malloc(c->size * sizeof(struct scored_result)); /* FIXME: This is overkill */ if (pthread_create(&workers[i].thread_id, NULL, &choices_search_worker, &workers[i])) { @@ -187,7 +187,7 @@ void choices_search(choices_t *c, const char *search) { } } - for (int i = 0; i < worker_count; i++) { + for (unsigned int i = 0; i < c->worker_count; i++) { struct worker *w = &workers[i]; if (pthread_join(w->thread_id, NULL)) { diff --git a/src/choices.h b/src/choices.h index d8d547c..319765e 100644 --- a/src/choices.h +++ b/src/choices.h @@ -20,6 +20,8 @@ typedef struct { size_t available; size_t selection; + + unsigned int worker_count; } choices_t; void choices_init(choices_t *c); -- cgit v1.2.3