… | |
… | |
173 | .IX Header "TIME REPRESENTATION" |
173 | .IX Header "TIME REPRESENTATION" |
174 | Libev represents time as a single floating point number, representing the |
174 | Libev represents time as a single floating point number, representing the |
175 | (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near |
175 | (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near |
176 | the beginning of 1970, details are complicated, don't ask). This type is |
176 | the beginning of 1970, details are complicated, don't ask). This type is |
177 | called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases |
177 | called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases |
178 | to the double type in C. |
178 | to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on |
|
|
179 | it, you should treat it as such. |
179 | .SH "GLOBAL FUNCTIONS" |
180 | .SH "GLOBAL FUNCTIONS" |
180 | .IX Header "GLOBAL FUNCTIONS" |
181 | .IX Header "GLOBAL FUNCTIONS" |
181 | These functions can be called anytime, even before initialising the |
182 | These functions can be called anytime, even before initialising the |
182 | library in any way. |
183 | library in any way. |
183 | .IP "ev_tstamp ev_time ()" 4 |
184 | .IP "ev_tstamp ev_time ()" 4 |
… | |
… | |
199 | .Sp |
200 | .Sp |
200 | Usually, it's a good idea to terminate if the major versions mismatch, |
201 | Usually, it's a good idea to terminate if the major versions mismatch, |
201 | as this indicates an incompatible change. Minor versions are usually |
202 | as this indicates an incompatible change. Minor versions are usually |
202 | compatible to older versions, so a larger minor version alone is usually |
203 | compatible to older versions, so a larger minor version alone is usually |
203 | not a problem. |
204 | not a problem. |
|
|
205 | .Sp |
|
|
206 | Example: make sure we haven't accidentally been linked against the wrong |
|
|
207 | version: |
|
|
208 | .Sp |
|
|
209 | .Vb 3 |
|
|
210 | \& assert (("libev version mismatch", |
|
|
211 | \& ev_version_major () == EV_VERSION_MAJOR |
|
|
212 | \& && ev_version_minor () >= EV_VERSION_MINOR)); |
|
|
213 | .Ve |
204 | .IP "unsigned int ev_supported_backends ()" 4 |
214 | .IP "unsigned int ev_supported_backends ()" 4 |
205 | .IX Item "unsigned int ev_supported_backends ()" |
215 | .IX Item "unsigned int ev_supported_backends ()" |
206 | Return the set of all backends (i.e. their corresponding \f(CW\*(C`EV_BACKEND_*\*(C'\fR |
216 | Return the set of all backends (i.e. their corresponding \f(CW\*(C`EV_BACKEND_*\*(C'\fR |
207 | value) compiled into this binary of libev (independent of their |
217 | value) compiled into this binary of libev (independent of their |
208 | availability on the system you are running on). See \f(CW\*(C`ev_default_loop\*(C'\fR for |
218 | availability on the system you are running on). See \f(CW\*(C`ev_default_loop\*(C'\fR for |
209 | a description of the set values. |
219 | a description of the set values. |
|
|
220 | .Sp |
|
|
221 | Example: make sure we have the epoll method, because yeah this is cool and |
|
|
222 | a must have and can we have a torrent of it please!!!11 |
|
|
223 | .Sp |
|
|
224 | .Vb 2 |
|
|
225 | \& assert (("sorry, no epoll, no sex", |
|
|
226 | \& ev_supported_backends () & EVBACKEND_EPOLL)); |
|
|
227 | .Ve |
210 | .IP "unsigned int ev_recommended_backends ()" 4 |
228 | .IP "unsigned int ev_recommended_backends ()" 4 |
211 | .IX Item "unsigned int ev_recommended_backends ()" |
229 | .IX Item "unsigned int ev_recommended_backends ()" |
212 | Return the set of all backends compiled into this binary of libev and also |
230 | Return the set of all backends compiled into this binary of libev and also |
213 | recommended for this platform. This set is often smaller than the one |
231 | recommended for this platform. This set is often smaller than the one |
214 | returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on |
232 | returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on |
… | |
… | |
224 | destructive action. The default is your system realloc function. |
242 | destructive action. The default is your system realloc function. |
225 | .Sp |
243 | .Sp |
226 | You could override this function in high-availability programs to, say, |
244 | You could override this function in high-availability programs to, say, |
227 | free some memory if it cannot allocate memory, to use a special allocator, |
245 | free some memory if it cannot allocate memory, to use a special allocator, |
228 | or even to sleep a while and retry until some memory is available. |
246 | or even to sleep a while and retry until some memory is available. |
|
|
247 | .Sp |
|
|
248 | Example: replace the libev allocator with one that waits a bit and then |
|
|
249 | retries: better than mine). |
|
|
250 | .Sp |
|
|
251 | .Vb 6 |
|
|
252 | \& static void * |
|
|
253 | \& persistent_realloc (void *ptr, long size) |
|
|
254 | \& { |
|
|
255 | \& for (;;) |
|
|
256 | \& { |
|
|
257 | \& void *newptr = realloc (ptr, size); |
|
|
258 | .Ve |
|
|
259 | .Sp |
|
|
260 | .Vb 2 |
|
|
261 | \& if (newptr) |
|
|
262 | \& return newptr; |
|
|
263 | .Ve |
|
|
264 | .Sp |
|
|
265 | .Vb 3 |
|
|
266 | \& sleep (60); |
|
|
267 | \& } |
|
|
268 | \& } |
|
|
269 | .Ve |
|
|
270 | .Sp |
|
|
271 | .Vb 2 |
|
|
272 | \& ... |
|
|
273 | \& ev_set_allocator (persistent_realloc); |
|
|
274 | .Ve |
229 | .IP "ev_set_syserr_cb (void (*cb)(const char *msg));" 4 |
275 | .IP "ev_set_syserr_cb (void (*cb)(const char *msg));" 4 |
230 | .IX Item "ev_set_syserr_cb (void (*cb)(const char *msg));" |
276 | .IX Item "ev_set_syserr_cb (void (*cb)(const char *msg));" |
231 | Set the callback function to call on a retryable syscall error (such |
277 | Set the callback function to call on a retryable syscall error (such |
232 | as failed select, poll, epoll_wait). The message is a printable string |
278 | as failed select, poll, epoll_wait). The message is a printable string |
233 | indicating the system call or subsystem causing the problem. If this |
279 | indicating the system call or subsystem causing the problem. If this |
234 | callback is set, then libev will expect it to remedy the sitution, no |
280 | callback is set, then libev will expect it to remedy the sitution, no |
235 | matter what, when it returns. That is, libev will generally retry the |
281 | matter what, when it returns. That is, libev will generally retry the |
236 | requested operation, or, if the condition doesn't go away, do bad stuff |
282 | requested operation, or, if the condition doesn't go away, do bad stuff |
237 | (such as abort). |
283 | (such as abort). |
|
|
284 | .Sp |
|
|
285 | Example: do the same thing as libev does internally: |
|
|
286 | .Sp |
|
|
287 | .Vb 6 |
|
|
288 | \& static void |
|
|
289 | \& fatal_error (const char *msg) |
|
|
290 | \& { |
|
|
291 | \& perror (msg); |
|
|
292 | \& abort (); |
|
|
293 | \& } |
|
|
294 | .Ve |
|
|
295 | .Sp |
|
|
296 | .Vb 2 |
|
|
297 | \& ... |
|
|
298 | \& ev_set_syserr_cb (fatal_error); |
|
|
299 | .Ve |
238 | .SH "FUNCTIONS CONTROLLING THE EVENT LOOP" |
300 | .SH "FUNCTIONS CONTROLLING THE EVENT LOOP" |
239 | .IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" |
301 | .IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" |
240 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two |
302 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two |
241 | types of such loops, the \fIdefault\fR loop, which supports signals and child |
303 | types of such loops, the \fIdefault\fR loop, which supports signals and child |
242 | events, and dynamically created loops which do not. |
304 | events, and dynamically created loops which do not. |
… | |
… | |
376 | .IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" |
438 | .IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" |
377 | Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is |
439 | Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is |
378 | always distinct from the default loop. Unlike the default loop, it cannot |
440 | always distinct from the default loop. Unlike the default loop, it cannot |
379 | handle signal and child watchers, and attempts to do so will be greeted by |
441 | handle signal and child watchers, and attempts to do so will be greeted by |
380 | undefined behaviour (or a failed assertion if assertions are enabled). |
442 | undefined behaviour (or a failed assertion if assertions are enabled). |
|
|
443 | .Sp |
|
|
444 | Example: try to create a event loop that uses epoll and nothing else. |
|
|
445 | .Sp |
|
|
446 | .Vb 3 |
|
|
447 | \& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); |
|
|
448 | \& if (!epoller) |
|
|
449 | \& fatal ("no epoll found here, maybe it hides under your chair"); |
|
|
450 | .Ve |
381 | .IP "ev_default_destroy ()" 4 |
451 | .IP "ev_default_destroy ()" 4 |
382 | .IX Item "ev_default_destroy ()" |
452 | .IX Item "ev_default_destroy ()" |
383 | Destroys the default loop again (frees all memory and kernel state |
453 | Destroys the default loop again (frees all memory and kernel state |
384 | etc.). This stops all registered event watchers (by not touching them in |
454 | etc.). This stops all registered event watchers (by not touching them in |
385 | any way whatsoever, although you cannot rely on this :). |
455 | any way whatsoever, although you cannot rely on this :). |
… | |
… | |
419 | Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in |
489 | Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in |
420 | use. |
490 | use. |
421 | .IP "ev_tstamp ev_now (loop)" 4 |
491 | .IP "ev_tstamp ev_now (loop)" 4 |
422 | .IX Item "ev_tstamp ev_now (loop)" |
492 | .IX Item "ev_tstamp ev_now (loop)" |
423 | Returns the current \*(L"event loop time\*(R", which is the time the event loop |
493 | Returns the current \*(L"event loop time\*(R", which is the time the event loop |
424 | got events and started processing them. This timestamp does not change |
494 | received events and started processing them. This timestamp does not |
425 | as long as callbacks are being processed, and this is also the base time |
495 | change as long as callbacks are being processed, and this is also the base |
426 | used for relative timers. You can treat it as the timestamp of the event |
496 | time used for relative timers. You can treat it as the timestamp of the |
427 | occuring (or more correctly, the mainloop finding out about it). |
497 | event occuring (or more correctly, libev finding out about it). |
428 | .IP "ev_loop (loop, int flags)" 4 |
498 | .IP "ev_loop (loop, int flags)" 4 |
429 | .IX Item "ev_loop (loop, int flags)" |
499 | .IX Item "ev_loop (loop, int flags)" |
430 | Finally, this is it, the event handler. This function usually is called |
500 | Finally, this is it, the event handler. This function usually is called |
431 | after you initialised all your watchers and you want to start handling |
501 | after you initialised all your watchers and you want to start handling |
432 | events. |
502 | events. |
433 | .Sp |
503 | .Sp |
434 | If the flags argument is specified as \f(CW0\fR, it will not return until |
504 | If the flags argument is specified as \f(CW0\fR, it will not return until |
435 | either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called. |
505 | either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called. |
|
|
506 | .Sp |
|
|
507 | Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than |
|
|
508 | relying on all watchers to be stopped when deciding when a program has |
|
|
509 | finished (especially in interactive programs), but having a program that |
|
|
510 | automatically loops as long as it has to and no longer by virtue of |
|
|
511 | relying on its watchers stopping correctly is a thing of beauty. |
436 | .Sp |
512 | .Sp |
437 | A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle |
513 | A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle |
438 | those events and any outstanding ones, but will not block your process in |
514 | those events and any outstanding ones, but will not block your process in |
439 | case there are no events and will return after one iteration of the loop. |
515 | case there are no events and will return after one iteration of the loop. |
440 | .Sp |
516 | .Sp |
… | |
… | |
465 | \& - Call all queued watchers in reverse order (i.e. check watchers first). |
541 | \& - Call all queued watchers in reverse order (i.e. check watchers first). |
466 | \& Signals and child watchers are implemented as I/O watchers, and will |
542 | \& Signals and child watchers are implemented as I/O watchers, and will |
467 | \& be handled here by queueing them when their watcher gets executed. |
543 | \& be handled here by queueing them when their watcher gets executed. |
468 | \& - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
544 | \& - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
469 | \& were used, return, otherwise continue with step *. |
545 | \& were used, return, otherwise continue with step *. |
|
|
546 | .Ve |
|
|
547 | .Sp |
|
|
548 | Example: queue some jobs and then loop until no events are outsanding |
|
|
549 | anymore. |
|
|
550 | .Sp |
|
|
551 | .Vb 4 |
|
|
552 | \& ... queue jobs here, make sure they register event watchers as long |
|
|
553 | \& ... as they still have work to do (even an idle watcher will do..) |
|
|
554 | \& ev_loop (my_loop, 0); |
|
|
555 | \& ... jobs done. yeah! |
470 | .Ve |
556 | .Ve |
471 | .IP "ev_unloop (loop, how)" 4 |
557 | .IP "ev_unloop (loop, how)" 4 |
472 | .IX Item "ev_unloop (loop, how)" |
558 | .IX Item "ev_unloop (loop, how)" |
473 | Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it |
559 | Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it |
474 | has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either |
560 | has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either |
… | |
… | |
488 | example, libev itself uses this for its internal signal pipe: It is not |
574 | example, libev itself uses this for its internal signal pipe: It is not |
489 | visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting if |
575 | visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting if |
490 | no event watchers registered by it are active. It is also an excellent |
576 | no event watchers registered by it are active. It is also an excellent |
491 | way to do this for generic recurring timers or from within third-party |
577 | way to do this for generic recurring timers or from within third-party |
492 | libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR. |
578 | libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR. |
|
|
579 | .Sp |
|
|
580 | Example: create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR |
|
|
581 | running when nothing else is active. |
|
|
582 | .Sp |
|
|
583 | .Vb 4 |
|
|
584 | \& struct dv_signal exitsig; |
|
|
585 | \& ev_signal_init (&exitsig, sig_cb, SIGINT); |
|
|
586 | \& ev_signal_start (myloop, &exitsig); |
|
|
587 | \& evf_unref (myloop); |
|
|
588 | .Ve |
|
|
589 | .Sp |
|
|
590 | Example: for some weird reason, unregister the above signal handler again. |
|
|
591 | .Sp |
|
|
592 | .Vb 2 |
|
|
593 | \& ev_ref (myloop); |
|
|
594 | \& ev_signal_stop (myloop, &exitsig); |
|
|
595 | .Ve |
493 | .SH "ANATOMY OF A WATCHER" |
596 | .SH "ANATOMY OF A WATCHER" |
494 | .IX Header "ANATOMY OF A WATCHER" |
597 | .IX Header "ANATOMY OF A WATCHER" |
495 | A watcher is a structure that you create and register to record your |
598 | A watcher is a structure that you create and register to record your |
496 | interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to |
599 | interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to |
497 | become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: |
600 | become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: |
… | |
… | |
685 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR, which don't suffer from this |
788 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR, which don't suffer from this |
686 | problem. Also note that it is quite easy to have your callback invoked |
789 | problem. Also note that it is quite easy to have your callback invoked |
687 | when the readyness condition is no longer valid even when employing |
790 | when the readyness condition is no longer valid even when employing |
688 | typical ways of handling events, so its a good idea to use non-blocking |
791 | typical ways of handling events, so its a good idea to use non-blocking |
689 | I/O unconditionally. |
792 | I/O unconditionally. |
|
|
793 | .PP |
|
|
794 | Example: call \f(CW\*(C`stdin_readable_cb\*(C'\fR when \s-1STDIN_FILENO\s0 has become, well |
|
|
795 | readable, but only once. Since it is likely line\-buffered, you could |
|
|
796 | attempt to read a whole line in the callback: |
|
|
797 | .PP |
|
|
798 | .Vb 6 |
|
|
799 | \& static void |
|
|
800 | \& stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
|
|
801 | \& { |
|
|
802 | \& ev_io_stop (loop, w); |
|
|
803 | \& .. read from stdin here (or from w->fd) and haqndle any I/O errors |
|
|
804 | \& } |
|
|
805 | .Ve |
|
|
806 | .PP |
|
|
807 | .Vb 6 |
|
|
808 | \& ... |
|
|
809 | \& struct ev_loop *loop = ev_default_init (0); |
|
|
810 | \& struct ev_io stdin_readable; |
|
|
811 | \& ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ); |
|
|
812 | \& ev_io_start (loop, &stdin_readable); |
|
|
813 | \& ev_loop (loop, 0); |
|
|
814 | .Ve |
690 | .ie n .Sh """ev_timer"" \- relative and optionally recurring timeouts" |
815 | .ie n .Sh """ev_timer"" \- relative and optionally recurring timeouts" |
691 | .el .Sh "\f(CWev_timer\fP \- relative and optionally recurring timeouts" |
816 | .el .Sh "\f(CWev_timer\fP \- relative and optionally recurring timeouts" |
692 | .IX Subsection "ev_timer - relative and optionally recurring timeouts" |
817 | .IX Subsection "ev_timer - relative and optionally recurring timeouts" |
693 | Timer watchers are simple relative timers that generate an event after a |
818 | Timer watchers are simple relative timers that generate an event after a |
694 | given time, and optionally repeating in regular intervals after that. |
819 | given time, and optionally repeating in regular intervals after that. |
… | |
… | |
744 | seconds of inactivity on the socket. The easiest way to do this is to |
869 | seconds of inactivity on the socket. The easiest way to do this is to |
745 | configure an \f(CW\*(C`ev_timer\*(C'\fR with after=repeat=60 and calling ev_timer_again each |
870 | configure an \f(CW\*(C`ev_timer\*(C'\fR with after=repeat=60 and calling ev_timer_again each |
746 | time you successfully read or write some data. If you go into an idle |
871 | time you successfully read or write some data. If you go into an idle |
747 | state where you do not expect data to travel on the socket, you can stop |
872 | state where you do not expect data to travel on the socket, you can stop |
748 | the timer, and again will automatically restart it if need be. |
873 | the timer, and again will automatically restart it if need be. |
|
|
874 | .PP |
|
|
875 | Example: create a timer that fires after 60 seconds. |
|
|
876 | .PP |
|
|
877 | .Vb 5 |
|
|
878 | \& static void |
|
|
879 | \& one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
|
|
880 | \& { |
|
|
881 | \& .. one minute over, w is actually stopped right here |
|
|
882 | \& } |
|
|
883 | .Ve |
|
|
884 | .PP |
|
|
885 | .Vb 3 |
|
|
886 | \& struct ev_timer mytimer; |
|
|
887 | \& ev_timer_init (&mytimer, one_minute_cb, 60., 0.); |
|
|
888 | \& ev_timer_start (loop, &mytimer); |
|
|
889 | .Ve |
|
|
890 | .PP |
|
|
891 | Example: create a timeout timer that times out after 10 seconds of |
|
|
892 | inactivity. |
|
|
893 | .PP |
|
|
894 | .Vb 5 |
|
|
895 | \& static void |
|
|
896 | \& timeout_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
|
|
897 | \& { |
|
|
898 | \& .. ten seconds without any activity |
|
|
899 | \& } |
|
|
900 | .Ve |
|
|
901 | .PP |
|
|
902 | .Vb 4 |
|
|
903 | \& struct ev_timer mytimer; |
|
|
904 | \& ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */ |
|
|
905 | \& ev_timer_again (&mytimer); /* start timer */ |
|
|
906 | \& ev_loop (loop, 0); |
|
|
907 | .Ve |
|
|
908 | .PP |
|
|
909 | .Vb 3 |
|
|
910 | \& // and in some piece of code that gets executed on any "activity": |
|
|
911 | \& // reset the timeout to start ticking again at 10 seconds |
|
|
912 | \& ev_timer_again (&mytimer); |
|
|
913 | .Ve |
749 | .ie n .Sh """ev_periodic"" \- to cron or not to cron" |
914 | .ie n .Sh """ev_periodic"" \- to cron or not to cron" |
750 | .el .Sh "\f(CWev_periodic\fP \- to cron or not to cron" |
915 | .el .Sh "\f(CWev_periodic\fP \- to cron or not to cron" |
751 | .IX Subsection "ev_periodic - to cron or not to cron" |
916 | .IX Subsection "ev_periodic - to cron or not to cron" |
752 | Periodic watchers are also timers of a kind, but they are very versatile |
917 | Periodic watchers are also timers of a kind, but they are very versatile |
753 | (and unfortunately a bit complex). |
918 | (and unfortunately a bit complex). |
… | |
… | |
845 | .IX Item "ev_periodic_again (loop, ev_periodic *)" |
1010 | .IX Item "ev_periodic_again (loop, ev_periodic *)" |
846 | Simply stops and restarts the periodic watcher again. This is only useful |
1011 | Simply stops and restarts the periodic watcher again. This is only useful |
847 | when you changed some parameters or the reschedule callback would return |
1012 | when you changed some parameters or the reschedule callback would return |
848 | a different time than the last time it was called (e.g. in a crond like |
1013 | a different time than the last time it was called (e.g. in a crond like |
849 | program when the crontabs have changed). |
1014 | program when the crontabs have changed). |
|
|
1015 | .PP |
|
|
1016 | Example: call a callback every hour, or, more precisely, whenever the |
|
|
1017 | system clock is divisible by 3600. The callback invocation times have |
|
|
1018 | potentially a lot of jittering, but good long-term stability. |
|
|
1019 | .PP |
|
|
1020 | .Vb 5 |
|
|
1021 | \& static void |
|
|
1022 | \& clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
|
|
1023 | \& { |
|
|
1024 | \& ... its now a full hour (UTC, or TAI or whatever your clock follows) |
|
|
1025 | \& } |
|
|
1026 | .Ve |
|
|
1027 | .PP |
|
|
1028 | .Vb 3 |
|
|
1029 | \& struct ev_periodic hourly_tick; |
|
|
1030 | \& ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0); |
|
|
1031 | \& ev_periodic_start (loop, &hourly_tick); |
|
|
1032 | .Ve |
|
|
1033 | .PP |
|
|
1034 | Example: the same as above, but use a reschedule callback to do it: |
|
|
1035 | .PP |
|
|
1036 | .Vb 1 |
|
|
1037 | \& #include <math.h> |
|
|
1038 | .Ve |
|
|
1039 | .PP |
|
|
1040 | .Vb 5 |
|
|
1041 | \& static ev_tstamp |
|
|
1042 | \& my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) |
|
|
1043 | \& { |
|
|
1044 | \& return fmod (now, 3600.) + 3600.; |
|
|
1045 | \& } |
|
|
1046 | .Ve |
|
|
1047 | .PP |
|
|
1048 | .Vb 1 |
|
|
1049 | \& ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); |
|
|
1050 | .Ve |
|
|
1051 | .PP |
|
|
1052 | Example: call a callback every hour, starting now: |
|
|
1053 | .PP |
|
|
1054 | .Vb 4 |
|
|
1055 | \& struct ev_periodic hourly_tick; |
|
|
1056 | \& ev_periodic_init (&hourly_tick, clock_cb, |
|
|
1057 | \& fmod (ev_now (loop), 3600.), 3600., 0); |
|
|
1058 | \& ev_periodic_start (loop, &hourly_tick); |
|
|
1059 | .Ve |
850 | .ie n .Sh """ev_signal"" \- signal me when a signal gets signalled" |
1060 | .ie n .Sh """ev_signal"" \- signal me when a signal gets signalled" |
851 | .el .Sh "\f(CWev_signal\fP \- signal me when a signal gets signalled" |
1061 | .el .Sh "\f(CWev_signal\fP \- signal me when a signal gets signalled" |
852 | .IX Subsection "ev_signal - signal me when a signal gets signalled" |
1062 | .IX Subsection "ev_signal - signal me when a signal gets signalled" |
853 | Signal watchers will trigger an event when the process receives a specific |
1063 | Signal watchers will trigger an event when the process receives a specific |
854 | signal one or more times. Even though signals are very asynchronous, libev |
1064 | signal one or more times. Even though signals are very asynchronous, libev |
… | |
… | |
884 | \&\fIany\fR process if \f(CW\*(C`pid\*(C'\fR is specified as \f(CW0\fR). The callback can look |
1094 | \&\fIany\fR process if \f(CW\*(C`pid\*(C'\fR is specified as \f(CW0\fR). The callback can look |
885 | at the \f(CW\*(C`rstatus\*(C'\fR member of the \f(CW\*(C`ev_child\*(C'\fR watcher structure to see |
1095 | at the \f(CW\*(C`rstatus\*(C'\fR member of the \f(CW\*(C`ev_child\*(C'\fR watcher structure to see |
886 | the status word (use the macros from \f(CW\*(C`sys/wait.h\*(C'\fR and see your systems |
1096 | the status word (use the macros from \f(CW\*(C`sys/wait.h\*(C'\fR and see your systems |
887 | \&\f(CW\*(C`waitpid\*(C'\fR documentation). The \f(CW\*(C`rpid\*(C'\fR member contains the pid of the |
1097 | \&\f(CW\*(C`waitpid\*(C'\fR documentation). The \f(CW\*(C`rpid\*(C'\fR member contains the pid of the |
888 | process causing the status change. |
1098 | process causing the status change. |
|
|
1099 | .PP |
|
|
1100 | Example: try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0. |
|
|
1101 | .PP |
|
|
1102 | .Vb 5 |
|
|
1103 | \& static void |
|
|
1104 | \& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
|
|
1105 | \& { |
|
|
1106 | \& ev_unloop (loop, EVUNLOOP_ALL); |
|
|
1107 | \& } |
|
|
1108 | .Ve |
|
|
1109 | .PP |
|
|
1110 | .Vb 3 |
|
|
1111 | \& struct ev_signal signal_watcher; |
|
|
1112 | \& ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
|
|
1113 | \& ev_signal_start (loop, &sigint_cb); |
|
|
1114 | .Ve |
889 | .ie n .Sh """ev_idle"" \- when you've got nothing better to do" |
1115 | .ie n .Sh """ev_idle"" \- when you've got nothing better to do" |
890 | .el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do" |
1116 | .el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do" |
891 | .IX Subsection "ev_idle - when you've got nothing better to do" |
1117 | .IX Subsection "ev_idle - when you've got nothing better to do" |
892 | Idle watchers trigger events when there are no other events are pending |
1118 | Idle watchers trigger events when there are no other events are pending |
893 | (prepare, check and other idle watchers do not count). That is, as long |
1119 | (prepare, check and other idle watchers do not count). That is, as long |
… | |
… | |
907 | .IP "ev_idle_init (ev_signal *, callback)" 4 |
1133 | .IP "ev_idle_init (ev_signal *, callback)" 4 |
908 | .IX Item "ev_idle_init (ev_signal *, callback)" |
1134 | .IX Item "ev_idle_init (ev_signal *, callback)" |
909 | Initialises and configures the idle watcher \- it has no parameters of any |
1135 | Initialises and configures the idle watcher \- it has no parameters of any |
910 | kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless, |
1136 | kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless, |
911 | believe me. |
1137 | believe me. |
|
|
1138 | .PP |
|
|
1139 | Example: dynamically allocate an \f(CW\*(C`ev_idle\*(C'\fR, start it, and in the |
|
|
1140 | callback, free it. Alos, use no error checking, as usual. |
|
|
1141 | .PP |
|
|
1142 | .Vb 7 |
|
|
1143 | \& static void |
|
|
1144 | \& idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) |
|
|
1145 | \& { |
|
|
1146 | \& free (w); |
|
|
1147 | \& // now do something you wanted to do when the program has |
|
|
1148 | \& // no longer asnything immediate to do. |
|
|
1149 | \& } |
|
|
1150 | .Ve |
|
|
1151 | .PP |
|
|
1152 | .Vb 3 |
|
|
1153 | \& struct ev_idle *idle_watcher = malloc (sizeof (struct ev_idle)); |
|
|
1154 | \& ev_idle_init (idle_watcher, idle_cb); |
|
|
1155 | \& ev_idle_start (loop, idle_cb); |
|
|
1156 | .Ve |
912 | .ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop" |
1157 | .ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop" |
913 | .el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop" |
1158 | .el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop" |
914 | .IX Subsection "ev_prepare and ev_check - customise your event loop" |
1159 | .IX Subsection "ev_prepare and ev_check - customise your event loop" |
915 | Prepare and check watchers are usually (but not always) used in tandem: |
1160 | Prepare and check watchers are usually (but not always) used in tandem: |
916 | prepare watchers get invoked before the process blocks and check watchers |
1161 | prepare watchers get invoked before the process blocks and check watchers |
… | |
… | |
944 | .IX Item "ev_check_init (ev_check *, callback)" |
1189 | .IX Item "ev_check_init (ev_check *, callback)" |
945 | .PD |
1190 | .PD |
946 | Initialises and configures the prepare or check watcher \- they have no |
1191 | Initialises and configures the prepare or check watcher \- they have no |
947 | parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR |
1192 | parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR |
948 | macros, but using them is utterly, utterly and completely pointless. |
1193 | macros, but using them is utterly, utterly and completely pointless. |
|
|
1194 | .PP |
|
|
1195 | Example: *TODO*. |
949 | .SH "OTHER FUNCTIONS" |
1196 | .SH "OTHER FUNCTIONS" |
950 | .IX Header "OTHER FUNCTIONS" |
1197 | .IX Header "OTHER FUNCTIONS" |
951 | There are some other functions of possible interest. Described. Here. Now. |
1198 | There are some other functions of possible interest. Described. Here. Now. |
952 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
1199 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
953 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
1200 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |