summaryrefslogtreecommitdiff
path: root/ot_fdstate.h
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-26 13:35:58 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-10-26 13:35:58 +0100
commit0194b1b8a00d6646f03291e6045918aaaf7cfdc4 (patch)
treea9ec6437af9b487595bfe4afe96a949a8556f409 /ot_fdstate.h
parent2e5b19945eb03fe4e3fa47562888401c7cbae71d (diff)
Introduce a sub system that can store io cookies for file descriptors, thread safely so
Diffstat (limited to 'ot_fdstate.h')
-rw-r--r--ot_fdstate.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/ot_fdstate.h b/ot_fdstate.h
new file mode 100644
index 0000000..2693a3b
--- /dev/null
+++ b/ot_fdstate.h
@@ -0,0 +1,52 @@
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_FDSTATE_H__
7#define OT_FDSTATE_H__
8
9#include <stdint.h>
10
11#include "trackerlogic.h"
12
13/* What the tracker knows about a file descriptor: who owns it, when it
14 should be given up on, and whether anyone is working on it right now.
15
16 A descriptor number says nothing about identity, since the kernel hands
17 the same number out again as soon as it is closed. Every slot therefore
18 carries a generation that is bumped when the descriptor is given up, and
19 every caller that was told about a descriptor has to say which generation
20 it was told about. A caller holding a stale generation is turned away
21 instead of being handed somebody else's connection.
22
23 The table is sized once from RLIMIT_NOFILE and never moves, so looking a
24 descriptor up needs no synchronisation at all. */
25
26int fdstate_init(void);
27void fdstate_deinit(void);
28size_t fdstate_slots(void);
29
30/* Take a freshly accepted descriptor into the table and return the
31 generation to quote when asking for it back. expiry of 0 never expires. */
32uint32_t fdstate_open(int fd, void *cookie, ot_time expiry);
33
34/* Ask for exclusive use. Returns 0 if the descriptor has been recycled since
35 generation was handed out, or if somebody else is using it. */
36int fdstate_claim(int fd, uint32_t generation);
37void fdstate_release(int fd);
38
39/* The cleaner's side of the same thing: takes the descriptor only if nobody
40 is working on it and it really has expired. Never waits. */
41int fdstate_claim_expired(int fd, ot_time now);
42
43/* Give the descriptor up. The generation is bumped here, which has to happen
44 before close(2) so that the kernel cannot reissue the number while events
45 quoting the old generation are still in flight. */
46void fdstate_close(int fd);
47
48/* Both only valid while the caller holds the descriptor. */
49void *fdstate_cookie(int fd);
50void fdstate_set_expiry(int fd, ot_time expiry);
51
52#endif