… | |
… | |
98 | Libev represents time as a single floating point number, representing the |
98 | Libev represents time as a single floating point number, representing the |
99 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
99 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
100 | the beginning of 1970, details are complicated, don't ask). This type is |
100 | the beginning of 1970, details are complicated, don't ask). This type is |
101 | called C<ev_tstamp>, which is what you should use too. It usually aliases |
101 | called C<ev_tstamp>, which is what you should use too. It usually aliases |
102 | to the C<double> type in C, and when you need to do any calculations on |
102 | to the C<double> type in C, and when you need to do any calculations on |
103 | it, you should treat it as such. |
103 | it, you should treat it as some floatingpoint value. Unlike the name |
|
|
104 | component C<stamp> might indicate, it is also used for time differences |
|
|
105 | throughout libev. |
104 | |
106 | |
105 | =head1 GLOBAL FUNCTIONS |
107 | =head1 GLOBAL FUNCTIONS |
106 | |
108 | |
107 | These functions can be called anytime, even before initialising the |
109 | These functions can be called anytime, even before initialising the |
108 | library in any way. |
110 | library in any way. |
… | |
… | |
117 | |
119 | |
118 | =item int ev_version_major () |
120 | =item int ev_version_major () |
119 | |
121 | |
120 | =item int ev_version_minor () |
122 | =item int ev_version_minor () |
121 | |
123 | |
122 | You can find out the major and minor version numbers of the library |
124 | You can find out the major and minor ABI version numbers of the library |
123 | you linked against by calling the functions C<ev_version_major> and |
125 | you linked against by calling the functions C<ev_version_major> and |
124 | C<ev_version_minor>. If you want, you can compare against the global |
126 | C<ev_version_minor>. If you want, you can compare against the global |
125 | symbols C<EV_VERSION_MAJOR> and C<EV_VERSION_MINOR>, which specify the |
127 | symbols C<EV_VERSION_MAJOR> and C<EV_VERSION_MINOR>, which specify the |
126 | version of the library your program was compiled against. |
128 | version of the library your program was compiled against. |
127 | |
129 | |
|
|
130 | These version numbers refer to the ABI version of the library, not the |
|
|
131 | release version. |
|
|
132 | |
128 | Usually, it's a good idea to terminate if the major versions mismatch, |
133 | Usually, it's a good idea to terminate if the major versions mismatch, |
129 | as this indicates an incompatible change. Minor versions are usually |
134 | as this indicates an incompatible change. Minor versions are usually |
130 | compatible to older versions, so a larger minor version alone is usually |
135 | compatible to older versions, so a larger minor version alone is usually |
131 | not a problem. |
136 | not a problem. |
132 | |
137 | |
133 | Example: Make sure we haven't accidentally been linked against the wrong |
138 | Example: Make sure we haven't accidentally been linked against the wrong |
134 | version. |
139 | version. |
… | |
… | |
399 | Destroys the default loop again (frees all memory and kernel state |
404 | Destroys the default loop again (frees all memory and kernel state |
400 | etc.). None of the active event watchers will be stopped in the normal |
405 | etc.). None of the active event watchers will be stopped in the normal |
401 | sense, so e.g. C<ev_is_active> might still return true. It is your |
406 | sense, so e.g. C<ev_is_active> might still return true. It is your |
402 | responsibility to either stop all watchers cleanly yoursef I<before> |
407 | responsibility to either stop all watchers cleanly yoursef I<before> |
403 | calling this function, or cope with the fact afterwards (which is usually |
408 | calling this function, or cope with the fact afterwards (which is usually |
404 | the easiest thing, youc na just ignore the watchers and/or C<free ()> them |
409 | the easiest thing, you can just ignore the watchers and/or C<free ()> them |
405 | for example). |
410 | for example). |
|
|
411 | |
|
|
412 | Note that certain global state, such as signal state, will not be freed by |
|
|
413 | this function, and related watchers (such as signal and child watchers) |
|
|
414 | would need to be stopped manually. |
|
|
415 | |
|
|
416 | In general it is not advisable to call this function except in the |
|
|
417 | rare occasion where you really need to free e.g. the signal handling |
|
|
418 | pipe fds. If you need dynamically allocated loops it is better to use |
|
|
419 | C<ev_loop_new> and C<ev_loop_destroy>). |
406 | |
420 | |
407 | =item ev_loop_destroy (loop) |
421 | =item ev_loop_destroy (loop) |
408 | |
422 | |
409 | Like C<ev_default_destroy>, but destroys an event loop created by an |
423 | Like C<ev_default_destroy>, but destroys an event loop created by an |
410 | earlier call to C<ev_loop_new>. |
424 | earlier call to C<ev_loop_new>. |
… | |
… | |
486 | libev watchers. However, a pair of C<ev_prepare>/C<ev_check> watchers is |
500 | libev watchers. However, a pair of C<ev_prepare>/C<ev_check> watchers is |
487 | usually a better approach for this kind of thing. |
501 | usually a better approach for this kind of thing. |
488 | |
502 | |
489 | Here are the gory details of what C<ev_loop> does: |
503 | Here are the gory details of what C<ev_loop> does: |
490 | |
504 | |
|
|
505 | - Before the first iteration, call any pending watchers. |
491 | * If there are no active watchers (reference count is zero), return. |
506 | * If there are no active watchers (reference count is zero), return. |
492 | - Queue prepare watchers and then call all outstanding watchers. |
507 | - Queue all prepare watchers and then call all outstanding watchers. |
493 | - If we have been forked, recreate the kernel state. |
508 | - If we have been forked, recreate the kernel state. |
494 | - Update the kernel state with all outstanding changes. |
509 | - Update the kernel state with all outstanding changes. |
495 | - Update the "event loop time". |
510 | - Update the "event loop time". |
496 | - Calculate for how long to block. |
511 | - Calculate for how long to block. |
497 | - Block the process, waiting for any events. |
512 | - Block the process, waiting for any events. |
… | |
… | |
777 | |
792 | |
778 | Setting a priority outside the range of C<EV_MINPRI> to C<EV_MAXPRI> is |
793 | Setting a priority outside the range of C<EV_MINPRI> to C<EV_MAXPRI> is |
779 | fine, as long as you do not mind that the priority value you query might |
794 | fine, as long as you do not mind that the priority value you query might |
780 | or might not have been adjusted to be within valid range. |
795 | or might not have been adjusted to be within valid range. |
781 | |
796 | |
|
|
797 | =item ev_invoke (loop, ev_TYPE *watcher, int revents) |
|
|
798 | |
|
|
799 | Invoke the C<watcher> with the given C<loop> and C<revents>. Neither |
|
|
800 | C<loop> nor C<revents> need to be valid as long as the watcher callback |
|
|
801 | can deal with that fact. |
|
|
802 | |
|
|
803 | =item int ev_clear_pending (loop, ev_TYPE *watcher) |
|
|
804 | |
|
|
805 | If the watcher is pending, this function returns clears its pending status |
|
|
806 | and returns its C<revents> bitset (as if its callback was invoked). If the |
|
|
807 | watcher isn't pending it does nothing and returns C<0>. |
|
|
808 | |
782 | =back |
809 | =back |
783 | |
810 | |
784 | |
811 | |
785 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
812 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
786 | |
813 | |
… | |
… | |
895 | play around with an Xlib connection), then you have to seperately re-test |
922 | play around with an Xlib connection), then you have to seperately re-test |
896 | whether a file descriptor is really ready with a known-to-be good interface |
923 | whether a file descriptor is really ready with a known-to-be good interface |
897 | such as poll (fortunately in our Xlib example, Xlib already does this on |
924 | such as poll (fortunately in our Xlib example, Xlib already does this on |
898 | its own, so its quite safe to use). |
925 | its own, so its quite safe to use). |
899 | |
926 | |
|
|
927 | =head3 The special problem of disappearing file descriptors |
|
|
928 | |
|
|
929 | Some backends (e.g kqueue, epoll) need to be told about closing a file |
|
|
930 | descriptor (either by calling C<close> explicitly or by any other means, |
|
|
931 | such as C<dup>). The reason is that you register interest in some file |
|
|
932 | descriptor, but when it goes away, the operating system will silently drop |
|
|
933 | this interest. If another file descriptor with the same number then is |
|
|
934 | registered with libev, there is no efficient way to see that this is, in |
|
|
935 | fact, a different file descriptor. |
|
|
936 | |
|
|
937 | To avoid having to explicitly tell libev about such cases, libev follows |
|
|
938 | the following policy: Each time C<ev_io_set> is being called, libev |
|
|
939 | will assume that this is potentially a new file descriptor, otherwise |
|
|
940 | it is assumed that the file descriptor stays the same. That means that |
|
|
941 | you I<have> to call C<ev_io_set> (or C<ev_io_init>) when you change the |
|
|
942 | descriptor even if the file descriptor number itself did not change. |
|
|
943 | |
|
|
944 | This is how one would do it normally anyway, the important point is that |
|
|
945 | the libev application should not optimise around libev but should leave |
|
|
946 | optimisations to libev. |
|
|
947 | |
|
|
948 | |
|
|
949 | =head3 Watcher-Specific Functions |
|
|
950 | |
900 | =over 4 |
951 | =over 4 |
901 | |
952 | |
902 | =item ev_io_init (ev_io *, callback, int fd, int events) |
953 | =item ev_io_init (ev_io *, callback, int fd, int events) |
903 | |
954 | |
904 | =item ev_io_set (ev_io *, int fd, int events) |
955 | =item ev_io_set (ev_io *, int fd, int events) |
… | |
… | |
956 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
1007 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
957 | |
1008 | |
958 | The callback is guarenteed to be invoked only when its timeout has passed, |
1009 | The callback is guarenteed to be invoked only when its timeout has passed, |
959 | but if multiple timers become ready during the same loop iteration then |
1010 | but if multiple timers become ready during the same loop iteration then |
960 | order of execution is undefined. |
1011 | order of execution is undefined. |
|
|
1012 | |
|
|
1013 | =head3 Watcher-Specific Functions and Data Members |
961 | |
1014 | |
962 | =over 4 |
1015 | =over 4 |
963 | |
1016 | |
964 | =item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) |
1017 | =item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) |
965 | |
1018 | |
… | |
… | |
1061 | but on wallclock time (absolute time). You can tell a periodic watcher |
1114 | but on wallclock time (absolute time). You can tell a periodic watcher |
1062 | to trigger "at" some specific point in time. For example, if you tell a |
1115 | to trigger "at" some specific point in time. For example, if you tell a |
1063 | periodic watcher to trigger in 10 seconds (by specifiying e.g. C<ev_now () |
1116 | periodic watcher to trigger in 10 seconds (by specifiying e.g. C<ev_now () |
1064 | + 10.>) and then reset your system clock to the last year, then it will |
1117 | + 10.>) and then reset your system clock to the last year, then it will |
1065 | take a year to trigger the event (unlike an C<ev_timer>, which would trigger |
1118 | take a year to trigger the event (unlike an C<ev_timer>, which would trigger |
1066 | roughly 10 seconds later and of course not if you reset your system time |
1119 | roughly 10 seconds later). |
1067 | again). |
|
|
1068 | |
1120 | |
1069 | They can also be used to implement vastly more complex timers, such as |
1121 | They can also be used to implement vastly more complex timers, such as |
1070 | triggering an event on eahc midnight, local time. |
1122 | triggering an event on each midnight, local time or other, complicated, |
|
|
1123 | rules. |
1071 | |
1124 | |
1072 | As with timers, the callback is guarenteed to be invoked only when the |
1125 | As with timers, the callback is guarenteed to be invoked only when the |
1073 | time (C<at>) has been passed, but if multiple periodic timers become ready |
1126 | time (C<at>) has been passed, but if multiple periodic timers become ready |
1074 | during the same loop iteration then order of execution is undefined. |
1127 | during the same loop iteration then order of execution is undefined. |
1075 | |
1128 | |
|
|
1129 | =head3 Watcher-Specific Functions and Data Members |
|
|
1130 | |
1076 | =over 4 |
1131 | =over 4 |
1077 | |
1132 | |
1078 | =item ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb) |
1133 | =item ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb) |
1079 | |
1134 | |
1080 | =item ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb) |
1135 | =item ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb) |
… | |
… | |
1082 | Lots of arguments, lets sort it out... There are basically three modes of |
1137 | Lots of arguments, lets sort it out... There are basically three modes of |
1083 | operation, and we will explain them from simplest to complex: |
1138 | operation, and we will explain them from simplest to complex: |
1084 | |
1139 | |
1085 | =over 4 |
1140 | =over 4 |
1086 | |
1141 | |
1087 | =item * absolute timer (interval = reschedule_cb = 0) |
1142 | =item * absolute timer (at = time, interval = reschedule_cb = 0) |
1088 | |
1143 | |
1089 | In this configuration the watcher triggers an event at the wallclock time |
1144 | In this configuration the watcher triggers an event at the wallclock time |
1090 | C<at> and doesn't repeat. It will not adjust when a time jump occurs, |
1145 | C<at> and doesn't repeat. It will not adjust when a time jump occurs, |
1091 | that is, if it is to be run at January 1st 2011 then it will run when the |
1146 | that is, if it is to be run at January 1st 2011 then it will run when the |
1092 | system time reaches or surpasses this time. |
1147 | system time reaches or surpasses this time. |
1093 | |
1148 | |
1094 | =item * non-repeating interval timer (interval > 0, reschedule_cb = 0) |
1149 | =item * non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1095 | |
1150 | |
1096 | In this mode the watcher will always be scheduled to time out at the next |
1151 | In this mode the watcher will always be scheduled to time out at the next |
1097 | C<at + N * interval> time (for some integer N) and then repeat, regardless |
1152 | C<at + N * interval> time (for some integer N, which can also be negative) |
1098 | of any time jumps. |
1153 | and then repeat, regardless of any time jumps. |
1099 | |
1154 | |
1100 | This can be used to create timers that do not drift with respect to system |
1155 | This can be used to create timers that do not drift with respect to system |
1101 | time: |
1156 | time: |
1102 | |
1157 | |
1103 | ev_periodic_set (&periodic, 0., 3600., 0); |
1158 | ev_periodic_set (&periodic, 0., 3600., 0); |
… | |
… | |
1109 | |
1164 | |
1110 | Another way to think about it (for the mathematically inclined) is that |
1165 | Another way to think about it (for the mathematically inclined) is that |
1111 | C<ev_periodic> will try to run the callback in this mode at the next possible |
1166 | C<ev_periodic> will try to run the callback in this mode at the next possible |
1112 | time where C<time = at (mod interval)>, regardless of any time jumps. |
1167 | time where C<time = at (mod interval)>, regardless of any time jumps. |
1113 | |
1168 | |
|
|
1169 | For numerical stability it is preferable that the C<at> value is near |
|
|
1170 | C<ev_now ()> (the current time), but there is no range requirement for |
|
|
1171 | this value. |
|
|
1172 | |
1114 | =item * manual reschedule mode (reschedule_cb = callback) |
1173 | =item * manual reschedule mode (at and interval ignored, reschedule_cb = callback) |
1115 | |
1174 | |
1116 | In this mode the values for C<interval> and C<at> are both being |
1175 | In this mode the values for C<interval> and C<at> are both being |
1117 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1176 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1118 | reschedule callback will be called with the watcher as first, and the |
1177 | reschedule callback will be called with the watcher as first, and the |
1119 | current time as second argument. |
1178 | current time as second argument. |
1120 | |
1179 | |
1121 | NOTE: I<This callback MUST NOT stop or destroy any periodic watcher, |
1180 | NOTE: I<This callback MUST NOT stop or destroy any periodic watcher, |
1122 | ever, or make any event loop modifications>. If you need to stop it, |
1181 | ever, or make any event loop modifications>. If you need to stop it, |
1123 | return C<now + 1e30> (or so, fudge fudge) and stop it afterwards (e.g. by |
1182 | return C<now + 1e30> (or so, fudge fudge) and stop it afterwards (e.g. by |
1124 | starting a prepare watcher). |
1183 | starting an C<ev_prepare> watcher, which is legal). |
1125 | |
1184 | |
1126 | Its prototype is C<ev_tstamp (*reschedule_cb)(struct ev_periodic *w, |
1185 | Its prototype is C<ev_tstamp (*reschedule_cb)(struct ev_periodic *w, |
1127 | ev_tstamp now)>, e.g.: |
1186 | ev_tstamp now)>, e.g.: |
1128 | |
1187 | |
1129 | static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
1188 | static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
… | |
… | |
1152 | Simply stops and restarts the periodic watcher again. This is only useful |
1211 | Simply stops and restarts the periodic watcher again. This is only useful |
1153 | when you changed some parameters or the reschedule callback would return |
1212 | when you changed some parameters or the reschedule callback would return |
1154 | a different time than the last time it was called (e.g. in a crond like |
1213 | a different time than the last time it was called (e.g. in a crond like |
1155 | program when the crontabs have changed). |
1214 | program when the crontabs have changed). |
1156 | |
1215 | |
|
|
1216 | =item ev_tstamp offset [read-write] |
|
|
1217 | |
|
|
1218 | When repeating, this contains the offset value, otherwise this is the |
|
|
1219 | absolute point in time (the C<at> value passed to C<ev_periodic_set>). |
|
|
1220 | |
|
|
1221 | Can be modified any time, but changes only take effect when the periodic |
|
|
1222 | timer fires or C<ev_periodic_again> is being called. |
|
|
1223 | |
1157 | =item ev_tstamp interval [read-write] |
1224 | =item ev_tstamp interval [read-write] |
1158 | |
1225 | |
1159 | The current interval value. Can be modified any time, but changes only |
1226 | The current interval value. Can be modified any time, but changes only |
1160 | take effect when the periodic timer fires or C<ev_periodic_again> is being |
1227 | take effect when the periodic timer fires or C<ev_periodic_again> is being |
1161 | called. |
1228 | called. |
… | |
… | |
1163 | =item ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write] |
1230 | =item ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write] |
1164 | |
1231 | |
1165 | The current reschedule callback, or C<0>, if this functionality is |
1232 | The current reschedule callback, or C<0>, if this functionality is |
1166 | switched off. Can be changed any time, but changes only take effect when |
1233 | switched off. Can be changed any time, but changes only take effect when |
1167 | the periodic timer fires or C<ev_periodic_again> is being called. |
1234 | the periodic timer fires or C<ev_periodic_again> is being called. |
|
|
1235 | |
|
|
1236 | =item ev_tstamp at [read-only] |
|
|
1237 | |
|
|
1238 | When active, contains the absolute time that the watcher is supposed to |
|
|
1239 | trigger next. |
1168 | |
1240 | |
1169 | =back |
1241 | =back |
1170 | |
1242 | |
1171 | Example: Call a callback every hour, or, more precisely, whenever the |
1243 | Example: Call a callback every hour, or, more precisely, whenever the |
1172 | system clock is divisible by 3600. The callback invocation times have |
1244 | system clock is divisible by 3600. The callback invocation times have |
… | |
… | |
1214 | with the kernel (thus it coexists with your own signal handlers as long |
1286 | with the kernel (thus it coexists with your own signal handlers as long |
1215 | as you don't register any with libev). Similarly, when the last signal |
1287 | as you don't register any with libev). Similarly, when the last signal |
1216 | watcher for a signal is stopped libev will reset the signal handler to |
1288 | watcher for a signal is stopped libev will reset the signal handler to |
1217 | SIG_DFL (regardless of what it was set to before). |
1289 | SIG_DFL (regardless of what it was set to before). |
1218 | |
1290 | |
|
|
1291 | =head3 Watcher-Specific Functions and Data Members |
|
|
1292 | |
1219 | =over 4 |
1293 | =over 4 |
1220 | |
1294 | |
1221 | =item ev_signal_init (ev_signal *, callback, int signum) |
1295 | =item ev_signal_init (ev_signal *, callback, int signum) |
1222 | |
1296 | |
1223 | =item ev_signal_set (ev_signal *, int signum) |
1297 | =item ev_signal_set (ev_signal *, int signum) |
… | |
… | |
1234 | |
1308 | |
1235 | =head2 C<ev_child> - watch out for process status changes |
1309 | =head2 C<ev_child> - watch out for process status changes |
1236 | |
1310 | |
1237 | Child watchers trigger when your process receives a SIGCHLD in response to |
1311 | Child watchers trigger when your process receives a SIGCHLD in response to |
1238 | some child status changes (most typically when a child of yours dies). |
1312 | some child status changes (most typically when a child of yours dies). |
|
|
1313 | |
|
|
1314 | =head3 Watcher-Specific Functions and Data Members |
1239 | |
1315 | |
1240 | =over 4 |
1316 | =over 4 |
1241 | |
1317 | |
1242 | =item ev_child_init (ev_child *, callback, int pid) |
1318 | =item ev_child_init (ev_child *, callback, int pid) |
1243 | |
1319 | |
… | |
… | |
1311 | reader). Inotify will be used to give hints only and should not change the |
1387 | reader). Inotify will be used to give hints only and should not change the |
1312 | semantics of C<ev_stat> watchers, which means that libev sometimes needs |
1388 | semantics of C<ev_stat> watchers, which means that libev sometimes needs |
1313 | to fall back to regular polling again even with inotify, but changes are |
1389 | to fall back to regular polling again even with inotify, but changes are |
1314 | usually detected immediately, and if the file exists there will be no |
1390 | usually detected immediately, and if the file exists there will be no |
1315 | polling. |
1391 | polling. |
|
|
1392 | |
|
|
1393 | =head3 Watcher-Specific Functions and Data Members |
1316 | |
1394 | |
1317 | =over 4 |
1395 | =over 4 |
1318 | |
1396 | |
1319 | =item ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval) |
1397 | =item ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval) |
1320 | |
1398 | |
… | |
… | |
1403 | Apart from keeping your process non-blocking (which is a useful |
1481 | Apart from keeping your process non-blocking (which is a useful |
1404 | effect on its own sometimes), idle watchers are a good place to do |
1482 | effect on its own sometimes), idle watchers are a good place to do |
1405 | "pseudo-background processing", or delay processing stuff to after the |
1483 | "pseudo-background processing", or delay processing stuff to after the |
1406 | event loop has handled all outstanding events. |
1484 | event loop has handled all outstanding events. |
1407 | |
1485 | |
|
|
1486 | =head3 Watcher-Specific Functions and Data Members |
|
|
1487 | |
1408 | =over 4 |
1488 | =over 4 |
1409 | |
1489 | |
1410 | =item ev_idle_init (ev_signal *, callback) |
1490 | =item ev_idle_init (ev_signal *, callback) |
1411 | |
1491 | |
1412 | Initialises and configures the idle watcher - it has no parameters of any |
1492 | Initialises and configures the idle watcher - it has no parameters of any |
… | |
… | |
1469 | with priority higher than or equal to the event loop and one coroutine |
1549 | with priority higher than or equal to the event loop and one coroutine |
1470 | of lower priority, but only once, using idle watchers to keep the event |
1550 | of lower priority, but only once, using idle watchers to keep the event |
1471 | loop from blocking if lower-priority coroutines are active, thus mapping |
1551 | loop from blocking if lower-priority coroutines are active, thus mapping |
1472 | low-priority coroutines to idle/background tasks). |
1552 | low-priority coroutines to idle/background tasks). |
1473 | |
1553 | |
|
|
1554 | It is recommended to give C<ev_check> watchers highest (C<EV_MAXPRI>) |
|
|
1555 | priority, to ensure that they are being run before any other watchers |
|
|
1556 | after the poll. Also, C<ev_check> watchers (and C<ev_prepare> watchers, |
|
|
1557 | too) should not activate ("feed") events into libev. While libev fully |
|
|
1558 | supports this, they will be called before other C<ev_check> watchers did |
|
|
1559 | their job. As C<ev_check> watchers are often used to embed other event |
|
|
1560 | loops those other event loops might be in an unusable state until their |
|
|
1561 | C<ev_check> watcher ran (always remind yourself to coexist peacefully with |
|
|
1562 | others). |
|
|
1563 | |
|
|
1564 | =head3 Watcher-Specific Functions and Data Members |
|
|
1565 | |
1474 | =over 4 |
1566 | =over 4 |
1475 | |
1567 | |
1476 | =item ev_prepare_init (ev_prepare *, callback) |
1568 | =item ev_prepare_init (ev_prepare *, callback) |
1477 | |
1569 | |
1478 | =item ev_check_init (ev_check *, callback) |
1570 | =item ev_check_init (ev_check *, callback) |
… | |
… | |
1481 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
1573 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
1482 | macros, but using them is utterly, utterly and completely pointless. |
1574 | macros, but using them is utterly, utterly and completely pointless. |
1483 | |
1575 | |
1484 | =back |
1576 | =back |
1485 | |
1577 | |
1486 | Example: To include a library such as adns, you would add IO watchers |
1578 | There are a number of principal ways to embed other event loops or modules |
1487 | and a timeout watcher in a prepare handler, as required by libadns, and |
1579 | into libev. Here are some ideas on how to include libadns into libev |
|
|
1580 | (there is a Perl module named C<EV::ADNS> that does this, which you could |
|
|
1581 | use for an actually working example. Another Perl module named C<EV::Glib> |
|
|
1582 | embeds a Glib main context into libev, and finally, C<Glib::EV> embeds EV |
|
|
1583 | into the Glib event loop). |
|
|
1584 | |
|
|
1585 | Method 1: Add IO watchers and a timeout watcher in a prepare handler, |
1488 | in a check watcher, destroy them and call into libadns. What follows is |
1586 | and in a check watcher, destroy them and call into libadns. What follows |
1489 | pseudo-code only of course: |
1587 | is pseudo-code only of course. This requires you to either use a low |
|
|
1588 | priority for the check watcher or use C<ev_clear_pending> explicitly, as |
|
|
1589 | the callbacks for the IO/timeout watchers might not have been called yet. |
1490 | |
1590 | |
1491 | static ev_io iow [nfd]; |
1591 | static ev_io iow [nfd]; |
1492 | static ev_timer tw; |
1592 | static ev_timer tw; |
1493 | |
1593 | |
1494 | static void |
1594 | static void |
1495 | io_cb (ev_loop *loop, ev_io *w, int revents) |
1595 | io_cb (ev_loop *loop, ev_io *w, int revents) |
1496 | { |
1596 | { |
1497 | // set the relevant poll flags |
|
|
1498 | // could also call adns_processreadable etc. here |
|
|
1499 | struct pollfd *fd = (struct pollfd *)w->data; |
|
|
1500 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
|
|
1501 | if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
|
|
1502 | } |
1597 | } |
1503 | |
1598 | |
1504 | // create io watchers for each fd and a timer before blocking |
1599 | // create io watchers for each fd and a timer before blocking |
1505 | static void |
1600 | static void |
1506 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
1601 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
… | |
… | |
1512 | |
1607 | |
1513 | /* the callback is illegal, but won't be called as we stop during check */ |
1608 | /* the callback is illegal, but won't be called as we stop during check */ |
1514 | ev_timer_init (&tw, 0, timeout * 1e-3); |
1609 | ev_timer_init (&tw, 0, timeout * 1e-3); |
1515 | ev_timer_start (loop, &tw); |
1610 | ev_timer_start (loop, &tw); |
1516 | |
1611 | |
1517 | // create on ev_io per pollfd |
1612 | // create one ev_io per pollfd |
1518 | for (int i = 0; i < nfd; ++i) |
1613 | for (int i = 0; i < nfd; ++i) |
1519 | { |
1614 | { |
1520 | ev_io_init (iow + i, io_cb, fds [i].fd, |
1615 | ev_io_init (iow + i, io_cb, fds [i].fd, |
1521 | ((fds [i].events & POLLIN ? EV_READ : 0) |
1616 | ((fds [i].events & POLLIN ? EV_READ : 0) |
1522 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
1617 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
1523 | |
1618 | |
1524 | fds [i].revents = 0; |
1619 | fds [i].revents = 0; |
1525 | iow [i].data = fds + i; |
|
|
1526 | ev_io_start (loop, iow + i); |
1620 | ev_io_start (loop, iow + i); |
1527 | } |
1621 | } |
1528 | } |
1622 | } |
1529 | |
1623 | |
1530 | // stop all watchers after blocking |
1624 | // stop all watchers after blocking |
… | |
… | |
1532 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
1626 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
1533 | { |
1627 | { |
1534 | ev_timer_stop (loop, &tw); |
1628 | ev_timer_stop (loop, &tw); |
1535 | |
1629 | |
1536 | for (int i = 0; i < nfd; ++i) |
1630 | for (int i = 0; i < nfd; ++i) |
|
|
1631 | { |
|
|
1632 | // set the relevant poll flags |
|
|
1633 | // could also call adns_processreadable etc. here |
|
|
1634 | struct pollfd *fd = fds + i; |
|
|
1635 | int revents = ev_clear_pending (iow + i); |
|
|
1636 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
|
|
1637 | if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
|
|
1638 | |
|
|
1639 | // now stop the watcher |
1537 | ev_io_stop (loop, iow + i); |
1640 | ev_io_stop (loop, iow + i); |
|
|
1641 | } |
1538 | |
1642 | |
1539 | adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); |
1643 | adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); |
|
|
1644 | } |
|
|
1645 | |
|
|
1646 | Method 2: This would be just like method 1, but you run C<adns_afterpoll> |
|
|
1647 | in the prepare watcher and would dispose of the check watcher. |
|
|
1648 | |
|
|
1649 | Method 3: If the module to be embedded supports explicit event |
|
|
1650 | notification (adns does), you can also make use of the actual watcher |
|
|
1651 | callbacks, and only destroy/create the watchers in the prepare watcher. |
|
|
1652 | |
|
|
1653 | static void |
|
|
1654 | timer_cb (EV_P_ ev_timer *w, int revents) |
|
|
1655 | { |
|
|
1656 | adns_state ads = (adns_state)w->data; |
|
|
1657 | update_now (EV_A); |
|
|
1658 | |
|
|
1659 | adns_processtimeouts (ads, &tv_now); |
|
|
1660 | } |
|
|
1661 | |
|
|
1662 | static void |
|
|
1663 | io_cb (EV_P_ ev_io *w, int revents) |
|
|
1664 | { |
|
|
1665 | adns_state ads = (adns_state)w->data; |
|
|
1666 | update_now (EV_A); |
|
|
1667 | |
|
|
1668 | if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now); |
|
|
1669 | if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now); |
|
|
1670 | } |
|
|
1671 | |
|
|
1672 | // do not ever call adns_afterpoll |
|
|
1673 | |
|
|
1674 | Method 4: Do not use a prepare or check watcher because the module you |
|
|
1675 | want to embed is too inflexible to support it. Instead, youc na override |
|
|
1676 | their poll function. The drawback with this solution is that the main |
|
|
1677 | loop is now no longer controllable by EV. The C<Glib::EV> module does |
|
|
1678 | this. |
|
|
1679 | |
|
|
1680 | static gint |
|
|
1681 | event_poll_func (GPollFD *fds, guint nfds, gint timeout) |
|
|
1682 | { |
|
|
1683 | int got_events = 0; |
|
|
1684 | |
|
|
1685 | for (n = 0; n < nfds; ++n) |
|
|
1686 | // create/start io watcher that sets the relevant bits in fds[n] and increment got_events |
|
|
1687 | |
|
|
1688 | if (timeout >= 0) |
|
|
1689 | // create/start timer |
|
|
1690 | |
|
|
1691 | // poll |
|
|
1692 | ev_loop (EV_A_ 0); |
|
|
1693 | |
|
|
1694 | // stop timer again |
|
|
1695 | if (timeout >= 0) |
|
|
1696 | ev_timer_stop (EV_A_ &to); |
|
|
1697 | |
|
|
1698 | // stop io watchers again - their callbacks should have set |
|
|
1699 | for (n = 0; n < nfds; ++n) |
|
|
1700 | ev_io_stop (EV_A_ iow [n]); |
|
|
1701 | |
|
|
1702 | return got_events; |
1540 | } |
1703 | } |
1541 | |
1704 | |
1542 | |
1705 | |
1543 | =head2 C<ev_embed> - when one backend isn't enough... |
1706 | =head2 C<ev_embed> - when one backend isn't enough... |
1544 | |
1707 | |
… | |
… | |
1608 | ev_embed_start (loop_hi, &embed); |
1771 | ev_embed_start (loop_hi, &embed); |
1609 | } |
1772 | } |
1610 | else |
1773 | else |
1611 | loop_lo = loop_hi; |
1774 | loop_lo = loop_hi; |
1612 | |
1775 | |
|
|
1776 | =head3 Watcher-Specific Functions and Data Members |
|
|
1777 | |
1613 | =over 4 |
1778 | =over 4 |
1614 | |
1779 | |
1615 | =item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) |
1780 | =item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) |
1616 | |
1781 | |
1617 | =item ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop) |
1782 | =item ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop) |
… | |
… | |
1643 | event loop blocks next and before C<ev_check> watchers are being called, |
1808 | event loop blocks next and before C<ev_check> watchers are being called, |
1644 | and only in the child after the fork. If whoever good citizen calling |
1809 | and only in the child after the fork. If whoever good citizen calling |
1645 | C<ev_default_fork> cheats and calls it in the wrong process, the fork |
1810 | C<ev_default_fork> cheats and calls it in the wrong process, the fork |
1646 | handlers will be invoked, too, of course. |
1811 | handlers will be invoked, too, of course. |
1647 | |
1812 | |
|
|
1813 | =head3 Watcher-Specific Functions and Data Members |
|
|
1814 | |
1648 | =over 4 |
1815 | =over 4 |
1649 | |
1816 | |
1650 | =item ev_fork_init (ev_signal *, callback) |
1817 | =item ev_fork_init (ev_signal *, callback) |
1651 | |
1818 | |
1652 | Initialises and configures the fork watcher - it has no parameters of any |
1819 | Initialises and configures the fork watcher - it has no parameters of any |
… | |
… | |
1832 | |
1999 | |
1833 | myclass obj; |
2000 | myclass obj; |
1834 | ev::io iow; |
2001 | ev::io iow; |
1835 | iow.set <myclass, &myclass::io_cb> (&obj); |
2002 | iow.set <myclass, &myclass::io_cb> (&obj); |
1836 | |
2003 | |
1837 | =item w->set (void (*function)(watcher &w, int), void *data = 0) |
2004 | =item w->set<function> (void *data = 0) |
1838 | |
2005 | |
1839 | Also sets a callback, but uses a static method or plain function as |
2006 | Also sets a callback, but uses a static method or plain function as |
1840 | callback. The optional C<data> argument will be stored in the watcher's |
2007 | callback. The optional C<data> argument will be stored in the watcher's |
1841 | C<data> member and is free for you to use. |
2008 | C<data> member and is free for you to use. |
1842 | |
2009 | |
|
|
2010 | The prototype of the C<function> must be C<void (*)(ev::TYPE &w, int)>. |
|
|
2011 | |
1843 | See the method-C<set> above for more details. |
2012 | See the method-C<set> above for more details. |
|
|
2013 | |
|
|
2014 | Example: |
|
|
2015 | |
|
|
2016 | static void io_cb (ev::io &w, int revents) { } |
|
|
2017 | iow.set <io_cb> (); |
1844 | |
2018 | |
1845 | =item w->set (struct ev_loop *) |
2019 | =item w->set (struct ev_loop *) |
1846 | |
2020 | |
1847 | Associates a different C<struct ev_loop> with this watcher. You can only |
2021 | Associates a different C<struct ev_loop> with this watcher. You can only |
1848 | do this when the watcher is inactive (and not pending either). |
2022 | do this when the watcher is inactive (and not pending either). |
… | |
… | |
1861 | |
2035 | |
1862 | =item w->stop () |
2036 | =item w->stop () |
1863 | |
2037 | |
1864 | Stops the watcher if it is active. Again, no C<loop> argument. |
2038 | Stops the watcher if it is active. Again, no C<loop> argument. |
1865 | |
2039 | |
1866 | =item w->again () C<ev::timer>, C<ev::periodic> only |
2040 | =item w->again () (C<ev::timer>, C<ev::periodic> only) |
1867 | |
2041 | |
1868 | For C<ev::timer> and C<ev::periodic>, this invokes the corresponding |
2042 | For C<ev::timer> and C<ev::periodic>, this invokes the corresponding |
1869 | C<ev_TYPE_again> function. |
2043 | C<ev_TYPE_again> function. |
1870 | |
2044 | |
1871 | =item w->sweep () C<ev::embed> only |
2045 | =item w->sweep () (C<ev::embed> only) |
1872 | |
2046 | |
1873 | Invokes C<ev_embed_sweep>. |
2047 | Invokes C<ev_embed_sweep>. |
1874 | |
2048 | |
1875 | =item w->update () C<ev::stat> only |
2049 | =item w->update () (C<ev::stat> only) |
1876 | |
2050 | |
1877 | Invokes C<ev_stat_stat>. |
2051 | Invokes C<ev_stat_stat>. |
1878 | |
2052 | |
1879 | =back |
2053 | =back |
1880 | |
2054 | |
… | |
… | |
1900 | } |
2074 | } |
1901 | |
2075 | |
1902 | |
2076 | |
1903 | =head1 MACRO MAGIC |
2077 | =head1 MACRO MAGIC |
1904 | |
2078 | |
1905 | Libev can be compiled with a variety of options, the most fundemantal is |
2079 | Libev can be compiled with a variety of options, the most fundamantal |
1906 | C<EV_MULTIPLICITY>. This option determines whether (most) functions and |
2080 | of which is C<EV_MULTIPLICITY>. This option determines whether (most) |
1907 | callbacks have an initial C<struct ev_loop *> argument. |
2081 | functions and callbacks have an initial C<struct ev_loop *> argument. |
1908 | |
2082 | |
1909 | To make it easier to write programs that cope with either variant, the |
2083 | To make it easier to write programs that cope with either variant, the |
1910 | following macros are defined: |
2084 | following macros are defined: |
1911 | |
2085 | |
1912 | =over 4 |
2086 | =over 4 |