1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <stdatomic.h> #include <pthread.h> #include <unistd.h>
#define MAX_THREADS 128
struct rcu_reader { atomic_int active; };
static struct rcu_reader readers[MAX_THREADS]; static atomic_int reader_count = 0; static __thread int rcu_id = -1;
static void rcu_register_thread(void) { if (rcu_id >= 0) return;
int id = atomic_fetch_add(&reader_count, 1); if (id >= MAX_THREADS) { fprintf(stderr, "too many threads\n"); abort(); }
rcu_id = id; atomic_store(&readers[id].active, 0); }
static void rcu_read_lock(void) { rcu_register_thread();
atomic_store_explicit(&readers[rcu_id].active, 1, memory_order_release); }
static void rcu_read_unlock(void) { atomic_store_explicit(&readers[rcu_id].active, 0, memory_order_release); }
static void synchronize_rcu(void) { int n = atomic_load_explicit(&reader_count, memory_order_acquire);
for (;;) { int all_quiet = 1;
for (int i = 0; i < n; i++) { if (atomic_load_explicit(&readers[i].active, memory_order_acquire)) { all_quiet = 0; break; } }
if (all_quiet) break;
sched_yield(); } }
struct config { int version; char name[64]; };
static _Atomic(struct config *) g_cfg;
static void *reader_thread(void *arg) { long id = (long)arg; rcu_register_thread();
while (1) { rcu_read_lock();
struct config *cfg = atomic_load_explicit(&g_cfg, memory_order_acquire);
if (cfg) { printf("reader %ld: version=%d, name=%s\n", id, cfg->version, cfg->name); }
rcu_read_unlock(); usleep(300*1000); }
return NULL; }
static void *writer_thread(void *arg) { (void)arg;
for (int i = 1; ; i++) { struct config *new_cfg = malloc(sizeof(*new_cfg)); new_cfg->version = i; snprintf(new_cfg->name, sizeof(new_cfg->name), "config-%d", i);
struct config *old_cfg = atomic_exchange_explicit(&g_cfg, new_cfg, memory_order_acq_rel);
printf("writer: updated config to version=%d, name=%s\n", new_cfg->version, new_cfg->name); struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start);
synchronize_rcu();
clock_gettime(CLOCK_MONOTONIC, &end); long elapsed_us = (end.tv_sec - start.tv_sec) * 1000*1000 + (end.tv_nsec - start.tv_nsec) / 1000; printf("writer: synchronize_rcu took %ld us\n", elapsed_us); free(old_cfg); sleep(2); }
return NULL; }
int main(void) { pthread_t r1, r2, r3, w;
struct config *init = malloc(sizeof(*init)); init->version = 0; snprintf(init->name, sizeof(init->name), "init");
atomic_store(&g_cfg, init);
pthread_create(&r1, NULL, reader_thread, (void *)1); pthread_create(&r2, NULL, reader_thread, (void *)2); pthread_create(&r3, NULL, reader_thread, (void *)3); pthread_create(&w, NULL, writer_thread, NULL);
pthread_join(r1, NULL); pthread_join(r2, NULL); pthread_join(r3, NULL); pthread_join(w, NULL);
return 0; }
|