… | |
… | |
39 | F<README.embed> in the libev distribution. If libev was configured without |
39 | F<README.embed> in the libev distribution. If libev was configured without |
40 | support for multiple event loops, then all functions taking an initial |
40 | support for multiple event loops, then all functions taking an initial |
41 | argument of name C<loop> (which is always of type C<struct ev_loop *>) |
41 | argument of name C<loop> (which is always of type C<struct ev_loop *>) |
42 | will not have this argument. |
42 | will not have this argument. |
43 | |
43 | |
44 | =head1 TIME AND OTHER GLOBAL FUNCTIONS |
44 | =head1 TIME REPRESENTATION |
45 | |
45 | |
46 | Libev represents time as a single floating point number, representing the |
46 | Libev represents time as a single floating point number, representing the |
47 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
47 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
48 | the beginning of 1970, details are complicated, don't ask). This type is |
48 | the beginning of 1970, details are complicated, don't ask). This type is |
49 | called C<ev_tstamp>, which is what you should use too. It usually aliases |
49 | called C<ev_tstamp>, which is what you should use too. It usually aliases |
50 | to the double type in C. |
50 | to the double type in C. |
|
|
51 | |
|
|
52 | =head1 GLOBAL FUNCTIONS |
51 | |
53 | |
52 | =over 4 |
54 | =over 4 |
53 | |
55 | |
54 | =item ev_tstamp ev_time () |
56 | =item ev_tstamp ev_time () |
55 | |
57 | |
… | |
… | |
99 | An event loop is described by a C<struct ev_loop *>. The library knows two |
101 | An event loop is described by a C<struct ev_loop *>. The library knows two |
100 | types of such loops, the I<default> loop, which supports signals and child |
102 | types of such loops, the I<default> loop, which supports signals and child |
101 | events, and dynamically created loops which do not. |
103 | events, and dynamically created loops which do not. |
102 | |
104 | |
103 | If you use threads, a common model is to run the default event loop |
105 | If you use threads, a common model is to run the default event loop |
104 | in your main thread (or in a separate thrad) and for each thread you |
106 | in your main thread (or in a separate thread) and for each thread you |
105 | create, you also create another event loop. Libev itself does no locking |
107 | create, you also create another event loop. Libev itself does no locking |
106 | whatsoever, so if you mix calls to the same event loop in different |
108 | whatsoever, so if you mix calls to the same event loop in different |
107 | threads, make sure you lock (this is usually a bad idea, though, even if |
109 | threads, make sure you lock (this is usually a bad idea, though, even if |
108 | done correctly, because it's hideous and inefficient). |
110 | done correctly, because it's hideous and inefficient). |
109 | |
111 | |
… | |
… | |
299 | |
301 | |
300 | As long as your watcher is active (has been started but not stopped) you |
302 | As long as your watcher is active (has been started but not stopped) you |
301 | must not touch the values stored in it. Most specifically you must never |
303 | must not touch the values stored in it. Most specifically you must never |
302 | reinitialise it or call its set method. |
304 | reinitialise it or call its set method. |
303 | |
305 | |
304 | You cna check whether an event is active by calling the C<ev_is_active |
306 | You can check whether an event is active by calling the C<ev_is_active |
305 | (watcher *)> macro. To see whether an event is outstanding (but the |
307 | (watcher *)> macro. To see whether an event is outstanding (but the |
306 | callback for it has not been called yet) you cna use the C<ev_is_pending |
308 | callback for it has not been called yet) you can use the C<ev_is_pending |
307 | (watcher *)> macro. |
309 | (watcher *)> macro. |
308 | |
310 | |
309 | Each and every callback receives the event loop pointer as first, the |
311 | Each and every callback receives the event loop pointer as first, the |
310 | registered watcher structure as second, and a bitset of received events as |
312 | registered watcher structure as second, and a bitset of received events as |
311 | third argument. |
313 | third argument. |
312 | |
314 | |
313 | The rceeived events usually include a single bit per event type received |
315 | The received events usually include a single bit per event type received |
314 | (you can receive multiple events at the same time). The possible bit masks |
316 | (you can receive multiple events at the same time). The possible bit masks |
315 | are: |
317 | are: |
316 | |
318 | |
317 | =over 4 |
319 | =over 4 |
318 | |
320 | |
… | |
… | |
372 | =back |
374 | =back |
373 | |
375 | |
374 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
376 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
375 | |
377 | |
376 | Each watcher has, by default, a member C<void *data> that you can change |
378 | Each watcher has, by default, a member C<void *data> that you can change |
377 | and read at any time, libev will completely ignore it. This cna be used |
379 | and read at any time, libev will completely ignore it. This can be used |
378 | to associate arbitrary data with your watcher. If you need more data and |
380 | to associate arbitrary data with your watcher. If you need more data and |
379 | don't want to allocate memory and store a pointer to it in that data |
381 | don't want to allocate memory and store a pointer to it in that data |
380 | member, you can also "subclass" the watcher type and provide your own |
382 | member, you can also "subclass" the watcher type and provide your own |
381 | data: |
383 | data: |
382 | |
384 | |
… | |
… | |
409 | =head2 C<ev_io> - is this file descriptor readable or writable |
411 | =head2 C<ev_io> - is this file descriptor readable or writable |
410 | |
412 | |
411 | I/O watchers check whether a file descriptor is readable or writable |
413 | I/O watchers check whether a file descriptor is readable or writable |
412 | in each iteration of the event loop (This behaviour is called |
414 | in each iteration of the event loop (This behaviour is called |
413 | level-triggering because you keep receiving events as long as the |
415 | level-triggering because you keep receiving events as long as the |
414 | condition persists. Remember you cna stop the watcher if you don't want to |
416 | condition persists. Remember you can stop the watcher if you don't want to |
415 | act on the event and neither want to receive future events). |
417 | act on the event and neither want to receive future events). |
416 | |
418 | |
417 | In general you can register as many read and/or write event watchers oer |
419 | In general you can register as many read and/or write event watchers oer |
418 | fd as you want (as long as you don't confuse yourself). Setting all file |
420 | fd as you want (as long as you don't confuse yourself). Setting all file |
419 | descriptors to non-blocking mode is also usually a good idea (but not |
421 | descriptors to non-blocking mode is also usually a good idea (but not |
… | |
… | |
495 | state where you do not expect data to travel on the socket, you can stop |
497 | state where you do not expect data to travel on the socket, you can stop |
496 | the timer, and again will automatically restart it if need be. |
498 | the timer, and again will automatically restart it if need be. |
497 | |
499 | |
498 | =back |
500 | =back |
499 | |
501 | |
500 | =head2 C<ev_periodic> - to cron or not to cron it |
502 | =head2 C<ev_periodic> - to cron or not to cron |
501 | |
503 | |
502 | Periodic watchers are also timers of a kind, but they are very versatile |
504 | Periodic watchers are also timers of a kind, but they are very versatile |
503 | (and unfortunately a bit complex). |
505 | (and unfortunately a bit complex). |
504 | |
506 | |
505 | Unlike C<ev_timer>'s, they are not based on real time (or relative time) |
507 | Unlike C<ev_timer>'s, they are not based on real time (or relative time) |
… | |
… | |
601 | Signal watchers will trigger an event when the process receives a specific |
603 | Signal watchers will trigger an event when the process receives a specific |
602 | signal one or more times. Even though signals are very asynchronous, libev |
604 | signal one or more times. Even though signals are very asynchronous, libev |
603 | will try it's best to deliver signals synchronously, i.e. as part of the |
605 | will try it's best to deliver signals synchronously, i.e. as part of the |
604 | normal event processing, like any other event. |
606 | normal event processing, like any other event. |
605 | |
607 | |
606 | You cna configure as many watchers as you like per signal. Only when the |
608 | You can configure as many watchers as you like per signal. Only when the |
607 | first watcher gets started will libev actually register a signal watcher |
609 | first watcher gets started will libev actually register a signal watcher |
608 | with the kernel (thus it coexists with your own signal handlers as long |
610 | with the kernel (thus it coexists with your own signal handlers as long |
609 | as you don't register any with libev). Similarly, when the last signal |
611 | as you don't register any with libev). Similarly, when the last signal |
610 | watcher for a signal is stopped libev will reset the signal handler to |
612 | watcher for a signal is stopped libev will reset the signal handler to |
611 | SIG_DFL (regardless of what it was set to before). |
613 | SIG_DFL (regardless of what it was set to before). |
… | |
… | |
633 | =item ev_child_set (ev_child *, int pid) |
635 | =item ev_child_set (ev_child *, int pid) |
634 | |
636 | |
635 | Configures the watcher to wait for status changes of process C<pid> (or |
637 | Configures the watcher to wait for status changes of process C<pid> (or |
636 | I<any> process if C<pid> is specified as C<0>). The callback can look |
638 | I<any> process if C<pid> is specified as C<0>). The callback can look |
637 | at the C<rstatus> member of the C<ev_child> watcher structure to see |
639 | at the C<rstatus> member of the C<ev_child> watcher structure to see |
638 | the status word (use the macros from C<sys/wait.h>). The C<rpid> member |
640 | the status word (use the macros from C<sys/wait.h> and see your systems |
639 | contains the pid of the process causing the status change. |
641 | C<waitpid> documentation). The C<rpid> member contains the pid of the |
|
|
642 | process causing the status change. |
640 | |
643 | |
641 | =back |
644 | =back |
642 | |
645 | |
643 | =head2 C<ev_idle> - when you've got nothing better to do |
646 | =head2 C<ev_idle> - when you've got nothing better to do |
644 | |
647 | |
645 | Idle watchers trigger events when there are no other I/O or timer (or |
648 | Idle watchers trigger events when there are no other events are pending |
646 | periodic) events pending. That is, as long as your process is busy |
649 | (prepare, check and other idle watchers do not count). That is, as long |
647 | handling sockets or timeouts it will not be called. But when your process |
650 | as your process is busy handling sockets or timeouts (or even signals, |
648 | is idle all idle watchers are being called again and again - until |
651 | imagine) it will not be triggered. But when your process is idle all idle |
|
|
652 | watchers are being called again and again, once per event loop iteration - |
649 | stopped, that is, or your process receives more events. |
653 | until stopped, that is, or your process receives more events and becomes |
|
|
654 | busy. |
650 | |
655 | |
651 | The most noteworthy effect is that as long as any idle watchers are |
656 | The most noteworthy effect is that as long as any idle watchers are |
652 | active, the process will not block when waiting for new events. |
657 | active, the process will not block when waiting for new events. |
653 | |
658 | |
654 | Apart from keeping your process non-blocking (which is a useful |
659 | Apart from keeping your process non-blocking (which is a useful |
… | |
… | |
664 | kind. There is a C<ev_idle_set> macro, but using it is utterly pointless, |
669 | kind. There is a C<ev_idle_set> macro, but using it is utterly pointless, |
665 | believe me. |
670 | believe me. |
666 | |
671 | |
667 | =back |
672 | =back |
668 | |
673 | |
669 | =head2 prepare and check - your hooks into the event loop |
674 | =head2 C<ev_prepare> and C<ev_check> - customise your event loop |
670 | |
675 | |
671 | Prepare and check watchers usually (but not always) are used in |
676 | Prepare and check watchers are usually (but not always) used in tandem: |
672 | tandom. Prepare watchers get invoked before the process blocks and check |
677 | Prepare watchers get invoked before the process blocks and check watchers |
673 | watchers afterwards. |
678 | afterwards. |
674 | |
679 | |
675 | Their main purpose is to integrate other event mechanisms into libev. This |
680 | Their main purpose is to integrate other event mechanisms into libev. This |
676 | could be used, for example, to track variable changes, implement your own |
681 | could be used, for example, to track variable changes, implement your own |
677 | watchers, integrate net-snmp or a coroutine library and lots more. |
682 | watchers, integrate net-snmp or a coroutine library and lots more. |
678 | |
683 | |
679 | This is done by examining in each prepare call which file descriptors need |
684 | This is done by examining in each prepare call which file descriptors need |
680 | to be watched by the other library, registering C<ev_io> watchers for them |
685 | to be watched by the other library, registering C<ev_io> watchers for |
681 | and starting an C<ev_timer> watcher for any timeouts (many libraries provide |
686 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
682 | just this functionality). Then, in the check watcher you check for any |
687 | provide just this functionality). Then, in the check watcher you check for |
683 | events that occured (by making your callbacks set soem flags for example) |
688 | any events that occured (by checking the pending status of all watchers |
684 | and call back into the library. |
689 | and stopping them) and call back into the library. The I/O and timer |
|
|
690 | callbacks will never actually be called (but must be valid neverthelles, |
|
|
691 | because you never know, you know?). |
685 | |
692 | |
686 | As another example, the perl Coro module uses these hooks to integrate |
693 | As another example, the Perl Coro module uses these hooks to integrate |
687 | coroutines into libev programs, by yielding to other active coroutines |
694 | coroutines into libev programs, by yielding to other active coroutines |
688 | during each prepare and only letting the process block if no coroutines |
695 | during each prepare and only letting the process block if no coroutines |
689 | are ready to run. |
696 | are ready to run (its actually more complicated, it only runs coroutines |
|
|
697 | with priority higher than the event loop and one lower priority once, |
|
|
698 | using idle watchers to keep the event loop from blocking if lower-priority |
|
|
699 | coroutines exist, thus mapping low-priority coroutines to idle/background |
|
|
700 | tasks). |
690 | |
701 | |
691 | =over 4 |
702 | =over 4 |
692 | |
703 | |
693 | =item ev_prepare_init (ev_prepare *, callback) |
704 | =item ev_prepare_init (ev_prepare *, callback) |
694 | |
705 | |
695 | =item ev_check_init (ev_check *, callback) |
706 | =item ev_check_init (ev_check *, callback) |
696 | |
707 | |
697 | Initialises and configures the prepare or check watcher - they have no |
708 | Initialises and configures the prepare or check watcher - they have no |
698 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
709 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
699 | macros, but using them is utterly, utterly pointless. |
710 | macros, but using them is utterly, utterly and completely pointless. |
700 | |
711 | |
701 | =back |
712 | =back |
702 | |
713 | |
703 | =head1 OTHER FUNCTIONS |
714 | =head1 OTHER FUNCTIONS |
704 | |
715 | |
705 | There are some other fucntions of possible interest. Described. Here. Now. |
716 | There are some other functions of possible interest. Described. Here. Now. |
706 | |
717 | |
707 | =over 4 |
718 | =over 4 |
708 | |
719 | |
709 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
720 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
710 | |
721 | |
… | |
… | |
712 | callback on whichever event happens first and automatically stop both |
723 | callback on whichever event happens first and automatically stop both |
713 | watchers. This is useful if you want to wait for a single event on an fd |
724 | watchers. This is useful if you want to wait for a single event on an fd |
714 | or timeout without havign to allocate/configure/start/stop/free one or |
725 | or timeout without havign to allocate/configure/start/stop/free one or |
715 | more watchers yourself. |
726 | more watchers yourself. |
716 | |
727 | |
717 | If C<fd> is less than 0, then no I/O watcher will be started and events is |
728 | If C<fd> is less than 0, then no I/O watcher will be started and events |
718 | ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and C<events> set |
729 | is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and |
719 | will be craeted and started. |
730 | C<events> set will be craeted and started. |
720 | |
731 | |
721 | If C<timeout> is less than 0, then no timeout watcher will be |
732 | If C<timeout> is less than 0, then no timeout watcher will be |
722 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and repeat |
733 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
723 | = 0) will be started. |
734 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
|
|
735 | dubious value. |
724 | |
736 | |
725 | The callback has the type C<void (*cb)(int revents, void *arg)> and |
737 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
726 | gets passed an events set (normally a combination of C<EV_ERROR>, C<EV_READ>, |
738 | passed an events set like normal event callbacks (with a combination of |
727 | C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> value passed to C<ev_once>: |
739 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
|
|
740 | value passed to C<ev_once>: |
728 | |
741 | |
729 | static void stdin_ready (int revents, void *arg) |
742 | static void stdin_ready (int revents, void *arg) |
730 | { |
743 | { |
731 | if (revents & EV_TIMEOUT) |
744 | if (revents & EV_TIMEOUT) |
732 | /* doh, nothing entered */ |
745 | /* doh, nothing entered */; |
733 | else if (revents & EV_READ) |
746 | else if (revents & EV_READ) |
734 | /* stdin might have data for us, joy! */ |
747 | /* stdin might have data for us, joy! */; |
735 | } |
748 | } |
736 | |
749 | |
737 | ev_once (STDIN_FILENO, EV_READm 10., stdin_ready, 0); |
750 | ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
738 | |
751 | |
739 | =item ev_feed_event (loop, watcher, int events) |
752 | =item ev_feed_event (loop, watcher, int events) |
740 | |
753 | |
741 | Feeds the given event set into the event loop, as if the specified event |
754 | Feeds the given event set into the event loop, as if the specified event |
742 | has happened for the specified watcher (which must be a pointer to an |
755 | had happened for the specified watcher (which must be a pointer to an |
743 | initialised but not necessarily active event watcher). |
756 | initialised but not necessarily started event watcher). |
744 | |
757 | |
745 | =item ev_feed_fd_event (loop, int fd, int revents) |
758 | =item ev_feed_fd_event (loop, int fd, int revents) |
746 | |
759 | |
747 | Feed an event on the given fd, as if a file descriptor backend detected it. |
760 | Feed an event on the given fd, as if a file descriptor backend detected |
|
|
761 | the given events it. |
748 | |
762 | |
749 | =item ev_feed_signal_event (loop, int signum) |
763 | =item ev_feed_signal_event (loop, int signum) |
750 | |
764 | |
751 | Feed an event as if the given signal occured (loop must be the default loop!). |
765 | Feed an event as if the given signal occured (loop must be the default loop!). |
752 | |
766 | |