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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
#include <stdio.h> #include <stdint.h> #include <string.h>
typedef enum { EVT_A = 0, EVT_B, EVT_LOG, EVT_MAX } event_id_t;
typedef struct { event_id_t id; union { int value; char msg[32]; } data; } event_t;
typedef void (*observer_cb_t)(const event_t *e, void *user);
typedef struct { observer_cb_t cb; void *user; } observer_t;
#define MAX_OBS 8
typedef struct { observer_t list[EVT_MAX][MAX_OBS]; uint8_t count[EVT_MAX]; } event_bus_t;
static void bus_init(event_bus_t *b) { for (int i = 0; i < EVT_MAX; i++) b->count[i] = 0; }
static int bus_subscribe(event_bus_t *b, event_id_t id, observer_cb_t cb, void *user) { if (!b || !cb || id >= EVT_MAX) return -1; uint8_t n = b->count[id]; if (n >= MAX_OBS) return -2; b->list[id][n].cb = cb; b->list[id][n].user = user; b->count[id] = (uint8_t)(n + 1); return 0; }
static void bus_notify(event_bus_t *b, const event_t *e) { if (!b || !e || e->id >= EVT_MAX) return; uint8_t n = b->count[e->id]; for (uint8_t i = 0; i < n; i++) { observer_t *o = &b->list[e->id][i]; o->cb(e, o->user); } }
#define QCAP 16
typedef struct { event_t q[QCAP]; uint8_t head, tail, size; uint32_t dropped; } event_queue_t;
static void q_init(event_queue_t *q) { q->head = q->tail = q->size = 0; q->dropped = 0; }
static int q_push(event_queue_t *q, const event_t *e) { if (!q || !e) return -1; if (q->size >= QCAP) { q->dropped++; return -2; } q->q[q->tail] = *e; q->tail = (uint8_t)((q->tail + 1) % QCAP); q->size++; return 0; }
static int q_pop(event_queue_t *q, event_t *out) { if (!q || !out) return -1; if (q->size == 0) return -2; *out = q->q[q->head]; q->head = (uint8_t)((q->head + 1) % QCAP); q->size--; return 0; }
static void dispatch_all(event_bus_t *bus, event_queue_t *q) { event_t e; while (q_pop(q, &e) == 0) { bus_notify(bus, &e); } }
static void obs_print(const event_t *e, void *user) { const char *name = (const char*)user; if (e->id == EVT_LOG) { printf("[%s] LOG: %s\n", name, e->data.msg); } else { printf("[%s] EVT_%d value=%d\n", name, (int)e->id, e->data.value); } }
typedef struct { int sum; } acc_t;
static void obs_acc(const event_t *e, void *user) { acc_t *a = (acc_t*)user; if (e->id == EVT_A || e->id == EVT_B) { a->sum += e->data.value; printf("[ACC] sum=%d\n", a->sum); } }
static void publish_A(event_queue_t *q, int v) { event_t e = { .id = EVT_A }; e.data.value = v; (void)q_push(q, &e); }
static void publish_B(event_queue_t *q, int v) { event_t e = { .id = EVT_B }; e.data.value = v; (void)q_push(q, &e); }
static void publish_LOG(event_queue_t *q, const char *msg) { event_t e = { .id = EVT_LOG }; snprintf(e.data.msg, sizeof(e.data.msg), "%s", msg); (void)q_push(q, &e); }
int main(void) { event_bus_t bus; event_queue_t q; bus_init(&bus); q_init(&q);
acc_t acc = {0};
bus_subscribe(&bus, EVT_A, obs_print, (void*)"PRINT"); bus_subscribe(&bus, EVT_B, obs_print, (void*)"PRINT"); bus_subscribe(&bus, EVT_LOG,obs_print, (void*)"PRINT"); bus_subscribe(&bus, EVT_A, obs_acc, &acc); bus_subscribe(&bus, EVT_B, obs_acc, &acc);
printf("=== Async Observer: publish only enqueues ===\n");
publish_A(&q, 10); publish_B(&q, 3); publish_LOG(&q, "hello (will print later)"); publish_A(&q, 7);
printf("\n(No output above from observers yet, because not dispatched)\n");
printf("\n=== dispatch_all() ===\n"); dispatch_all(&bus, &q);
printf("\n=== publish more, then dispatch again ===\n"); publish_LOG(&q, "tick2 start"); publish_B(&q, 100); publish_A(&q, -5);
dispatch_all(&bus, &q);
printf("\nQueue dropped=%u (if queue overflow happened)\n", (unsigned)q.dropped); return 0; }
|