diff options
Diffstat (limited to '')
-rw-r--r-- | barrier.c (renamed from createbarriergroup.c) | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/createbarriergroup.c b/barrier.c index 694032b..cd7bae8 100644 --- a/createbarriergroup.c +++ b/barrier.c @@ -2,6 +2,35 @@ #include "common.h" +void +barrierwait(pthread_barrier_t *barrier) +{ +#ifndef SINGLE_THREADED + int r = pthread_barrier_wait(barrier); + if (r && r != PTHREAD_BARRIER_SERIAL_THREAD) { + errno = r; + eprintf("pthread_barrier_wait:"); + } +#else + (void) barrier; +#endif +} + + +void +barriersend(struct barrier_group *group, struct global_data *global, void (*action)(struct algorithm *, struct global_data *)) +{ + size_t index; + global->action = action; + if (group->nthreads) + barrierwait(&group->barrier); + for (index = 0; index < global->nalgorithms; index += group->nthreads + 1U) + (*action)(&global->algorithms[index], global); + if (group->nthreads) + barrierwait(&group->barrier); +} + + #ifndef SINGLE_THREADED static void * slaveloop(void *thread_param) @@ -31,8 +60,6 @@ createbarriergroup(struct barrier_group *group_out, size_t count, struct global_ #ifndef SINGLE_THREADED size_t i; - count = MAX(count, 64); - group_out->nthreads = count - 1U; group_out->threads = NULL; if (!group_out->nthreads) @@ -57,3 +84,30 @@ createbarriergroup(struct barrier_group *group_out, size_t count, struct global_ (void) count; #endif } + + +void +killbarriergroup(struct barrier_group *group, struct global_data *global) +{ +#ifndef SINGLE_THREADED + size_t i; + + if (!group->nthreads) + return; + + global->action = NULL; + for (i = 0; i < group->nthreads; i++) + group->threads[i].global = global; + barrierwait(&group->barrier); + + for (i = 0; i < group->nthreads; i++) + if ((errno = pthread_join(group->threads[i].thread, NULL))) + weprintf("pthread_join:"); + pthread_barrier_destroy(&group->barrier); + + free(group->threads); +#else + (void) group; + (void) global; +#endif +} |