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_worker.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 ot_worker.h (limited to 'ot_worker.h') diff --git a/ot_worker.h b/ot_worker.h new file mode 100644 index 0000000..ea4f379 --- /dev/null +++ b/ot_worker.h @@ -0,0 +1,39 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. + + $id$ */ + +#ifndef OT_WORKER_H__ +#define OT_WORKER_H__ + +#include +#include +#include + +#include "trackerlogic.h" + +struct ot_worker; + +/* What a worker does between being started and being told to stop. It reads + worker->sock and works out of worker->ws; everything around it, from the + buffers to the counters to saying goodbye, is handled here. */ +typedef void (*ot_worker_run)(struct ot_worker *worker); + +/* A thread that serves requests on a socket of its own, with the buffers and + counters it needs. Each is allocated on its own, so taking on another one + never moves the record a running worker holds. */ +struct ot_worker { + pthread_t thread; + _Atomic int left; + int64_t sock; + ot_worker_run run; + struct ot_workstruct ws; +}; + +/* Start one. Returns -1 if it could not be started. */ +int worker_start(ot_worker_run run, int64_t sock); + +/* Wake every worker until it has left, then join it */ +void worker_stop_all(void); + +#endif -- cgit v1.3