|
| 1 | +#include <stdbool.h> |
| 2 | +#include <stddef.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +typedef struct list_node { |
| 7 | + struct list_node *next; |
| 8 | + void *val; |
| 9 | +} list_node_t; |
| 10 | + |
| 11 | +typedef struct { |
| 12 | + list_node_t *head; |
| 13 | + uint32_t length; |
| 14 | +} list_t; |
| 15 | + |
| 16 | +static volatile uint32_t list_retries_empty = 0, list_retries_populated = 0; |
| 17 | +static const list_node_t *empty = NULL; |
| 18 | + |
| 19 | +static list_t *list_new() |
| 20 | +{ |
| 21 | + list_t *l = calloc(1, sizeof(list_node_t)); |
| 22 | + l->head = (list_node_t *) empty; |
| 23 | + l->length = 0; |
| 24 | + return l; |
| 25 | +} |
| 26 | + |
| 27 | +static void list_add(list_t *l, void *val) |
| 28 | +{ |
| 29 | + /* wrap the value as a node in the linked list */ |
| 30 | + list_node_t *v = calloc(1, sizeof(list_node_t)); |
| 31 | + v->val = val; |
| 32 | + |
| 33 | + /* try adding to the front of the list */ |
| 34 | + while (true) { |
| 35 | + list_node_t *n = l->head; |
| 36 | + if (n == empty) { /* if this is the first link in the list */ |
| 37 | + v->next = NULL; |
| 38 | + if (__atomic_compare_exchange(&l->head, &empty, &v, false, |
| 39 | + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
| 40 | + __atomic_fetch_add(&l->length, 1, __ATOMIC_SEQ_CST); |
| 41 | + return; |
| 42 | + } |
| 43 | + list_retries_empty++; |
| 44 | + } else { /* inserting when an existing link is present */ |
| 45 | + v->next = n; |
| 46 | + if (__atomic_compare_exchange(&l->head, &n, &v, false, |
| 47 | + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
| 48 | + __atomic_fetch_add(&l->length, 1, __ATOMIC_SEQ_CST); |
| 49 | + return; |
| 50 | + } |
| 51 | + list_retries_populated++; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#include "free_later.h" |
| 57 | + |
| 58 | +#define CAS(a, b, c) \ |
| 59 | + __extension__({ \ |
| 60 | + typeof(*a) _old = b, _new = c; \ |
| 61 | + __atomic_compare_exchange(a, &_old, &_new, 0, __ATOMIC_SEQ_CST, \ |
| 62 | + __ATOMIC_SEQ_CST); \ |
| 63 | + _old; \ |
| 64 | + }) |
| 65 | + |
| 66 | +static inline void acquire_lock(volatile bool *lock) |
| 67 | +{ |
| 68 | + while (CAS(lock, false, true)) |
| 69 | + ; |
| 70 | +} |
| 71 | + |
| 72 | +static inline void release_lock(volatile bool *lock) |
| 73 | +{ |
| 74 | + int l = *lock; |
| 75 | + CAS(&l, true, false); |
| 76 | +} |
| 77 | + |
| 78 | +typedef struct { |
| 79 | + void *var; |
| 80 | + void (*free)(void *var); |
| 81 | +} free_later_t; |
| 82 | + |
| 83 | +/* track expired variables to cleanup later */ |
| 84 | +static list_t *buffer = NULL, *buffer_prev = NULL; |
| 85 | + |
| 86 | +int free_later_init() |
| 87 | +{ |
| 88 | + buffer = list_new(); |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +/* register a var for cleanup */ |
| 93 | +void free_later(void *var, void release(void *var)) |
| 94 | +{ |
| 95 | + free_later_t *cv = malloc(sizeof(free_later_t)); |
| 96 | + cv->var = var; |
| 97 | + cv->free = release; |
| 98 | + list_add(buffer, cv); |
| 99 | +} |
| 100 | + |
| 101 | +/* signal that worker threads are done with old references */ |
| 102 | +void free_later_stage(void) |
| 103 | +{ |
| 104 | + /* lock to ensure that multiple threads do not clean up simultaneously */ |
| 105 | + static bool lock = false; |
| 106 | + |
| 107 | + /* CAS-based lock in case multiple threads are calling this */ |
| 108 | + acquire_lock(&lock); |
| 109 | + |
| 110 | + if (!buffer_prev || buffer_prev->length == 0) { |
| 111 | + release_lock(&lock); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + /* swap the buffers */ |
| 116 | + buffer_prev = buffer; |
| 117 | + buffer = list_new(); |
| 118 | + |
| 119 | + release_lock(&lock); |
| 120 | +} |
| 121 | + |
| 122 | +void free_later_run() |
| 123 | +{ |
| 124 | + /* lock to ensure that multiple threads do not clean up simultaneously */ |
| 125 | + static bool lock = false; |
| 126 | + |
| 127 | + /* skip if there is nothing to return */ |
| 128 | + if (!buffer_prev) |
| 129 | + return; |
| 130 | + |
| 131 | + /* CAS-based lock in case multiple threads are calling this */ |
| 132 | + acquire_lock(&lock); |
| 133 | + |
| 134 | + /* At this point, all workers have processed one or more new flow since the |
| 135 | + * free_later buffer was filled. No threads are using the old, deleted data. |
| 136 | + */ |
| 137 | + for (list_node_t *n = buffer_prev->head; n; n = n->next) { |
| 138 | + free_later_t *v = n->val; |
| 139 | + v->free(v->var); |
| 140 | + free(n); |
| 141 | + } |
| 142 | + |
| 143 | + free(buffer_prev); |
| 144 | + buffer_prev = NULL; |
| 145 | + |
| 146 | + release_lock(&lock); |
| 147 | +} |
| 148 | + |
| 149 | +int free_later_exit() |
| 150 | +{ |
| 151 | + /* purge anything that is buffered */ |
| 152 | + free_later_run(); |
| 153 | + |
| 154 | + /* stage and purge anything that was unbuffered */ |
| 155 | + free_later_stage(); |
| 156 | + free_later_run(); |
| 157 | + |
| 158 | + /* release memory for the buffer */ |
| 159 | + free(buffer); |
| 160 | + buffer = NULL; |
| 161 | + return 0; |
| 162 | +} |
0 commit comments