From af0f2cb1be5aa10bb6a4bdd9f7ef4aa3950ecb31 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sun, 1 Nov 2026 00:53:53 +0100 Subject: Generalize the worker setup/runloop/teardown logic by adding a smol ot_worker helper --- ot_udp.c | 69 +++++----------------------------------------------------------- 1 file changed, 5 insertions(+), 64 deletions(-) (limited to 'ot_udp.c') diff --git a/ot_udp.c b/ot_udp.c index 00ec566..309cbdb 100644 --- a/ot_udp.c +++ b/ot_udp.c @@ -16,21 +16,11 @@ #include "ot_random.h" #include "ot_rijndael.h" #include "ot_socket.h" +#include "ot_worker.h" #include "ot_stats.h" #include "ot_udp.h" #include "trackerlogic.h" -/* One of these per worker, each allocated on its own so that growing the - array below never moves the one a running worker holds. */ -struct udp_worker { - pthread_t thread; - int64_t sock; - _Atomic int left; -}; - -static struct udp_worker **g_workers; -static size_t g_worker_count; - static uint32_t g_rijndael_round_key[44] = {0}; static volatile uint32_t g_key_of_the_hour[2] = {0}; static volatile ot_time g_hour_of_the_key; @@ -217,22 +207,9 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { return 1; } -static void *udp_worker(void *args) { - struct udp_worker *me = args; - struct ot_workstruct ws; - - if (workstruct_init(&ws) == -1) { - fprintf(stderr, "Warning: a udp worker could not get its buffers and will not run.\n"); - atomic_store(&me->left, 1); - return NULL; - } - +static void udp_run(struct ot_worker *me) { while (g_opentracker_running) - handle_udp6(me->sock, &ws); - - workstruct_deinit(&ws); - atomic_store(&me->left, 1); - return NULL; + handle_udp6(me->sock, &me->ws); } void udp_init(void) { @@ -241,47 +218,11 @@ void udp_init(void) { } void udp_worker_install(int64_t sock) { - struct udp_worker **grown = realloc(g_workers, (g_worker_count + 1) * sizeof(*g_workers)); - struct udp_worker *worker; - - if (!grown || !(worker = calloc(1, sizeof(*worker)))) { - fprintf(stderr, "Warning: out of memory installing a udp worker, this socket stays with the workers already running.\n"); - g_workers = grown ? grown : g_workers; - return; - } - g_workers = grown; - worker->sock = sock; - #ifdef _DEBUG fprintf(stderr, "Installing a worker on udp socket %ld\n", (unsigned long)sock); #endif socket_set_blocking((int)sock, 1); - if (pthread_create(&worker->thread, NULL, udp_worker, worker)) - free(worker); - else - g_workers[g_worker_count++] = worker; + if (worker_start(udp_run, sock) == -1) + fprintf(stderr, "Warning: could not install a udp worker, this socket stays with the workers already running.\n"); } - -void udp_workers_deinit(void) { - size_t i; - - for (i = 0; i < g_worker_count; ++i) { - struct udp_worker *worker = g_workers[i]; - - /* Repeated because a signal only interrupts a read already under way: a - worker that was between two reads when the first one arrived is inside - the next one by now. */ - while (!atomic_load(&worker->left)) { - pthread_kill(worker->thread, OT_SIGNAL_WAKE); - usleep(1000); - } - pthread_join(worker->thread, NULL); - free(worker); - } - - free(g_workers); - g_workers = NULL; - g_worker_count = 0; -} - -- cgit v1.3