Debian Codegraph @

third-party/libhv-v1.3.3/event/wepoll/README.md

raw symbols SCIP
1 # wepoll - epoll for windows
2
3 [![][ci status badge]][ci status link]
4
5 This library implements the [epoll][man epoll] API for Windows
6 applications. It is fast and scalable, and it closely resembles the API
7 and behavior of Linux' epoll.
8
9 ## Rationale
10
11 Unlike Linux, OS X, and many other operating systems, Windows doesn't
12 have a good API for receiving socket state notifications. It only
13 supports the `select` and `WSAPoll` APIs, but they
14 [don't scale][select scale] and suffer from
15 [other issues][wsapoll broken].
16
17 Using I/O completion ports isn't always practical when software is
18 designed to be cross-platform. Wepoll offers an alternative that is
19 much closer to a drop-in replacement for software that was designed
20 to run on Linux.
21
22 ## Features
23
24 * Can poll 100000s of sockets efficiently.
25 * Fully thread-safe.
26 * Multiple threads can poll the same epoll port.
27 * Sockets can be added to multiple epoll sets.
28 * All epoll events (`EPOLLIN`, `EPOLLOUT`, `EPOLLPRI`, `EPOLLRDHUP`)
29 are supported.
30 * Level-triggered and one-shot (`EPOLLONESTHOT`) modes are supported
31 * Trivial to embed: you need [only two files][dist].
32
33 ## Limitations
34
35 * Only works with sockets.
36 * Edge-triggered (`EPOLLET`) mode isn't supported.
37
38 ## How to use
39
40 The library is [distributed][dist] as a single source file
41 ([wepoll.c][wepoll.c]) and a single header file ([wepoll.h][wepoll.h]).<br>
42 Compile the .c file as part of your project, and include the header wherever
43 needed.
44
45 ## Compatibility
46
47 * Requires Windows Vista or higher.
48 * Can be compiled with recent versions of MSVC, Clang, and GCC.
49
50 ## API
51
52 ### General remarks
53
54 * The epoll port is a `HANDLE`, not a file descriptor.
55 * All functions set both `errno` and `GetLastError()` on failure.
56 * For more extensive documentation, see the [epoll(7) man page][man epoll],
57 and the per-function man pages that are linked below.
58
59 ### epoll_create/epoll_create1
60
61 ```c
62 HANDLE epoll_create(int size);
63 HANDLE epoll_create1(int flags);
64 ```
65
66 * Create a new epoll instance (port).
67 * `size` is ignored but most be greater than zero.
68 * `flags` must be zero as there are no supported flags.
69 * Returns `NULL` on failure.
70 * [Linux man page][man epoll_create]
71
72 ### epoll_close
73
74 ```c
75 int epoll_close(HANDLE ephnd);
76 ```
77
78 * Close an epoll port.
79 * Do not attempt to close the epoll port with `close()`,
80 `CloseHandle()` or `closesocket()`.
81
82 ### epoll_ctl
83
84 ```c
85 int epoll_ctl(HANDLE ephnd,
86 int op,
87 SOCKET sock,
88 struct epoll_event* event);
89 ```
90
91 * Control which socket events are monitored by an epoll port.
92 * `ephnd` must be a HANDLE created by
93 [`epoll_create()`](#epoll_createepoll_create1) or
94 [`epoll_create1()`](#epoll_createepoll_create1).
95 * `op` must be one of `EPOLL_CTL_ADD`, `EPOLL_CTL_MOD`, `EPOLL_CTL_DEL`.
96 * `sock` must be a valid socket created by [`socket()`][msdn socket],
97 [`WSASocket()`][msdn wsasocket], or [`accept()`][msdn accept].
98 * `event` should be a pointer to a [`struct epoll_event`](#struct-epoll_event).<br>
99 If `op` is `EPOLL_CTL_DEL` then the `event` parameter is ignored, and it
100 may be `NULL`.
101 * Returns 0 on success, -1 on failure.
102 * It is recommended to always explicitly remove a socket from its epoll
103 set using `EPOLL_CTL_DEL` *before* closing it.<br>
104 As on Linux, closed sockets are automatically removed from the epoll set, but
105 wepoll may not be able to detect that a socket was closed until the next call
106 to [`epoll_wait()`](#epoll_wait).
107 * [Linux man page][man epoll_ctl]
108
109 ### epoll_wait
110
111 ```c
112 int epoll_wait(HANDLE ephnd,
113 struct epoll_event* events,
114 int maxevents,
115 int timeout);
116 ```
117
118 * Receive socket events from an epoll port.
119 * `events` should point to a caller-allocated array of
120 [`epoll_event`](#struct-epoll_event) structs, which will receive the
121 reported events.
122 * `maxevents` is the maximum number of events that will be written to the
123 `events` array, and must be greater than zero.
124 * `timeout` specifies whether to block when no events are immediately available.
125 - `<0` block indefinitely
126 - `0` report any events that are already waiting, but don't block
127 - `≥1` block for at most N milliseconds
128 * Return value:
129 - `-1` an error occurred
130 - `0` timed out without any events to report
131 - `≥1` the number of events stored in the `events` buffer
132 * [Linux man page][man epoll_wait]
133
134 ### struct epoll_event
135
136 ```c
137 typedef union epoll_data {
138 void* ptr;
139 int fd;
140 uint32_t u32;
141 uint64_t u64;
142 SOCKET sock; /* Windows specific */
143 HANDLE hnd; /* Windows specific */
144 } epoll_data_t;
145 ```
146
147 ```c
148 struct epoll_event {
149 uint32_t events; /* Epoll events and flags */
150 epoll_data_t data; /* User data variable */
151 };
152 ```
153
154 * The `events` field is a bit mask containing the events being
155 monitored/reported, and optional flags.<br>
156 Flags are accepted by [`epoll_ctl()`](#epoll_ctl), but they are not reported
157 back by [`epoll_wait()`](#epoll_wait).
158 * The `data` field can be used to associate application-specific information
159 with a socket; its value will be returned unmodified by
160 [`epoll_wait()`](#epoll_wait).
161 * [Linux man page][man epoll_ctl]
162
163 | Event | Description |
164 |---------------|----------------------------------------------------------------------|
165 | `EPOLLIN` | incoming data available, or incoming connection ready to be accepted |
166 | `EPOLLOUT` | ready to send data, or outgoing connection successfully established |
167 | `EPOLLRDHUP` | remote peer initiated graceful socket shutdown |
168 | `EPOLLPRI` | out-of-band data available for reading |
169 | `EPOLLERR` | socket error<sup>1</sup> |
170 | `EPOLLHUP` | socket hang-up<sup>1</sup> |
171 | `EPOLLRDNORM` | same as `EPOLLIN` |
172 | `EPOLLRDBAND` | same as `EPOLLPRI` |
173 | `EPOLLWRNORM` | same as `EPOLLOUT` |
174 | `EPOLLWRBAND` | same as `EPOLLOUT` |
175 | `EPOLLMSG` | never reported |
176
177 | Flag | Description |
178 |------------------|---------------------------|
179 | `EPOLLONESHOT` | report event(s) only once |
180 | `EPOLLET` | not supported by wepoll |
181 | `EPOLLEXCLUSIVE` | not supported by wepoll |
182 | `EPOLLWAKEUP` | not supported by wepoll |
183
184 <sup>1</sup>: the `EPOLLERR` and `EPOLLHUP` events may always be reported by
185 [`epoll_wait()`](#epoll_wait), regardless of the event mask that was passed to
186 [`epoll_ctl()`](#epoll_ctl).
187
188
189 [ci status badge]: https://ci.appveyor.com/api/projects/status/github/piscisaureus/wepoll?branch=master&svg=true
190 [ci status link]: https://ci.appveyor.com/project/piscisaureus/wepoll/branch/master
191 [dist]: https://github.com/piscisaureus/wepoll/tree/dist
192 [man epoll]: http://man7.org/linux/man-pages/man7/epoll.7.html
193 [man epoll_create]: http://man7.org/linux/man-pages/man2/epoll_create.2.html
194 [man epoll_ctl]: http://man7.org/linux/man-pages/man2/epoll_ctl.2.html
195 [man epoll_wait]: http://man7.org/linux/man-pages/man2/epoll_wait.2.html
196 [msdn accept]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms737526(v=vs.85).aspx
197 [msdn socket]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740506(v=vs.85).aspx
198 [msdn wsasocket]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms742212(v=vs.85).aspx
199 [select scale]: https://daniel.haxx.se/docs/poll-vs-select.html
200 [wsapoll broken]: https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
201 [wepoll.c]: https://github.com/piscisaureus/wepoll/blob/dist/wepoll.c
202 [wepoll.h]: https://github.com/piscisaureus/wepoll/blob/dist/wepoll.h