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

Comparing libev/ev.pod (file contents):
Revision 1.46 by root, Mon Nov 26 10:20:43 2007 UTC vs.
Revision 1.52 by root, Tue Nov 27 19:41:52 2007 UTC

48the beginning of 1970, details are complicated, don't ask). This type is 48the beginning of 1970, details are complicated, don't ask). This type is
49called C<ev_tstamp>, which is what you should use too. It usually aliases 49called C<ev_tstamp>, which is what you should use too. It usually aliases
50to the C<double> type in C, and when you need to do any calculations on 50to the C<double> type in C, and when you need to do any calculations on
51it, you should treat it as such. 51it, you should treat it as such.
52 52
53
54=head1 GLOBAL FUNCTIONS 53=head1 GLOBAL FUNCTIONS
55 54
56These functions can be called anytime, even before initialising the 55These functions can be called anytime, even before initialising the
57library in any way. 56library in any way.
58 57
116C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for 115C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for
117recommended ones. 116recommended ones.
118 117
119See the description of C<ev_embed> watchers for more info. 118See the description of C<ev_embed> watchers for more info.
120 119
121=item ev_set_allocator (void *(*cb)(void *ptr, long size)) 120=item ev_set_allocator (void *(*cb)(void *ptr, size_t size))
122 121
123Sets the allocation function to use (the prototype is similar to the 122Sets the allocation function to use (the prototype and semantics are
124realloc C function, the semantics are identical). It is used to allocate 123identical to the realloc C function). It is used to allocate and free
125and free memory (no surprises here). If it returns zero when memory 124memory (no surprises here). If it returns zero when memory needs to be
126needs to be allocated, the library might abort or take some potentially 125allocated, the library might abort or take some potentially destructive
127destructive action. The default is your system realloc function. 126action. The default is your system realloc function.
128 127
129You could override this function in high-availability programs to, say, 128You could override this function in high-availability programs to, say,
130free some memory if it cannot allocate memory, to use a special allocator, 129free some memory if it cannot allocate memory, to use a special allocator,
131or even to sleep a while and retry until some memory is available. 130or even to sleep a while and retry until some memory is available.
132 131
133Example: replace the libev allocator with one that waits a bit and then 132Example: replace the libev allocator with one that waits a bit and then
134retries: better than mine). 133retries: better than mine).
135 134
136 static void * 135 static void *
137 persistent_realloc (void *ptr, long size) 136 persistent_realloc (void *ptr, size_t size)
138 { 137 {
139 for (;;) 138 for (;;)
140 { 139 {
141 void *newptr = realloc (ptr, size); 140 void *newptr = realloc (ptr, size);
142 141
545 544
546=item C<EV_CHILD> 545=item C<EV_CHILD>
547 546
548The pid specified in the C<ev_child> watcher has received a status change. 547The pid specified in the C<ev_child> watcher has received a status change.
549 548
549=item C<EV_STAT>
550
551The path specified in the C<ev_stat> watcher changed its attributes somehow.
552
550=item C<EV_IDLE> 553=item C<EV_IDLE>
551 554
552The C<ev_idle> watcher has determined that you have nothing better to do. 555The C<ev_idle> watcher has determined that you have nothing better to do.
553 556
554=item C<EV_PREPARE> 557=item C<EV_PREPARE>
561received events. Callbacks of both watcher types can start and stop as 564received events. Callbacks of both watcher types can start and stop as
562many watchers as they want, and all of them will be taken into account 565many watchers as they want, and all of them will be taken into account
563(for example, a C<ev_prepare> watcher might start an idle watcher to keep 566(for example, a C<ev_prepare> watcher might start an idle watcher to keep
564C<ev_loop> from blocking). 567C<ev_loop> from blocking).
565 568
569=item C<EV_EMBED>
570
571The embedded event loop specified in the C<ev_embed> watcher needs attention.
572
573=item C<EV_FORK>
574
575The event loop has been resumed in the child process after fork (see
576C<ev_fork>).
577
566=item C<EV_ERROR> 578=item C<EV_ERROR>
567 579
568An unspecified error has occured, the watcher has been stopped. This might 580An unspecified error has occured, the watcher has been stopped. This might
569happen because the watcher could not be properly started because libev 581happen because the watcher could not be properly started because libev
570ran out of memory, a file descriptor was found to be closed or any other 582ran out of memory, a file descriptor was found to be closed or any other
689 701
690 702
691=head1 WATCHER TYPES 703=head1 WATCHER TYPES
692 704
693This section describes each watcher in detail, but will not repeat 705This section describes each watcher in detail, but will not repeat
694information given in the last section. 706information given in the last section. Any initialisation/set macros,
707functions and members specific to the watcher type are explained.
708
709Members are additionally marked with either I<[read-only]>, meaning that,
710while the watcher is active, you can look at the member and expect some
711sensible content, but you must not modify it (you can modify it while the
712watcher is stopped to your hearts content), or I<[read-write]>, which
713means you can expect it to have some sensible content while the watcher
714is active, but you can also modify it. Modifying it may not do something
715sensible or take immediate effect (or do anything at all), but libev will
716not crash or malfunction in any way.
695 717
696 718
697=head2 C<ev_io> - is this file descriptor readable or writable? 719=head2 C<ev_io> - is this file descriptor readable or writable?
698 720
699I/O watchers check whether a file descriptor is readable or writable 721I/O watchers check whether a file descriptor is readable or writable
742 764
743Configures an C<ev_io> watcher. The C<fd> is the file descriptor to 765Configures an C<ev_io> watcher. The C<fd> is the file descriptor to
744rceeive events for and events is either C<EV_READ>, C<EV_WRITE> or 766rceeive events for and events is either C<EV_READ>, C<EV_WRITE> or
745C<EV_READ | EV_WRITE> to receive the given events. 767C<EV_READ | EV_WRITE> to receive the given events.
746 768
769=item int fd [read-only]
770
771The file descriptor being watched.
772
773=item int events [read-only]
774
775The events being watched.
776
747=back 777=back
748 778
749Example: call C<stdin_readable_cb> when STDIN_FILENO has become, well 779Example: call C<stdin_readable_cb> when STDIN_FILENO has become, well
750readable, but only once. Since it is likely line-buffered, you could 780readable, but only once. Since it is likely line-buffered, you could
751attempt to read a whole line in the callback: 781attempt to read a whole line in the callback:
814 844
815If the timer is repeating, either start it if necessary (with the repeat 845If the timer is repeating, either start it if necessary (with the repeat
816value), or reset the running timer to the repeat value. 846value), or reset the running timer to the repeat value.
817 847
818This sounds a bit complicated, but here is a useful and typical 848This sounds a bit complicated, but here is a useful and typical
819example: Imagine you have a tcp connection and you want a so-called idle 849example: Imagine you have a tcp connection and you want a so-called
820timeout, that is, you want to be called when there have been, say, 60 850idle timeout, that is, you want to be called when there have been,
821seconds of inactivity on the socket. The easiest way to do this is to 851say, 60 seconds of inactivity on the socket. The easiest way to do
822configure an C<ev_timer> with after=repeat=60 and calling ev_timer_again each 852this is to configure an C<ev_timer> with C<after>=C<repeat>=C<60> and calling
823time you successfully read or write some data. If you go into an idle 853C<ev_timer_again> each time you successfully read or write some data. If
824state where you do not expect data to travel on the socket, you can stop 854you go into an idle state where you do not expect data to travel on the
825the timer, and again will automatically restart it if need be. 855socket, you can stop the timer, and again will automatically restart it if
856need be.
857
858You can also ignore the C<after> value and C<ev_timer_start> altogether
859and only ever use the C<repeat> value:
860
861 ev_timer_init (timer, callback, 0., 5.);
862 ev_timer_again (loop, timer);
863 ...
864 timer->again = 17.;
865 ev_timer_again (loop, timer);
866 ...
867 timer->again = 10.;
868 ev_timer_again (loop, timer);
869
870This is more efficient then stopping/starting the timer eahc time you want
871to modify its timeout value.
872
873=item ev_tstamp repeat [read-write]
874
875The current C<repeat> value. Will be used each time the watcher times out
876or C<ev_timer_again> is called and determines the next timeout (if any),
877which is also when any modifications are taken into account.
826 878
827=back 879=back
828 880
829Example: create a timer that fires after 60 seconds. 881Example: create a timer that fires after 60 seconds.
830 882
957Simply stops and restarts the periodic watcher again. This is only useful 1009Simply stops and restarts the periodic watcher again. This is only useful
958when you changed some parameters or the reschedule callback would return 1010when you changed some parameters or the reschedule callback would return
959a different time than the last time it was called (e.g. in a crond like 1011a different time than the last time it was called (e.g. in a crond like
960program when the crontabs have changed). 1012program when the crontabs have changed).
961 1013
1014=item ev_tstamp interval [read-write]
1015
1016The current interval value. Can be modified any time, but changes only
1017take effect when the periodic timer fires or C<ev_periodic_again> is being
1018called.
1019
1020=item ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write]
1021
1022The current reschedule callback, or C<0>, if this functionality is
1023switched off. Can be changed any time, but changes only take effect when
1024the periodic timer fires or C<ev_periodic_again> is being called.
1025
962=back 1026=back
963 1027
964Example: call a callback every hour, or, more precisely, whenever the 1028Example: call a callback every hour, or, more precisely, whenever the
965system clock is divisible by 3600. The callback invocation times have 1029system clock is divisible by 3600. The callback invocation times have
966potentially a lot of jittering, but good long-term stability. 1030potentially a lot of jittering, but good long-term stability.
1016=item ev_signal_set (ev_signal *, int signum) 1080=item ev_signal_set (ev_signal *, int signum)
1017 1081
1018Configures the watcher to trigger on the given signal number (usually one 1082Configures the watcher to trigger on the given signal number (usually one
1019of the C<SIGxxx> constants). 1083of the C<SIGxxx> constants).
1020 1084
1085=item int signum [read-only]
1086
1087The signal the watcher watches out for.
1088
1021=back 1089=back
1022 1090
1023 1091
1024=head2 C<ev_child> - watch out for process status changes 1092=head2 C<ev_child> - watch out for process status changes
1025 1093
1037at the C<rstatus> member of the C<ev_child> watcher structure to see 1105at the C<rstatus> member of the C<ev_child> watcher structure to see
1038the status word (use the macros from C<sys/wait.h> and see your systems 1106the status word (use the macros from C<sys/wait.h> and see your systems
1039C<waitpid> documentation). The C<rpid> member contains the pid of the 1107C<waitpid> documentation). The C<rpid> member contains the pid of the
1040process causing the status change. 1108process causing the status change.
1041 1109
1110=item int pid [read-only]
1111
1112The process id this watcher watches out for, or C<0>, meaning any process id.
1113
1114=item int rpid [read-write]
1115
1116The process id that detected a status change.
1117
1118=item int rstatus [read-write]
1119
1120The process exit/trace status caused by C<rpid> (see your systems
1121C<waitpid> and C<sys/wait.h> documentation for details).
1122
1042=back 1123=back
1043 1124
1044Example: try to exit cleanly on SIGINT and SIGTERM. 1125Example: try to exit cleanly on SIGINT and SIGTERM.
1045 1126
1046 static void 1127 static void
1050 } 1131 }
1051 1132
1052 struct ev_signal signal_watcher; 1133 struct ev_signal signal_watcher;
1053 ev_signal_init (&signal_watcher, sigint_cb, SIGINT); 1134 ev_signal_init (&signal_watcher, sigint_cb, SIGINT);
1054 ev_signal_start (loop, &sigint_cb); 1135 ev_signal_start (loop, &sigint_cb);
1136
1137
1138=head2 C<ev_stat> - did the file attributes just change?
1139
1140This watches a filesystem path for attribute changes. That is, it calls
1141C<stat> regularly (or when the OS says it changed) and sees if it changed
1142compared to the last time, invoking the callback if it did.
1143
1144The path does not need to exist: changing from "path exists" to "path does
1145not exist" is a status change like any other. The condition "path does
1146not exist" is signified by the C<st_nlink> field being zero (which is
1147otherwise always forced to be at least one) and all the other fields of
1148the stat buffer having unspecified contents.
1149
1150Since there is no standard to do this, the portable implementation simply
1151calls C<stat (2)> regulalry on the path to see if it changed somehow. You
1152can specify a recommended polling interval for this case. If you specify
1153a polling interval of C<0> (highly recommended!) then a I<suitable,
1154unspecified default> value will be used (which you can expect to be around
1155five seconds, although this might change dynamically). Libev will also
1156impose a minimum interval which is currently around C<0.1>, but thats
1157usually overkill.
1158
1159This watcher type is not meant for massive numbers of stat watchers,
1160as even with OS-supported change notifications, this can be
1161resource-intensive.
1162
1163At the time of this writing, no specific OS backends are implemented, but
1164if demand increases, at least a kqueue and inotify backend will be added.
1165
1166=over 4
1167
1168=item ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)
1169
1170=item ev_stat_set (ev_stat *, const char *path, ev_tstamp interval)
1171
1172Configures the watcher to wait for status changes of the given
1173C<path>. The C<interval> is a hint on how quickly a change is expected to
1174be detected and should normally be specified as C<0> to let libev choose
1175a suitable value. The memory pointed to by C<path> must point to the same
1176path for as long as the watcher is active.
1177
1178The callback will be receive C<EV_STAT> when a change was detected,
1179relative to the attributes at the time the watcher was started (or the
1180last change was detected).
1181
1182=item ev_stat_stat (ev_stat *)
1183
1184Updates the stat buffer immediately with new values. If you change the
1185watched path in your callback, you could call this fucntion to avoid
1186detecting this change (while introducing a race condition). Can also be
1187useful simply to find out the new values.
1188
1189=item ev_statdata attr [read-only]
1190
1191The most-recently detected attributes of the file. Although the type is of
1192C<ev_statdata>, this is usually the (or one of the) C<struct stat> types
1193suitable for your system. If the C<st_nlink> member is C<0>, then there
1194was some error while C<stat>ing the file.
1195
1196=item ev_statdata prev [read-only]
1197
1198The previous attributes of the file. The callback gets invoked whenever
1199C<prev> != C<attr>.
1200
1201=item ev_tstamp interval [read-only]
1202
1203The specified interval.
1204
1205=item const char *path [read-only]
1206
1207The filesystem path that is being watched.
1208
1209=back
1210
1211Example: Watch C</etc/passwd> for attribute changes.
1212
1213 static void
1214 passwd_cb (struct ev_loop *loop, ev_stat *w, int revents)
1215 {
1216 /* /etc/passwd changed in some way */
1217 if (w->attr.st_nlink)
1218 {
1219 printf ("passwd current size %ld\n", (long)w->attr.st_size);
1220 printf ("passwd current atime %ld\n", (long)w->attr.st_mtime);
1221 printf ("passwd current mtime %ld\n", (long)w->attr.st_mtime);
1222 }
1223 else
1224 /* you shalt not abuse printf for puts */
1225 puts ("wow, /etc/passwd is not there, expect problems. "
1226 "if this is windows, they already arrived\n");
1227 }
1228
1229 ...
1230 ev_stat passwd;
1231
1232 ev_stat_init (&passwd, passwd_cb, "/etc/passwd");
1233 ev_stat_start (loop, &passwd);
1055 1234
1056 1235
1057=head2 C<ev_idle> - when you've got nothing better to do... 1236=head2 C<ev_idle> - when you've got nothing better to do...
1058 1237
1059Idle watchers trigger events when there are no other events are pending 1238Idle watchers trigger events when there are no other events are pending
1292 1471
1293Make a single, non-blocking sweep over the embedded loop. This works 1472Make a single, non-blocking sweep over the embedded loop. This works
1294similarly to C<ev_loop (embedded_loop, EVLOOP_NONBLOCK)>, but in the most 1473similarly to C<ev_loop (embedded_loop, EVLOOP_NONBLOCK)>, but in the most
1295apropriate way for embedded loops. 1474apropriate way for embedded loops.
1296 1475
1476=item struct ev_loop *loop [read-only]
1477
1478The embedded event loop.
1479
1480=back
1481
1482
1483=head2 C<ev_fork> - the audacity to resume the event loop after a fork
1484
1485Fork watchers are called when a C<fork ()> was detected (usually because
1486whoever is a good citizen cared to tell libev about it by calling
1487C<ev_default_fork> or C<ev_loop_fork>). The invocation is done before the
1488event loop blocks next and before C<ev_check> watchers are being called,
1489and only in the child after the fork. If whoever good citizen calling
1490C<ev_default_fork> cheats and calls it in the wrong process, the fork
1491handlers will be invoked, too, of course.
1492
1493=over 4
1494
1495=item ev_fork_init (ev_signal *, callback)
1496
1497Initialises and configures the fork watcher - it has no parameters of any
1498kind. There is a C<ev_fork_set> macro, but using it is utterly pointless,
1499believe me.
1500
1297=back 1501=back
1298 1502
1299 1503
1300=head1 OTHER FUNCTIONS 1504=head1 OTHER FUNCTIONS
1301 1505
1463 1667
1464=item w->sweep () C<ev::embed> only 1668=item w->sweep () C<ev::embed> only
1465 1669
1466Invokes C<ev_embed_sweep>. 1670Invokes C<ev_embed_sweep>.
1467 1671
1672=item w->update () C<ev::stat> only
1673
1674Invokes C<ev_stat_stat>.
1675
1468=back 1676=back
1469 1677
1470=back 1678=back
1471 1679
1472Example: Define a class with an IO and idle watcher, start one of them in 1680Example: Define a class with an IO and idle watcher, start one of them in
1484 : io (this, &myclass::io_cb), 1692 : io (this, &myclass::io_cb),
1485 idle (this, &myclass::idle_cb) 1693 idle (this, &myclass::idle_cb)
1486 { 1694 {
1487 io.start (fd, ev::READ); 1695 io.start (fd, ev::READ);
1488 } 1696 }
1697
1698
1699=head1 MACRO MAGIC
1700
1701Libev can be compiled with a variety of options, the most fundemantal is
1702C<EV_MULTIPLICITY>. This option determines wether (most) functions and
1703callbacks have an initial C<struct ev_loop *> argument.
1704
1705To make it easier to write programs that cope with either variant, the
1706following macros are defined:
1707
1708=over 4
1709
1710=item C<EV_A>, C<EV_A_>
1711
1712This provides the loop I<argument> for functions, if one is required ("ev
1713loop argument"). The C<EV_A> form is used when this is the sole argument,
1714C<EV_A_> is used when other arguments are following. Example:
1715
1716 ev_unref (EV_A);
1717 ev_timer_add (EV_A_ watcher);
1718 ev_loop (EV_A_ 0);
1719
1720It assumes the variable C<loop> of type C<struct ev_loop *> is in scope,
1721which is often provided by the following macro.
1722
1723=item C<EV_P>, C<EV_P_>
1724
1725This provides the loop I<parameter> for functions, if one is required ("ev
1726loop parameter"). The C<EV_P> form is used when this is the sole parameter,
1727C<EV_P_> is used when other parameters are following. Example:
1728
1729 // this is how ev_unref is being declared
1730 static void ev_unref (EV_P);
1731
1732 // this is how you can declare your typical callback
1733 static void cb (EV_P_ ev_timer *w, int revents)
1734
1735It declares a parameter C<loop> of type C<struct ev_loop *>, quite
1736suitable for use with C<EV_A>.
1737
1738=item C<EV_DEFAULT>, C<EV_DEFAULT_>
1739
1740Similar to the other two macros, this gives you the value of the default
1741loop, if multiple loops are supported ("ev loop default").
1742
1743=back
1744
1745Example: Declare and initialise a check watcher, working regardless of
1746wether multiple loops are supported or not.
1747
1748 static void
1749 check_cb (EV_P_ ev_timer *w, int revents)
1750 {
1751 ev_check_stop (EV_A_ w);
1752 }
1753
1754 ev_check check;
1755 ev_check_init (&check, check_cb);
1756 ev_check_start (EV_DEFAULT_ &check);
1757 ev_loop (EV_DEFAULT_ 0);
1758
1489 1759
1490=head1 EMBEDDING 1760=head1 EMBEDDING
1491 1761
1492Libev can (and often is) directly embedded into host 1762Libev can (and often is) directly embedded into host
1493applications. Examples of applications that embed it include the Deliantra 1763applications. Examples of applications that embed it include the Deliantra
1700will have the C<struct ev_loop *> as first argument, and you can create 1970will have the C<struct ev_loop *> as first argument, and you can create
1701additional independent event loops. Otherwise there will be no support 1971additional independent event loops. Otherwise there will be no support
1702for multiple event loops and there is no first event loop pointer 1972for multiple event loops and there is no first event loop pointer
1703argument. Instead, all functions act on the single default loop. 1973argument. Instead, all functions act on the single default loop.
1704 1974
1705=item EV_PERIODICS 1975=item EV_PERIODIC_ENABLE
1706 1976
1707If undefined or defined to be C<1>, then periodic timers are supported, 1977If undefined or defined to be C<1>, then periodic timers are supported. If
1708otherwise not. This saves a few kb of code. 1978defined to be C<0>, then they are not. Disabling them saves a few kB of
1979code.
1980
1981=item EV_EMBED_ENABLE
1982
1983If undefined or defined to be C<1>, then embed watchers are supported. If
1984defined to be C<0>, then they are not.
1985
1986=item EV_STAT_ENABLE
1987
1988If undefined or defined to be C<1>, then stat watchers are supported. If
1989defined to be C<0>, then they are not.
1990
1991=item EV_FORK_ENABLE
1992
1993If undefined or defined to be C<1>, then fork watchers are supported. If
1994defined to be C<0>, then they are not.
1995
1996=item EV_MINIMAL
1997
1998If you need to shave off some kilobytes of code at the expense of some
1999speed, define this symbol to C<1>. Currently only used for gcc to override
2000some inlining decisions, saves roughly 30% codesize of amd64.
2001
2002=item EV_PID_HASHSIZE
2003
2004C<ev_child> watchers use a small hash table to distribute workload by
2005pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more
2006than enough. If you need to manage thousands of children you might want to
2007increase this value.
1709 2008
1710=item EV_COMMON 2009=item EV_COMMON
1711 2010
1712By default, all watchers have a C<void *data> member. By redefining 2011By default, all watchers have a C<void *data> member. By redefining
1713this macro to a something else you can include more and other types of 2012this macro to a something else you can include more and other types of

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines