… | |
… | |
1318 | |
1318 | |
1319 | =item callback ev_cb (ev_TYPE *watcher) |
1319 | =item callback ev_cb (ev_TYPE *watcher) |
1320 | |
1320 | |
1321 | Returns the callback currently set on the watcher. |
1321 | Returns the callback currently set on the watcher. |
1322 | |
1322 | |
1323 | =item ev_cb_set (ev_TYPE *watcher, callback) |
1323 | =item ev_set_cb (ev_TYPE *watcher, callback) |
1324 | |
1324 | |
1325 | Change the callback. You can change the callback at virtually any time |
1325 | Change the callback. You can change the callback at virtually any time |
1326 | (modulo threads). |
1326 | (modulo threads). |
1327 | |
1327 | |
1328 | =item ev_set_priority (ev_TYPE *watcher, int priority) |
1328 | =item ev_set_priority (ev_TYPE *watcher, int priority) |
… | |
… | |
2858 | |
2858 | |
2859 | This mode of operation can be useful together with an C<ev_check> watcher, |
2859 | This mode of operation can be useful together with an C<ev_check> watcher, |
2860 | to do something on each event loop iteration - for example to balance load |
2860 | to do something on each event loop iteration - for example to balance load |
2861 | between different connections. |
2861 | between different connections. |
2862 | |
2862 | |
2863 | See L<Abusing an C<ev_check> watcher for its side-effect> for a longer |
2863 | See L<< Abusing an C<ev_check> watcher for its side-effect >> for a longer |
2864 | example. |
2864 | example. |
2865 | |
2865 | |
2866 | =head3 Watcher-Specific Functions and Data Members |
2866 | =head3 Watcher-Specific Functions and Data Members |
2867 | |
2867 | |
2868 | =over 4 |
2868 | =over 4 |
… | |
… | |
2881 | callback, free it. Also, use no error checking, as usual. |
2881 | callback, free it. Also, use no error checking, as usual. |
2882 | |
2882 | |
2883 | static void |
2883 | static void |
2884 | idle_cb (struct ev_loop *loop, ev_idle *w, int revents) |
2884 | idle_cb (struct ev_loop *loop, ev_idle *w, int revents) |
2885 | { |
2885 | { |
|
|
2886 | // stop the watcher |
|
|
2887 | ev_idle_stop (loop, w); |
|
|
2888 | |
|
|
2889 | // now we can free it |
2886 | free (w); |
2890 | free (w); |
|
|
2891 | |
2887 | // now do something you wanted to do when the program has |
2892 | // now do something you wanted to do when the program has |
2888 | // no longer anything immediate to do. |
2893 | // no longer anything immediate to do. |
2889 | } |
2894 | } |
2890 | |
2895 | |
2891 | ev_idle *idle_watcher = malloc (sizeof (ev_idle)); |
2896 | ev_idle *idle_watcher = malloc (sizeof (ev_idle)); |
… | |
… | |
3870 | called): |
3875 | called): |
3871 | |
3876 | |
3872 | void |
3877 | void |
3873 | wait_for_event (ev_watcher *w) |
3878 | wait_for_event (ev_watcher *w) |
3874 | { |
3879 | { |
3875 | ev_cb_set (w) = current_coro; |
3880 | ev_set_cb (w, current_coro); |
3876 | switch_to (libev_coro); |
3881 | switch_to (libev_coro); |
3877 | } |
3882 | } |
3878 | |
3883 | |
3879 | That basically suspends the coroutine inside C<wait_for_event> and |
3884 | That basically suspends the coroutine inside C<wait_for_event> and |
3880 | continues the libev coroutine, which, when appropriate, switches back to |
3885 | continues the libev coroutine, which, when appropriate, switches back to |