… | |
… | |
425 | required if you know what you are doing). |
425 | required if you know what you are doing). |
426 | |
426 | |
427 | You have to be careful with dup'ed file descriptors, though. Some backends |
427 | You have to be careful with dup'ed file descriptors, though. Some backends |
428 | (the linux epoll backend is a notable example) cannot handle dup'ed file |
428 | (the linux epoll backend is a notable example) cannot handle dup'ed file |
429 | descriptors correctly if you register interest in two or more fds pointing |
429 | descriptors correctly if you register interest in two or more fds pointing |
430 | to the same file/socket etc. description. |
430 | to the same file/socket etc. description (that is, they share the same |
|
|
431 | underlying "file open"). |
431 | |
432 | |
432 | If you must do this, then force the use of a known-to-be-good backend |
433 | If you must do this, then force the use of a known-to-be-good backend |
433 | (at the time of this writing, this includes only EVMETHOD_SELECT and |
434 | (at the time of this writing, this includes only EVMETHOD_SELECT and |
434 | EVMETHOD_POLL). |
435 | EVMETHOD_POLL). |
435 | |
436 | |
… | |
… | |
449 | |
450 | |
450 | Timer watchers are simple relative timers that generate an event after a |
451 | Timer watchers are simple relative timers that generate an event after a |
451 | given time, and optionally repeating in regular intervals after that. |
452 | given time, and optionally repeating in regular intervals after that. |
452 | |
453 | |
453 | The timers are based on real time, that is, if you register an event that |
454 | The timers are based on real time, that is, if you register an event that |
454 | times out after an hour and youreset your system clock to last years |
455 | times out after an hour and you reset your system clock to last years |
455 | time, it will still time out after (roughly) and hour. "Roughly" because |
456 | time, it will still time out after (roughly) and hour. "Roughly" because |
456 | detecting time jumps is hard, and soem inaccuracies are unavoidable (the |
457 | detecting time jumps is hard, and soem inaccuracies are unavoidable (the |
457 | monotonic clock option helps a lot here). |
458 | monotonic clock option helps a lot here). |
458 | |
459 | |
459 | The relative timeouts are calculated relative to the C<ev_now ()> |
460 | The relative timeouts are calculated relative to the C<ev_now ()> |
460 | time. This is usually the right thing as this timestamp refers to the time |
461 | time. This is usually the right thing as this timestamp refers to the time |
461 | of the event triggering whatever timeout you are modifying/starting. If |
462 | of the event triggering whatever timeout you are modifying/starting. If |
462 | you suspect event processing to be delayed and you *need* to base the timeout |
463 | you suspect event processing to be delayed and you *need* to base the timeout |
463 | ion the current time, use something like this to adjust for this: |
464 | on the current time, use something like this to adjust for this: |
464 | |
465 | |
465 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
466 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
466 | |
467 | |
467 | =over 4 |
468 | =over 4 |
468 | |
469 | |
… | |
… | |
476 | later, again, and again, until stopped manually. |
477 | later, again, and again, until stopped manually. |
477 | |
478 | |
478 | The timer itself will do a best-effort at avoiding drift, that is, if you |
479 | The timer itself will do a best-effort at avoiding drift, that is, if you |
479 | configure a timer to trigger every 10 seconds, then it will trigger at |
480 | configure a timer to trigger every 10 seconds, then it will trigger at |
480 | exactly 10 second intervals. If, however, your program cannot keep up with |
481 | exactly 10 second intervals. If, however, your program cannot keep up with |
481 | the timer (ecause it takes longer than those 10 seconds to do stuff) the |
482 | the timer (because it takes longer than those 10 seconds to do stuff) the |
482 | timer will not fire more than once per event loop iteration. |
483 | timer will not fire more than once per event loop iteration. |
483 | |
484 | |
484 | =item ev_timer_again (loop) |
485 | =item ev_timer_again (loop) |
485 | |
486 | |
486 | This will act as if the timer timed out and restart it again if it is |
487 | This will act as if the timer timed out and restart it again if it is |
… | |
… | |
677 | =back |
678 | =back |
678 | |
679 | |
679 | =head2 C<ev_prepare> and C<ev_check> - customise your event loop |
680 | =head2 C<ev_prepare> and C<ev_check> - customise your event loop |
680 | |
681 | |
681 | Prepare and check watchers are usually (but not always) used in tandem: |
682 | Prepare and check watchers are usually (but not always) used in tandem: |
682 | Prepare watchers get invoked before the process blocks and check watchers |
683 | prepare watchers get invoked before the process blocks and check watchers |
683 | afterwards. |
684 | afterwards. |
684 | |
685 | |
685 | Their main purpose is to integrate other event mechanisms into libev. This |
686 | Their main purpose is to integrate other event mechanisms into libev. This |
686 | could be used, for example, to track variable changes, implement your own |
687 | could be used, for example, to track variable changes, implement your own |
687 | watchers, integrate net-snmp or a coroutine library and lots more. |
688 | watchers, integrate net-snmp or a coroutine library and lots more. |
… | |
… | |
690 | to be watched by the other library, registering C<ev_io> watchers for |
691 | to be watched by the other library, registering C<ev_io> watchers for |
691 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
692 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
692 | provide just this functionality). Then, in the check watcher you check for |
693 | provide just this functionality). Then, in the check watcher you check for |
693 | any events that occured (by checking the pending status of all watchers |
694 | any events that occured (by checking the pending status of all watchers |
694 | and stopping them) and call back into the library. The I/O and timer |
695 | and stopping them) and call back into the library. The I/O and timer |
695 | callbacks will never actually be called (but must be valid neverthelles, |
696 | callbacks will never actually be called (but must be valid nevertheless, |
696 | because you never know, you know?). |
697 | because you never know, you know?). |
697 | |
698 | |
698 | As another example, the Perl Coro module uses these hooks to integrate |
699 | As another example, the Perl Coro module uses these hooks to integrate |
699 | coroutines into libev programs, by yielding to other active coroutines |
700 | coroutines into libev programs, by yielding to other active coroutines |
700 | during each prepare and only letting the process block if no coroutines |
701 | during each prepare and only letting the process block if no coroutines |
701 | are ready to run (its actually more complicated, it only runs coroutines |
702 | are ready to run (it's actually more complicated: it only runs coroutines |
702 | with priority higher than the event loop and one lower priority once, |
703 | with priority higher than or equal to the event loop and one coroutine |
703 | using idle watchers to keep the event loop from blocking if lower-priority |
704 | of lower priority, but only once, using idle watchers to keep the event |
704 | coroutines exist, thus mapping low-priority coroutines to idle/background |
705 | loop from blocking if lower-priority coroutines are active, thus mapping |
705 | tasks). |
706 | low-priority coroutines to idle/background tasks). |
706 | |
707 | |
707 | =over 4 |
708 | =over 4 |
708 | |
709 | |
709 | =item ev_prepare_init (ev_prepare *, callback) |
710 | =item ev_prepare_init (ev_prepare *, callback) |
710 | |
711 | |
… | |
… | |
725 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
726 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
726 | |
727 | |
727 | This function combines a simple timer and an I/O watcher, calls your |
728 | This function combines a simple timer and an I/O watcher, calls your |
728 | callback on whichever event happens first and automatically stop both |
729 | callback on whichever event happens first and automatically stop both |
729 | watchers. This is useful if you want to wait for a single event on an fd |
730 | watchers. This is useful if you want to wait for a single event on an fd |
730 | or timeout without havign to allocate/configure/start/stop/free one or |
731 | or timeout without having to allocate/configure/start/stop/free one or |
731 | more watchers yourself. |
732 | more watchers yourself. |
732 | |
733 | |
733 | If C<fd> is less than 0, then no I/O watcher will be started and events |
734 | If C<fd> is less than 0, then no I/O watcher will be started and events |
734 | is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and |
735 | is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and |
735 | C<events> set will be craeted and started. |
736 | C<events> set will be craeted and started. |
… | |
… | |
738 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
739 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
739 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
740 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
740 | dubious value. |
741 | dubious value. |
741 | |
742 | |
742 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
743 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
743 | passed an events set like normal event callbacks (with a combination of |
744 | passed an C<revents> set like normal event callbacks (a combination of |
744 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
745 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
745 | value passed to C<ev_once>: |
746 | value passed to C<ev_once>: |
746 | |
747 | |
747 | static void stdin_ready (int revents, void *arg) |
748 | static void stdin_ready (int revents, void *arg) |
748 | { |
749 | { |
… | |
… | |
769 | |
770 | |
770 | Feed an event as if the given signal occured (loop must be the default loop!). |
771 | Feed an event as if the given signal occured (loop must be the default loop!). |
771 | |
772 | |
772 | =back |
773 | =back |
773 | |
774 | |
|
|
775 | =head1 LIBEVENT EMULATION |
|
|
776 | |
|
|
777 | TBD. |
|
|
778 | |
|
|
779 | =head1 C++ SUPPORT |
|
|
780 | |
|
|
781 | TBD. |
|
|
782 | |
774 | =head1 AUTHOR |
783 | =head1 AUTHOR |
775 | |
784 | |
776 | Marc Lehmann <libev@schmorp.de>. |
785 | Marc Lehmann <libev@schmorp.de>. |
777 | |
786 | |