David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 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 | int iolooper_poll( IoLooper* iol ); |
Vladimir Chtchetkine | d8ba2ae | 2010-11-15 11:02:49 -0800 | [diff] [blame^] | 20 | /* 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' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 25 | int iolooper_wait( IoLooper* iol, int64_t duration ); |
| 26 | |
| 27 | int iolooper_is_read( IoLooper* iol, int fd ); |
| 28 | int iolooper_is_write( IoLooper* iol, int fd ); |
Ot ten Thije | cc19d3e | 2010-07-19 13:10:18 +0100 | [diff] [blame] | 29 | /* Returns 1 if this IoLooper has one or more file descriptor to interact with */ |
| 30 | int iolooper_has_operations( IoLooper* iol ); |
Vladimir Chtchetkine | d8ba2ae | 2010-11-15 11:02:49 -0800 | [diff] [blame^] | 31 | /* Gets current time in milliseconds. |
| 32 | * Return: |
| 33 | * Number of milliseconds corresponded to the current time on success, or -1 |
| 34 | * on failure. |
| 35 | */ |
| 36 | int64_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 | */ |
| 60 | int iolooper_wait_absolute(IoLooper* iol, int64_t deadline); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 61 | |
| 62 | #endif /* IOLOOPER_H */ |