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