From 8fb1c09df1540aa3fca1148bed6a08ffc3090669 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Tue, 27 Oct 2026 19:15:22 +0100 Subject: All OSes now do have a cheap monotonic clock, so no caching thread is needed. Also add some relative time helpers --- trackerlogic.h | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'trackerlogic.h') diff --git a/trackerlogic.h b/trackerlogic.h index 63abf78..6af5d6a 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -6,6 +6,7 @@ #ifndef OT_TRACKERLOGIC_H__ #define OT_TRACKERLOGIC_H__ +#include #include #include #include @@ -68,9 +69,31 @@ typedef struct { #define RANDOMTORRENTS (1024 * 1024 * 1) /* From opentracker.c */ -extern time_t g_now_seconds; -extern volatile int g_opentracker_running; -#define g_now_minutes (g_now_seconds / 60) +/* The kernel already keeps a coarse wall clock, updated every tick and + published where reading it costs no system call. That is what opentracker + used to keep a thread for, at five second resolution. */ +#if defined(CLOCK_REALTIME_COARSE) +#define OT_CLOCK_CHEAP CLOCK_REALTIME_COARSE /* Linux */ +#elif defined(CLOCK_REALTIME_FAST) +#define OT_CLOCK_CHEAP CLOCK_REALTIME_FAST /* the BSDs */ +#else +#define OT_CLOCK_CHEAP CLOCK_REALTIME /* macOS and whatever else */ +#endif + +static inline ot_time time_now_seconds(void) { + struct timespec now; + clock_gettime(OT_CLOCK_CHEAP, &now); + return (ot_time)now.tv_sec; +} + +static inline ot_time time_now_minutes(void) { return time_now_seconds() / 60; } + +/* For the deadlines the tracker sets on itself, which read better as the + span they allow than as an addition to the current second */ +static inline ot_time time_future_seconds(int span) { return time_now_seconds() + span; } +/* Cleared from the interrupt handler and read by every worker */ +extern _Atomic int g_opentracker_running; + extern uint32_t g_tracker_id; typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG; -- cgit v1.3