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

Comparing libev/ev.pod (file contents):
Revision 1.74 by root, Sat Dec 8 14:12:08 2007 UTC vs.
Revision 1.88 by ayin, Tue Dec 18 13:06:18 2007 UTC

98Libev represents time as a single floating point number, representing the 98Libev 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
100the beginning of 1970, details are complicated, don't ask). This type is 100the beginning of 1970, details are complicated, don't ask). This type is
101called C<ev_tstamp>, which is what you should use too. It usually aliases 101called C<ev_tstamp>, which is what you should use too. It usually aliases
102to the C<double> type in C, and when you need to do any calculations on 102to the C<double> type in C, and when you need to do any calculations on
103it, you should treat it as such. 103it, you should treat it as some floatingpoint value. Unlike the name
104component C<stamp> might indicate, it is also used for time differences
105throughout libev.
104 106
105=head1 GLOBAL FUNCTIONS 107=head1 GLOBAL FUNCTIONS
106 108
107These functions can be called anytime, even before initialising the 109These functions can be called anytime, even before initialising the
108library in any way. 110library 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
122You can find out the major and minor version numbers of the library 124You can find out the major and minor ABI version numbers of the library
123you linked against by calling the functions C<ev_version_major> and 125you linked against by calling the functions C<ev_version_major> and
124C<ev_version_minor>. If you want, you can compare against the global 126C<ev_version_minor>. If you want, you can compare against the global
125symbols C<EV_VERSION_MAJOR> and C<EV_VERSION_MINOR>, which specify the 127symbols C<EV_VERSION_MAJOR> and C<EV_VERSION_MINOR>, which specify the
126version of the library your program was compiled against. 128version of the library your program was compiled against.
127 129
130These version numbers refer to the ABI version of the library, not the
131release version.
132
128Usually, it's a good idea to terminate if the major versions mismatch, 133Usually, it's a good idea to terminate if the major versions mismatch,
129as this indicates an incompatible change. Minor versions are usually 134as this indicates an incompatible change. Minor versions are usually
130compatible to older versions, so a larger minor version alone is usually 135compatible to older versions, so a larger minor version alone is usually
131not a problem. 136not a problem.
132 137
133Example: Make sure we haven't accidentally been linked against the wrong 138Example: Make sure we haven't accidentally been linked against the wrong
134version. 139version.
399Destroys the default loop again (frees all memory and kernel state 404Destroys the default loop again (frees all memory and kernel state
400etc.). None of the active event watchers will be stopped in the normal 405etc.). None of the active event watchers will be stopped in the normal
401sense, so e.g. C<ev_is_active> might still return true. It is your 406sense, so e.g. C<ev_is_active> might still return true. It is your
402responsibility to either stop all watchers cleanly yoursef I<before> 407responsibility to either stop all watchers cleanly yoursef I<before>
403calling this function, or cope with the fact afterwards (which is usually 408calling this function, or cope with the fact afterwards (which is usually
404the easiest thing, youc na just ignore the watchers and/or C<free ()> them 409the easiest thing, you can just ignore the watchers and/or C<free ()> them
405for example). 410for example).
411
412Note that certain global state, such as signal state, will not be freed by
413this function, and related watchers (such as signal and child watchers)
414would need to be stopped manually.
415
416In general it is not advisable to call this function except in the
417rare occasion where you really need to free e.g. the signal handling
418pipe fds. If you need dynamically allocated loops it is better to use
419C<ev_loop_new> and C<ev_loop_destroy>).
406 420
407=item ev_loop_destroy (loop) 421=item ev_loop_destroy (loop)
408 422
409Like C<ev_default_destroy>, but destroys an event loop created by an 423Like C<ev_default_destroy>, but destroys an event loop created by an
410earlier call to C<ev_loop_new>. 424earlier call to C<ev_loop_new>.
486libev watchers. However, a pair of C<ev_prepare>/C<ev_check> watchers is 500libev watchers. However, a pair of C<ev_prepare>/C<ev_check> watchers is
487usually a better approach for this kind of thing. 501usually a better approach for this kind of thing.
488 502
489Here are the gory details of what C<ev_loop> does: 503Here 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.
907play around with an Xlib connection), then you have to seperately re-test 922play around with an Xlib connection), then you have to seperately re-test
908whether a file descriptor is really ready with a known-to-be good interface 923whether a file descriptor is really ready with a known-to-be good interface
909such as poll (fortunately in our Xlib example, Xlib already does this on 924such as poll (fortunately in our Xlib example, Xlib already does this on
910its own, so its quite safe to use). 925its own, so its quite safe to use).
911 926
927=head3 The special problem of disappearing file descriptors
928
929Some backends (e.g kqueue, epoll) need to be told about closing a file
930descriptor (either by calling C<close> explicitly or by any other means,
931such as C<dup>). The reason is that you register interest in some file
932descriptor, but when it goes away, the operating system will silently drop
933this interest. If another file descriptor with the same number then is
934registered with libev, there is no efficient way to see that this is, in
935fact, a different file descriptor.
936
937To avoid having to explicitly tell libev about such cases, libev follows
938the following policy: Each time C<ev_io_set> is being called, libev
939will assume that this is potentially a new file descriptor, otherwise
940it is assumed that the file descriptor stays the same. That means that
941you I<have> to call C<ev_io_set> (or C<ev_io_init>) when you change the
942descriptor even if the file descriptor number itself did not change.
943
944This is how one would do it normally anyway, the important point is that
945the libev application should not optimise around libev but should leave
946optimisations to libev.
947
948
949=head3 Watcher-Specific Functions
950
912=over 4 951=over 4
913 952
914=item ev_io_init (ev_io *, callback, int fd, int events) 953=item ev_io_init (ev_io *, callback, int fd, int events)
915 954
916=item ev_io_set (ev_io *, int fd, int events) 955=item ev_io_set (ev_io *, int fd, int events)
968 ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); 1007 ev_timer_set (&timer, after + ev_now () - ev_time (), 0.);
969 1008
970The callback is guarenteed to be invoked only when its timeout has passed, 1009The callback is guarenteed to be invoked only when its timeout has passed,
971but if multiple timers become ready during the same loop iteration then 1010but if multiple timers become ready during the same loop iteration then
972order of execution is undefined. 1011order of execution is undefined.
1012
1013=head3 Watcher-Specific Functions and Data Members
973 1014
974=over 4 1015=over 4
975 1016
976=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)
977 1018
1073but on wallclock time (absolute time). You can tell a periodic watcher 1114but on wallclock time (absolute time). You can tell a periodic watcher
1074to trigger "at" some specific point in time. For example, if you tell a 1115to trigger "at" some specific point in time. For example, if you tell a
1075periodic watcher to trigger in 10 seconds (by specifiying e.g. C<ev_now () 1116periodic watcher to trigger in 10 seconds (by specifiying e.g. C<ev_now ()
1076+ 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
1077take a year to trigger the event (unlike an C<ev_timer>, which would trigger 1118take a year to trigger the event (unlike an C<ev_timer>, which would trigger
1078roughly 10 seconds later and of course not if you reset your system time 1119roughly 10 seconds later).
1079again).
1080 1120
1081They can also be used to implement vastly more complex timers, such as 1121They can also be used to implement vastly more complex timers, such as
1082triggering an event on eahc midnight, local time. 1122triggering an event on each midnight, local time or other, complicated,
1123rules.
1083 1124
1084As with timers, the callback is guarenteed to be invoked only when the 1125As with timers, the callback is guarenteed to be invoked only when the
1085time (C<at>) has been passed, but if multiple periodic timers become ready 1126time (C<at>) has been passed, but if multiple periodic timers become ready
1086during the same loop iteration then order of execution is undefined. 1127during the same loop iteration then order of execution is undefined.
1087 1128
1129=head3 Watcher-Specific Functions and Data Members
1130
1088=over 4 1131=over 4
1089 1132
1090=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)
1091 1134
1092=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)
1094Lots of arguments, lets sort it out... There are basically three modes of 1137Lots of arguments, lets sort it out... There are basically three modes of
1095operation, and we will explain them from simplest to complex: 1138operation, and we will explain them from simplest to complex:
1096 1139
1097=over 4 1140=over 4
1098 1141
1099=item * absolute timer (interval = reschedule_cb = 0) 1142=item * absolute timer (at = time, interval = reschedule_cb = 0)
1100 1143
1101In this configuration the watcher triggers an event at the wallclock time 1144In this configuration the watcher triggers an event at the wallclock time
1102C<at> and doesn't repeat. It will not adjust when a time jump occurs, 1145C<at> and doesn't repeat. It will not adjust when a time jump occurs,
1103that is, if it is to be run at January 1st 2011 then it will run when the 1146that is, if it is to be run at January 1st 2011 then it will run when the
1104system time reaches or surpasses this time. 1147system time reaches or surpasses this time.
1105 1148
1106=item * non-repeating interval timer (interval > 0, reschedule_cb = 0) 1149=item * non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)
1107 1150
1108In this mode the watcher will always be scheduled to time out at the next 1151In this mode the watcher will always be scheduled to time out at the next
1109C<at + N * interval> time (for some integer N) and then repeat, regardless 1152C<at + N * interval> time (for some integer N, which can also be negative)
1110of any time jumps. 1153and then repeat, regardless of any time jumps.
1111 1154
1112This can be used to create timers that do not drift with respect to system 1155This can be used to create timers that do not drift with respect to system
1113time: 1156time:
1114 1157
1115 ev_periodic_set (&periodic, 0., 3600., 0); 1158 ev_periodic_set (&periodic, 0., 3600., 0);
1121 1164
1122Another way to think about it (for the mathematically inclined) is that 1165Another way to think about it (for the mathematically inclined) is that
1123C<ev_periodic> will try to run the callback in this mode at the next possible 1166C<ev_periodic> will try to run the callback in this mode at the next possible
1124time where C<time = at (mod interval)>, regardless of any time jumps. 1167time where C<time = at (mod interval)>, regardless of any time jumps.
1125 1168
1169For numerical stability it is preferable that the C<at> value is near
1170C<ev_now ()> (the current time), but there is no range requirement for
1171this value.
1172
1126=item * manual reschedule mode (reschedule_cb = callback) 1173=item * manual reschedule mode (at and interval ignored, reschedule_cb = callback)
1127 1174
1128In this mode the values for C<interval> and C<at> are both being 1175In this mode the values for C<interval> and C<at> are both being
1129ignored. Instead, each time the periodic watcher gets scheduled, the 1176ignored. Instead, each time the periodic watcher gets scheduled, the
1130reschedule callback will be called with the watcher as first, and the 1177reschedule callback will be called with the watcher as first, and the
1131current time as second argument. 1178current time as second argument.
1132 1179
1133NOTE: I<This callback MUST NOT stop or destroy any periodic watcher, 1180NOTE: I<This callback MUST NOT stop or destroy any periodic watcher,
1134ever, or make any event loop modifications>. If you need to stop it, 1181ever, or make any event loop modifications>. If you need to stop it,
1135return C<now + 1e30> (or so, fudge fudge) and stop it afterwards (e.g. by 1182return C<now + 1e30> (or so, fudge fudge) and stop it afterwards (e.g. by
1136starting a prepare watcher). 1183starting an C<ev_prepare> watcher, which is legal).
1137 1184
1138Its prototype is C<ev_tstamp (*reschedule_cb)(struct ev_periodic *w, 1185Its prototype is C<ev_tstamp (*reschedule_cb)(struct ev_periodic *w,
1139ev_tstamp now)>, e.g.: 1186ev_tstamp now)>, e.g.:
1140 1187
1141 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)
1164Simply stops and restarts the periodic watcher again. This is only useful 1211Simply stops and restarts the periodic watcher again. This is only useful
1165when you changed some parameters or the reschedule callback would return 1212when you changed some parameters or the reschedule callback would return
1166a different time than the last time it was called (e.g. in a crond like 1213a different time than the last time it was called (e.g. in a crond like
1167program when the crontabs have changed). 1214program when the crontabs have changed).
1168 1215
1216=item ev_tstamp offset [read-write]
1217
1218When repeating, this contains the offset value, otherwise this is the
1219absolute point in time (the C<at> value passed to C<ev_periodic_set>).
1220
1221Can be modified any time, but changes only take effect when the periodic
1222timer fires or C<ev_periodic_again> is being called.
1223
1169=item ev_tstamp interval [read-write] 1224=item ev_tstamp interval [read-write]
1170 1225
1171The current interval value. Can be modified any time, but changes only 1226The current interval value. Can be modified any time, but changes only
1172take effect when the periodic timer fires or C<ev_periodic_again> is being 1227take effect when the periodic timer fires or C<ev_periodic_again> is being
1173called. 1228called.
1175=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]
1176 1231
1177The current reschedule callback, or C<0>, if this functionality is 1232The current reschedule callback, or C<0>, if this functionality is
1178switched off. Can be changed any time, but changes only take effect when 1233switched off. Can be changed any time, but changes only take effect when
1179the periodic timer fires or C<ev_periodic_again> is being called. 1234the periodic timer fires or C<ev_periodic_again> is being called.
1235
1236=item ev_tstamp at [read-only]
1237
1238When active, contains the absolute time that the watcher is supposed to
1239trigger next.
1180 1240
1181=back 1241=back
1182 1242
1183Example: Call a callback every hour, or, more precisely, whenever the 1243Example: Call a callback every hour, or, more precisely, whenever the
1184system clock is divisible by 3600. The callback invocation times have 1244system clock is divisible by 3600. The callback invocation times have
1226with the kernel (thus it coexists with your own signal handlers as long 1286with the kernel (thus it coexists with your own signal handlers as long
1227as you don't register any with libev). Similarly, when the last signal 1287as you don't register any with libev). Similarly, when the last signal
1228watcher for a signal is stopped libev will reset the signal handler to 1288watcher for a signal is stopped libev will reset the signal handler to
1229SIG_DFL (regardless of what it was set to before). 1289SIG_DFL (regardless of what it was set to before).
1230 1290
1291=head3 Watcher-Specific Functions and Data Members
1292
1231=over 4 1293=over 4
1232 1294
1233=item ev_signal_init (ev_signal *, callback, int signum) 1295=item ev_signal_init (ev_signal *, callback, int signum)
1234 1296
1235=item ev_signal_set (ev_signal *, int signum) 1297=item ev_signal_set (ev_signal *, int signum)
1246 1308
1247=head2 C<ev_child> - watch out for process status changes 1309=head2 C<ev_child> - watch out for process status changes
1248 1310
1249Child watchers trigger when your process receives a SIGCHLD in response to 1311Child watchers trigger when your process receives a SIGCHLD in response to
1250some child status changes (most typically when a child of yours dies). 1312some child status changes (most typically when a child of yours dies).
1313
1314=head3 Watcher-Specific Functions and Data Members
1251 1315
1252=over 4 1316=over 4
1253 1317
1254=item ev_child_init (ev_child *, callback, int pid) 1318=item ev_child_init (ev_child *, callback, int pid)
1255 1319
1323reader). Inotify will be used to give hints only and should not change the 1387reader). Inotify will be used to give hints only and should not change the
1324semantics of C<ev_stat> watchers, which means that libev sometimes needs 1388semantics of C<ev_stat> watchers, which means that libev sometimes needs
1325to fall back to regular polling again even with inotify, but changes are 1389to fall back to regular polling again even with inotify, but changes are
1326usually detected immediately, and if the file exists there will be no 1390usually detected immediately, and if the file exists there will be no
1327polling. 1391polling.
1392
1393=head3 Watcher-Specific Functions and Data Members
1328 1394
1329=over 4 1395=over 4
1330 1396
1331=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)
1332 1398
1415Apart from keeping your process non-blocking (which is a useful 1481Apart from keeping your process non-blocking (which is a useful
1416effect on its own sometimes), idle watchers are a good place to do 1482effect on its own sometimes), idle watchers are a good place to do
1417"pseudo-background processing", or delay processing stuff to after the 1483"pseudo-background processing", or delay processing stuff to after the
1418event loop has handled all outstanding events. 1484event loop has handled all outstanding events.
1419 1485
1486=head3 Watcher-Specific Functions and Data Members
1487
1420=over 4 1488=over 4
1421 1489
1422=item ev_idle_init (ev_signal *, callback) 1490=item ev_idle_init (ev_signal *, callback)
1423 1491
1424Initialises and configures the idle watcher - it has no parameters of any 1492Initialises and configures the idle watcher - it has no parameters of any
1481with priority higher than or equal to the event loop and one coroutine 1549with priority higher than or equal to the event loop and one coroutine
1482of lower priority, but only once, using idle watchers to keep the event 1550of lower priority, but only once, using idle watchers to keep the event
1483loop from blocking if lower-priority coroutines are active, thus mapping 1551loop from blocking if lower-priority coroutines are active, thus mapping
1484low-priority coroutines to idle/background tasks). 1552low-priority coroutines to idle/background tasks).
1485 1553
1554It is recommended to give C<ev_check> watchers highest (C<EV_MAXPRI>)
1555priority, to ensure that they are being run before any other watchers
1556after the poll. Also, C<ev_check> watchers (and C<ev_prepare> watchers,
1557too) should not activate ("feed") events into libev. While libev fully
1558supports this, they will be called before other C<ev_check> watchers did
1559their job. As C<ev_check> watchers are often used to embed other event
1560loops those other event loops might be in an unusable state until their
1561C<ev_check> watcher ran (always remind yourself to coexist peacefully with
1562others).
1563
1564=head3 Watcher-Specific Functions and Data Members
1565
1486=over 4 1566=over 4
1487 1567
1488=item ev_prepare_init (ev_prepare *, callback) 1568=item ev_prepare_init (ev_prepare *, callback)
1489 1569
1490=item ev_check_init (ev_check *, callback) 1570=item ev_check_init (ev_check *, callback)
1493parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> 1573parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set>
1494macros, but using them is utterly, utterly and completely pointless. 1574macros, but using them is utterly, utterly and completely pointless.
1495 1575
1496=back 1576=back
1497 1577
1498Example: To include a library such as adns, you would add IO watchers 1578There are a number of principal ways to embed other event loops or modules
1499and a timeout watcher in a prepare handler, as required by libadns, and 1579into 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
1581use for an actually working example. Another Perl module named C<EV::Glib>
1582embeds a Glib main context into libev, and finally, C<Glib::EV> embeds EV
1583into the Glib event loop).
1584
1585Method 1: Add IO watchers and a timeout watcher in a prepare handler,
1500in a check watcher, destroy them and call into libadns. What follows is 1586and in a check watcher, destroy them and call into libadns. What follows
1501pseudo-code only of course: 1587is pseudo-code only of course. This requires you to either use a low
1588priority for the check watcher or use C<ev_clear_pending> explicitly, as
1589the callbacks for the IO/timeout watchers might not have been called yet.
1502 1590
1503 static ev_io iow [nfd]; 1591 static ev_io iow [nfd];
1504 static ev_timer tw; 1592 static ev_timer tw;
1505 1593
1506 static void 1594 static void
1507 io_cb (ev_loop *loop, ev_io *w, int revents) 1595 io_cb (ev_loop *loop, ev_io *w, int revents)
1508 { 1596 {
1509 // set the relevant poll flags
1510 // could also call adns_processreadable etc. here
1511 struct pollfd *fd = (struct pollfd *)w->data;
1512 if (revents & EV_READ ) fd->revents |= fd->events & POLLIN;
1513 if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT;
1514 } 1597 }
1515 1598
1516 // create io watchers for each fd and a timer before blocking 1599 // create io watchers for each fd and a timer before blocking
1517 static void 1600 static void
1518 adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) 1601 adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents)
1524 1607
1525 /* 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 */
1526 ev_timer_init (&tw, 0, timeout * 1e-3); 1609 ev_timer_init (&tw, 0, timeout * 1e-3);
1527 ev_timer_start (loop, &tw); 1610 ev_timer_start (loop, &tw);
1528 1611
1529 // create on ev_io per pollfd 1612 // create one ev_io per pollfd
1530 for (int i = 0; i < nfd; ++i) 1613 for (int i = 0; i < nfd; ++i)
1531 { 1614 {
1532 ev_io_init (iow + i, io_cb, fds [i].fd, 1615 ev_io_init (iow + i, io_cb, fds [i].fd,
1533 ((fds [i].events & POLLIN ? EV_READ : 0) 1616 ((fds [i].events & POLLIN ? EV_READ : 0)
1534 | (fds [i].events & POLLOUT ? EV_WRITE : 0))); 1617 | (fds [i].events & POLLOUT ? EV_WRITE : 0)));
1535 1618
1536 fds [i].revents = 0; 1619 fds [i].revents = 0;
1537 iow [i].data = fds + i;
1538 ev_io_start (loop, iow + i); 1620 ev_io_start (loop, iow + i);
1539 } 1621 }
1540 } 1622 }
1541 1623
1542 // stop all watchers after blocking 1624 // stop all watchers after blocking
1544 adns_check_cb (ev_loop *loop, ev_check *w, int revents) 1626 adns_check_cb (ev_loop *loop, ev_check *w, int revents)
1545 { 1627 {
1546 ev_timer_stop (loop, &tw); 1628 ev_timer_stop (loop, &tw);
1547 1629
1548 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
1549 ev_io_stop (loop, iow + i); 1640 ev_io_stop (loop, iow + i);
1641 }
1550 1642
1551 adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); 1643 adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop));
1644 }
1645
1646Method 2: This would be just like method 1, but you run C<adns_afterpoll>
1647in the prepare watcher and would dispose of the check watcher.
1648
1649Method 3: If the module to be embedded supports explicit event
1650notification (adns does), you can also make use of the actual watcher
1651callbacks, 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
1674Method 4: Do not use a prepare or check watcher because the module you
1675want to embed is too inflexible to support it. Instead, youc na override
1676their poll function. The drawback with this solution is that the main
1677loop is now no longer controllable by EV. The C<Glib::EV> module does
1678this.
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;
1552 } 1703 }
1553 1704
1554 1705
1555=head2 C<ev_embed> - when one backend isn't enough... 1706=head2 C<ev_embed> - when one backend isn't enough...
1556 1707
1620 ev_embed_start (loop_hi, &embed); 1771 ev_embed_start (loop_hi, &embed);
1621 } 1772 }
1622 else 1773 else
1623 loop_lo = loop_hi; 1774 loop_lo = loop_hi;
1624 1775
1776=head3 Watcher-Specific Functions and Data Members
1777
1625=over 4 1778=over 4
1626 1779
1627=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)
1628 1781
1629=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)
1655event loop blocks next and before C<ev_check> watchers are being called, 1808event loop blocks next and before C<ev_check> watchers are being called,
1656and only in the child after the fork. If whoever good citizen calling 1809and only in the child after the fork. If whoever good citizen calling
1657C<ev_default_fork> cheats and calls it in the wrong process, the fork 1810C<ev_default_fork> cheats and calls it in the wrong process, the fork
1658handlers will be invoked, too, of course. 1811handlers will be invoked, too, of course.
1659 1812
1813=head3 Watcher-Specific Functions and Data Members
1814
1660=over 4 1815=over 4
1661 1816
1662=item ev_fork_init (ev_signal *, callback) 1817=item ev_fork_init (ev_signal *, callback)
1663 1818
1664Initialises and configures the fork watcher - it has no parameters of any 1819Initialises and configures the fork watcher - it has no parameters of any
1844 1999
1845 myclass obj; 2000 myclass obj;
1846 ev::io iow; 2001 ev::io iow;
1847 iow.set <myclass, &myclass::io_cb> (&obj); 2002 iow.set <myclass, &myclass::io_cb> (&obj);
1848 2003
1849=item w->set (void (*function)(watcher &w, int), void *data = 0) 2004=item w->set<function> (void *data = 0)
1850 2005
1851Also sets a callback, but uses a static method or plain function as 2006Also sets a callback, but uses a static method or plain function as
1852callback. The optional C<data> argument will be stored in the watcher's 2007callback. The optional C<data> argument will be stored in the watcher's
1853C<data> member and is free for you to use. 2008C<data> member and is free for you to use.
1854 2009
2010The prototype of the C<function> must be C<void (*)(ev::TYPE &w, int)>.
2011
1855See the method-C<set> above for more details. 2012See the method-C<set> above for more details.
2013
2014Example:
2015
2016 static void io_cb (ev::io &w, int revents) { }
2017 iow.set <io_cb> ();
1856 2018
1857=item w->set (struct ev_loop *) 2019=item w->set (struct ev_loop *)
1858 2020
1859Associates a different C<struct ev_loop> with this watcher. You can only 2021Associates a different C<struct ev_loop> with this watcher. You can only
1860do this when the watcher is inactive (and not pending either). 2022do this when the watcher is inactive (and not pending either).
1873 2035
1874=item w->stop () 2036=item w->stop ()
1875 2037
1876Stops the watcher if it is active. Again, no C<loop> argument. 2038Stops the watcher if it is active. Again, no C<loop> argument.
1877 2039
1878=item w->again () C<ev::timer>, C<ev::periodic> only 2040=item w->again () (C<ev::timer>, C<ev::periodic> only)
1879 2041
1880For C<ev::timer> and C<ev::periodic>, this invokes the corresponding 2042For C<ev::timer> and C<ev::periodic>, this invokes the corresponding
1881C<ev_TYPE_again> function. 2043C<ev_TYPE_again> function.
1882 2044
1883=item w->sweep () C<ev::embed> only 2045=item w->sweep () (C<ev::embed> only)
1884 2046
1885Invokes C<ev_embed_sweep>. 2047Invokes C<ev_embed_sweep>.
1886 2048
1887=item w->update () C<ev::stat> only 2049=item w->update () (C<ev::stat> only)
1888 2050
1889Invokes C<ev_stat_stat>. 2051Invokes C<ev_stat_stat>.
1890 2052
1891=back 2053=back
1892 2054
1912 } 2074 }
1913 2075
1914 2076
1915=head1 MACRO MAGIC 2077=head1 MACRO MAGIC
1916 2078
1917Libev can be compiled with a variety of options, the most fundemantal is 2079Libev can be compiled with a variety of options, the most fundamantal
1918C<EV_MULTIPLICITY>. This option determines whether (most) functions and 2080of which is C<EV_MULTIPLICITY>. This option determines whether (most)
1919callbacks have an initial C<struct ev_loop *> argument. 2081functions and callbacks have an initial C<struct ev_loop *> argument.
1920 2082
1921To make it easier to write programs that cope with either variant, the 2083To make it easier to write programs that cope with either variant, the
1922following macros are defined: 2084following macros are defined:
1923 2085
1924=over 4 2086=over 4

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines