… | |
… | |
105 | recommended for this platform. This set is often smaller than the one |
105 | recommended for this platform. This set is often smaller than the one |
106 | returned by C<ev_supported_backends>, as for example kqueue is broken on |
106 | returned by C<ev_supported_backends>, as for example kqueue is broken on |
107 | most BSDs and will not be autodetected unless you explicitly request it |
107 | most BSDs and will not be autodetected unless you explicitly request it |
108 | (assuming you know what you are doing). This is the set of backends that |
108 | (assuming you know what you are doing). This is the set of backends that |
109 | libev will probe for if you specify no backends explicitly. |
109 | libev will probe for if you specify no backends explicitly. |
|
|
110 | |
|
|
111 | =item unsigned int ev_embeddable_backends () |
|
|
112 | |
|
|
113 | Returns the set of backends that are embeddable in other event loops. This |
|
|
114 | is the theoretical, all-platform, value. To find which backends |
|
|
115 | might be supported on the current system, you would need to look at |
|
|
116 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
|
|
117 | recommended ones. |
|
|
118 | |
|
|
119 | See the description of C<ev_embed> watchers for more info. |
110 | |
120 | |
111 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
121 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
112 | |
122 | |
113 | Sets the allocation function to use (the prototype is similar to the |
123 | Sets the allocation function to use (the prototype is similar to the |
114 | realloc C function, the semantics are identical). It is used to allocate |
124 | realloc C function, the semantics are identical). It is used to allocate |
… | |
… | |
495 | *) >>), and you can stop watching for events at any time by calling the |
505 | *) >>), and you can stop watching for events at any time by calling the |
496 | corresponding stop function (C<< ev_<type>_stop (loop, watcher *) >>. |
506 | corresponding stop function (C<< ev_<type>_stop (loop, watcher *) >>. |
497 | |
507 | |
498 | As long as your watcher is active (has been started but not stopped) you |
508 | As long as your watcher is active (has been started but not stopped) you |
499 | must not touch the values stored in it. Most specifically you must never |
509 | must not touch the values stored in it. Most specifically you must never |
500 | reinitialise it or call its set macro. |
510 | reinitialise it or call its C<set> macro. |
501 | |
|
|
502 | You can check whether an event is active by calling the C<ev_is_active |
|
|
503 | (watcher *)> macro. To see whether an event is outstanding (but the |
|
|
504 | callback for it has not been called yet) you can use the C<ev_is_pending |
|
|
505 | (watcher *)> macro. |
|
|
506 | |
511 | |
507 | Each and every callback receives the event loop pointer as first, the |
512 | Each and every callback receives the event loop pointer as first, the |
508 | registered watcher structure as second, and a bitset of received events as |
513 | registered watcher structure as second, and a bitset of received events as |
509 | third argument. |
514 | third argument. |
510 | |
515 | |
… | |
… | |
566 | your callbacks is well-written it can just attempt the operation and cope |
571 | your callbacks is well-written it can just attempt the operation and cope |
567 | with the error from read() or write(). This will not work in multithreaded |
572 | with the error from read() or write(). This will not work in multithreaded |
568 | programs, though, so beware. |
573 | programs, though, so beware. |
569 | |
574 | |
570 | =back |
575 | =back |
|
|
576 | |
|
|
577 | =head2 SUMMARY OF GENERIC WATCHER FUNCTIONS |
|
|
578 | |
|
|
579 | In the following description, C<TYPE> stands for the watcher type, |
|
|
580 | e.g. C<timer> for C<ev_timer> watchers and C<io> for C<ev_io> watchers. |
|
|
581 | |
|
|
582 | =over 4 |
|
|
583 | |
|
|
584 | =item C<ev_init> (ev_TYPE *watcher, callback) |
|
|
585 | |
|
|
586 | This macro initialises the generic portion of a watcher. The contents |
|
|
587 | of the watcher object can be arbitrary (so C<malloc> will do). Only |
|
|
588 | the generic parts of the watcher are initialised, you I<need> to call |
|
|
589 | the type-specific C<ev_TYPE_set> macro afterwards to initialise the |
|
|
590 | type-specific parts. For each type there is also a C<ev_TYPE_init> macro |
|
|
591 | which rolls both calls into one. |
|
|
592 | |
|
|
593 | You can reinitialise a watcher at any time as long as it has been stopped |
|
|
594 | (or never started) and there are no pending events outstanding. |
|
|
595 | |
|
|
596 | The callbakc is always of type C<void (*)(ev_loop *loop, ev_TYPE *watcher, |
|
|
597 | int revents)>. |
|
|
598 | |
|
|
599 | =item C<ev_TYPE_set> (ev_TYPE *, [args]) |
|
|
600 | |
|
|
601 | This macro initialises the type-specific parts of a watcher. You need to |
|
|
602 | call C<ev_init> at least once before you call this macro, but you can |
|
|
603 | call C<ev_TYPE_set> any number of times. You must not, however, call this |
|
|
604 | macro on a watcher that is active (it can be pending, however, which is a |
|
|
605 | difference to the C<ev_init> macro). |
|
|
606 | |
|
|
607 | Although some watcher types do not have type-specific arguments |
|
|
608 | (e.g. C<ev_prepare>) you still need to call its C<set> macro. |
|
|
609 | |
|
|
610 | =item C<ev_TYPE_init> (ev_TYPE *watcher, callback, [args]) |
|
|
611 | |
|
|
612 | This convinience macro rolls both C<ev_init> and C<ev_TYPE_set> macro |
|
|
613 | calls into a single call. This is the most convinient method to initialise |
|
|
614 | a watcher. The same limitations apply, of course. |
|
|
615 | |
|
|
616 | =item C<ev_TYPE_start> (loop *, ev_TYPE *watcher) |
|
|
617 | |
|
|
618 | Starts (activates) the given watcher. Only active watchers will receive |
|
|
619 | events. If the watcher is already active nothing will happen. |
|
|
620 | |
|
|
621 | =item C<ev_TYPE_stop> (loop *, ev_TYPE *watcher) |
|
|
622 | |
|
|
623 | Stops the given watcher again (if active) and clears the pending |
|
|
624 | status. It is possible that stopped watchers are pending (for example, |
|
|
625 | non-repeating timers are being stopped when they become pending), but |
|
|
626 | C<ev_TYPE_stop> ensures that the watcher is neither active nor pending. If |
|
|
627 | you want to free or reuse the memory used by the watcher it is therefore a |
|
|
628 | good idea to always call its C<ev_TYPE_stop> function. |
|
|
629 | |
|
|
630 | =item bool ev_is_active (ev_TYPE *watcher) |
|
|
631 | |
|
|
632 | Returns a true value iff the watcher is active (i.e. it has been started |
|
|
633 | and not yet been stopped). As long as a watcher is active you must not modify |
|
|
634 | it. |
|
|
635 | |
|
|
636 | =item bool ev_is_pending (ev_TYPE *watcher) |
|
|
637 | |
|
|
638 | Returns a true value iff the watcher is pending, (i.e. it has outstanding |
|
|
639 | events but its callback has not yet been invoked). As long as a watcher |
|
|
640 | is pending (but not active) you must not call an init function on it (but |
|
|
641 | C<ev_TYPE_set> is safe) and you must make sure the watcher is available to |
|
|
642 | libev (e.g. you cnanot C<free ()> it). |
|
|
643 | |
|
|
644 | =item callback = ev_cb (ev_TYPE *watcher) |
|
|
645 | |
|
|
646 | Returns the callback currently set on the watcher. |
|
|
647 | |
|
|
648 | =item ev_cb_set (ev_TYPE *watcher, callback) |
|
|
649 | |
|
|
650 | Change the callback. You can change the callback at virtually any time |
|
|
651 | (modulo threads). |
|
|
652 | |
|
|
653 | =back |
|
|
654 | |
571 | |
655 | |
572 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
656 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
573 | |
657 | |
574 | Each watcher has, by default, a member C<void *data> that you can change |
658 | Each watcher has, by default, a member C<void *data> that you can change |
575 | and read at any time, libev will completely ignore it. This can be used |
659 | and read at any time, libev will completely ignore it. This can be used |
… | |
… | |
923 | Configures the watcher to trigger on the given signal number (usually one |
1007 | Configures the watcher to trigger on the given signal number (usually one |
924 | of the C<SIGxxx> constants). |
1008 | of the C<SIGxxx> constants). |
925 | |
1009 | |
926 | =back |
1010 | =back |
927 | |
1011 | |
|
|
1012 | |
928 | =head2 C<ev_child> - wait for pid status changes |
1013 | =head2 C<ev_child> - wait for pid status changes |
929 | |
1014 | |
930 | Child watchers trigger when your process receives a SIGCHLD in response to |
1015 | Child watchers trigger when your process receives a SIGCHLD in response to |
931 | some child status changes (most typically when a child of yours dies). |
1016 | some child status changes (most typically when a child of yours dies). |
932 | |
1017 | |
… | |
… | |
1006 | |
1091 | |
1007 | Prepare and check watchers are usually (but not always) used in tandem: |
1092 | Prepare and check watchers are usually (but not always) used in tandem: |
1008 | prepare watchers get invoked before the process blocks and check watchers |
1093 | prepare watchers get invoked before the process blocks and check watchers |
1009 | afterwards. |
1094 | afterwards. |
1010 | |
1095 | |
1011 | Their main purpose is to integrate other event mechanisms into libev. This |
1096 | Their main purpose is to integrate other event mechanisms into libev and |
1012 | could be used, for example, to track variable changes, implement your own |
1097 | their use is somewhat advanced. This could be used, for example, to track |
1013 | watchers, integrate net-snmp or a coroutine library and lots more. |
1098 | variable changes, implement your own watchers, integrate net-snmp or a |
|
|
1099 | coroutine library and lots more. |
1014 | |
1100 | |
1015 | This is done by examining in each prepare call which file descriptors need |
1101 | This is done by examining in each prepare call which file descriptors need |
1016 | to be watched by the other library, registering C<ev_io> watchers for |
1102 | to be watched by the other library, registering C<ev_io> watchers for |
1017 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
1103 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
1018 | provide just this functionality). Then, in the check watcher you check for |
1104 | provide just this functionality). Then, in the check watcher you check for |
… | |
… | |
1043 | =back |
1129 | =back |
1044 | |
1130 | |
1045 | Example: *TODO*. |
1131 | Example: *TODO*. |
1046 | |
1132 | |
1047 | |
1133 | |
|
|
1134 | =head2 C<ev_embed> - when one backend isn't enough |
|
|
1135 | |
|
|
1136 | This is a rather advanced watcher type that lets you embed one event loop |
|
|
1137 | into another (currently only C<ev_io> events are supported in the embedded |
|
|
1138 | loop, other types of watchers might be handled in a delayed or incorrect |
|
|
1139 | fashion and must not be used). |
|
|
1140 | |
|
|
1141 | There are primarily two reasons you would want that: work around bugs and |
|
|
1142 | prioritise I/O. |
|
|
1143 | |
|
|
1144 | As an example for a bug workaround, the kqueue backend might only support |
|
|
1145 | sockets on some platform, so it is unusable as generic backend, but you |
|
|
1146 | still want to make use of it because you have many sockets and it scales |
|
|
1147 | so nicely. In this case, you would create a kqueue-based loop and embed it |
|
|
1148 | into your default loop (which might use e.g. poll). Overall operation will |
|
|
1149 | be a bit slower because first libev has to poll and then call kevent, but |
|
|
1150 | at least you can use both at what they are best. |
|
|
1151 | |
|
|
1152 | As for prioritising I/O: rarely you have the case where some fds have |
|
|
1153 | to be watched and handled very quickly (with low latency), and even |
|
|
1154 | priorities and idle watchers might have too much overhead. In this case |
|
|
1155 | you would put all the high priority stuff in one loop and all the rest in |
|
|
1156 | a second one, and embed the second one in the first. |
|
|
1157 | |
|
|
1158 | As long as the watcher is active, the callback will be invoked every time |
|
|
1159 | there might be events pending in the embedded loop. The callback must then |
|
|
1160 | call C<ev_embed_sweep (mainloop, watcher)> to make a single sweep and invoke |
|
|
1161 | their callbacks (you could also start an idle watcher to give the embedded |
|
|
1162 | loop strictly lower priority for example). You can also set the callback |
|
|
1163 | to C<0>, in which case the embed watcher will automatically execute the |
|
|
1164 | embedded loop sweep. |
|
|
1165 | |
|
|
1166 | As long as the watcher is started it will automatically handle events. The |
|
|
1167 | callback will be invoked whenever some events have been handled. You can |
|
|
1168 | set the callback to C<0> to avoid having to specify one if you are not |
|
|
1169 | interested in that. |
|
|
1170 | |
|
|
1171 | Also, there have not currently been made special provisions for forking: |
|
|
1172 | when you fork, you not only have to call C<ev_loop_fork> on both loops, |
|
|
1173 | but you will also have to stop and restart any C<ev_embed> watchers |
|
|
1174 | yourself. |
|
|
1175 | |
|
|
1176 | Unfortunately, not all backends are embeddable, only the ones returned by |
|
|
1177 | C<ev_embeddable_backends> are, which, unfortunately, does not include any |
|
|
1178 | portable one. |
|
|
1179 | |
|
|
1180 | So when you want to use this feature you will always have to be prepared |
|
|
1181 | that you cannot get an embeddable loop. The recommended way to get around |
|
|
1182 | this is to have a separate variables for your embeddable loop, try to |
|
|
1183 | create it, and if that fails, use the normal loop for everything: |
|
|
1184 | |
|
|
1185 | struct ev_loop *loop_hi = ev_default_init (0); |
|
|
1186 | struct ev_loop *loop_lo = 0; |
|
|
1187 | struct ev_embed embed; |
|
|
1188 | |
|
|
1189 | // see if there is a chance of getting one that works |
|
|
1190 | // (remember that a flags value of 0 means autodetection) |
|
|
1191 | loop_lo = ev_embeddable_backends () & ev_recommended_backends () |
|
|
1192 | ? ev_loop_new (ev_embeddable_backends () & ev_recommended_backends ()) |
|
|
1193 | : 0; |
|
|
1194 | |
|
|
1195 | // if we got one, then embed it, otherwise default to loop_hi |
|
|
1196 | if (loop_lo) |
|
|
1197 | { |
|
|
1198 | ev_embed_init (&embed, 0, loop_lo); |
|
|
1199 | ev_embed_start (loop_hi, &embed); |
|
|
1200 | } |
|
|
1201 | else |
|
|
1202 | loop_lo = loop_hi; |
|
|
1203 | |
|
|
1204 | =over 4 |
|
|
1205 | |
|
|
1206 | =item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) |
|
|
1207 | |
|
|
1208 | =item ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop) |
|
|
1209 | |
|
|
1210 | Configures the watcher to embed the given loop, which must be |
|
|
1211 | embeddable. If the callback is C<0>, then C<ev_embed_sweep> will be |
|
|
1212 | invoked automatically, otherwise it is the responsibility of the callback |
|
|
1213 | to invoke it (it will continue to be called until the sweep has been done, |
|
|
1214 | if you do not want thta, you need to temporarily stop the embed watcher). |
|
|
1215 | |
|
|
1216 | =item ev_embed_sweep (loop, ev_embed *) |
|
|
1217 | |
|
|
1218 | Make a single, non-blocking sweep over the embedded loop. This works |
|
|
1219 | similarly to C<ev_loop (embedded_loop, EVLOOP_NONBLOCK)>, but in the most |
|
|
1220 | apropriate way for embedded loops. |
|
|
1221 | |
|
|
1222 | =back |
|
|
1223 | |
|
|
1224 | |
1048 | =head1 OTHER FUNCTIONS |
1225 | =head1 OTHER FUNCTIONS |
1049 | |
1226 | |
1050 | There are some other functions of possible interest. Described. Here. Now. |
1227 | There are some other functions of possible interest. Described. Here. Now. |
1051 | |
1228 | |
1052 | =over 4 |
1229 | =over 4 |
… | |
… | |
1081 | /* stdin might have data for us, joy! */; |
1258 | /* stdin might have data for us, joy! */; |
1082 | } |
1259 | } |
1083 | |
1260 | |
1084 | ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
1261 | ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
1085 | |
1262 | |
1086 | =item ev_feed_event (loop, watcher, int events) |
1263 | =item ev_feed_event (ev_loop *, watcher *, int revents) |
1087 | |
1264 | |
1088 | Feeds the given event set into the event loop, as if the specified event |
1265 | Feeds the given event set into the event loop, as if the specified event |
1089 | had happened for the specified watcher (which must be a pointer to an |
1266 | had happened for the specified watcher (which must be a pointer to an |
1090 | initialised but not necessarily started event watcher). |
1267 | initialised but not necessarily started event watcher). |
1091 | |
1268 | |
1092 | =item ev_feed_fd_event (loop, int fd, int revents) |
1269 | =item ev_feed_fd_event (ev_loop *, int fd, int revents) |
1093 | |
1270 | |
1094 | Feed an event on the given fd, as if a file descriptor backend detected |
1271 | Feed an event on the given fd, as if a file descriptor backend detected |
1095 | the given events it. |
1272 | the given events it. |
1096 | |
1273 | |
1097 | =item ev_feed_signal_event (loop, int signum) |
1274 | =item ev_feed_signal_event (ev_loop *loop, int signum) |
1098 | |
1275 | |
1099 | Feed an event as if the given signal occured (loop must be the default loop!). |
1276 | Feed an event as if the given signal occured (C<loop> must be the default |
|
|
1277 | loop!). |
1100 | |
1278 | |
1101 | =back |
1279 | =back |
1102 | |
1280 | |
1103 | |
1281 | |
1104 | =head1 LIBEVENT EMULATION |
1282 | =head1 LIBEVENT EMULATION |