summaryrefslogtreecommitdiff
path: root/ot_fdstate.h
diff options
context:
space:
mode:
Diffstat (limited to 'ot_fdstate.h')
-rw-r--r--ot_fdstate.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/ot_fdstate.h b/ot_fdstate.h
new file mode 100644
index 0000000..c09f018
--- /dev/null
+++ b/ot_fdstate.h
@@ -0,0 +1,61 @@
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/* Which generation a descriptor currently carries, to check an event that
35 was handed out earlier against the connection living there now. */
36uint32_t fdstate_generation(int fd);
37
38/* Ask for exclusive use. Returns 0 if the descriptor has been recycled since
39 generation was handed out, or if somebody else is using it. */
40int fdstate_claim(int fd, uint32_t generation);
41void fdstate_release(int fd);
42
43/* The cleaner's side of the same thing: takes the descriptor only if nobody
44 is working on it and it really has expired. Never waits. */
45int fdstate_claim_expired(int fd, ot_time now);
46
47/* Give the descriptor up. The generation is bumped here, which has to happen
48 before close(2) so that the kernel cannot reissue the number while events
49 quoting the old generation are still in flight. */
50void fdstate_close(int fd);
51
52/* Walks the table for descriptors that have expired, handing back one at a
53 time until it returns -1. The cursor belongs to the caller, so several
54 sweepers can run without sharing anything. */
55int fdstate_next_expired(ot_time now, int *cursor);
56
57/* Both only valid while the caller holds the descriptor. */
58void *fdstate_cookie(int fd);
59void fdstate_set_expiry(int fd, ot_time expiry);
60
61#endif