… | |
… | |
132 | .TH "<STANDARD INPUT>" 1 "2007-11-27" "perl v5.8.8" "User Contributed Perl Documentation" |
132 | .TH "<STANDARD INPUT>" 1 "2007-11-27" "perl v5.8.8" "User Contributed Perl Documentation" |
133 | .SH "NAME" |
133 | .SH "NAME" |
134 | libev \- a high performance full\-featured event loop written in C |
134 | libev \- a high performance full\-featured event loop written in C |
135 | .SH "SYNOPSIS" |
135 | .SH "SYNOPSIS" |
136 | .IX Header "SYNOPSIS" |
136 | .IX Header "SYNOPSIS" |
137 | .Vb 1 |
137 | .Vb 2 |
|
|
138 | \& /* this is the only header you need */ |
138 | \& #include <ev.h> |
139 | \& #include <ev.h> |
|
|
140 | .Ve |
|
|
141 | .PP |
|
|
142 | .Vb 3 |
|
|
143 | \& /* what follows is a fully working example program */ |
|
|
144 | \& ev_io stdin_watcher; |
|
|
145 | \& ev_timer timeout_watcher; |
|
|
146 | .Ve |
|
|
147 | .PP |
|
|
148 | .Vb 8 |
|
|
149 | \& /* called when data readable on stdin */ |
|
|
150 | \& static void |
|
|
151 | \& stdin_cb (EV_P_ struct ev_io *w, int revents) |
|
|
152 | \& { |
|
|
153 | \& /* puts ("stdin ready"); */ |
|
|
154 | \& ev_io_stop (EV_A_ w); /* just a syntax example */ |
|
|
155 | \& ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */ |
|
|
156 | \& } |
|
|
157 | .Ve |
|
|
158 | .PP |
|
|
159 | .Vb 6 |
|
|
160 | \& static void |
|
|
161 | \& timeout_cb (EV_P_ struct ev_timer *w, int revents) |
|
|
162 | \& { |
|
|
163 | \& /* puts ("timeout"); */ |
|
|
164 | \& ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */ |
|
|
165 | \& } |
|
|
166 | .Ve |
|
|
167 | .PP |
|
|
168 | .Vb 4 |
|
|
169 | \& int |
|
|
170 | \& main (void) |
|
|
171 | \& { |
|
|
172 | \& struct ev_loop *loop = ev_default_loop (0); |
|
|
173 | .Ve |
|
|
174 | .PP |
|
|
175 | .Vb 3 |
|
|
176 | \& /* initialise an io watcher, then start it */ |
|
|
177 | \& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
|
|
178 | \& ev_io_start (loop, &stdin_watcher); |
|
|
179 | .Ve |
|
|
180 | .PP |
|
|
181 | .Vb 3 |
|
|
182 | \& /* simple non-repeating 5.5 second timeout */ |
|
|
183 | \& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
|
|
184 | \& ev_timer_start (loop, &timeout_watcher); |
|
|
185 | .Ve |
|
|
186 | .PP |
|
|
187 | .Vb 2 |
|
|
188 | \& /* loop till timeout or data ready */ |
|
|
189 | \& ev_loop (loop, 0); |
|
|
190 | .Ve |
|
|
191 | .PP |
|
|
192 | .Vb 2 |
|
|
193 | \& return 0; |
|
|
194 | \& } |
139 | .Ve |
195 | .Ve |
140 | .SH "DESCRIPTION" |
196 | .SH "DESCRIPTION" |
141 | .IX Header "DESCRIPTION" |
197 | .IX Header "DESCRIPTION" |
142 | Libev is an event loop: you register interest in certain events (such as a |
198 | Libev is an event loop: you register interest in certain events (such as a |
143 | file descriptor being readable or a timeout occuring), and it will manage |
199 | file descriptor being readable or a timeout occuring), and it will manage |
… | |
… | |
240 | might be supported on the current system, you would need to look at |
296 | might be supported on the current system, you would need to look at |
241 | \&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for |
297 | \&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for |
242 | recommended ones. |
298 | recommended ones. |
243 | .Sp |
299 | .Sp |
244 | See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
300 | See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
245 | .IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 |
301 | .IP "ev_set_allocator (void *(*cb)(void *ptr, size_t size))" 4 |
246 | .IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" |
302 | .IX Item "ev_set_allocator (void *(*cb)(void *ptr, size_t size))" |
247 | Sets the allocation function to use (the prototype is similar to the |
303 | Sets the allocation function to use (the prototype and semantics are |
248 | realloc C function, the semantics are identical). It is used to allocate |
304 | identical to the realloc C function). It is used to allocate and free |
249 | and free memory (no surprises here). If it returns zero when memory |
305 | memory (no surprises here). If it returns zero when memory needs to be |
250 | needs to be allocated, the library might abort or take some potentially |
306 | allocated, the library might abort or take some potentially destructive |
251 | destructive action. The default is your system realloc function. |
307 | action. The default is your system realloc function. |
252 | .Sp |
308 | .Sp |
253 | You could override this function in high-availability programs to, say, |
309 | You could override this function in high-availability programs to, say, |
254 | free some memory if it cannot allocate memory, to use a special allocator, |
310 | free some memory if it cannot allocate memory, to use a special allocator, |
255 | or even to sleep a while and retry until some memory is available. |
311 | or even to sleep a while and retry until some memory is available. |
256 | .Sp |
312 | .Sp |
257 | Example: replace the libev allocator with one that waits a bit and then |
313 | Example: replace the libev allocator with one that waits a bit and then |
258 | retries: better than mine). |
314 | retries: better than mine). |
259 | .Sp |
315 | .Sp |
260 | .Vb 6 |
316 | .Vb 6 |
261 | \& static void * |
317 | \& static void * |
262 | \& persistent_realloc (void *ptr, long size) |
318 | \& persistent_realloc (void *ptr, size_t size) |
263 | \& { |
319 | \& { |
264 | \& for (;;) |
320 | \& for (;;) |
265 | \& { |
321 | \& { |
266 | \& void *newptr = realloc (ptr, size); |
322 | \& void *newptr = realloc (ptr, size); |
267 | .Ve |
323 | .Ve |