summaryrefslogtreecommitdiff
path: root/ot_fdstate.h
blob: 88f685d09a7f841037749bba2b711059ebd90b7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.

   $id$ */

#ifndef OT_FDSTATE_H__
#define OT_FDSTATE_H__

#include <stdint.h>

#include "trackerlogic.h"

/* What the tracker knows about a file descriptor: who owns it, when it
   should be given up on, and whether anyone is working on it right now.

   A descriptor number says nothing about identity, since the kernel hands
   the same number out again as soon as it is closed. Every slot therefore
   carries a generation that is bumped when the descriptor is given up, and
   every caller that was told about a descriptor has to say which generation
   it was told about. A caller holding a stale generation is turned away
   instead of being handed somebody else's connection.

   The table is sized once from RLIMIT_NOFILE and never moves, so looking a
   descriptor up needs no synchronisation at all. */

int      fdstate_init(void);
void     fdstate_deinit(void);
size_t   fdstate_slots(void);

/* Take a freshly accepted descriptor into the table and return the
   generation to quote when asking for it back. expiry of 0 never expires. */
uint32_t fdstate_open(int fd, void *cookie, ot_time expiry);

/* Ask for exclusive use. Returns 0 if the descriptor has been recycled since
   generation was handed out, or if somebody else is using it. */
int      fdstate_claim(int fd, uint32_t generation);
void     fdstate_release(int fd);

/* The cleaner's side of the same thing: takes the descriptor only if nobody
   is working on it and it really has expired. Never waits. */
int      fdstate_claim_expired(int fd, ot_time now);

/* Give the descriptor up. The generation is bumped here, which has to happen
   before close(2) so that the kernel cannot reissue the number while events
   quoting the old generation are still in flight. */
void     fdstate_close(int fd);

/* Walks the table for descriptors that have expired, handing back one at a
   time until it returns -1. The cursor belongs to the caller, so several
   sweepers can run without sharing anything. */
int      fdstate_next_expired(ot_time now, int *cursor);

/* Both only valid while the caller holds the descriptor. */
void    *fdstate_cookie(int fd);
void     fdstate_set_expiry(int fd, ot_time expiry);

#endif