diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-01 00:53:53 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-01 00:53:53 +0100 |
| commit | af0f2cb1be5aa10bb6a4bdd9f7ef4aa3950ecb31 (patch) | |
| tree | bd366224016709d65bbb0b8fbc3148c29719e8c4 /ot_worker.h | |
| parent | b7d9dc14eb560b1d19f506610535815f29085579 (diff) | |
Generalize the worker setup/runloop/teardown logic by adding a smol ot_worker helper
Diffstat (limited to 'ot_worker.h')
| -rw-r--r-- | ot_worker.h | 39 |
1 files changed, 39 insertions, 0 deletions
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 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | #ifndef OT_WORKER_H__ | ||
| 7 | #define OT_WORKER_H__ | ||
| 8 | |||
| 9 | #include <pthread.h> | ||
| 10 | #include <stdatomic.h> | ||
| 11 | #include <stdint.h> | ||
| 12 | |||
| 13 | #include "trackerlogic.h" | ||
| 14 | |||
| 15 | struct ot_worker; | ||
| 16 | |||
| 17 | /* What a worker does between being started and being told to stop. It reads | ||
| 18 | worker->sock and works out of worker->ws; everything around it, from the | ||
| 19 | buffers to the counters to saying goodbye, is handled here. */ | ||
| 20 | typedef void (*ot_worker_run)(struct ot_worker *worker); | ||
| 21 | |||
| 22 | /* A thread that serves requests on a socket of its own, with the buffers and | ||
| 23 | counters it needs. Each is allocated on its own, so taking on another one | ||
| 24 | never moves the record a running worker holds. */ | ||
| 25 | struct ot_worker { | ||
| 26 | pthread_t thread; | ||
| 27 | _Atomic int left; | ||
| 28 | int64_t sock; | ||
| 29 | ot_worker_run run; | ||
| 30 | struct ot_workstruct ws; | ||
| 31 | }; | ||
| 32 | |||
| 33 | /* Start one. Returns -1 if it could not be started. */ | ||
| 34 | int worker_start(ot_worker_run run, int64_t sock); | ||
| 35 | |||
| 36 | /* Wake every worker until it has left, then join it */ | ||
| 37 | void worker_stop_all(void); | ||
| 38 | |||
| 39 | #endif | ||
