summaryrefslogtreecommitdiff
path: root/ot_fdstate.h
diff options
context:
space:
mode:
Diffstat (limited to 'ot_fdstate.h')
-rw-r--r--ot_fdstate.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/ot_fdstate.h b/ot_fdstate.h
new file mode 100644
index 0000000..88f685d
--- /dev/null
+++ b/ot_fdstate.h
@@ -0,0 +1,57 @@
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/* Walks the table for descriptors that have expired, handing back one at a
49 time until it returns -1. The cursor belongs to the caller, so several
50 sweepers can run without sharing anything. */
51int fdstate_next_expired(ot_time now, int *cursor);
52
53/* Both only valid while the caller holds the descriptor. */
54void *fdstate_cookie(int fd);
55void fdstate_set_expiry(int fd, ot_time expiry);
56
57#endif