blob: f051d07fb209dc42bf50a4b51c09aa3493784696 [file] [log] [blame]
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001#ifndef IOLOOPER_H
2#define IOLOOPER_H
3
4#include <stdint.h>
5
6/* An IOLooper is an abstraction for select() */
7
8typedef struct IoLooper IoLooper;
9
10IoLooper* iolooper_new(void);
11void iolooper_free( IoLooper* iol );
12void iolooper_reset( IoLooper* iol );
13
14void iolooper_add_read( IoLooper* iol, int fd );
15void iolooper_add_write( IoLooper* iol, int fd );
16void iolooper_del_read( IoLooper* iol, int fd );
17void iolooper_del_write( IoLooper* iol, int fd );
18
19int iolooper_poll( IoLooper* iol );
Vladimir Chtchetkined8ba2ae2010-11-15 11:02:49 -080020/* Wrapper around select()
21 * Return:
22 * > 0 in case an I/O has occurred, or < 0 on error, or 0 on timeout with
23 * errno set to ETIMEDOUT.
24 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070025int iolooper_wait( IoLooper* iol, int64_t duration );
26
27int iolooper_is_read( IoLooper* iol, int fd );
28int iolooper_is_write( IoLooper* iol, int fd );
Ot ten Thijecc19d3e2010-07-19 13:10:18 +010029/* Returns 1 if this IoLooper has one or more file descriptor to interact with */
30int iolooper_has_operations( IoLooper* iol );
Vladimir Chtchetkined8ba2ae2010-11-15 11:02:49 -080031/* Gets current time in milliseconds.
32 * Return:
33 * Number of milliseconds corresponded to the current time on success, or -1
34 * on failure.
35 */
36int64_t iolooper_now(void);
37/* Waits for an I/O to occur before specific absolute time.
38 * This routine should be used (instead of iolooper_wait) in cases when multiple
39 * sequential I/O should be completed within given time interval. For instance,
40 * consider the scenario, when "server" does two sequential writes, and "client"
41 * now has to read data transferred with these two distinct writes. It might be
42 * wasteful to do two reads, each with the same (large) timeout. Instead, it
43 * would be better to assign a deadline for both reads before the first read,
44 * and call iolooper_wait_absoulte with the same deadline value:
45 * int64_t deadline = iolooper_now() + TIMEOUT;
46 * if (iolooper_wait_absoulte(iol, deadline)) {
47 * // Process first buffer.
48 * (iolooper_wait_absoulte(iol, deadline)) {
49 * // Process second read
50 * }
51 * }
52 * Param:
53 * iol IoLooper instance for an I/O.
54 * deadline Deadline (absoulte time in milliseconds) before which an I/O should
55 * occur.
56 * Return:
57 * Number of I/O descriptors set in iol, if an I/O has occurred, 0 if no I/O
58 * occurred before the deadline, or -1 on error.
59 */
60int iolooper_wait_absolute(IoLooper* iol, int64_t deadline);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070061
62#endif /* IOLOOPER_H */