ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.pod
(Generate patch)

Comparing libev/ev.pod (file contents):
Revision 1.18 by root, Mon Nov 12 09:01:12 2007 UTC vs.
Revision 1.22 by root, Mon Nov 12 18:36:42 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines