|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdatomic.h> |
| 3 | +#include <threads.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <stdbool.h> |
| 6 | +#include <assert.h> |
| 7 | +#include <math.h> |
| 8 | + |
| 9 | +#define PRECISION 100 /* upper bound in BPP sum */ |
| 10 | +#define CACHE_LINE_SIZE 64 |
| 11 | +#define N_THREADS 64 |
| 12 | + |
| 13 | +struct tpool_future { |
| 14 | + void *result; |
| 15 | + void *arg; |
| 16 | + atomic_flag flag; |
| 17 | +}; |
| 18 | + |
| 19 | +typedef struct job { |
| 20 | + void *(*func)(void *); |
| 21 | + struct tpool_future *future; |
| 22 | + struct job *next, *prev; |
| 23 | +} job_t; |
| 24 | + |
| 25 | +typedef struct idle_job { |
| 26 | + union { |
| 27 | + struct { |
| 28 | + _Atomic(job_t *) prev; |
| 29 | + unsigned long long version; |
| 30 | + }; |
| 31 | + _Atomic struct versioned_prev { |
| 32 | + job_t *ptr; |
| 33 | + unsigned long long _version; |
| 34 | + } v_prev; |
| 35 | + }; |
| 36 | + char padding[CACHE_LINE_SIZE - sizeof(_Atomic(job_t *)) - |
| 37 | + sizeof(unsigned long long)]; /* avoid false sharing */ |
| 38 | + job_t job; |
| 39 | +} idle_job_t; |
| 40 | + |
| 41 | +enum state { idle, running, cancelled }; |
| 42 | + |
| 43 | +typedef struct tpool { |
| 44 | + atomic_flag initialezed; |
| 45 | + int size; |
| 46 | + thrd_t *pool; |
| 47 | + atomic_int state; |
| 48 | + thrd_start_t func; |
| 49 | + idle_job_t *head; /* job queue is a SPMC ring buffer */ |
| 50 | +} tpool_t; |
| 51 | + |
| 52 | +static struct tpool_future *tpool_future_create(void *arg) |
| 53 | +{ |
| 54 | + struct tpool_future *future = malloc(sizeof(struct tpool_future)); |
| 55 | + if (future) { |
| 56 | + future->result = NULL; |
| 57 | + future->arg = arg; |
| 58 | + atomic_flag_clear(&future->flag); |
| 59 | + atomic_flag_test_and_set(&future->flag); |
| 60 | + } |
| 61 | + return future; |
| 62 | +} |
| 63 | + |
| 64 | +void tpool_future_wait(struct tpool_future *future) |
| 65 | +{ |
| 66 | + while (atomic_flag_test_and_set(&future->flag)) |
| 67 | + ; |
| 68 | +} |
| 69 | + |
| 70 | +void tpool_future_destroy(struct tpool_future *future) |
| 71 | +{ |
| 72 | + free(future->result); |
| 73 | + free(future); |
| 74 | +} |
| 75 | + |
| 76 | +static int worker(void *args) |
| 77 | +{ |
| 78 | + if (!args) |
| 79 | + return EXIT_FAILURE; |
| 80 | + tpool_t *thrd_pool = (tpool_t *)args; |
| 81 | + |
| 82 | + while (1) { |
| 83 | + /* worker is laid off */ |
| 84 | + if (atomic_load(&thrd_pool->state) == cancelled) |
| 85 | + return EXIT_SUCCESS; |
| 86 | + if (atomic_load(&thrd_pool->state) == running) { |
| 87 | + /* worker takes the job */ |
| 88 | + struct versioned_prev job = atomic_load(&thrd_pool->head->v_prev); |
| 89 | + /* worker checks if there is only an idle job in the job queue */ |
| 90 | + if (job.ptr == &thrd_pool->head->job) { |
| 91 | + /* worker says it is idle */ |
| 92 | + atomic_store(&thrd_pool->state, idle); |
| 93 | + thrd_yield(); |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + struct versioned_prev next; |
| 98 | + /* compare 16 byte at once */ |
| 99 | + do { |
| 100 | + next.ptr = job.ptr->prev; |
| 101 | + next._version = job._version; |
| 102 | + } while (!atomic_compare_exchange_weak(&thrd_pool->head->v_prev, |
| 103 | + &job, next)); |
| 104 | + |
| 105 | + job.ptr->future->result = |
| 106 | + (void *)job.ptr->func(job.ptr->future->arg); |
| 107 | + atomic_flag_clear(&job.ptr->future->flag); |
| 108 | + free(job.ptr); |
| 109 | + } else { |
| 110 | + /* worker is idle */ |
| 111 | + thrd_yield(); |
| 112 | + } |
| 113 | + }; |
| 114 | + return EXIT_SUCCESS; |
| 115 | +} |
| 116 | + |
| 117 | +static bool tpool_init(tpool_t *thrd_pool, size_t size) |
| 118 | +{ |
| 119 | + if (atomic_flag_test_and_set(&thrd_pool->initialezed)) { |
| 120 | + printf("This thread pool has already been initialized.\n"); |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + assert(size > 0); |
| 125 | + thrd_pool->pool = malloc(sizeof(thrd_t) * size); |
| 126 | + if (!thrd_pool->pool) { |
| 127 | + printf("Failed to allocate thread identifiers.\n"); |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + idle_job_t *idle_job = malloc(sizeof(idle_job_t)); |
| 132 | + if (!idle_job) { |
| 133 | + printf("Failed to allocate idle job.\n"); |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + /* idle_job will always be the first job */ |
| 138 | + idle_job->job.next = &idle_job->job; |
| 139 | + idle_job->job.prev = &idle_job->job; |
| 140 | + idle_job->prev = &idle_job->job; |
| 141 | + idle_job->version = 0ULL; |
| 142 | + thrd_pool->func = worker; |
| 143 | + thrd_pool->head = idle_job; |
| 144 | + thrd_pool->state = idle; |
| 145 | + thrd_pool->size = size; |
| 146 | + |
| 147 | + /* employer hires many workers */ |
| 148 | + for (size_t i = 0; i < size; i++) |
| 149 | + thrd_create(thrd_pool->pool + i, worker, thrd_pool); |
| 150 | + |
| 151 | + return true; |
| 152 | +} |
| 153 | + |
| 154 | +static void tpool_destroy(tpool_t *thrd_pool) |
| 155 | +{ |
| 156 | + if (atomic_exchange(&thrd_pool->state, cancelled)) |
| 157 | + printf("Thread pool cancelled with jobs still running.\n"); |
| 158 | + |
| 159 | + for (int i = 0; i < thrd_pool->size; i++) |
| 160 | + thrd_join(thrd_pool->pool[i], NULL); |
| 161 | + |
| 162 | + while (thrd_pool->head->prev != &thrd_pool->head->job) { |
| 163 | + job_t *job = thrd_pool->head->prev->prev; |
| 164 | + free(thrd_pool->head->prev); |
| 165 | + thrd_pool->head->prev = job; |
| 166 | + } |
| 167 | + free(thrd_pool->head); |
| 168 | + free(thrd_pool->pool); |
| 169 | + atomic_fetch_and(&thrd_pool->state, 0); |
| 170 | + atomic_flag_clear(&thrd_pool->initialezed); |
| 171 | +} |
| 172 | + |
| 173 | +/* Use Bailey–Borwein–Plouffe formula to approximate PI */ |
| 174 | +static void *bbp(void *arg) |
| 175 | +{ |
| 176 | + int k = *(int *)arg; |
| 177 | + double sum = (4.0 / (8 * k + 1)) - (2.0 / (8 * k + 4)) - |
| 178 | + (1.0 / (8 * k + 5)) - (1.0 / (8 * k + 6)); |
| 179 | + double *product = malloc(sizeof(double)); |
| 180 | + if (!product) |
| 181 | + return NULL; |
| 182 | + |
| 183 | + *product = 1 / pow(16, k) * sum; |
| 184 | + return (void *)product; |
| 185 | +} |
| 186 | + |
| 187 | +struct tpool_future *add_job(tpool_t *thrd_pool, void *(*func)(void *), |
| 188 | + void *arg) |
| 189 | +{ |
| 190 | + job_t *job = malloc(sizeof(job_t)); |
| 191 | + if (!job) |
| 192 | + return NULL; |
| 193 | + |
| 194 | + struct tpool_future *future = tpool_future_create(arg); |
| 195 | + if (!future) { |
| 196 | + free(job); |
| 197 | + return NULL; |
| 198 | + } |
| 199 | + |
| 200 | + job->func = func; |
| 201 | + job->future = future; |
| 202 | + job->next = thrd_pool->head->job.next; |
| 203 | + job->prev = &thrd_pool->head->job; |
| 204 | + thrd_pool->head->job.next->prev = job; |
| 205 | + thrd_pool->head->job.next = job; |
| 206 | + if (thrd_pool->head->prev == &thrd_pool->head->job) { |
| 207 | + thrd_pool->head->prev = job; |
| 208 | + thrd_pool->head->version += 1; |
| 209 | + /* the previous job of the idle job is itself */ |
| 210 | + thrd_pool->head->job.prev = &thrd_pool->head->job; |
| 211 | + } |
| 212 | + return future; |
| 213 | +} |
| 214 | + |
| 215 | +static inline void wait_until(tpool_t *thrd_pool, int state) |
| 216 | +{ |
| 217 | + while (atomic_load(&thrd_pool->state) != state) |
| 218 | + thrd_yield(); |
| 219 | +} |
| 220 | + |
| 221 | +int main() |
| 222 | +{ |
| 223 | + int bbp_args[PRECISION]; |
| 224 | + struct tpool_future *futures[PRECISION]; |
| 225 | + double bbp_sum = 0; |
| 226 | + |
| 227 | + tpool_t thrd_pool = { .initialezed = ATOMIC_FLAG_INIT }; |
| 228 | + if (!tpool_init(&thrd_pool, N_THREADS)) { |
| 229 | + printf("failed to init.\n"); |
| 230 | + return 0; |
| 231 | + } |
| 232 | + /* employer ask workers to work */ |
| 233 | + atomic_store(&thrd_pool.state, running); |
| 234 | + |
| 235 | + /* employer wait ... until workers are idle */ |
| 236 | + wait_until(&thrd_pool, idle); |
| 237 | + |
| 238 | + /* employer add more job to the job queue */ |
| 239 | + for (int i = 0; i < PRECISION; i++) { |
| 240 | + bbp_args[i] = i; |
| 241 | + futures[i] = add_job(&thrd_pool, bbp, &bbp_args[i]); |
| 242 | + } |
| 243 | + |
| 244 | + /* employer ask workers to work */ |
| 245 | + atomic_store(&thrd_pool.state, running); |
| 246 | + |
| 247 | + /* employer wait for the result of job */ |
| 248 | + for (int i = 0; i < PRECISION; i++) { |
| 249 | + tpool_future_wait(futures[i]); |
| 250 | + bbp_sum += *(double *)(futures[i]->result); |
| 251 | + tpool_future_destroy(futures[i]); |
| 252 | + } |
| 253 | + |
| 254 | + /* employer destroys the job queue and lays workers off */ |
| 255 | + tpool_destroy(&thrd_pool); |
| 256 | + printf("PI calculated with %d terms: %.15f\n", PRECISION, bbp_sum); |
| 257 | + return 0; |
| 258 | +} |
0 commit comments