|
| 1 | +#include <stdalign.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdatomic.h> |
| 4 | +#include <threads.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <stdbool.h> |
| 7 | +#include <assert.h> |
| 8 | + |
| 9 | +#define CACHE_LINE_SIZE 64 |
| 10 | +#define N_JOBS 16 |
| 11 | +#define N_THREADS 8 |
| 12 | + |
| 13 | +typedef struct job { |
| 14 | + void *args; |
| 15 | + struct job *next, *prev; |
| 16 | +} job_t; |
| 17 | + |
| 18 | +typedef struct idle_job { |
| 19 | + union { |
| 20 | + struct { |
| 21 | + _Atomic(job_t *) prev; |
| 22 | + unsigned long long version; |
| 23 | + }; |
| 24 | + _Atomic struct B16 { |
| 25 | + job_t *_prev; |
| 26 | + unsigned long long _version; |
| 27 | + } DCAS; |
| 28 | + }; |
| 29 | + char padding[CACHE_LINE_SIZE - sizeof(_Atomic(job_t *)) - |
| 30 | + sizeof(unsigned long long)]; |
| 31 | + job_t job; |
| 32 | +} idle_job_t; |
| 33 | + |
| 34 | +enum state { idle, running, cancelled }; |
| 35 | + |
| 36 | +typedef struct thread_pool { |
| 37 | + atomic_flag initialezed; |
| 38 | + int size; |
| 39 | + thrd_t *pool; |
| 40 | + atomic_int state; |
| 41 | + thrd_start_t func; |
| 42 | + // job queue is a SPMC ring buffer |
| 43 | + idle_job_t *head; |
| 44 | +} thread_pool_t; |
| 45 | + |
| 46 | +static int worker(void *args) |
| 47 | +{ |
| 48 | + if (!args) |
| 49 | + return EXIT_FAILURE; |
| 50 | + thread_pool_t *thrd_pool = (thread_pool_t *)args; |
| 51 | + |
| 52 | + while (1) { |
| 53 | + if (atomic_load(&thrd_pool->state) == cancelled) |
| 54 | + return EXIT_SUCCESS; |
| 55 | + if (atomic_load(&thrd_pool->state) == running) { |
| 56 | + // claim the job |
| 57 | + struct B16 job = atomic_load(&thrd_pool->head->DCAS); |
| 58 | + struct B16 next; |
| 59 | + do { |
| 60 | + next._prev = job._prev->prev; |
| 61 | + next._version = job._version; |
| 62 | + } while (!atomic_compare_exchange_weak(&thrd_pool->head->DCAS, &job, |
| 63 | + next)); |
| 64 | + |
| 65 | + if (job._prev->args == NULL) { |
| 66 | + atomic_store(&thrd_pool->state, idle); |
| 67 | + } else { |
| 68 | + printf("Hello from job %d\n", *(int *)job._prev->args); |
| 69 | + free(job._prev->args); |
| 70 | + free(job._prev); // could cause dangling pointer in other threads |
| 71 | + } |
| 72 | + } else { |
| 73 | + /* To auto run when jobs added, set status to running if job queue is not empty. |
| 74 | + * As long as the producer is protected */ |
| 75 | + thrd_yield(); |
| 76 | + continue; |
| 77 | + } |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +static bool thread_pool_init(thread_pool_t *thrd_pool, size_t size) |
| 82 | +{ |
| 83 | + if (atomic_flag_test_and_set(&thrd_pool->initialezed)) { |
| 84 | + printf("This thread pool has already been initialized.\n"); |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + assert(size > 0); |
| 89 | + thrd_pool->pool = malloc(sizeof(thrd_t) * size); |
| 90 | + if (!thrd_pool->pool) { |
| 91 | + printf("Failed to allocate thread identifiers.\n"); |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + // May use memory pool for jobs |
| 96 | + idle_job_t *idle_job = malloc(sizeof(idle_job_t)); |
| 97 | + if (!idle_job) { |
| 98 | + printf("Failed to allocate idle job.\n"); |
| 99 | + return false; |
| 100 | + } |
| 101 | + // idle_job will always be the first job |
| 102 | + idle_job->job.args = NULL; |
| 103 | + idle_job->job.next = &idle_job->job; |
| 104 | + idle_job->job.prev = &idle_job->job; |
| 105 | + idle_job->prev = &idle_job->job; |
| 106 | + idle_job->version = 0ULL; |
| 107 | + thrd_pool->func = worker; |
| 108 | + thrd_pool->head = idle_job; |
| 109 | + thrd_pool->state = idle; |
| 110 | + thrd_pool->size = size; |
| 111 | + |
| 112 | + for (size_t i = 0; i < size; i++) { |
| 113 | + thrd_create(thrd_pool->pool + i, worker, thrd_pool); |
| 114 | + //TODO: error handling |
| 115 | + } |
| 116 | + |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +static void thread_pool_destroy(thread_pool_t *thrd_pool) |
| 121 | +{ |
| 122 | + if (atomic_exchange(&thrd_pool->state, cancelled)) |
| 123 | + printf("Thread pool cancelled with jobs still running.\n"); |
| 124 | + for (int i = 0; i < thrd_pool->size; i++) { |
| 125 | + thrd_join(thrd_pool->pool[i], NULL); |
| 126 | + } |
| 127 | + while (thrd_pool->head->prev != &thrd_pool->head->job) { |
| 128 | + job_t *job = thrd_pool->head->prev->prev; |
| 129 | + free(thrd_pool->head->prev); |
| 130 | + thrd_pool->head->prev = job; |
| 131 | + } |
| 132 | + free(thrd_pool->head); |
| 133 | + free(thrd_pool->pool); |
| 134 | + atomic_fetch_and(&thrd_pool->state, 0); |
| 135 | + atomic_flag_clear(&thrd_pool->initialezed); |
| 136 | +} |
| 137 | + |
| 138 | +__attribute__((nonnull(2))) static bool add_job(thread_pool_t *thrd_pool, |
| 139 | + void *args) |
| 140 | +{ |
| 141 | + // May use memory pool for jobs |
| 142 | + job_t *job = malloc(sizeof(job_t)); |
| 143 | + if (!job) |
| 144 | + return false; |
| 145 | + |
| 146 | + // unprotected producer |
| 147 | + job->args = args; |
| 148 | + job->next = thrd_pool->head->job.next; |
| 149 | + job->prev = &thrd_pool->head->job; |
| 150 | + thrd_pool->head->job.next->prev = job; |
| 151 | + thrd_pool->head->job.next = job; |
| 152 | + if (thrd_pool->head->prev == &thrd_pool->head->job) { |
| 153 | + thrd_pool->head->prev = job; |
| 154 | + thrd_pool->head->version += 1; |
| 155 | + // trap worker at idle job |
| 156 | + thrd_pool->head->job.prev = &thrd_pool->head->job; |
| 157 | + } |
| 158 | + |
| 159 | + return true; |
| 160 | +} |
| 161 | + |
| 162 | +static inline void wait_until(thread_pool_t *thrd_pool, int state) |
| 163 | +{ |
| 164 | + while (atomic_load(&thrd_pool->state) != state) { |
| 165 | + thrd_yield(); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +int main() |
| 170 | +{ |
| 171 | + thread_pool_t thrd_pool = { .initialezed = ATOMIC_FLAG_INIT }; |
| 172 | + if (!thread_pool_init(&thrd_pool, N_THREADS)) { |
| 173 | + printf("failed to init.\n"); |
| 174 | + return 0; |
| 175 | + } |
| 176 | + for (int i = 0; i < N_JOBS; i++) { |
| 177 | + int *id = malloc(sizeof(int)); |
| 178 | + *id = i; |
| 179 | + add_job(&thrd_pool, id); |
| 180 | + } |
| 181 | + // Due to simplified job queue (not protecting producer), starting the pool manually |
| 182 | + atomic_store(&thrd_pool.state, running); |
| 183 | + wait_until(&thrd_pool, idle); |
| 184 | + for (int i = 0; i < N_JOBS; i++) { |
| 185 | + int *id = malloc(sizeof(int)); |
| 186 | + *id = i; |
| 187 | + add_job(&thrd_pool, id); |
| 188 | + } |
| 189 | + atomic_store(&thrd_pool.state, running); |
| 190 | + thread_pool_destroy(&thrd_pool); |
| 191 | + return 0; |
| 192 | +} |
0 commit comments