1*93823e6cSPaul Bohm=encoding utf-8 2*93823e6cSPaul Bohm 3e3a38431SPaul Bohm=head1 NAME 4e3a38431SPaul Bohm 5e3a38431SPaul Bohmlibev - a high performance full-featured event loop written in C 6e3a38431SPaul Bohm 7e3a38431SPaul Bohm=head1 SYNOPSIS 8e3a38431SPaul Bohm 9e3a38431SPaul Bohm #include <ev.h> 10e3a38431SPaul Bohm 11e3a38431SPaul Bohm=head2 EXAMPLE PROGRAM 12e3a38431SPaul Bohm 13e3a38431SPaul Bohm // a single header file is required 14e3a38431SPaul Bohm #include <ev.h> 15e3a38431SPaul Bohm 16e3a38431SPaul Bohm #include <stdio.h> // for puts 17e3a38431SPaul Bohm 18e3a38431SPaul Bohm // every watcher type has its own typedef'd struct 19e3a38431SPaul Bohm // with the name ev_TYPE 20e3a38431SPaul Bohm ev_io stdin_watcher; 21e3a38431SPaul Bohm ev_timer timeout_watcher; 22e3a38431SPaul Bohm 23e3a38431SPaul Bohm // all watcher callbacks have a similar signature 24e3a38431SPaul Bohm // this callback is called when data is readable on stdin 25e3a38431SPaul Bohm static void 26e3a38431SPaul Bohm stdin_cb (EV_P_ ev_io *w, int revents) 27e3a38431SPaul Bohm { 28e3a38431SPaul Bohm puts ("stdin ready"); 29e3a38431SPaul Bohm // for one-shot events, one must manually stop the watcher 30e3a38431SPaul Bohm // with its corresponding stop function. 31e3a38431SPaul Bohm ev_io_stop (EV_A_ w); 32e3a38431SPaul Bohm 33e3a38431SPaul Bohm // this causes all nested ev_run's to stop iterating 34e3a38431SPaul Bohm ev_break (EV_A_ EVBREAK_ALL); 35e3a38431SPaul Bohm } 36e3a38431SPaul Bohm 37e3a38431SPaul Bohm // another callback, this time for a time-out 38e3a38431SPaul Bohm static void 39e3a38431SPaul Bohm timeout_cb (EV_P_ ev_timer *w, int revents) 40e3a38431SPaul Bohm { 41e3a38431SPaul Bohm puts ("timeout"); 42e3a38431SPaul Bohm // this causes the innermost ev_run to stop iterating 43e3a38431SPaul Bohm ev_break (EV_A_ EVBREAK_ONE); 44e3a38431SPaul Bohm } 45e3a38431SPaul Bohm 46e3a38431SPaul Bohm int 47e3a38431SPaul Bohm main (void) 48e3a38431SPaul Bohm { 49e3a38431SPaul Bohm // use the default event loop unless you have special needs 50e3a38431SPaul Bohm struct ev_loop *loop = EV_DEFAULT; 51e3a38431SPaul Bohm 52e3a38431SPaul Bohm // initialise an io watcher, then start it 53e3a38431SPaul Bohm // this one will watch for stdin to become readable 54e3a38431SPaul Bohm ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); 55e3a38431SPaul Bohm ev_io_start (loop, &stdin_watcher); 56e3a38431SPaul Bohm 57e3a38431SPaul Bohm // initialise a timer watcher, then start it 58e3a38431SPaul Bohm // simple non-repeating 5.5 second timeout 59e3a38431SPaul Bohm ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); 60e3a38431SPaul Bohm ev_timer_start (loop, &timeout_watcher); 61e3a38431SPaul Bohm 62e3a38431SPaul Bohm // now wait for events to arrive 63e3a38431SPaul Bohm ev_run (loop, 0); 64e3a38431SPaul Bohm 65e3a38431SPaul Bohm // break was called, so exit 66e3a38431SPaul Bohm return 0; 67e3a38431SPaul Bohm } 68e3a38431SPaul Bohm 69e3a38431SPaul Bohm=head1 ABOUT THIS DOCUMENT 70e3a38431SPaul Bohm 71e3a38431SPaul BohmThis document documents the libev software package. 72e3a38431SPaul Bohm 73e3a38431SPaul BohmThe newest version of this document is also available as an html-formatted 74e3a38431SPaul Bohmweb page you might find easier to navigate when reading it for the first 75e3a38431SPaul Bohmtime: L<http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>. 76e3a38431SPaul Bohm 77e3a38431SPaul BohmWhile this document tries to be as complete as possible in documenting 78e3a38431SPaul Bohmlibev, its usage and the rationale behind its design, it is not a tutorial 79e3a38431SPaul Bohmon event-based programming, nor will it introduce event-based programming 80e3a38431SPaul Bohmwith libev. 81e3a38431SPaul Bohm 82e3a38431SPaul BohmFamiliarity with event based programming techniques in general is assumed 83e3a38431SPaul Bohmthroughout this document. 84e3a38431SPaul Bohm 85e3a38431SPaul Bohm=head1 WHAT TO READ WHEN IN A HURRY 86e3a38431SPaul Bohm 87e3a38431SPaul BohmThis manual tries to be very detailed, but unfortunately, this also makes 88e3a38431SPaul Bohmit very long. If you just want to know the basics of libev, I suggest 89*93823e6cSPaul Bohmreading L</ANATOMY OF A WATCHER>, then the L</EXAMPLE PROGRAM> above and 90*93823e6cSPaul Bohmlook up the missing functions in L</GLOBAL FUNCTIONS> and the C<ev_io> and 91*93823e6cSPaul BohmC<ev_timer> sections in L</WATCHER TYPES>. 92e3a38431SPaul Bohm 93e3a38431SPaul Bohm=head1 ABOUT LIBEV 94e3a38431SPaul Bohm 95e3a38431SPaul BohmLibev is an event loop: you register interest in certain events (such as a 96e3a38431SPaul Bohmfile descriptor being readable or a timeout occurring), and it will manage 97e3a38431SPaul Bohmthese event sources and provide your program with events. 98e3a38431SPaul Bohm 99e3a38431SPaul BohmTo do this, it must take more or less complete control over your process 100e3a38431SPaul Bohm(or thread) by executing the I<event loop> handler, and will then 101e3a38431SPaul Bohmcommunicate events via a callback mechanism. 102e3a38431SPaul Bohm 103e3a38431SPaul BohmYou register interest in certain events by registering so-called I<event 104e3a38431SPaul Bohmwatchers>, which are relatively small C structures you initialise with the 105e3a38431SPaul Bohmdetails of the event, and then hand it over to libev by I<starting> the 106e3a38431SPaul Bohmwatcher. 107e3a38431SPaul Bohm 108e3a38431SPaul Bohm=head2 FEATURES 109e3a38431SPaul Bohm 110e3a38431SPaul BohmLibev supports C<select>, C<poll>, the Linux-specific C<epoll>, the 111e3a38431SPaul BohmBSD-specific C<kqueue> and the Solaris-specific event port mechanisms 112e3a38431SPaul Bohmfor file descriptor events (C<ev_io>), the Linux C<inotify> interface 113e3a38431SPaul Bohm(for C<ev_stat>), Linux eventfd/signalfd (for faster and cleaner 114e3a38431SPaul Bohminter-thread wakeup (C<ev_async>)/signal handling (C<ev_signal>)) relative 115e3a38431SPaul Bohmtimers (C<ev_timer>), absolute timers with customised rescheduling 116e3a38431SPaul Bohm(C<ev_periodic>), synchronous signals (C<ev_signal>), process status 117e3a38431SPaul Bohmchange events (C<ev_child>), and event watchers dealing with the event 118e3a38431SPaul Bohmloop mechanism itself (C<ev_idle>, C<ev_embed>, C<ev_prepare> and 119e3a38431SPaul BohmC<ev_check> watchers) as well as file watchers (C<ev_stat>) and even 120e3a38431SPaul Bohmlimited support for fork events (C<ev_fork>). 121e3a38431SPaul Bohm 122e3a38431SPaul BohmIt also is quite fast (see this 123e3a38431SPaul BohmL<benchmark|http://libev.schmorp.de/bench.html> comparing it to libevent 124e3a38431SPaul Bohmfor example). 125e3a38431SPaul Bohm 126e3a38431SPaul Bohm=head2 CONVENTIONS 127e3a38431SPaul Bohm 128e3a38431SPaul BohmLibev is very configurable. In this manual the default (and most common) 129e3a38431SPaul Bohmconfiguration will be described, which supports multiple event loops. For 130e3a38431SPaul Bohmmore info about various configuration options please have a look at 131e3a38431SPaul BohmB<EMBED> section in this manual. If libev was configured without support 132e3a38431SPaul Bohmfor multiple event loops, then all functions taking an initial argument of 133e3a38431SPaul Bohmname C<loop> (which is always of type C<struct ev_loop *>) will not have 134e3a38431SPaul Bohmthis argument. 135e3a38431SPaul Bohm 136e3a38431SPaul Bohm=head2 TIME REPRESENTATION 137e3a38431SPaul Bohm 138e3a38431SPaul BohmLibev represents time as a single floating point number, representing 139e3a38431SPaul Bohmthe (fractional) number of seconds since the (POSIX) epoch (in practice 140e3a38431SPaul Bohmsomewhere near the beginning of 1970, details are complicated, don't 141e3a38431SPaul Bohmask). This type is called C<ev_tstamp>, which is what you should use 142e3a38431SPaul Bohmtoo. It usually aliases to the C<double> type in C. When you need to do 143e3a38431SPaul Bohmany calculations on it, you should treat it as some floating point value. 144e3a38431SPaul Bohm 145e3a38431SPaul BohmUnlike the name component C<stamp> might indicate, it is also used for 146e3a38431SPaul Bohmtime differences (e.g. delays) throughout libev. 147e3a38431SPaul Bohm 148e3a38431SPaul Bohm=head1 ERROR HANDLING 149e3a38431SPaul Bohm 150e3a38431SPaul BohmLibev knows three classes of errors: operating system errors, usage errors 151e3a38431SPaul Bohmand internal errors (bugs). 152e3a38431SPaul Bohm 153e3a38431SPaul BohmWhen libev catches an operating system error it cannot handle (for example 154e3a38431SPaul Bohma system call indicating a condition libev cannot fix), it calls the callback 155e3a38431SPaul Bohmset via C<ev_set_syserr_cb>, which is supposed to fix the problem or 156e3a38431SPaul Bohmabort. The default is to print a diagnostic message and to call C<abort 157e3a38431SPaul Bohm()>. 158e3a38431SPaul Bohm 159e3a38431SPaul BohmWhen libev detects a usage error such as a negative timer interval, then 160e3a38431SPaul Bohmit will print a diagnostic message and abort (via the C<assert> mechanism, 161e3a38431SPaul Bohmso C<NDEBUG> will disable this checking): these are programming errors in 162e3a38431SPaul Bohmthe libev caller and need to be fixed there. 163e3a38431SPaul Bohm 164e3a38431SPaul BohmLibev also has a few internal error-checking C<assert>ions, and also has 165e3a38431SPaul Bohmextensive consistency checking code. These do not trigger under normal 166e3a38431SPaul Bohmcircumstances, as they indicate either a bug in libev or worse. 167e3a38431SPaul Bohm 168e3a38431SPaul Bohm 169e3a38431SPaul Bohm=head1 GLOBAL FUNCTIONS 170e3a38431SPaul Bohm 171e3a38431SPaul BohmThese functions can be called anytime, even before initialising the 172e3a38431SPaul Bohmlibrary in any way. 173e3a38431SPaul Bohm 174e3a38431SPaul Bohm=over 4 175e3a38431SPaul Bohm 176e3a38431SPaul Bohm=item ev_tstamp ev_time () 177e3a38431SPaul Bohm 178e3a38431SPaul BohmReturns the current time as libev would use it. Please note that the 179e3a38431SPaul BohmC<ev_now> function is usually faster and also often returns the timestamp 180e3a38431SPaul Bohmyou actually want to know. Also interesting is the combination of 181*93823e6cSPaul BohmC<ev_now_update> and C<ev_now>. 182e3a38431SPaul Bohm 183e3a38431SPaul Bohm=item ev_sleep (ev_tstamp interval) 184e3a38431SPaul Bohm 185*93823e6cSPaul BohmSleep for the given interval: The current thread will be blocked 186*93823e6cSPaul Bohmuntil either it is interrupted or the given time interval has 187*93823e6cSPaul Bohmpassed (approximately - it might return a bit earlier even if not 188*93823e6cSPaul Bohminterrupted). Returns immediately if C<< interval <= 0 >>. 189*93823e6cSPaul Bohm 190*93823e6cSPaul BohmBasically this is a sub-second-resolution C<sleep ()>. 191*93823e6cSPaul Bohm 192*93823e6cSPaul BohmThe range of the C<interval> is limited - libev only guarantees to work 193*93823e6cSPaul Bohmwith sleep times of up to one day (C<< interval <= 86400 >>). 194e3a38431SPaul Bohm 195e3a38431SPaul Bohm=item int ev_version_major () 196e3a38431SPaul Bohm 197e3a38431SPaul Bohm=item int ev_version_minor () 198e3a38431SPaul Bohm 199e3a38431SPaul BohmYou can find out the major and minor ABI version numbers of the library 200e3a38431SPaul Bohmyou linked against by calling the functions C<ev_version_major> and 201e3a38431SPaul BohmC<ev_version_minor>. If you want, you can compare against the global 202e3a38431SPaul Bohmsymbols C<EV_VERSION_MAJOR> and C<EV_VERSION_MINOR>, which specify the 203e3a38431SPaul Bohmversion of the library your program was compiled against. 204e3a38431SPaul Bohm 205e3a38431SPaul BohmThese version numbers refer to the ABI version of the library, not the 206e3a38431SPaul Bohmrelease version. 207e3a38431SPaul Bohm 208e3a38431SPaul BohmUsually, it's a good idea to terminate if the major versions mismatch, 209e3a38431SPaul Bohmas this indicates an incompatible change. Minor versions are usually 210e3a38431SPaul Bohmcompatible to older versions, so a larger minor version alone is usually 211e3a38431SPaul Bohmnot a problem. 212e3a38431SPaul Bohm 213e3a38431SPaul BohmExample: Make sure we haven't accidentally been linked against the wrong 214e3a38431SPaul Bohmversion (note, however, that this will not detect other ABI mismatches, 215e3a38431SPaul Bohmsuch as LFS or reentrancy). 216e3a38431SPaul Bohm 217e3a38431SPaul Bohm assert (("libev version mismatch", 218e3a38431SPaul Bohm ev_version_major () == EV_VERSION_MAJOR 219e3a38431SPaul Bohm && ev_version_minor () >= EV_VERSION_MINOR)); 220e3a38431SPaul Bohm 221e3a38431SPaul Bohm=item unsigned int ev_supported_backends () 222e3a38431SPaul Bohm 223e3a38431SPaul BohmReturn the set of all backends (i.e. their corresponding C<EV_BACKEND_*> 224e3a38431SPaul Bohmvalue) compiled into this binary of libev (independent of their 225e3a38431SPaul Bohmavailability on the system you are running on). See C<ev_default_loop> for 226e3a38431SPaul Bohma description of the set values. 227e3a38431SPaul Bohm 228e3a38431SPaul BohmExample: make sure we have the epoll method, because yeah this is cool and 229e3a38431SPaul Bohma must have and can we have a torrent of it please!!!11 230e3a38431SPaul Bohm 231e3a38431SPaul Bohm assert (("sorry, no epoll, no sex", 232e3a38431SPaul Bohm ev_supported_backends () & EVBACKEND_EPOLL)); 233e3a38431SPaul Bohm 234e3a38431SPaul Bohm=item unsigned int ev_recommended_backends () 235e3a38431SPaul Bohm 236e3a38431SPaul BohmReturn the set of all backends compiled into this binary of libev and 237e3a38431SPaul Bohmalso recommended for this platform, meaning it will work for most file 238e3a38431SPaul Bohmdescriptor types. This set is often smaller than the one returned by 239e3a38431SPaul BohmC<ev_supported_backends>, as for example kqueue is broken on most BSDs 240e3a38431SPaul Bohmand will not be auto-detected unless you explicitly request it (assuming 241e3a38431SPaul Bohmyou know what you are doing). This is the set of backends that libev will 242e3a38431SPaul Bohmprobe for if you specify no backends explicitly. 243e3a38431SPaul Bohm 244e3a38431SPaul Bohm=item unsigned int ev_embeddable_backends () 245e3a38431SPaul Bohm 246e3a38431SPaul BohmReturns the set of backends that are embeddable in other event loops. This 247e3a38431SPaul Bohmvalue is platform-specific but can include backends not available on the 248e3a38431SPaul Bohmcurrent system. To find which embeddable backends might be supported on 249e3a38431SPaul Bohmthe current system, you would need to look at C<ev_embeddable_backends () 250e3a38431SPaul Bohm& ev_supported_backends ()>, likewise for recommended ones. 251e3a38431SPaul Bohm 252e3a38431SPaul BohmSee the description of C<ev_embed> watchers for more info. 253e3a38431SPaul Bohm 254*93823e6cSPaul Bohm=item ev_set_allocator (void *(*cb)(void *ptr, long size) throw ()) 255e3a38431SPaul Bohm 256e3a38431SPaul BohmSets the allocation function to use (the prototype is similar - the 257e3a38431SPaul Bohmsemantics are identical to the C<realloc> C89/SuS/POSIX function). It is 258e3a38431SPaul Bohmused to allocate and free memory (no surprises here). If it returns zero 259e3a38431SPaul Bohmwhen memory needs to be allocated (C<size != 0>), the library might abort 260e3a38431SPaul Bohmor take some potentially destructive action. 261e3a38431SPaul Bohm 262e3a38431SPaul BohmSince some systems (at least OpenBSD and Darwin) fail to implement 263e3a38431SPaul Bohmcorrect C<realloc> semantics, libev will use a wrapper around the system 264e3a38431SPaul BohmC<realloc> and C<free> functions by default. 265e3a38431SPaul Bohm 266e3a38431SPaul BohmYou could override this function in high-availability programs to, say, 267e3a38431SPaul Bohmfree some memory if it cannot allocate memory, to use a special allocator, 268e3a38431SPaul Bohmor even to sleep a while and retry until some memory is available. 269e3a38431SPaul Bohm 270e3a38431SPaul BohmExample: Replace the libev allocator with one that waits a bit and then 271e3a38431SPaul Bohmretries (example requires a standards-compliant C<realloc>). 272e3a38431SPaul Bohm 273e3a38431SPaul Bohm static void * 274e3a38431SPaul Bohm persistent_realloc (void *ptr, size_t size) 275e3a38431SPaul Bohm { 276e3a38431SPaul Bohm for (;;) 277e3a38431SPaul Bohm { 278e3a38431SPaul Bohm void *newptr = realloc (ptr, size); 279e3a38431SPaul Bohm 280e3a38431SPaul Bohm if (newptr) 281e3a38431SPaul Bohm return newptr; 282e3a38431SPaul Bohm 283e3a38431SPaul Bohm sleep (60); 284e3a38431SPaul Bohm } 285e3a38431SPaul Bohm } 286e3a38431SPaul Bohm 287e3a38431SPaul Bohm ... 288e3a38431SPaul Bohm ev_set_allocator (persistent_realloc); 289e3a38431SPaul Bohm 290*93823e6cSPaul Bohm=item ev_set_syserr_cb (void (*cb)(const char *msg) throw ()) 291e3a38431SPaul Bohm 292e3a38431SPaul BohmSet the callback function to call on a retryable system call error (such 293e3a38431SPaul Bohmas failed select, poll, epoll_wait). The message is a printable string 294e3a38431SPaul Bohmindicating the system call or subsystem causing the problem. If this 295e3a38431SPaul Bohmcallback is set, then libev will expect it to remedy the situation, no 296e3a38431SPaul Bohmmatter what, when it returns. That is, libev will generally retry the 297e3a38431SPaul Bohmrequested operation, or, if the condition doesn't go away, do bad stuff 298e3a38431SPaul Bohm(such as abort). 299e3a38431SPaul Bohm 300e3a38431SPaul BohmExample: This is basically the same thing that libev does internally, too. 301e3a38431SPaul Bohm 302e3a38431SPaul Bohm static void 303e3a38431SPaul Bohm fatal_error (const char *msg) 304e3a38431SPaul Bohm { 305e3a38431SPaul Bohm perror (msg); 306e3a38431SPaul Bohm abort (); 307e3a38431SPaul Bohm } 308e3a38431SPaul Bohm 309e3a38431SPaul Bohm ... 310e3a38431SPaul Bohm ev_set_syserr_cb (fatal_error); 311e3a38431SPaul Bohm 312e3a38431SPaul Bohm=item ev_feed_signal (int signum) 313e3a38431SPaul Bohm 314e3a38431SPaul BohmThis function can be used to "simulate" a signal receive. It is completely 315e3a38431SPaul Bohmsafe to call this function at any time, from any context, including signal 316e3a38431SPaul Bohmhandlers or random threads. 317e3a38431SPaul Bohm 318e3a38431SPaul BohmIts main use is to customise signal handling in your process, especially 319e3a38431SPaul Bohmin the presence of threads. For example, you could block signals 320e3a38431SPaul Bohmby default in all threads (and specifying C<EVFLAG_NOSIGMASK> when 321e3a38431SPaul Bohmcreating any loops), and in one thread, use C<sigwait> or any other 322e3a38431SPaul Bohmmechanism to wait for signals, then "deliver" them to libev by calling 323e3a38431SPaul BohmC<ev_feed_signal>. 324e3a38431SPaul Bohm 325e3a38431SPaul Bohm=back 326e3a38431SPaul Bohm 327e3a38431SPaul Bohm=head1 FUNCTIONS CONTROLLING EVENT LOOPS 328e3a38431SPaul Bohm 329e3a38431SPaul BohmAn event loop is described by a C<struct ev_loop *> (the C<struct> is 330e3a38431SPaul BohmI<not> optional in this case unless libev 3 compatibility is disabled, as 331e3a38431SPaul Bohmlibev 3 had an C<ev_loop> function colliding with the struct name). 332e3a38431SPaul Bohm 333e3a38431SPaul BohmThe library knows two types of such loops, the I<default> loop, which 334e3a38431SPaul Bohmsupports child process events, and dynamically created event loops which 335e3a38431SPaul Bohmdo not. 336e3a38431SPaul Bohm 337e3a38431SPaul Bohm=over 4 338e3a38431SPaul Bohm 339e3a38431SPaul Bohm=item struct ev_loop *ev_default_loop (unsigned int flags) 340e3a38431SPaul Bohm 341e3a38431SPaul BohmThis returns the "default" event loop object, which is what you should 342e3a38431SPaul Bohmnormally use when you just need "the event loop". Event loop objects and 343e3a38431SPaul Bohmthe C<flags> parameter are described in more detail in the entry for 344e3a38431SPaul BohmC<ev_loop_new>. 345e3a38431SPaul Bohm 346e3a38431SPaul BohmIf the default loop is already initialised then this function simply 347e3a38431SPaul Bohmreturns it (and ignores the flags. If that is troubling you, check 348e3a38431SPaul BohmC<ev_backend ()> afterwards). Otherwise it will create it with the given 349e3a38431SPaul Bohmflags, which should almost always be C<0>, unless the caller is also the 350e3a38431SPaul Bohmone calling C<ev_run> or otherwise qualifies as "the main program". 351e3a38431SPaul Bohm 352e3a38431SPaul BohmIf you don't know what event loop to use, use the one returned from this 353e3a38431SPaul Bohmfunction (or via the C<EV_DEFAULT> macro). 354e3a38431SPaul Bohm 355e3a38431SPaul BohmNote that this function is I<not> thread-safe, so if you want to use it 356e3a38431SPaul Bohmfrom multiple threads, you have to employ some kind of mutex (note also 357e3a38431SPaul Bohmthat this case is unlikely, as loops cannot be shared easily between 358e3a38431SPaul Bohmthreads anyway). 359e3a38431SPaul Bohm 360e3a38431SPaul BohmThe default loop is the only loop that can handle C<ev_child> watchers, 361e3a38431SPaul Bohmand to do this, it always registers a handler for C<SIGCHLD>. If this is 362e3a38431SPaul Bohma problem for your application you can either create a dynamic loop with 363e3a38431SPaul BohmC<ev_loop_new> which doesn't do that, or you can simply overwrite the 364e3a38431SPaul BohmC<SIGCHLD> signal handler I<after> calling C<ev_default_init>. 365e3a38431SPaul Bohm 366e3a38431SPaul BohmExample: This is the most typical usage. 367e3a38431SPaul Bohm 368e3a38431SPaul Bohm if (!ev_default_loop (0)) 369e3a38431SPaul Bohm fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); 370e3a38431SPaul Bohm 371e3a38431SPaul BohmExample: Restrict libev to the select and poll backends, and do not allow 372e3a38431SPaul Bohmenvironment settings to be taken into account: 373e3a38431SPaul Bohm 374e3a38431SPaul Bohm ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); 375e3a38431SPaul Bohm 376e3a38431SPaul Bohm=item struct ev_loop *ev_loop_new (unsigned int flags) 377e3a38431SPaul Bohm 378e3a38431SPaul BohmThis will create and initialise a new event loop object. If the loop 379e3a38431SPaul Bohmcould not be initialised, returns false. 380e3a38431SPaul Bohm 381e3a38431SPaul BohmThis function is thread-safe, and one common way to use libev with 382e3a38431SPaul Bohmthreads is indeed to create one loop per thread, and using the default 383e3a38431SPaul Bohmloop in the "main" or "initial" thread. 384e3a38431SPaul Bohm 385e3a38431SPaul BohmThe flags argument can be used to specify special behaviour or specific 386e3a38431SPaul Bohmbackends to use, and is usually specified as C<0> (or C<EVFLAG_AUTO>). 387e3a38431SPaul Bohm 388e3a38431SPaul BohmThe following flags are supported: 389e3a38431SPaul Bohm 390e3a38431SPaul Bohm=over 4 391e3a38431SPaul Bohm 392e3a38431SPaul Bohm=item C<EVFLAG_AUTO> 393e3a38431SPaul Bohm 394e3a38431SPaul BohmThe default flags value. Use this if you have no clue (it's the right 395e3a38431SPaul Bohmthing, believe me). 396e3a38431SPaul Bohm 397e3a38431SPaul Bohm=item C<EVFLAG_NOENV> 398e3a38431SPaul Bohm 399e3a38431SPaul BohmIf this flag bit is or'ed into the flag value (or the program runs setuid 400e3a38431SPaul Bohmor setgid) then libev will I<not> look at the environment variable 401e3a38431SPaul BohmC<LIBEV_FLAGS>. Otherwise (the default), this environment variable will 402e3a38431SPaul Bohmoverride the flags completely if it is found in the environment. This is 403*93823e6cSPaul Bohmuseful to try out specific backends to test their performance, to work 404*93823e6cSPaul Bohmaround bugs, or to make libev threadsafe (accessing environment variables 405*93823e6cSPaul Bohmcannot be done in a threadsafe way, but usually it works if no other 406*93823e6cSPaul Bohmthread modifies them). 407e3a38431SPaul Bohm 408e3a38431SPaul Bohm=item C<EVFLAG_FORKCHECK> 409e3a38431SPaul Bohm 410e3a38431SPaul BohmInstead of calling C<ev_loop_fork> manually after a fork, you can also 411e3a38431SPaul Bohmmake libev check for a fork in each iteration by enabling this flag. 412e3a38431SPaul Bohm 413e3a38431SPaul BohmThis works by calling C<getpid ()> on every iteration of the loop, 414e3a38431SPaul Bohmand thus this might slow down your event loop if you do a lot of loop 415e3a38431SPaul Bohmiterations and little real work, but is usually not noticeable (on my 416e3a38431SPaul BohmGNU/Linux system for example, C<getpid> is actually a simple 5-insn sequence 417e3a38431SPaul Bohmwithout a system call and thus I<very> fast, but my GNU/Linux system also has 418e3a38431SPaul BohmC<pthread_atfork> which is even faster). 419e3a38431SPaul Bohm 420e3a38431SPaul BohmThe big advantage of this flag is that you can forget about fork (and 421*93823e6cSPaul Bohmforget about forgetting to tell libev about forking, although you still 422*93823e6cSPaul Bohmhave to ignore C<SIGPIPE>) when you use this flag. 423e3a38431SPaul Bohm 424e3a38431SPaul BohmThis flag setting cannot be overridden or specified in the C<LIBEV_FLAGS> 425e3a38431SPaul Bohmenvironment variable. 426e3a38431SPaul Bohm 427e3a38431SPaul Bohm=item C<EVFLAG_NOINOTIFY> 428e3a38431SPaul Bohm 429e3a38431SPaul BohmWhen this flag is specified, then libev will not attempt to use the 430e3a38431SPaul BohmI<inotify> API for its C<ev_stat> watchers. Apart from debugging and 431e3a38431SPaul Bohmtesting, this flag can be useful to conserve inotify file descriptors, as 432e3a38431SPaul Bohmotherwise each loop using C<ev_stat> watchers consumes one inotify handle. 433e3a38431SPaul Bohm 434e3a38431SPaul Bohm=item C<EVFLAG_SIGNALFD> 435e3a38431SPaul Bohm 436e3a38431SPaul BohmWhen this flag is specified, then libev will attempt to use the 437e3a38431SPaul BohmI<signalfd> API for its C<ev_signal> (and C<ev_child>) watchers. This API 438e3a38431SPaul Bohmdelivers signals synchronously, which makes it both faster and might make 439e3a38431SPaul Bohmit possible to get the queued signal data. It can also simplify signal 440e3a38431SPaul Bohmhandling with threads, as long as you properly block signals in your 441e3a38431SPaul Bohmthreads that are not interested in handling them. 442e3a38431SPaul Bohm 443e3a38431SPaul BohmSignalfd will not be used by default as this changes your signal mask, and 444e3a38431SPaul Bohmthere are a lot of shoddy libraries and programs (glib's threadpool for 445e3a38431SPaul Bohmexample) that can't properly initialise their signal masks. 446e3a38431SPaul Bohm 447e3a38431SPaul Bohm=item C<EVFLAG_NOSIGMASK> 448e3a38431SPaul Bohm 449e3a38431SPaul BohmWhen this flag is specified, then libev will avoid to modify the signal 450*93823e6cSPaul Bohmmask. Specifically, this means you have to make sure signals are unblocked 451e3a38431SPaul Bohmwhen you want to receive them. 452e3a38431SPaul Bohm 453e3a38431SPaul BohmThis behaviour is useful when you want to do your own signal handling, or 454e3a38431SPaul Bohmwant to handle signals only in specific threads and want to avoid libev 455e3a38431SPaul Bohmunblocking the signals. 456e3a38431SPaul Bohm 457e3a38431SPaul BohmIt's also required by POSIX in a threaded program, as libev calls 458e3a38431SPaul BohmC<sigprocmask>, whose behaviour is officially unspecified. 459e3a38431SPaul Bohm 460e3a38431SPaul BohmThis flag's behaviour will become the default in future versions of libev. 461e3a38431SPaul Bohm 462e3a38431SPaul Bohm=item C<EVBACKEND_SELECT> (value 1, portable select backend) 463e3a38431SPaul Bohm 464e3a38431SPaul BohmThis is your standard select(2) backend. Not I<completely> standard, as 465e3a38431SPaul Bohmlibev tries to roll its own fd_set with no limits on the number of fds, 466e3a38431SPaul Bohmbut if that fails, expect a fairly low limit on the number of fds when 467e3a38431SPaul Bohmusing this backend. It doesn't scale too well (O(highest_fd)), but its 468e3a38431SPaul Bohmusually the fastest backend for a low number of (low-numbered :) fds. 469e3a38431SPaul Bohm 470e3a38431SPaul BohmTo get good performance out of this backend you need a high amount of 471e3a38431SPaul Bohmparallelism (most of the file descriptors should be busy). If you are 472e3a38431SPaul Bohmwriting a server, you should C<accept ()> in a loop to accept as many 473e3a38431SPaul Bohmconnections as possible during one iteration. You might also want to have 474e3a38431SPaul Bohma look at C<ev_set_io_collect_interval ()> to increase the amount of 475e3a38431SPaul Bohmreadiness notifications you get per iteration. 476e3a38431SPaul Bohm 477e3a38431SPaul BohmThis backend maps C<EV_READ> to the C<readfds> set and C<EV_WRITE> to the 478e3a38431SPaul BohmC<writefds> set (and to work around Microsoft Windows bugs, also onto the 479e3a38431SPaul BohmC<exceptfds> set on that platform). 480e3a38431SPaul Bohm 481e3a38431SPaul Bohm=item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows) 482e3a38431SPaul Bohm 483e3a38431SPaul BohmAnd this is your standard poll(2) backend. It's more complicated 484e3a38431SPaul Bohmthan select, but handles sparse fds better and has no artificial 485e3a38431SPaul Bohmlimit on the number of fds you can use (except it will slow down 486e3a38431SPaul Bohmconsiderably with a lot of inactive fds). It scales similarly to select, 487e3a38431SPaul Bohmi.e. O(total_fds). See the entry for C<EVBACKEND_SELECT>, above, for 488e3a38431SPaul Bohmperformance tips. 489e3a38431SPaul Bohm 490e3a38431SPaul BohmThis backend maps C<EV_READ> to C<POLLIN | POLLERR | POLLHUP>, and 491e3a38431SPaul BohmC<EV_WRITE> to C<POLLOUT | POLLERR | POLLHUP>. 492e3a38431SPaul Bohm 493e3a38431SPaul Bohm=item C<EVBACKEND_EPOLL> (value 4, Linux) 494e3a38431SPaul Bohm 495e3a38431SPaul BohmUse the linux-specific epoll(7) interface (for both pre- and post-2.6.9 496e3a38431SPaul Bohmkernels). 497e3a38431SPaul Bohm 498*93823e6cSPaul BohmFor few fds, this backend is a bit little slower than poll and select, but 499*93823e6cSPaul Bohmit scales phenomenally better. While poll and select usually scale like 500*93823e6cSPaul BohmO(total_fds) where total_fds is the total number of fds (or the highest 501*93823e6cSPaul Bohmfd), epoll scales either O(1) or O(active_fds). 502e3a38431SPaul Bohm 503e3a38431SPaul BohmThe epoll mechanism deserves honorable mention as the most misdesigned 504e3a38431SPaul Bohmof the more advanced event mechanisms: mere annoyances include silently 505e3a38431SPaul Bohmdropping file descriptors, requiring a system call per change per file 506e3a38431SPaul Bohmdescriptor (and unnecessary guessing of parameters), problems with dup, 507e3a38431SPaul Bohmreturning before the timeout value, resulting in additional iterations 508e3a38431SPaul Bohm(and only giving 5ms accuracy while select on the same platform gives 509e3a38431SPaul Bohm0.1ms) and so on. The biggest issue is fork races, however - if a program 510e3a38431SPaul Bohmforks then I<both> parent and child process have to recreate the epoll 511e3a38431SPaul Bohmset, which can take considerable time (one syscall per file descriptor) 512e3a38431SPaul Bohmand is of course hard to detect. 513e3a38431SPaul Bohm 514*93823e6cSPaul BohmEpoll is also notoriously buggy - embedding epoll fds I<should> work, 515*93823e6cSPaul Bohmbut of course I<doesn't>, and epoll just loves to report events for 516*93823e6cSPaul Bohmtotally I<different> file descriptors (even already closed ones, so 517*93823e6cSPaul Bohmone cannot even remove them from the set) than registered in the set 518*93823e6cSPaul Bohm(especially on SMP systems). Libev tries to counter these spurious 519*93823e6cSPaul Bohmnotifications by employing an additional generation counter and comparing 520*93823e6cSPaul Bohmthat against the events to filter out spurious ones, recreating the set 521*93823e6cSPaul Bohmwhen required. Epoll also erroneously rounds down timeouts, but gives you 522*93823e6cSPaul Bohmno way to know when and by how much, so sometimes you have to busy-wait 523*93823e6cSPaul Bohmbecause epoll returns immediately despite a nonzero timeout. And last 524e3a38431SPaul Bohmnot least, it also refuses to work with some file descriptors which work 525e3a38431SPaul Bohmperfectly fine with C<select> (files, many character devices...). 526e3a38431SPaul Bohm 527*93823e6cSPaul BohmEpoll is truly the train wreck among event poll mechanisms, a frankenpoll, 528*93823e6cSPaul Bohmcobbled together in a hurry, no thought to design or interaction with 529*93823e6cSPaul Bohmothers. Oh, the pain, will it ever stop... 530e3a38431SPaul Bohm 531e3a38431SPaul BohmWhile stopping, setting and starting an I/O watcher in the same iteration 532e3a38431SPaul Bohmwill result in some caching, there is still a system call per such 533e3a38431SPaul Bohmincident (because the same I<file descriptor> could point to a different 534e3a38431SPaul BohmI<file description> now), so its best to avoid that. Also, C<dup ()>'ed 535e3a38431SPaul Bohmfile descriptors might not work very well if you register events for both 536e3a38431SPaul Bohmfile descriptors. 537e3a38431SPaul Bohm 538e3a38431SPaul BohmBest performance from this backend is achieved by not unregistering all 539e3a38431SPaul Bohmwatchers for a file descriptor until it has been closed, if possible, 540e3a38431SPaul Bohmi.e. keep at least one watcher active per fd at all times. Stopping and 541e3a38431SPaul Bohmstarting a watcher (without re-setting it) also usually doesn't cause 542e3a38431SPaul Bohmextra overhead. A fork can both result in spurious notifications as well 543e3a38431SPaul Bohmas in libev having to destroy and recreate the epoll object, which can 544e3a38431SPaul Bohmtake considerable time and thus should be avoided. 545e3a38431SPaul Bohm 546e3a38431SPaul BohmAll this means that, in practice, C<EVBACKEND_SELECT> can be as fast or 547e3a38431SPaul Bohmfaster than epoll for maybe up to a hundred file descriptors, depending on 548e3a38431SPaul Bohmthe usage. So sad. 549e3a38431SPaul Bohm 550e3a38431SPaul BohmWhile nominally embeddable in other event loops, this feature is broken in 551e3a38431SPaul Bohmall kernel versions tested so far. 552e3a38431SPaul Bohm 553e3a38431SPaul BohmThis backend maps C<EV_READ> and C<EV_WRITE> in the same way as 554e3a38431SPaul BohmC<EVBACKEND_POLL>. 555e3a38431SPaul Bohm 556e3a38431SPaul Bohm=item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) 557e3a38431SPaul Bohm 558e3a38431SPaul BohmKqueue deserves special mention, as at the time of this writing, it 559e3a38431SPaul Bohmwas broken on all BSDs except NetBSD (usually it doesn't work reliably 560e3a38431SPaul Bohmwith anything but sockets and pipes, except on Darwin, where of course 561e3a38431SPaul Bohmit's completely useless). Unlike epoll, however, whose brokenness 562e3a38431SPaul Bohmis by design, these kqueue bugs can (and eventually will) be fixed 563e3a38431SPaul Bohmwithout API changes to existing programs. For this reason it's not being 564e3a38431SPaul Bohm"auto-detected" unless you explicitly specify it in the flags (i.e. using 565e3a38431SPaul BohmC<EVBACKEND_KQUEUE>) or libev was compiled on a known-to-be-good (-enough) 566e3a38431SPaul Bohmsystem like NetBSD. 567e3a38431SPaul Bohm 568e3a38431SPaul BohmYou still can embed kqueue into a normal poll or select backend and use it 569e3a38431SPaul Bohmonly for sockets (after having made sure that sockets work with kqueue on 570e3a38431SPaul Bohmthe target platform). See C<ev_embed> watchers for more info. 571e3a38431SPaul Bohm 572e3a38431SPaul BohmIt scales in the same way as the epoll backend, but the interface to the 573e3a38431SPaul Bohmkernel is more efficient (which says nothing about its actual speed, of 574e3a38431SPaul Bohmcourse). While stopping, setting and starting an I/O watcher does never 575e3a38431SPaul Bohmcause an extra system call as with C<EVBACKEND_EPOLL>, it still adds up to 576*93823e6cSPaul Bohmtwo event changes per incident. Support for C<fork ()> is very bad (you 577*93823e6cSPaul Bohmmight have to leak fd's on fork, but it's more sane than epoll) and it 578*93823e6cSPaul Bohmdrops fds silently in similarly hard-to-detect cases. 579e3a38431SPaul Bohm 580e3a38431SPaul BohmThis backend usually performs well under most conditions. 581e3a38431SPaul Bohm 582e3a38431SPaul BohmWhile nominally embeddable in other event loops, this doesn't work 583e3a38431SPaul Bohmeverywhere, so you might need to test for this. And since it is broken 584e3a38431SPaul Bohmalmost everywhere, you should only use it when you have a lot of sockets 585e3a38431SPaul Bohm(for which it usually works), by embedding it into another event loop 586e3a38431SPaul Bohm(e.g. C<EVBACKEND_SELECT> or C<EVBACKEND_POLL> (but C<poll> is of course 587e3a38431SPaul Bohmalso broken on OS X)) and, did I mention it, using it only for sockets. 588e3a38431SPaul Bohm 589e3a38431SPaul BohmThis backend maps C<EV_READ> into an C<EVFILT_READ> kevent with 590e3a38431SPaul BohmC<NOTE_EOF>, and C<EV_WRITE> into an C<EVFILT_WRITE> kevent with 591e3a38431SPaul BohmC<NOTE_EOF>. 592e3a38431SPaul Bohm 593e3a38431SPaul Bohm=item C<EVBACKEND_DEVPOLL> (value 16, Solaris 8) 594e3a38431SPaul Bohm 595e3a38431SPaul BohmThis is not implemented yet (and might never be, unless you send me an 596e3a38431SPaul Bohmimplementation). According to reports, C</dev/poll> only supports sockets 597e3a38431SPaul Bohmand is not embeddable, which would limit the usefulness of this backend 598e3a38431SPaul Bohmimmensely. 599e3a38431SPaul Bohm 600e3a38431SPaul Bohm=item C<EVBACKEND_PORT> (value 32, Solaris 10) 601e3a38431SPaul Bohm 602e3a38431SPaul BohmThis uses the Solaris 10 event port mechanism. As with everything on Solaris, 603e3a38431SPaul Bohmit's really slow, but it still scales very well (O(active_fds)). 604e3a38431SPaul Bohm 605e3a38431SPaul BohmWhile this backend scales well, it requires one system call per active 606e3a38431SPaul Bohmfile descriptor per loop iteration. For small and medium numbers of file 607e3a38431SPaul Bohmdescriptors a "slow" C<EVBACKEND_SELECT> or C<EVBACKEND_POLL> backend 608e3a38431SPaul Bohmmight perform better. 609e3a38431SPaul Bohm 610e3a38431SPaul BohmOn the positive side, this backend actually performed fully to 611e3a38431SPaul Bohmspecification in all tests and is fully embeddable, which is a rare feat 612e3a38431SPaul Bohmamong the OS-specific backends (I vastly prefer correctness over speed 613e3a38431SPaul Bohmhacks). 614e3a38431SPaul Bohm 615e3a38431SPaul BohmOn the negative side, the interface is I<bizarre> - so bizarre that 616e3a38431SPaul Bohmeven sun itself gets it wrong in their code examples: The event polling 617*93823e6cSPaul Bohmfunction sometimes returns events to the caller even though an error 618e3a38431SPaul Bohmoccurred, but with no indication whether it has done so or not (yes, it's 619*93823e6cSPaul Bohmeven documented that way) - deadly for edge-triggered interfaces where you 620*93823e6cSPaul Bohmabsolutely have to know whether an event occurred or not because you have 621*93823e6cSPaul Bohmto re-arm the watcher. 622e3a38431SPaul Bohm 623e3a38431SPaul BohmFortunately libev seems to be able to work around these idiocies. 624e3a38431SPaul Bohm 625e3a38431SPaul BohmThis backend maps C<EV_READ> and C<EV_WRITE> in the same way as 626e3a38431SPaul BohmC<EVBACKEND_POLL>. 627e3a38431SPaul Bohm 628e3a38431SPaul Bohm=item C<EVBACKEND_ALL> 629e3a38431SPaul Bohm 630e3a38431SPaul BohmTry all backends (even potentially broken ones that wouldn't be tried 631e3a38431SPaul Bohmwith C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as 632e3a38431SPaul BohmC<EVBACKEND_ALL & ~EVBACKEND_KQUEUE>. 633e3a38431SPaul Bohm 634e3a38431SPaul BohmIt is definitely not recommended to use this flag, use whatever 635e3a38431SPaul BohmC<ev_recommended_backends ()> returns, or simply do not specify a backend 636e3a38431SPaul Bohmat all. 637e3a38431SPaul Bohm 638e3a38431SPaul Bohm=item C<EVBACKEND_MASK> 639e3a38431SPaul Bohm 640e3a38431SPaul BohmNot a backend at all, but a mask to select all backend bits from a 641e3a38431SPaul BohmC<flags> value, in case you want to mask out any backends from a flags 642e3a38431SPaul Bohmvalue (e.g. when modifying the C<LIBEV_FLAGS> environment variable). 643e3a38431SPaul Bohm 644e3a38431SPaul Bohm=back 645e3a38431SPaul Bohm 646e3a38431SPaul BohmIf one or more of the backend flags are or'ed into the flags value, 647e3a38431SPaul Bohmthen only these backends will be tried (in the reverse order as listed 648e3a38431SPaul Bohmhere). If none are specified, all backends in C<ev_recommended_backends 649e3a38431SPaul Bohm()> will be tried. 650e3a38431SPaul Bohm 651e3a38431SPaul BohmExample: Try to create a event loop that uses epoll and nothing else. 652e3a38431SPaul Bohm 653e3a38431SPaul Bohm struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); 654e3a38431SPaul Bohm if (!epoller) 655e3a38431SPaul Bohm fatal ("no epoll found here, maybe it hides under your chair"); 656e3a38431SPaul Bohm 657e3a38431SPaul BohmExample: Use whatever libev has to offer, but make sure that kqueue is 658e3a38431SPaul Bohmused if available. 659e3a38431SPaul Bohm 660e3a38431SPaul Bohm struct ev_loop *loop = ev_loop_new (ev_recommended_backends () | EVBACKEND_KQUEUE); 661e3a38431SPaul Bohm 662e3a38431SPaul Bohm=item ev_loop_destroy (loop) 663e3a38431SPaul Bohm 664e3a38431SPaul BohmDestroys an event loop object (frees all memory and kernel state 665e3a38431SPaul Bohmetc.). None of the active event watchers will be stopped in the normal 666e3a38431SPaul Bohmsense, so e.g. C<ev_is_active> might still return true. It is your 667e3a38431SPaul Bohmresponsibility to either stop all watchers cleanly yourself I<before> 668e3a38431SPaul Bohmcalling this function, or cope with the fact afterwards (which is usually 669e3a38431SPaul Bohmthe easiest thing, you can just ignore the watchers and/or C<free ()> them 670e3a38431SPaul Bohmfor example). 671e3a38431SPaul Bohm 672e3a38431SPaul BohmNote that certain global state, such as signal state (and installed signal 673e3a38431SPaul Bohmhandlers), will not be freed by this function, and related watchers (such 674e3a38431SPaul Bohmas signal and child watchers) would need to be stopped manually. 675e3a38431SPaul Bohm 676e3a38431SPaul BohmThis function is normally used on loop objects allocated by 677e3a38431SPaul BohmC<ev_loop_new>, but it can also be used on the default loop returned by 678e3a38431SPaul BohmC<ev_default_loop>, in which case it is not thread-safe. 679e3a38431SPaul Bohm 680e3a38431SPaul BohmNote that it is not advisable to call this function on the default loop 681e3a38431SPaul Bohmexcept in the rare occasion where you really need to free its resources. 682e3a38431SPaul BohmIf you need dynamically allocated loops it is better to use C<ev_loop_new> 683e3a38431SPaul Bohmand C<ev_loop_destroy>. 684e3a38431SPaul Bohm 685e3a38431SPaul Bohm=item ev_loop_fork (loop) 686e3a38431SPaul Bohm 687*93823e6cSPaul BohmThis function sets a flag that causes subsequent C<ev_run> iterations 688*93823e6cSPaul Bohmto reinitialise the kernel state for backends that have one. Despite 689*93823e6cSPaul Bohmthe name, you can call it anytime you are allowed to start or stop 690*93823e6cSPaul Bohmwatchers (except inside an C<ev_prepare> callback), but it makes most 691*93823e6cSPaul Bohmsense after forking, in the child process. You I<must> call it (or use 692*93823e6cSPaul BohmC<EVFLAG_FORKCHECK>) in the child before resuming or calling C<ev_run>. 693*93823e6cSPaul Bohm 694*93823e6cSPaul BohmIn addition, if you want to reuse a loop (via this function or 695*93823e6cSPaul BohmC<EVFLAG_FORKCHECK>), you I<also> have to ignore C<SIGPIPE>. 696e3a38431SPaul Bohm 697e3a38431SPaul BohmAgain, you I<have> to call it on I<any> loop that you want to re-use after 698e3a38431SPaul Bohma fork, I<even if you do not plan to use the loop in the parent>. This is 699e3a38431SPaul Bohmbecause some kernel interfaces *cough* I<kqueue> *cough* do funny things 700e3a38431SPaul Bohmduring fork. 701e3a38431SPaul Bohm 702e3a38431SPaul BohmOn the other hand, you only need to call this function in the child 703e3a38431SPaul Bohmprocess if and only if you want to use the event loop in the child. If 704e3a38431SPaul Bohmyou just fork+exec or create a new loop in the child, you don't have to 705e3a38431SPaul Bohmcall it at all (in fact, C<epoll> is so badly broken that it makes a 706e3a38431SPaul Bohmdifference, but libev will usually detect this case on its own and do a 707e3a38431SPaul Bohmcostly reset of the backend). 708e3a38431SPaul Bohm 709e3a38431SPaul BohmThe function itself is quite fast and it's usually not a problem to call 710e3a38431SPaul Bohmit just in case after a fork. 711e3a38431SPaul Bohm 712e3a38431SPaul BohmExample: Automate calling C<ev_loop_fork> on the default loop when 713e3a38431SPaul Bohmusing pthreads. 714e3a38431SPaul Bohm 715e3a38431SPaul Bohm static void 716e3a38431SPaul Bohm post_fork_child (void) 717e3a38431SPaul Bohm { 718e3a38431SPaul Bohm ev_loop_fork (EV_DEFAULT); 719e3a38431SPaul Bohm } 720e3a38431SPaul Bohm 721e3a38431SPaul Bohm ... 722e3a38431SPaul Bohm pthread_atfork (0, 0, post_fork_child); 723e3a38431SPaul Bohm 724e3a38431SPaul Bohm=item int ev_is_default_loop (loop) 725e3a38431SPaul Bohm 726e3a38431SPaul BohmReturns true when the given loop is, in fact, the default loop, and false 727e3a38431SPaul Bohmotherwise. 728e3a38431SPaul Bohm 729e3a38431SPaul Bohm=item unsigned int ev_iteration (loop) 730e3a38431SPaul Bohm 731e3a38431SPaul BohmReturns the current iteration count for the event loop, which is identical 732e3a38431SPaul Bohmto the number of times libev did poll for new events. It starts at C<0> 733e3a38431SPaul Bohmand happily wraps around with enough iterations. 734e3a38431SPaul Bohm 735e3a38431SPaul BohmThis value can sometimes be useful as a generation counter of sorts (it 736e3a38431SPaul Bohm"ticks" the number of loop iterations), as it roughly corresponds with 737e3a38431SPaul BohmC<ev_prepare> and C<ev_check> calls - and is incremented between the 738e3a38431SPaul Bohmprepare and check phases. 739e3a38431SPaul Bohm 740e3a38431SPaul Bohm=item unsigned int ev_depth (loop) 741e3a38431SPaul Bohm 742e3a38431SPaul BohmReturns the number of times C<ev_run> was entered minus the number of 743e3a38431SPaul Bohmtimes C<ev_run> was exited normally, in other words, the recursion depth. 744e3a38431SPaul Bohm 745e3a38431SPaul BohmOutside C<ev_run>, this number is zero. In a callback, this number is 746e3a38431SPaul BohmC<1>, unless C<ev_run> was invoked recursively (or from another thread), 747e3a38431SPaul Bohmin which case it is higher. 748e3a38431SPaul Bohm 749e3a38431SPaul BohmLeaving C<ev_run> abnormally (setjmp/longjmp, cancelling the thread, 750e3a38431SPaul Bohmthrowing an exception etc.), doesn't count as "exit" - consider this 751e3a38431SPaul Bohmas a hint to avoid such ungentleman-like behaviour unless it's really 752e3a38431SPaul Bohmconvenient, in which case it is fully supported. 753e3a38431SPaul Bohm 754e3a38431SPaul Bohm=item unsigned int ev_backend (loop) 755e3a38431SPaul Bohm 756e3a38431SPaul BohmReturns one of the C<EVBACKEND_*> flags indicating the event backend in 757e3a38431SPaul Bohmuse. 758e3a38431SPaul Bohm 759e3a38431SPaul Bohm=item ev_tstamp ev_now (loop) 760e3a38431SPaul Bohm 761e3a38431SPaul BohmReturns the current "event loop time", which is the time the event loop 762e3a38431SPaul Bohmreceived events and started processing them. This timestamp does not 763e3a38431SPaul Bohmchange as long as callbacks are being processed, and this is also the base 764e3a38431SPaul Bohmtime used for relative timers. You can treat it as the timestamp of the 765e3a38431SPaul Bohmevent occurring (or more correctly, libev finding out about it). 766e3a38431SPaul Bohm 767e3a38431SPaul Bohm=item ev_now_update (loop) 768e3a38431SPaul Bohm 769e3a38431SPaul BohmEstablishes the current time by querying the kernel, updating the time 770e3a38431SPaul Bohmreturned by C<ev_now ()> in the progress. This is a costly operation and 771e3a38431SPaul Bohmis usually done automatically within C<ev_run ()>. 772e3a38431SPaul Bohm 773e3a38431SPaul BohmThis function is rarely useful, but when some event callback runs for a 774e3a38431SPaul Bohmvery long time without entering the event loop, updating libev's idea of 775e3a38431SPaul Bohmthe current time is a good idea. 776e3a38431SPaul Bohm 777*93823e6cSPaul BohmSee also L</The special problem of time updates> in the C<ev_timer> section. 778e3a38431SPaul Bohm 779e3a38431SPaul Bohm=item ev_suspend (loop) 780e3a38431SPaul Bohm 781e3a38431SPaul Bohm=item ev_resume (loop) 782e3a38431SPaul Bohm 783e3a38431SPaul BohmThese two functions suspend and resume an event loop, for use when the 784e3a38431SPaul Bohmloop is not used for a while and timeouts should not be processed. 785e3a38431SPaul Bohm 786e3a38431SPaul BohmA typical use case would be an interactive program such as a game: When 787e3a38431SPaul Bohmthe user presses C<^Z> to suspend the game and resumes it an hour later it 788e3a38431SPaul Bohmwould be best to handle timeouts as if no time had actually passed while 789e3a38431SPaul Bohmthe program was suspended. This can be achieved by calling C<ev_suspend> 790e3a38431SPaul Bohmin your C<SIGTSTP> handler, sending yourself a C<SIGSTOP> and calling 791e3a38431SPaul BohmC<ev_resume> directly afterwards to resume timer processing. 792e3a38431SPaul Bohm 793e3a38431SPaul BohmEffectively, all C<ev_timer> watchers will be delayed by the time spend 794e3a38431SPaul Bohmbetween C<ev_suspend> and C<ev_resume>, and all C<ev_periodic> watchers 795e3a38431SPaul Bohmwill be rescheduled (that is, they will lose any events that would have 796e3a38431SPaul Bohmoccurred while suspended). 797e3a38431SPaul Bohm 798e3a38431SPaul BohmAfter calling C<ev_suspend> you B<must not> call I<any> function on the 799e3a38431SPaul Bohmgiven loop other than C<ev_resume>, and you B<must not> call C<ev_resume> 800e3a38431SPaul Bohmwithout a previous call to C<ev_suspend>. 801e3a38431SPaul Bohm 802e3a38431SPaul BohmCalling C<ev_suspend>/C<ev_resume> has the side effect of updating the 803e3a38431SPaul Bohmevent loop time (see C<ev_now_update>). 804e3a38431SPaul Bohm 805*93823e6cSPaul Bohm=item bool ev_run (loop, int flags) 806e3a38431SPaul Bohm 807e3a38431SPaul BohmFinally, this is it, the event handler. This function usually is called 808e3a38431SPaul Bohmafter you have initialised all your watchers and you want to start 809e3a38431SPaul Bohmhandling events. It will ask the operating system for any new events, call 810*93823e6cSPaul Bohmthe watcher callbacks, and then repeat the whole process indefinitely: This 811e3a38431SPaul Bohmis why event loops are called I<loops>. 812e3a38431SPaul Bohm 813e3a38431SPaul BohmIf the flags argument is specified as C<0>, it will keep handling events 814e3a38431SPaul Bohmuntil either no event watchers are active anymore or C<ev_break> was 815e3a38431SPaul Bohmcalled. 816e3a38431SPaul Bohm 817*93823e6cSPaul BohmThe return value is false if there are no more active watchers (which 818*93823e6cSPaul Bohmusually means "all jobs done" or "deadlock"), and true in all other cases 819*93823e6cSPaul Bohm(which usually means " you should call C<ev_run> again"). 820*93823e6cSPaul Bohm 821e3a38431SPaul BohmPlease note that an explicit C<ev_break> is usually better than 822e3a38431SPaul Bohmrelying on all watchers to be stopped when deciding when a program has 823e3a38431SPaul Bohmfinished (especially in interactive programs), but having a program 824e3a38431SPaul Bohmthat automatically loops as long as it has to and no longer by virtue 825e3a38431SPaul Bohmof relying on its watchers stopping correctly, that is truly a thing of 826e3a38431SPaul Bohmbeauty. 827e3a38431SPaul Bohm 828*93823e6cSPaul BohmThis function is I<mostly> exception-safe - you can break out of a 829*93823e6cSPaul BohmC<ev_run> call by calling C<longjmp> in a callback, throwing a C++ 830e3a38431SPaul Bohmexception and so on. This does not decrement the C<ev_depth> value, nor 831e3a38431SPaul Bohmwill it clear any outstanding C<EVBREAK_ONE> breaks. 832e3a38431SPaul Bohm 833e3a38431SPaul BohmA flags value of C<EVRUN_NOWAIT> will look for new events, will handle 834e3a38431SPaul Bohmthose events and any already outstanding ones, but will not wait and 835e3a38431SPaul Bohmblock your process in case there are no events and will return after one 836e3a38431SPaul Bohmiteration of the loop. This is sometimes useful to poll and handle new 837e3a38431SPaul Bohmevents while doing lengthy calculations, to keep the program responsive. 838e3a38431SPaul Bohm 839e3a38431SPaul BohmA flags value of C<EVRUN_ONCE> will look for new events (waiting if 840e3a38431SPaul Bohmnecessary) and will handle those and any already outstanding ones. It 841e3a38431SPaul Bohmwill block your process until at least one new event arrives (which could 842e3a38431SPaul Bohmbe an event internal to libev itself, so there is no guarantee that a 843e3a38431SPaul Bohmuser-registered callback will be called), and will return after one 844e3a38431SPaul Bohmiteration of the loop. 845e3a38431SPaul Bohm 846e3a38431SPaul BohmThis is useful if you are waiting for some external event in conjunction 847e3a38431SPaul Bohmwith something not expressible using other libev watchers (i.e. "roll your 848e3a38431SPaul Bohmown C<ev_run>"). However, a pair of C<ev_prepare>/C<ev_check> watchers is 849e3a38431SPaul Bohmusually a better approach for this kind of thing. 850e3a38431SPaul Bohm 851*93823e6cSPaul BohmHere are the gory details of what C<ev_run> does (this is for your 852*93823e6cSPaul Bohmunderstanding, not a guarantee that things will work exactly like this in 853*93823e6cSPaul Bohmfuture versions): 854e3a38431SPaul Bohm 855e3a38431SPaul Bohm - Increment loop depth. 856e3a38431SPaul Bohm - Reset the ev_break status. 857e3a38431SPaul Bohm - Before the first iteration, call any pending watchers. 858e3a38431SPaul Bohm LOOP: 859e3a38431SPaul Bohm - If EVFLAG_FORKCHECK was used, check for a fork. 860e3a38431SPaul Bohm - If a fork was detected (by any means), queue and call all fork watchers. 861e3a38431SPaul Bohm - Queue and call all prepare watchers. 862e3a38431SPaul Bohm - If ev_break was called, goto FINISH. 863e3a38431SPaul Bohm - If we have been forked, detach and recreate the kernel state 864e3a38431SPaul Bohm as to not disturb the other process. 865e3a38431SPaul Bohm - Update the kernel state with all outstanding changes. 866e3a38431SPaul Bohm - Update the "event loop time" (ev_now ()). 867e3a38431SPaul Bohm - Calculate for how long to sleep or block, if at all 868e3a38431SPaul Bohm (active idle watchers, EVRUN_NOWAIT or not having 869e3a38431SPaul Bohm any active watchers at all will result in not sleeping). 870e3a38431SPaul Bohm - Sleep if the I/O and timer collect interval say so. 871e3a38431SPaul Bohm - Increment loop iteration counter. 872e3a38431SPaul Bohm - Block the process, waiting for any events. 873e3a38431SPaul Bohm - Queue all outstanding I/O (fd) events. 874e3a38431SPaul Bohm - Update the "event loop time" (ev_now ()), and do time jump adjustments. 875e3a38431SPaul Bohm - Queue all expired timers. 876e3a38431SPaul Bohm - Queue all expired periodics. 877e3a38431SPaul Bohm - Queue all idle watchers with priority higher than that of pending events. 878e3a38431SPaul Bohm - Queue all check watchers. 879e3a38431SPaul Bohm - Call all queued watchers in reverse order (i.e. check watchers first). 880e3a38431SPaul Bohm Signals and child watchers are implemented as I/O watchers, and will 881e3a38431SPaul Bohm be handled here by queueing them when their watcher gets executed. 882e3a38431SPaul Bohm - If ev_break has been called, or EVRUN_ONCE or EVRUN_NOWAIT 883e3a38431SPaul Bohm were used, or there are no active watchers, goto FINISH, otherwise 884e3a38431SPaul Bohm continue with step LOOP. 885e3a38431SPaul Bohm FINISH: 886e3a38431SPaul Bohm - Reset the ev_break status iff it was EVBREAK_ONE. 887e3a38431SPaul Bohm - Decrement the loop depth. 888e3a38431SPaul Bohm - Return. 889e3a38431SPaul Bohm 890e3a38431SPaul BohmExample: Queue some jobs and then loop until no events are outstanding 891e3a38431SPaul Bohmanymore. 892e3a38431SPaul Bohm 893e3a38431SPaul Bohm ... queue jobs here, make sure they register event watchers as long 894e3a38431SPaul Bohm ... as they still have work to do (even an idle watcher will do..) 895e3a38431SPaul Bohm ev_run (my_loop, 0); 896e3a38431SPaul Bohm ... jobs done or somebody called break. yeah! 897e3a38431SPaul Bohm 898e3a38431SPaul Bohm=item ev_break (loop, how) 899e3a38431SPaul Bohm 900e3a38431SPaul BohmCan be used to make a call to C<ev_run> return early (but only after it 901e3a38431SPaul Bohmhas processed all outstanding events). The C<how> argument must be either 902e3a38431SPaul BohmC<EVBREAK_ONE>, which will make the innermost C<ev_run> call return, or 903e3a38431SPaul BohmC<EVBREAK_ALL>, which will make all nested C<ev_run> calls return. 904e3a38431SPaul Bohm 905e3a38431SPaul BohmThis "break state" will be cleared on the next call to C<ev_run>. 906e3a38431SPaul Bohm 907e3a38431SPaul BohmIt is safe to call C<ev_break> from outside any C<ev_run> calls, too, in 908e3a38431SPaul Bohmwhich case it will have no effect. 909e3a38431SPaul Bohm 910e3a38431SPaul Bohm=item ev_ref (loop) 911e3a38431SPaul Bohm 912e3a38431SPaul Bohm=item ev_unref (loop) 913e3a38431SPaul Bohm 914e3a38431SPaul BohmRef/unref can be used to add or remove a reference count on the event 915e3a38431SPaul Bohmloop: Every watcher keeps one reference, and as long as the reference 916e3a38431SPaul Bohmcount is nonzero, C<ev_run> will not return on its own. 917e3a38431SPaul Bohm 918e3a38431SPaul BohmThis is useful when you have a watcher that you never intend to 919e3a38431SPaul Bohmunregister, but that nevertheless should not keep C<ev_run> from 920e3a38431SPaul Bohmreturning. In such a case, call C<ev_unref> after starting, and C<ev_ref> 921e3a38431SPaul Bohmbefore stopping it. 922e3a38431SPaul Bohm 923e3a38431SPaul BohmAs an example, libev itself uses this for its internal signal pipe: It 924e3a38431SPaul Bohmis not visible to the libev user and should not keep C<ev_run> from 925e3a38431SPaul Bohmexiting if no event watchers registered by it are active. It is also an 926e3a38431SPaul Bohmexcellent way to do this for generic recurring timers or from within 927e3a38431SPaul Bohmthird-party libraries. Just remember to I<unref after start> and I<ref 928e3a38431SPaul Bohmbefore stop> (but only if the watcher wasn't active before, or was active 929e3a38431SPaul Bohmbefore, respectively. Note also that libev might stop watchers itself 930e3a38431SPaul Bohm(e.g. non-repeating timers) in which case you have to C<ev_ref> 931e3a38431SPaul Bohmin the callback). 932e3a38431SPaul Bohm 933e3a38431SPaul BohmExample: Create a signal watcher, but keep it from keeping C<ev_run> 934e3a38431SPaul Bohmrunning when nothing else is active. 935e3a38431SPaul Bohm 936e3a38431SPaul Bohm ev_signal exitsig; 937e3a38431SPaul Bohm ev_signal_init (&exitsig, sig_cb, SIGINT); 938e3a38431SPaul Bohm ev_signal_start (loop, &exitsig); 939e3a38431SPaul Bohm ev_unref (loop); 940e3a38431SPaul Bohm 941e3a38431SPaul BohmExample: For some weird reason, unregister the above signal handler again. 942e3a38431SPaul Bohm 943e3a38431SPaul Bohm ev_ref (loop); 944e3a38431SPaul Bohm ev_signal_stop (loop, &exitsig); 945e3a38431SPaul Bohm 946e3a38431SPaul Bohm=item ev_set_io_collect_interval (loop, ev_tstamp interval) 947e3a38431SPaul Bohm 948e3a38431SPaul Bohm=item ev_set_timeout_collect_interval (loop, ev_tstamp interval) 949e3a38431SPaul Bohm 950e3a38431SPaul BohmThese advanced functions influence the time that libev will spend waiting 951e3a38431SPaul Bohmfor events. Both time intervals are by default C<0>, meaning that libev 952e3a38431SPaul Bohmwill try to invoke timer/periodic callbacks and I/O callbacks with minimum 953e3a38431SPaul Bohmlatency. 954e3a38431SPaul Bohm 955e3a38431SPaul BohmSetting these to a higher value (the C<interval> I<must> be >= C<0>) 956e3a38431SPaul Bohmallows libev to delay invocation of I/O and timer/periodic callbacks 957e3a38431SPaul Bohmto increase efficiency of loop iterations (or to increase power-saving 958e3a38431SPaul Bohmopportunities). 959e3a38431SPaul Bohm 960e3a38431SPaul BohmThe idea is that sometimes your program runs just fast enough to handle 961e3a38431SPaul Bohmone (or very few) event(s) per loop iteration. While this makes the 962e3a38431SPaul Bohmprogram responsive, it also wastes a lot of CPU time to poll for new 963e3a38431SPaul Bohmevents, especially with backends like C<select ()> which have a high 964e3a38431SPaul Bohmoverhead for the actual polling but can deliver many events at once. 965e3a38431SPaul Bohm 966e3a38431SPaul BohmBy setting a higher I<io collect interval> you allow libev to spend more 967e3a38431SPaul Bohmtime collecting I/O events, so you can handle more events per iteration, 968e3a38431SPaul Bohmat the cost of increasing latency. Timeouts (both C<ev_periodic> and 969*93823e6cSPaul BohmC<ev_timer>) will not be affected. Setting this to a non-null value will 970e3a38431SPaul Bohmintroduce an additional C<ev_sleep ()> call into most loop iterations. The 971e3a38431SPaul Bohmsleep time ensures that libev will not poll for I/O events more often then 972*93823e6cSPaul Bohmonce per this interval, on average (as long as the host time resolution is 973*93823e6cSPaul Bohmgood enough). 974e3a38431SPaul Bohm 975e3a38431SPaul BohmLikewise, by setting a higher I<timeout collect interval> you allow libev 976e3a38431SPaul Bohmto spend more time collecting timeouts, at the expense of increased 977e3a38431SPaul Bohmlatency/jitter/inexactness (the watcher callback will be called 978e3a38431SPaul Bohmlater). C<ev_io> watchers will not be affected. Setting this to a non-null 979e3a38431SPaul Bohmvalue will not introduce any overhead in libev. 980e3a38431SPaul Bohm 981e3a38431SPaul BohmMany (busy) programs can usually benefit by setting the I/O collect 982e3a38431SPaul Bohminterval to a value near C<0.1> or so, which is often enough for 983e3a38431SPaul Bohminteractive servers (of course not for games), likewise for timeouts. It 984e3a38431SPaul Bohmusually doesn't make much sense to set it to a lower value than C<0.01>, 985e3a38431SPaul Bohmas this approaches the timing granularity of most systems. Note that if 986e3a38431SPaul Bohmyou do transactions with the outside world and you can't increase the 987e3a38431SPaul Bohmparallelity, then this setting will limit your transaction rate (if you 988e3a38431SPaul Bohmneed to poll once per transaction and the I/O collect interval is 0.01, 989e3a38431SPaul Bohmthen you can't do more than 100 transactions per second). 990e3a38431SPaul Bohm 991e3a38431SPaul BohmSetting the I<timeout collect interval> can improve the opportunity for 992e3a38431SPaul Bohmsaving power, as the program will "bundle" timer callback invocations that 993e3a38431SPaul Bohmare "near" in time together, by delaying some, thus reducing the number of 994e3a38431SPaul Bohmtimes the process sleeps and wakes up again. Another useful technique to 995e3a38431SPaul Bohmreduce iterations/wake-ups is to use C<ev_periodic> watchers and make sure 996e3a38431SPaul Bohmthey fire on, say, one-second boundaries only. 997e3a38431SPaul Bohm 998e3a38431SPaul BohmExample: we only need 0.1s timeout granularity, and we wish not to poll 999e3a38431SPaul Bohmmore often than 100 times per second: 1000e3a38431SPaul Bohm 1001e3a38431SPaul Bohm ev_set_timeout_collect_interval (EV_DEFAULT_UC_ 0.1); 1002e3a38431SPaul Bohm ev_set_io_collect_interval (EV_DEFAULT_UC_ 0.01); 1003e3a38431SPaul Bohm 1004e3a38431SPaul Bohm=item ev_invoke_pending (loop) 1005e3a38431SPaul Bohm 1006e3a38431SPaul BohmThis call will simply invoke all pending watchers while resetting their 1007e3a38431SPaul Bohmpending state. Normally, C<ev_run> does this automatically when required, 1008e3a38431SPaul Bohmbut when overriding the invoke callback this call comes handy. This 1009e3a38431SPaul Bohmfunction can be invoked from a watcher - this can be useful for example 1010e3a38431SPaul Bohmwhen you want to do some lengthy calculation and want to pass further 1011e3a38431SPaul Bohmevent handling to another thread (you still have to make sure only one 1012e3a38431SPaul Bohmthread executes within C<ev_invoke_pending> or C<ev_run> of course). 1013e3a38431SPaul Bohm 1014e3a38431SPaul Bohm=item int ev_pending_count (loop) 1015e3a38431SPaul Bohm 1016e3a38431SPaul BohmReturns the number of pending watchers - zero indicates that no watchers 1017e3a38431SPaul Bohmare pending. 1018e3a38431SPaul Bohm 1019e3a38431SPaul Bohm=item ev_set_invoke_pending_cb (loop, void (*invoke_pending_cb)(EV_P)) 1020e3a38431SPaul Bohm 1021e3a38431SPaul BohmThis overrides the invoke pending functionality of the loop: Instead of 1022e3a38431SPaul Bohminvoking all pending watchers when there are any, C<ev_run> will call 1023e3a38431SPaul Bohmthis callback instead. This is useful, for example, when you want to 1024e3a38431SPaul Bohminvoke the actual watchers inside another context (another thread etc.). 1025e3a38431SPaul Bohm 1026e3a38431SPaul BohmIf you want to reset the callback, use C<ev_invoke_pending> as new 1027e3a38431SPaul Bohmcallback. 1028e3a38431SPaul Bohm 1029*93823e6cSPaul Bohm=item ev_set_loop_release_cb (loop, void (*release)(EV_P) throw (), void (*acquire)(EV_P) throw ()) 1030e3a38431SPaul Bohm 1031e3a38431SPaul BohmSometimes you want to share the same loop between multiple threads. This 1032e3a38431SPaul Bohmcan be done relatively simply by putting mutex_lock/unlock calls around 1033e3a38431SPaul Bohmeach call to a libev function. 1034e3a38431SPaul Bohm 1035e3a38431SPaul BohmHowever, C<ev_run> can run an indefinite time, so it is not feasible 1036e3a38431SPaul Bohmto wait for it to return. One way around this is to wake up the event 1037*93823e6cSPaul Bohmloop via C<ev_break> and C<ev_async_send>, another way is to set these 1038e3a38431SPaul BohmI<release> and I<acquire> callbacks on the loop. 1039e3a38431SPaul Bohm 1040e3a38431SPaul BohmWhen set, then C<release> will be called just before the thread is 1041e3a38431SPaul Bohmsuspended waiting for new events, and C<acquire> is called just 1042e3a38431SPaul Bohmafterwards. 1043e3a38431SPaul Bohm 1044e3a38431SPaul BohmIdeally, C<release> will just call your mutex_unlock function, and 1045e3a38431SPaul BohmC<acquire> will just call the mutex_lock function again. 1046e3a38431SPaul Bohm 1047e3a38431SPaul BohmWhile event loop modifications are allowed between invocations of 1048e3a38431SPaul BohmC<release> and C<acquire> (that's their only purpose after all), no 1049e3a38431SPaul Bohmmodifications done will affect the event loop, i.e. adding watchers will 1050e3a38431SPaul Bohmhave no effect on the set of file descriptors being watched, or the time 1051e3a38431SPaul Bohmwaited. Use an C<ev_async> watcher to wake up C<ev_run> when you want it 1052e3a38431SPaul Bohmto take note of any changes you made. 1053e3a38431SPaul Bohm 1054e3a38431SPaul BohmIn theory, threads executing C<ev_run> will be async-cancel safe between 1055e3a38431SPaul Bohminvocations of C<release> and C<acquire>. 1056e3a38431SPaul Bohm 1057e3a38431SPaul BohmSee also the locking example in the C<THREADS> section later in this 1058e3a38431SPaul Bohmdocument. 1059e3a38431SPaul Bohm 1060e3a38431SPaul Bohm=item ev_set_userdata (loop, void *data) 1061e3a38431SPaul Bohm 1062e3a38431SPaul Bohm=item void *ev_userdata (loop) 1063e3a38431SPaul Bohm 1064e3a38431SPaul BohmSet and retrieve a single C<void *> associated with a loop. When 1065e3a38431SPaul BohmC<ev_set_userdata> has never been called, then C<ev_userdata> returns 1066e3a38431SPaul BohmC<0>. 1067e3a38431SPaul Bohm 1068e3a38431SPaul BohmThese two functions can be used to associate arbitrary data with a loop, 1069e3a38431SPaul Bohmand are intended solely for the C<invoke_pending_cb>, C<release> and 1070e3a38431SPaul BohmC<acquire> callbacks described above, but of course can be (ab-)used for 1071e3a38431SPaul Bohmany other purpose as well. 1072e3a38431SPaul Bohm 1073e3a38431SPaul Bohm=item ev_verify (loop) 1074e3a38431SPaul Bohm 1075e3a38431SPaul BohmThis function only does something when C<EV_VERIFY> support has been 1076e3a38431SPaul Bohmcompiled in, which is the default for non-minimal builds. It tries to go 1077e3a38431SPaul Bohmthrough all internal structures and checks them for validity. If anything 1078e3a38431SPaul Bohmis found to be inconsistent, it will print an error message to standard 1079e3a38431SPaul Bohmerror and call C<abort ()>. 1080e3a38431SPaul Bohm 1081e3a38431SPaul BohmThis can be used to catch bugs inside libev itself: under normal 1082e3a38431SPaul Bohmcircumstances, this function will never abort as of course libev keeps its 1083e3a38431SPaul Bohmdata structures consistent. 1084e3a38431SPaul Bohm 1085e3a38431SPaul Bohm=back 1086e3a38431SPaul Bohm 1087e3a38431SPaul Bohm 1088e3a38431SPaul Bohm=head1 ANATOMY OF A WATCHER 1089e3a38431SPaul Bohm 1090e3a38431SPaul BohmIn the following description, uppercase C<TYPE> in names stands for the 1091e3a38431SPaul Bohmwatcher type, e.g. C<ev_TYPE_start> can mean C<ev_timer_start> for timer 1092e3a38431SPaul Bohmwatchers and C<ev_io_start> for I/O watchers. 1093e3a38431SPaul Bohm 1094e3a38431SPaul BohmA watcher is an opaque structure that you allocate and register to record 1095e3a38431SPaul Bohmyour interest in some event. To make a concrete example, imagine you want 1096e3a38431SPaul Bohmto wait for STDIN to become readable, you would create an C<ev_io> watcher 1097e3a38431SPaul Bohmfor that: 1098e3a38431SPaul Bohm 1099e3a38431SPaul Bohm static void my_cb (struct ev_loop *loop, ev_io *w, int revents) 1100e3a38431SPaul Bohm { 1101e3a38431SPaul Bohm ev_io_stop (w); 1102e3a38431SPaul Bohm ev_break (loop, EVBREAK_ALL); 1103e3a38431SPaul Bohm } 1104e3a38431SPaul Bohm 1105e3a38431SPaul Bohm struct ev_loop *loop = ev_default_loop (0); 1106e3a38431SPaul Bohm 1107e3a38431SPaul Bohm ev_io stdin_watcher; 1108e3a38431SPaul Bohm 1109e3a38431SPaul Bohm ev_init (&stdin_watcher, my_cb); 1110e3a38431SPaul Bohm ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ); 1111e3a38431SPaul Bohm ev_io_start (loop, &stdin_watcher); 1112e3a38431SPaul Bohm 1113e3a38431SPaul Bohm ev_run (loop, 0); 1114e3a38431SPaul Bohm 1115e3a38431SPaul BohmAs you can see, you are responsible for allocating the memory for your 1116e3a38431SPaul Bohmwatcher structures (and it is I<usually> a bad idea to do this on the 1117e3a38431SPaul Bohmstack). 1118e3a38431SPaul Bohm 1119e3a38431SPaul BohmEach watcher has an associated watcher structure (called C<struct ev_TYPE> 1120e3a38431SPaul Bohmor simply C<ev_TYPE>, as typedefs are provided for all watcher structs). 1121e3a38431SPaul Bohm 1122e3a38431SPaul BohmEach watcher structure must be initialised by a call to C<ev_init (watcher 1123e3a38431SPaul Bohm*, callback)>, which expects a callback to be provided. This callback is 1124e3a38431SPaul Bohminvoked each time the event occurs (or, in the case of I/O watchers, each 1125e3a38431SPaul Bohmtime the event loop detects that the file descriptor given is readable 1126e3a38431SPaul Bohmand/or writable). 1127e3a38431SPaul Bohm 1128e3a38431SPaul BohmEach watcher type further has its own C<< ev_TYPE_set (watcher *, ...) >> 1129e3a38431SPaul Bohmmacro to configure it, with arguments specific to the watcher type. There 1130e3a38431SPaul Bohmis also a macro to combine initialisation and setting in one call: C<< 1131e3a38431SPaul Bohmev_TYPE_init (watcher *, callback, ...) >>. 1132e3a38431SPaul Bohm 1133e3a38431SPaul BohmTo make the watcher actually watch out for events, you have to start it 1134e3a38431SPaul Bohmwith a watcher-specific start function (C<< ev_TYPE_start (loop, watcher 1135e3a38431SPaul Bohm*) >>), and you can stop watching for events at any time by calling the 1136e3a38431SPaul Bohmcorresponding stop function (C<< ev_TYPE_stop (loop, watcher *) >>. 1137e3a38431SPaul Bohm 1138e3a38431SPaul BohmAs long as your watcher is active (has been started but not stopped) you 1139e3a38431SPaul Bohmmust not touch the values stored in it. Most specifically you must never 1140e3a38431SPaul Bohmreinitialise it or call its C<ev_TYPE_set> macro. 1141e3a38431SPaul Bohm 1142e3a38431SPaul BohmEach and every callback receives the event loop pointer as first, the 1143e3a38431SPaul Bohmregistered watcher structure as second, and a bitset of received events as 1144e3a38431SPaul Bohmthird argument. 1145e3a38431SPaul Bohm 1146e3a38431SPaul BohmThe received events usually include a single bit per event type received 1147e3a38431SPaul Bohm(you can receive multiple events at the same time). The possible bit masks 1148e3a38431SPaul Bohmare: 1149e3a38431SPaul Bohm 1150e3a38431SPaul Bohm=over 4 1151e3a38431SPaul Bohm 1152e3a38431SPaul Bohm=item C<EV_READ> 1153e3a38431SPaul Bohm 1154e3a38431SPaul Bohm=item C<EV_WRITE> 1155e3a38431SPaul Bohm 1156e3a38431SPaul BohmThe file descriptor in the C<ev_io> watcher has become readable and/or 1157e3a38431SPaul Bohmwritable. 1158e3a38431SPaul Bohm 1159e3a38431SPaul Bohm=item C<EV_TIMER> 1160e3a38431SPaul Bohm 1161e3a38431SPaul BohmThe C<ev_timer> watcher has timed out. 1162e3a38431SPaul Bohm 1163e3a38431SPaul Bohm=item C<EV_PERIODIC> 1164e3a38431SPaul Bohm 1165e3a38431SPaul BohmThe C<ev_periodic> watcher has timed out. 1166e3a38431SPaul Bohm 1167e3a38431SPaul Bohm=item C<EV_SIGNAL> 1168e3a38431SPaul Bohm 1169e3a38431SPaul BohmThe signal specified in the C<ev_signal> watcher has been received by a thread. 1170e3a38431SPaul Bohm 1171e3a38431SPaul Bohm=item C<EV_CHILD> 1172e3a38431SPaul Bohm 1173e3a38431SPaul BohmThe pid specified in the C<ev_child> watcher has received a status change. 1174e3a38431SPaul Bohm 1175e3a38431SPaul Bohm=item C<EV_STAT> 1176e3a38431SPaul Bohm 1177e3a38431SPaul BohmThe path specified in the C<ev_stat> watcher changed its attributes somehow. 1178e3a38431SPaul Bohm 1179e3a38431SPaul Bohm=item C<EV_IDLE> 1180e3a38431SPaul Bohm 1181e3a38431SPaul BohmThe C<ev_idle> watcher has determined that you have nothing better to do. 1182e3a38431SPaul Bohm 1183e3a38431SPaul Bohm=item C<EV_PREPARE> 1184e3a38431SPaul Bohm 1185e3a38431SPaul Bohm=item C<EV_CHECK> 1186e3a38431SPaul Bohm 1187*93823e6cSPaul BohmAll C<ev_prepare> watchers are invoked just I<before> C<ev_run> starts to 1188*93823e6cSPaul Bohmgather new events, and all C<ev_check> watchers are queued (not invoked) 1189*93823e6cSPaul Bohmjust after C<ev_run> has gathered them, but before it queues any callbacks 1190*93823e6cSPaul Bohmfor any received events. That means C<ev_prepare> watchers are the last 1191*93823e6cSPaul Bohmwatchers invoked before the event loop sleeps or polls for new events, and 1192*93823e6cSPaul BohmC<ev_check> watchers will be invoked before any other watchers of the same 1193*93823e6cSPaul Bohmor lower priority within an event loop iteration. 1194*93823e6cSPaul Bohm 1195*93823e6cSPaul BohmCallbacks of both watcher types can start and stop as many watchers as 1196*93823e6cSPaul Bohmthey want, and all of them will be taken into account (for example, a 1197*93823e6cSPaul BohmC<ev_prepare> watcher might start an idle watcher to keep C<ev_run> from 1198*93823e6cSPaul Bohmblocking). 1199e3a38431SPaul Bohm 1200e3a38431SPaul Bohm=item C<EV_EMBED> 1201e3a38431SPaul Bohm 1202e3a38431SPaul BohmThe embedded event loop specified in the C<ev_embed> watcher needs attention. 1203e3a38431SPaul Bohm 1204e3a38431SPaul Bohm=item C<EV_FORK> 1205e3a38431SPaul Bohm 1206e3a38431SPaul BohmThe event loop has been resumed in the child process after fork (see 1207e3a38431SPaul BohmC<ev_fork>). 1208e3a38431SPaul Bohm 1209e3a38431SPaul Bohm=item C<EV_CLEANUP> 1210e3a38431SPaul Bohm 1211e3a38431SPaul BohmThe event loop is about to be destroyed (see C<ev_cleanup>). 1212e3a38431SPaul Bohm 1213e3a38431SPaul Bohm=item C<EV_ASYNC> 1214e3a38431SPaul Bohm 1215e3a38431SPaul BohmThe given async watcher has been asynchronously notified (see C<ev_async>). 1216e3a38431SPaul Bohm 1217e3a38431SPaul Bohm=item C<EV_CUSTOM> 1218e3a38431SPaul Bohm 1219e3a38431SPaul BohmNot ever sent (or otherwise used) by libev itself, but can be freely used 1220e3a38431SPaul Bohmby libev users to signal watchers (e.g. via C<ev_feed_event>). 1221e3a38431SPaul Bohm 1222e3a38431SPaul Bohm=item C<EV_ERROR> 1223e3a38431SPaul Bohm 1224e3a38431SPaul BohmAn unspecified error has occurred, the watcher has been stopped. This might 1225e3a38431SPaul Bohmhappen because the watcher could not be properly started because libev 1226e3a38431SPaul Bohmran out of memory, a file descriptor was found to be closed or any other 1227e3a38431SPaul Bohmproblem. Libev considers these application bugs. 1228e3a38431SPaul Bohm 1229e3a38431SPaul BohmYou best act on it by reporting the problem and somehow coping with the 1230e3a38431SPaul Bohmwatcher being stopped. Note that well-written programs should not receive 1231e3a38431SPaul Bohman error ever, so when your watcher receives it, this usually indicates a 1232e3a38431SPaul Bohmbug in your program. 1233e3a38431SPaul Bohm 1234e3a38431SPaul BohmLibev will usually signal a few "dummy" events together with an error, for 1235e3a38431SPaul Bohmexample it might indicate that a fd is readable or writable, and if your 1236e3a38431SPaul Bohmcallbacks is well-written it can just attempt the operation and cope with 1237e3a38431SPaul Bohmthe error from read() or write(). This will not work in multi-threaded 1238e3a38431SPaul Bohmprograms, though, as the fd could already be closed and reused for another 1239e3a38431SPaul Bohmthing, so beware. 1240e3a38431SPaul Bohm 1241e3a38431SPaul Bohm=back 1242e3a38431SPaul Bohm 1243e3a38431SPaul Bohm=head2 GENERIC WATCHER FUNCTIONS 1244e3a38431SPaul Bohm 1245e3a38431SPaul Bohm=over 4 1246e3a38431SPaul Bohm 1247e3a38431SPaul Bohm=item C<ev_init> (ev_TYPE *watcher, callback) 1248e3a38431SPaul Bohm 1249e3a38431SPaul BohmThis macro initialises the generic portion of a watcher. The contents 1250e3a38431SPaul Bohmof the watcher object can be arbitrary (so C<malloc> will do). Only 1251e3a38431SPaul Bohmthe generic parts of the watcher are initialised, you I<need> to call 1252e3a38431SPaul Bohmthe type-specific C<ev_TYPE_set> macro afterwards to initialise the 1253e3a38431SPaul Bohmtype-specific parts. For each type there is also a C<ev_TYPE_init> macro 1254e3a38431SPaul Bohmwhich rolls both calls into one. 1255e3a38431SPaul Bohm 1256e3a38431SPaul BohmYou can reinitialise a watcher at any time as long as it has been stopped 1257e3a38431SPaul Bohm(or never started) and there are no pending events outstanding. 1258e3a38431SPaul Bohm 1259e3a38431SPaul BohmThe callback is always of type C<void (*)(struct ev_loop *loop, ev_TYPE *watcher, 1260e3a38431SPaul Bohmint revents)>. 1261e3a38431SPaul Bohm 1262e3a38431SPaul BohmExample: Initialise an C<ev_io> watcher in two steps. 1263e3a38431SPaul Bohm 1264e3a38431SPaul Bohm ev_io w; 1265e3a38431SPaul Bohm ev_init (&w, my_cb); 1266e3a38431SPaul Bohm ev_io_set (&w, STDIN_FILENO, EV_READ); 1267e3a38431SPaul Bohm 1268e3a38431SPaul Bohm=item C<ev_TYPE_set> (ev_TYPE *watcher, [args]) 1269e3a38431SPaul Bohm 1270e3a38431SPaul BohmThis macro initialises the type-specific parts of a watcher. You need to 1271e3a38431SPaul Bohmcall C<ev_init> at least once before you call this macro, but you can 1272e3a38431SPaul Bohmcall C<ev_TYPE_set> any number of times. You must not, however, call this 1273e3a38431SPaul Bohmmacro on a watcher that is active (it can be pending, however, which is a 1274e3a38431SPaul Bohmdifference to the C<ev_init> macro). 1275e3a38431SPaul Bohm 1276e3a38431SPaul BohmAlthough some watcher types do not have type-specific arguments 1277e3a38431SPaul Bohm(e.g. C<ev_prepare>) you still need to call its C<set> macro. 1278e3a38431SPaul Bohm 1279e3a38431SPaul BohmSee C<ev_init>, above, for an example. 1280e3a38431SPaul Bohm 1281e3a38431SPaul Bohm=item C<ev_TYPE_init> (ev_TYPE *watcher, callback, [args]) 1282e3a38431SPaul Bohm 1283e3a38431SPaul BohmThis convenience macro rolls both C<ev_init> and C<ev_TYPE_set> macro 1284e3a38431SPaul Bohmcalls into a single call. This is the most convenient method to initialise 1285e3a38431SPaul Bohma watcher. The same limitations apply, of course. 1286e3a38431SPaul Bohm 1287e3a38431SPaul BohmExample: Initialise and set an C<ev_io> watcher in one step. 1288e3a38431SPaul Bohm 1289e3a38431SPaul Bohm ev_io_init (&w, my_cb, STDIN_FILENO, EV_READ); 1290e3a38431SPaul Bohm 1291e3a38431SPaul Bohm=item C<ev_TYPE_start> (loop, ev_TYPE *watcher) 1292e3a38431SPaul Bohm 1293e3a38431SPaul BohmStarts (activates) the given watcher. Only active watchers will receive 1294e3a38431SPaul Bohmevents. If the watcher is already active nothing will happen. 1295e3a38431SPaul Bohm 1296e3a38431SPaul BohmExample: Start the C<ev_io> watcher that is being abused as example in this 1297e3a38431SPaul Bohmwhole section. 1298e3a38431SPaul Bohm 1299e3a38431SPaul Bohm ev_io_start (EV_DEFAULT_UC, &w); 1300e3a38431SPaul Bohm 1301e3a38431SPaul Bohm=item C<ev_TYPE_stop> (loop, ev_TYPE *watcher) 1302e3a38431SPaul Bohm 1303e3a38431SPaul BohmStops the given watcher if active, and clears the pending status (whether 1304e3a38431SPaul Bohmthe watcher was active or not). 1305e3a38431SPaul Bohm 1306e3a38431SPaul BohmIt is possible that stopped watchers are pending - for example, 1307e3a38431SPaul Bohmnon-repeating timers are being stopped when they become pending - but 1308e3a38431SPaul Bohmcalling C<ev_TYPE_stop> ensures that the watcher is neither active nor 1309e3a38431SPaul Bohmpending. If you want to free or reuse the memory used by the watcher it is 1310e3a38431SPaul Bohmtherefore a good idea to always call its C<ev_TYPE_stop> function. 1311e3a38431SPaul Bohm 1312e3a38431SPaul Bohm=item bool ev_is_active (ev_TYPE *watcher) 1313e3a38431SPaul Bohm 1314e3a38431SPaul BohmReturns a true value iff the watcher is active (i.e. it has been started 1315e3a38431SPaul Bohmand not yet been stopped). As long as a watcher is active you must not modify 1316e3a38431SPaul Bohmit. 1317e3a38431SPaul Bohm 1318e3a38431SPaul Bohm=item bool ev_is_pending (ev_TYPE *watcher) 1319e3a38431SPaul Bohm 1320e3a38431SPaul BohmReturns a true value iff the watcher is pending, (i.e. it has outstanding 1321e3a38431SPaul Bohmevents but its callback has not yet been invoked). As long as a watcher 1322e3a38431SPaul Bohmis pending (but not active) you must not call an init function on it (but 1323e3a38431SPaul BohmC<ev_TYPE_set> is safe), you must not change its priority, and you must 1324e3a38431SPaul Bohmmake sure the watcher is available to libev (e.g. you cannot C<free ()> 1325e3a38431SPaul Bohmit). 1326e3a38431SPaul Bohm 1327e3a38431SPaul Bohm=item callback ev_cb (ev_TYPE *watcher) 1328e3a38431SPaul Bohm 1329e3a38431SPaul BohmReturns the callback currently set on the watcher. 1330e3a38431SPaul Bohm 1331*93823e6cSPaul Bohm=item ev_set_cb (ev_TYPE *watcher, callback) 1332e3a38431SPaul Bohm 1333e3a38431SPaul BohmChange the callback. You can change the callback at virtually any time 1334e3a38431SPaul Bohm(modulo threads). 1335e3a38431SPaul Bohm 1336e3a38431SPaul Bohm=item ev_set_priority (ev_TYPE *watcher, int priority) 1337e3a38431SPaul Bohm 1338e3a38431SPaul Bohm=item int ev_priority (ev_TYPE *watcher) 1339e3a38431SPaul Bohm 1340e3a38431SPaul BohmSet and query the priority of the watcher. The priority is a small 1341e3a38431SPaul Bohminteger between C<EV_MAXPRI> (default: C<2>) and C<EV_MINPRI> 1342e3a38431SPaul Bohm(default: C<-2>). Pending watchers with higher priority will be invoked 1343e3a38431SPaul Bohmbefore watchers with lower priority, but priority will not keep watchers 1344e3a38431SPaul Bohmfrom being executed (except for C<ev_idle> watchers). 1345e3a38431SPaul Bohm 1346e3a38431SPaul BohmIf you need to suppress invocation when higher priority events are pending 1347e3a38431SPaul Bohmyou need to look at C<ev_idle> watchers, which provide this functionality. 1348e3a38431SPaul Bohm 1349e3a38431SPaul BohmYou I<must not> change the priority of a watcher as long as it is active or 1350e3a38431SPaul Bohmpending. 1351e3a38431SPaul Bohm 1352e3a38431SPaul BohmSetting a priority outside the range of C<EV_MINPRI> to C<EV_MAXPRI> is 1353e3a38431SPaul Bohmfine, as long as you do not mind that the priority value you query might 1354e3a38431SPaul Bohmor might not have been clamped to the valid range. 1355e3a38431SPaul Bohm 1356e3a38431SPaul BohmThe default priority used by watchers when no priority has been set is 1357e3a38431SPaul Bohmalways C<0>, which is supposed to not be too high and not be too low :). 1358e3a38431SPaul Bohm 1359*93823e6cSPaul BohmSee L</WATCHER PRIORITY MODELS>, below, for a more thorough treatment of 1360e3a38431SPaul Bohmpriorities. 1361e3a38431SPaul Bohm 1362e3a38431SPaul Bohm=item ev_invoke (loop, ev_TYPE *watcher, int revents) 1363e3a38431SPaul Bohm 1364e3a38431SPaul BohmInvoke the C<watcher> with the given C<loop> and C<revents>. Neither 1365e3a38431SPaul BohmC<loop> nor C<revents> need to be valid as long as the watcher callback 1366e3a38431SPaul Bohmcan deal with that fact, as both are simply passed through to the 1367e3a38431SPaul Bohmcallback. 1368e3a38431SPaul Bohm 1369e3a38431SPaul Bohm=item int ev_clear_pending (loop, ev_TYPE *watcher) 1370e3a38431SPaul Bohm 1371e3a38431SPaul BohmIf the watcher is pending, this function clears its pending status and 1372e3a38431SPaul Bohmreturns its C<revents> bitset (as if its callback was invoked). If the 1373e3a38431SPaul Bohmwatcher isn't pending it does nothing and returns C<0>. 1374e3a38431SPaul Bohm 1375e3a38431SPaul BohmSometimes it can be useful to "poll" a watcher instead of waiting for its 1376e3a38431SPaul Bohmcallback to be invoked, which can be accomplished with this function. 1377e3a38431SPaul Bohm 1378e3a38431SPaul Bohm=item ev_feed_event (loop, ev_TYPE *watcher, int revents) 1379e3a38431SPaul Bohm 1380e3a38431SPaul BohmFeeds the given event set into the event loop, as if the specified event 1381e3a38431SPaul Bohmhad happened for the specified watcher (which must be a pointer to an 1382e3a38431SPaul Bohminitialised but not necessarily started event watcher). Obviously you must 1383e3a38431SPaul Bohmnot free the watcher as long as it has pending events. 1384e3a38431SPaul Bohm 1385e3a38431SPaul BohmStopping the watcher, letting libev invoke it, or calling 1386e3a38431SPaul BohmC<ev_clear_pending> will clear the pending event, even if the watcher was 1387e3a38431SPaul Bohmnot started in the first place. 1388e3a38431SPaul Bohm 1389e3a38431SPaul BohmSee also C<ev_feed_fd_event> and C<ev_feed_signal_event> for related 1390e3a38431SPaul Bohmfunctions that do not need a watcher. 1391e3a38431SPaul Bohm 1392e3a38431SPaul Bohm=back 1393e3a38431SPaul Bohm 1394*93823e6cSPaul BohmSee also the L</ASSOCIATING CUSTOM DATA WITH A WATCHER> and L</BUILDING YOUR 1395e3a38431SPaul BohmOWN COMPOSITE WATCHERS> idioms. 1396e3a38431SPaul Bohm 1397e3a38431SPaul Bohm=head2 WATCHER STATES 1398e3a38431SPaul Bohm 1399e3a38431SPaul BohmThere are various watcher states mentioned throughout this manual - 1400e3a38431SPaul Bohmactive, pending and so on. In this section these states and the rules to 1401e3a38431SPaul Bohmtransition between them will be described in more detail - and while these 1402e3a38431SPaul Bohmrules might look complicated, they usually do "the right thing". 1403e3a38431SPaul Bohm 1404e3a38431SPaul Bohm=over 4 1405e3a38431SPaul Bohm 1406*93823e6cSPaul Bohm=item initialised 1407e3a38431SPaul Bohm 1408*93823e6cSPaul BohmBefore a watcher can be registered with the event loop it has to be 1409e3a38431SPaul Bohminitialised. This can be done with a call to C<ev_TYPE_init>, or calls to 1410e3a38431SPaul BohmC<ev_init> followed by the watcher-specific C<ev_TYPE_set> function. 1411e3a38431SPaul Bohm 1412e3a38431SPaul BohmIn this state it is simply some block of memory that is suitable for 1413e3a38431SPaul Bohmuse in an event loop. It can be moved around, freed, reused etc. at 1414e3a38431SPaul Bohmwill - as long as you either keep the memory contents intact, or call 1415e3a38431SPaul BohmC<ev_TYPE_init> again. 1416e3a38431SPaul Bohm 1417e3a38431SPaul Bohm=item started/running/active 1418e3a38431SPaul Bohm 1419e3a38431SPaul BohmOnce a watcher has been started with a call to C<ev_TYPE_start> it becomes 1420e3a38431SPaul Bohmproperty of the event loop, and is actively waiting for events. While in 1421e3a38431SPaul Bohmthis state it cannot be accessed (except in a few documented ways), moved, 1422e3a38431SPaul Bohmfreed or anything else - the only legal thing is to keep a pointer to it, 1423e3a38431SPaul Bohmand call libev functions on it that are documented to work on active watchers. 1424e3a38431SPaul Bohm 1425e3a38431SPaul Bohm=item pending 1426e3a38431SPaul Bohm 1427e3a38431SPaul BohmIf a watcher is active and libev determines that an event it is interested 1428e3a38431SPaul Bohmin has occurred (such as a timer expiring), it will become pending. It will 1429e3a38431SPaul Bohmstay in this pending state until either it is stopped or its callback is 1430e3a38431SPaul Bohmabout to be invoked, so it is not normally pending inside the watcher 1431e3a38431SPaul Bohmcallback. 1432e3a38431SPaul Bohm 1433e3a38431SPaul BohmThe watcher might or might not be active while it is pending (for example, 1434e3a38431SPaul Bohman expired non-repeating timer can be pending but no longer active). If it 1435e3a38431SPaul Bohmis stopped, it can be freely accessed (e.g. by calling C<ev_TYPE_set>), 1436e3a38431SPaul Bohmbut it is still property of the event loop at this time, so cannot be 1437e3a38431SPaul Bohmmoved, freed or reused. And if it is active the rules described in the 1438e3a38431SPaul Bohmprevious item still apply. 1439e3a38431SPaul Bohm 1440e3a38431SPaul BohmIt is also possible to feed an event on a watcher that is not active (e.g. 1441e3a38431SPaul Bohmvia C<ev_feed_event>), in which case it becomes pending without being 1442e3a38431SPaul Bohmactive. 1443e3a38431SPaul Bohm 1444e3a38431SPaul Bohm=item stopped 1445e3a38431SPaul Bohm 1446e3a38431SPaul BohmA watcher can be stopped implicitly by libev (in which case it might still 1447e3a38431SPaul Bohmbe pending), or explicitly by calling its C<ev_TYPE_stop> function. The 1448e3a38431SPaul Bohmlatter will clear any pending state the watcher might be in, regardless 1449e3a38431SPaul Bohmof whether it was active or not, so stopping a watcher explicitly before 1450e3a38431SPaul Bohmfreeing it is often a good idea. 1451e3a38431SPaul Bohm 1452e3a38431SPaul BohmWhile stopped (and not pending) the watcher is essentially in the 1453e3a38431SPaul Bohminitialised state, that is, it can be reused, moved, modified in any way 1454e3a38431SPaul Bohmyou wish (but when you trash the memory block, you need to C<ev_TYPE_init> 1455e3a38431SPaul Bohmit again). 1456e3a38431SPaul Bohm 1457e3a38431SPaul Bohm=back 1458e3a38431SPaul Bohm 1459e3a38431SPaul Bohm=head2 WATCHER PRIORITY MODELS 1460e3a38431SPaul Bohm 1461e3a38431SPaul BohmMany event loops support I<watcher priorities>, which are usually small 1462e3a38431SPaul Bohmintegers that influence the ordering of event callback invocation 1463e3a38431SPaul Bohmbetween watchers in some way, all else being equal. 1464e3a38431SPaul Bohm 1465e3a38431SPaul BohmIn libev, Watcher priorities can be set using C<ev_set_priority>. See its 1466e3a38431SPaul Bohmdescription for the more technical details such as the actual priority 1467e3a38431SPaul Bohmrange. 1468e3a38431SPaul Bohm 1469e3a38431SPaul BohmThere are two common ways how these these priorities are being interpreted 1470e3a38431SPaul Bohmby event loops: 1471e3a38431SPaul Bohm 1472e3a38431SPaul BohmIn the more common lock-out model, higher priorities "lock out" invocation 1473e3a38431SPaul Bohmof lower priority watchers, which means as long as higher priority 1474e3a38431SPaul Bohmwatchers receive events, lower priority watchers are not being invoked. 1475e3a38431SPaul Bohm 1476e3a38431SPaul BohmThe less common only-for-ordering model uses priorities solely to order 1477e3a38431SPaul Bohmcallback invocation within a single event loop iteration: Higher priority 1478e3a38431SPaul Bohmwatchers are invoked before lower priority ones, but they all get invoked 1479e3a38431SPaul Bohmbefore polling for new events. 1480e3a38431SPaul Bohm 1481e3a38431SPaul BohmLibev uses the second (only-for-ordering) model for all its watchers 1482e3a38431SPaul Bohmexcept for idle watchers (which use the lock-out model). 1483e3a38431SPaul Bohm 1484e3a38431SPaul BohmThe rationale behind this is that implementing the lock-out model for 1485e3a38431SPaul Bohmwatchers is not well supported by most kernel interfaces, and most event 1486e3a38431SPaul Bohmlibraries will just poll for the same events again and again as long as 1487e3a38431SPaul Bohmtheir callbacks have not been executed, which is very inefficient in the 1488e3a38431SPaul Bohmcommon case of one high-priority watcher locking out a mass of lower 1489e3a38431SPaul Bohmpriority ones. 1490e3a38431SPaul Bohm 1491e3a38431SPaul BohmStatic (ordering) priorities are most useful when you have two or more 1492e3a38431SPaul Bohmwatchers handling the same resource: a typical usage example is having an 1493e3a38431SPaul BohmC<ev_io> watcher to receive data, and an associated C<ev_timer> to handle 1494e3a38431SPaul Bohmtimeouts. Under load, data might be received while the program handles 1495e3a38431SPaul Bohmother jobs, but since timers normally get invoked first, the timeout 1496e3a38431SPaul Bohmhandler will be executed before checking for data. In that case, giving 1497e3a38431SPaul Bohmthe timer a lower priority than the I/O watcher ensures that I/O will be 1498e3a38431SPaul Bohmhandled first even under adverse conditions (which is usually, but not 1499e3a38431SPaul Bohmalways, what you want). 1500e3a38431SPaul Bohm 1501e3a38431SPaul BohmSince idle watchers use the "lock-out" model, meaning that idle watchers 1502e3a38431SPaul Bohmwill only be executed when no same or higher priority watchers have 1503e3a38431SPaul Bohmreceived events, they can be used to implement the "lock-out" model when 1504e3a38431SPaul Bohmrequired. 1505e3a38431SPaul Bohm 1506e3a38431SPaul BohmFor example, to emulate how many other event libraries handle priorities, 1507e3a38431SPaul Bohmyou can associate an C<ev_idle> watcher to each such watcher, and in 1508e3a38431SPaul Bohmthe normal watcher callback, you just start the idle watcher. The real 1509e3a38431SPaul Bohmprocessing is done in the idle watcher callback. This causes libev to 1510e3a38431SPaul Bohmcontinuously poll and process kernel event data for the watcher, but when 1511e3a38431SPaul Bohmthe lock-out case is known to be rare (which in turn is rare :), this is 1512e3a38431SPaul Bohmworkable. 1513e3a38431SPaul Bohm 1514e3a38431SPaul BohmUsually, however, the lock-out model implemented that way will perform 1515e3a38431SPaul Bohmmiserably under the type of load it was designed to handle. In that case, 1516e3a38431SPaul Bohmit might be preferable to stop the real watcher before starting the 1517e3a38431SPaul Bohmidle watcher, so the kernel will not have to process the event in case 1518e3a38431SPaul Bohmthe actual processing will be delayed for considerable time. 1519e3a38431SPaul Bohm 1520e3a38431SPaul BohmHere is an example of an I/O watcher that should run at a strictly lower 1521e3a38431SPaul Bohmpriority than the default, and which should only process data when no 1522e3a38431SPaul Bohmother events are pending: 1523e3a38431SPaul Bohm 1524e3a38431SPaul Bohm ev_idle idle; // actual processing watcher 1525e3a38431SPaul Bohm ev_io io; // actual event watcher 1526e3a38431SPaul Bohm 1527e3a38431SPaul Bohm static void 1528e3a38431SPaul Bohm io_cb (EV_P_ ev_io *w, int revents) 1529e3a38431SPaul Bohm { 1530e3a38431SPaul Bohm // stop the I/O watcher, we received the event, but 1531e3a38431SPaul Bohm // are not yet ready to handle it. 1532e3a38431SPaul Bohm ev_io_stop (EV_A_ w); 1533e3a38431SPaul Bohm 1534e3a38431SPaul Bohm // start the idle watcher to handle the actual event. 1535e3a38431SPaul Bohm // it will not be executed as long as other watchers 1536e3a38431SPaul Bohm // with the default priority are receiving events. 1537e3a38431SPaul Bohm ev_idle_start (EV_A_ &idle); 1538e3a38431SPaul Bohm } 1539e3a38431SPaul Bohm 1540e3a38431SPaul Bohm static void 1541e3a38431SPaul Bohm idle_cb (EV_P_ ev_idle *w, int revents) 1542e3a38431SPaul Bohm { 1543e3a38431SPaul Bohm // actual processing 1544e3a38431SPaul Bohm read (STDIN_FILENO, ...); 1545e3a38431SPaul Bohm 1546e3a38431SPaul Bohm // have to start the I/O watcher again, as 1547e3a38431SPaul Bohm // we have handled the event 1548e3a38431SPaul Bohm ev_io_start (EV_P_ &io); 1549e3a38431SPaul Bohm } 1550e3a38431SPaul Bohm 1551e3a38431SPaul Bohm // initialisation 1552e3a38431SPaul Bohm ev_idle_init (&idle, idle_cb); 1553e3a38431SPaul Bohm ev_io_init (&io, io_cb, STDIN_FILENO, EV_READ); 1554e3a38431SPaul Bohm ev_io_start (EV_DEFAULT_ &io); 1555e3a38431SPaul Bohm 1556e3a38431SPaul BohmIn the "real" world, it might also be beneficial to start a timer, so that 1557e3a38431SPaul Bohmlow-priority connections can not be locked out forever under load. This 1558e3a38431SPaul Bohmenables your program to keep a lower latency for important connections 1559e3a38431SPaul Bohmduring short periods of high load, while not completely locking out less 1560e3a38431SPaul Bohmimportant ones. 1561e3a38431SPaul Bohm 1562e3a38431SPaul Bohm 1563e3a38431SPaul Bohm=head1 WATCHER TYPES 1564e3a38431SPaul Bohm 1565e3a38431SPaul BohmThis section describes each watcher in detail, but will not repeat 1566e3a38431SPaul Bohminformation given in the last section. Any initialisation/set macros, 1567e3a38431SPaul Bohmfunctions and members specific to the watcher type are explained. 1568e3a38431SPaul Bohm 1569e3a38431SPaul BohmMembers are additionally marked with either I<[read-only]>, meaning that, 1570e3a38431SPaul Bohmwhile the watcher is active, you can look at the member and expect some 1571e3a38431SPaul Bohmsensible content, but you must not modify it (you can modify it while the 1572e3a38431SPaul Bohmwatcher is stopped to your hearts content), or I<[read-write]>, which 1573e3a38431SPaul Bohmmeans you can expect it to have some sensible content while the watcher 1574e3a38431SPaul Bohmis active, but you can also modify it. Modifying it may not do something 1575e3a38431SPaul Bohmsensible or take immediate effect (or do anything at all), but libev will 1576e3a38431SPaul Bohmnot crash or malfunction in any way. 1577e3a38431SPaul Bohm 1578e3a38431SPaul Bohm 1579e3a38431SPaul Bohm=head2 C<ev_io> - is this file descriptor readable or writable? 1580e3a38431SPaul Bohm 1581e3a38431SPaul BohmI/O watchers check whether a file descriptor is readable or writable 1582e3a38431SPaul Bohmin each iteration of the event loop, or, more precisely, when reading 1583e3a38431SPaul Bohmwould not block the process and writing would at least be able to write 1584e3a38431SPaul Bohmsome data. This behaviour is called level-triggering because you keep 1585e3a38431SPaul Bohmreceiving events as long as the condition persists. Remember you can stop 1586e3a38431SPaul Bohmthe watcher if you don't want to act on the event and neither want to 1587e3a38431SPaul Bohmreceive future events. 1588e3a38431SPaul Bohm 1589e3a38431SPaul BohmIn general you can register as many read and/or write event watchers per 1590e3a38431SPaul Bohmfd as you want (as long as you don't confuse yourself). Setting all file 1591e3a38431SPaul Bohmdescriptors to non-blocking mode is also usually a good idea (but not 1592e3a38431SPaul Bohmrequired if you know what you are doing). 1593e3a38431SPaul Bohm 1594e3a38431SPaul BohmAnother thing you have to watch out for is that it is quite easy to 1595e3a38431SPaul Bohmreceive "spurious" readiness notifications, that is, your callback might 1596e3a38431SPaul Bohmbe called with C<EV_READ> but a subsequent C<read>(2) will actually block 1597e3a38431SPaul Bohmbecause there is no data. It is very easy to get into this situation even 1598e3a38431SPaul Bohmwith a relatively standard program structure. Thus it is best to always 1599e3a38431SPaul Bohmuse non-blocking I/O: An extra C<read>(2) returning C<EAGAIN> is far 1600e3a38431SPaul Bohmpreferable to a program hanging until some data arrives. 1601e3a38431SPaul Bohm 1602e3a38431SPaul BohmIf you cannot run the fd in non-blocking mode (for example you should 1603e3a38431SPaul Bohmnot play around with an Xlib connection), then you have to separately 1604e3a38431SPaul Bohmre-test whether a file descriptor is really ready with a known-to-be good 1605e3a38431SPaul Bohminterface such as poll (fortunately in the case of Xlib, it already does 1606e3a38431SPaul Bohmthis on its own, so its quite safe to use). Some people additionally 1607e3a38431SPaul Bohmuse C<SIGALRM> and an interval timer, just to be sure you won't block 1608e3a38431SPaul Bohmindefinitely. 1609e3a38431SPaul Bohm 1610e3a38431SPaul BohmBut really, best use non-blocking mode. 1611e3a38431SPaul Bohm 1612e3a38431SPaul Bohm=head3 The special problem of disappearing file descriptors 1613e3a38431SPaul Bohm 1614e3a38431SPaul BohmSome backends (e.g. kqueue, epoll) need to be told about closing a file 1615e3a38431SPaul Bohmdescriptor (either due to calling C<close> explicitly or any other means, 1616e3a38431SPaul Bohmsuch as C<dup2>). The reason is that you register interest in some file 1617e3a38431SPaul Bohmdescriptor, but when it goes away, the operating system will silently drop 1618e3a38431SPaul Bohmthis interest. If another file descriptor with the same number then is 1619e3a38431SPaul Bohmregistered with libev, there is no efficient way to see that this is, in 1620e3a38431SPaul Bohmfact, a different file descriptor. 1621e3a38431SPaul Bohm 1622e3a38431SPaul BohmTo avoid having to explicitly tell libev about such cases, libev follows 1623e3a38431SPaul Bohmthe following policy: Each time C<ev_io_set> is being called, libev 1624e3a38431SPaul Bohmwill assume that this is potentially a new file descriptor, otherwise 1625e3a38431SPaul Bohmit is assumed that the file descriptor stays the same. That means that 1626e3a38431SPaul Bohmyou I<have> to call C<ev_io_set> (or C<ev_io_init>) when you change the 1627e3a38431SPaul Bohmdescriptor even if the file descriptor number itself did not change. 1628e3a38431SPaul Bohm 1629e3a38431SPaul BohmThis is how one would do it normally anyway, the important point is that 1630e3a38431SPaul Bohmthe libev application should not optimise around libev but should leave 1631e3a38431SPaul Bohmoptimisations to libev. 1632e3a38431SPaul Bohm 1633e3a38431SPaul Bohm=head3 The special problem of dup'ed file descriptors 1634e3a38431SPaul Bohm 1635e3a38431SPaul BohmSome backends (e.g. epoll), cannot register events for file descriptors, 1636e3a38431SPaul Bohmbut only events for the underlying file descriptions. That means when you 1637e3a38431SPaul Bohmhave C<dup ()>'ed file descriptors or weirder constellations, and register 1638e3a38431SPaul Bohmevents for them, only one file descriptor might actually receive events. 1639e3a38431SPaul Bohm 1640e3a38431SPaul BohmThere is no workaround possible except not registering events 1641e3a38431SPaul Bohmfor potentially C<dup ()>'ed file descriptors, or to resort to 1642e3a38431SPaul BohmC<EVBACKEND_SELECT> or C<EVBACKEND_POLL>. 1643e3a38431SPaul Bohm 1644e3a38431SPaul Bohm=head3 The special problem of files 1645e3a38431SPaul Bohm 1646e3a38431SPaul BohmMany people try to use C<select> (or libev) on file descriptors 1647e3a38431SPaul Bohmrepresenting files, and expect it to become ready when their program 1648e3a38431SPaul Bohmdoesn't block on disk accesses (which can take a long time on their own). 1649e3a38431SPaul Bohm 1650e3a38431SPaul BohmHowever, this cannot ever work in the "expected" way - you get a readiness 1651e3a38431SPaul Bohmnotification as soon as the kernel knows whether and how much data is 1652e3a38431SPaul Bohmthere, and in the case of open files, that's always the case, so you 1653e3a38431SPaul Bohmalways get a readiness notification instantly, and your read (or possibly 1654e3a38431SPaul Bohmwrite) will still block on the disk I/O. 1655e3a38431SPaul Bohm 1656e3a38431SPaul BohmAnother way to view it is that in the case of sockets, pipes, character 1657e3a38431SPaul Bohmdevices and so on, there is another party (the sender) that delivers data 1658e3a38431SPaul Bohmon its own, but in the case of files, there is no such thing: the disk 1659e3a38431SPaul Bohmwill not send data on its own, simply because it doesn't know what you 1660e3a38431SPaul Bohmwish to read - you would first have to request some data. 1661e3a38431SPaul Bohm 1662e3a38431SPaul BohmSince files are typically not-so-well supported by advanced notification 1663e3a38431SPaul Bohmmechanism, libev tries hard to emulate POSIX behaviour with respect 1664e3a38431SPaul Bohmto files, even though you should not use it. The reason for this is 1665e3a38431SPaul Bohmconvenience: sometimes you want to watch STDIN or STDOUT, which is 1666e3a38431SPaul Bohmusually a tty, often a pipe, but also sometimes files or special devices 1667e3a38431SPaul Bohm(for example, C<epoll> on Linux works with F</dev/random> but not with 1668e3a38431SPaul BohmF</dev/urandom>), and even though the file might better be served with 1669e3a38431SPaul Bohmasynchronous I/O instead of with non-blocking I/O, it is still useful when 1670e3a38431SPaul Bohmit "just works" instead of freezing. 1671e3a38431SPaul Bohm 1672e3a38431SPaul BohmSo avoid file descriptors pointing to files when you know it (e.g. use 1673e3a38431SPaul Bohmlibeio), but use them when it is convenient, e.g. for STDIN/STDOUT, or 1674e3a38431SPaul Bohmwhen you rarely read from a file instead of from a socket, and want to 1675e3a38431SPaul Bohmreuse the same code path. 1676e3a38431SPaul Bohm 1677e3a38431SPaul Bohm=head3 The special problem of fork 1678e3a38431SPaul Bohm 1679e3a38431SPaul BohmSome backends (epoll, kqueue) do not support C<fork ()> at all or exhibit 1680e3a38431SPaul Bohmuseless behaviour. Libev fully supports fork, but needs to be told about 1681e3a38431SPaul Bohmit in the child if you want to continue to use it in the child. 1682e3a38431SPaul Bohm 1683e3a38431SPaul BohmTo support fork in your child processes, you have to call C<ev_loop_fork 1684e3a38431SPaul Bohm()> after a fork in the child, enable C<EVFLAG_FORKCHECK>, or resort to 1685e3a38431SPaul BohmC<EVBACKEND_SELECT> or C<EVBACKEND_POLL>. 1686e3a38431SPaul Bohm 1687e3a38431SPaul Bohm=head3 The special problem of SIGPIPE 1688e3a38431SPaul Bohm 1689e3a38431SPaul BohmWhile not really specific to libev, it is easy to forget about C<SIGPIPE>: 1690e3a38431SPaul Bohmwhen writing to a pipe whose other end has been closed, your program gets 1691e3a38431SPaul Bohmsent a SIGPIPE, which, by default, aborts your program. For most programs 1692e3a38431SPaul Bohmthis is sensible behaviour, for daemons, this is usually undesirable. 1693e3a38431SPaul Bohm 1694e3a38431SPaul BohmSo when you encounter spurious, unexplained daemon exits, make sure you 1695e3a38431SPaul Bohmignore SIGPIPE (and maybe make sure you log the exit status of your daemon 1696e3a38431SPaul Bohmsomewhere, as that would have given you a big clue). 1697e3a38431SPaul Bohm 1698e3a38431SPaul Bohm=head3 The special problem of accept()ing when you can't 1699e3a38431SPaul Bohm 1700e3a38431SPaul BohmMany implementations of the POSIX C<accept> function (for example, 1701e3a38431SPaul Bohmfound in post-2004 Linux) have the peculiar behaviour of not removing a 1702e3a38431SPaul Bohmconnection from the pending queue in all error cases. 1703e3a38431SPaul Bohm 1704e3a38431SPaul BohmFor example, larger servers often run out of file descriptors (because 1705e3a38431SPaul Bohmof resource limits), causing C<accept> to fail with C<ENFILE> but not 1706e3a38431SPaul Bohmrejecting the connection, leading to libev signalling readiness on 1707e3a38431SPaul Bohmthe next iteration again (the connection still exists after all), and 1708e3a38431SPaul Bohmtypically causing the program to loop at 100% CPU usage. 1709e3a38431SPaul Bohm 1710e3a38431SPaul BohmUnfortunately, the set of errors that cause this issue differs between 1711e3a38431SPaul Bohmoperating systems, there is usually little the app can do to remedy the 1712e3a38431SPaul Bohmsituation, and no known thread-safe method of removing the connection to 1713e3a38431SPaul Bohmcope with overload is known (to me). 1714e3a38431SPaul Bohm 1715e3a38431SPaul BohmOne of the easiest ways to handle this situation is to just ignore it 1716e3a38431SPaul Bohm- when the program encounters an overload, it will just loop until the 1717e3a38431SPaul Bohmsituation is over. While this is a form of busy waiting, no OS offers an 1718e3a38431SPaul Bohmevent-based way to handle this situation, so it's the best one can do. 1719e3a38431SPaul Bohm 1720e3a38431SPaul BohmA better way to handle the situation is to log any errors other than 1721e3a38431SPaul BohmC<EAGAIN> and C<EWOULDBLOCK>, making sure not to flood the log with such 1722e3a38431SPaul Bohmmessages, and continue as usual, which at least gives the user an idea of 1723e3a38431SPaul Bohmwhat could be wrong ("raise the ulimit!"). For extra points one could stop 1724e3a38431SPaul Bohmthe C<ev_io> watcher on the listening fd "for a while", which reduces CPU 1725e3a38431SPaul Bohmusage. 1726e3a38431SPaul Bohm 1727e3a38431SPaul BohmIf your program is single-threaded, then you could also keep a dummy file 1728e3a38431SPaul Bohmdescriptor for overload situations (e.g. by opening F</dev/null>), and 1729e3a38431SPaul Bohmwhen you run into C<ENFILE> or C<EMFILE>, close it, run C<accept>, 1730e3a38431SPaul Bohmclose that fd, and create a new dummy fd. This will gracefully refuse 1731e3a38431SPaul Bohmclients under typical overload conditions. 1732e3a38431SPaul Bohm 1733e3a38431SPaul BohmThe last way to handle it is to simply log the error and C<exit>, as 1734e3a38431SPaul Bohmis often done with C<malloc> failures, but this results in an easy 1735e3a38431SPaul Bohmopportunity for a DoS attack. 1736e3a38431SPaul Bohm 1737e3a38431SPaul Bohm=head3 Watcher-Specific Functions 1738e3a38431SPaul Bohm 1739e3a38431SPaul Bohm=over 4 1740e3a38431SPaul Bohm 1741e3a38431SPaul Bohm=item ev_io_init (ev_io *, callback, int fd, int events) 1742e3a38431SPaul Bohm 1743e3a38431SPaul Bohm=item ev_io_set (ev_io *, int fd, int events) 1744e3a38431SPaul Bohm 1745e3a38431SPaul BohmConfigures an C<ev_io> watcher. The C<fd> is the file descriptor to 1746e3a38431SPaul Bohmreceive events for and C<events> is either C<EV_READ>, C<EV_WRITE> or 1747e3a38431SPaul BohmC<EV_READ | EV_WRITE>, to express the desire to receive the given events. 1748e3a38431SPaul Bohm 1749e3a38431SPaul Bohm=item int fd [read-only] 1750e3a38431SPaul Bohm 1751e3a38431SPaul BohmThe file descriptor being watched. 1752e3a38431SPaul Bohm 1753e3a38431SPaul Bohm=item int events [read-only] 1754e3a38431SPaul Bohm 1755e3a38431SPaul BohmThe events being watched. 1756e3a38431SPaul Bohm 1757e3a38431SPaul Bohm=back 1758e3a38431SPaul Bohm 1759e3a38431SPaul Bohm=head3 Examples 1760e3a38431SPaul Bohm 1761e3a38431SPaul BohmExample: Call C<stdin_readable_cb> when STDIN_FILENO has become, well 1762e3a38431SPaul Bohmreadable, but only once. Since it is likely line-buffered, you could 1763e3a38431SPaul Bohmattempt to read a whole line in the callback. 1764e3a38431SPaul Bohm 1765e3a38431SPaul Bohm static void 1766e3a38431SPaul Bohm stdin_readable_cb (struct ev_loop *loop, ev_io *w, int revents) 1767e3a38431SPaul Bohm { 1768e3a38431SPaul Bohm ev_io_stop (loop, w); 1769e3a38431SPaul Bohm .. read from stdin here (or from w->fd) and handle any I/O errors 1770e3a38431SPaul Bohm } 1771e3a38431SPaul Bohm 1772e3a38431SPaul Bohm ... 1773e3a38431SPaul Bohm struct ev_loop *loop = ev_default_init (0); 1774e3a38431SPaul Bohm ev_io stdin_readable; 1775e3a38431SPaul Bohm ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ); 1776e3a38431SPaul Bohm ev_io_start (loop, &stdin_readable); 1777e3a38431SPaul Bohm ev_run (loop, 0); 1778e3a38431SPaul Bohm 1779e3a38431SPaul Bohm 1780e3a38431SPaul Bohm=head2 C<ev_timer> - relative and optionally repeating timeouts 1781e3a38431SPaul Bohm 1782e3a38431SPaul BohmTimer watchers are simple relative timers that generate an event after a 1783e3a38431SPaul Bohmgiven time, and optionally repeating in regular intervals after that. 1784e3a38431SPaul Bohm 1785e3a38431SPaul BohmThe timers are based on real time, that is, if you register an event that 1786e3a38431SPaul Bohmtimes out after an hour and you reset your system clock to January last 1787e3a38431SPaul Bohmyear, it will still time out after (roughly) one hour. "Roughly" because 1788e3a38431SPaul Bohmdetecting time jumps is hard, and some inaccuracies are unavoidable (the 1789e3a38431SPaul Bohmmonotonic clock option helps a lot here). 1790e3a38431SPaul Bohm 1791e3a38431SPaul BohmThe callback is guaranteed to be invoked only I<after> its timeout has 1792e3a38431SPaul Bohmpassed (not I<at>, so on systems with very low-resolution clocks this 1793*93823e6cSPaul Bohmmight introduce a small delay, see "the special problem of being too 1794*93823e6cSPaul Bohmearly", below). If multiple timers become ready during the same loop 1795*93823e6cSPaul Bohmiteration then the ones with earlier time-out values are invoked before 1796*93823e6cSPaul Bohmones of the same priority with later time-out values (but this is no 1797*93823e6cSPaul Bohmlonger true when a callback calls C<ev_run> recursively). 1798e3a38431SPaul Bohm 1799e3a38431SPaul Bohm=head3 Be smart about timeouts 1800e3a38431SPaul Bohm 1801e3a38431SPaul BohmMany real-world problems involve some kind of timeout, usually for error 1802e3a38431SPaul Bohmrecovery. A typical example is an HTTP request - if the other side hangs, 1803e3a38431SPaul Bohmyou want to raise some error after a while. 1804e3a38431SPaul Bohm 1805e3a38431SPaul BohmWhat follows are some ways to handle this problem, from obvious and 1806e3a38431SPaul Bohminefficient to smart and efficient. 1807e3a38431SPaul Bohm 1808e3a38431SPaul BohmIn the following, a 60 second activity timeout is assumed - a timeout that 1809e3a38431SPaul Bohmgets reset to 60 seconds each time there is activity (e.g. each time some 1810e3a38431SPaul Bohmdata or other life sign was received). 1811e3a38431SPaul Bohm 1812e3a38431SPaul Bohm=over 4 1813e3a38431SPaul Bohm 1814e3a38431SPaul Bohm=item 1. Use a timer and stop, reinitialise and start it on activity. 1815e3a38431SPaul Bohm 1816e3a38431SPaul BohmThis is the most obvious, but not the most simple way: In the beginning, 1817e3a38431SPaul Bohmstart the watcher: 1818e3a38431SPaul Bohm 1819e3a38431SPaul Bohm ev_timer_init (timer, callback, 60., 0.); 1820e3a38431SPaul Bohm ev_timer_start (loop, timer); 1821e3a38431SPaul Bohm 1822e3a38431SPaul BohmThen, each time there is some activity, C<ev_timer_stop> it, initialise it 1823e3a38431SPaul Bohmand start it again: 1824e3a38431SPaul Bohm 1825e3a38431SPaul Bohm ev_timer_stop (loop, timer); 1826e3a38431SPaul Bohm ev_timer_set (timer, 60., 0.); 1827e3a38431SPaul Bohm ev_timer_start (loop, timer); 1828e3a38431SPaul Bohm 1829e3a38431SPaul BohmThis is relatively simple to implement, but means that each time there is 1830e3a38431SPaul Bohmsome activity, libev will first have to remove the timer from its internal 1831e3a38431SPaul Bohmdata structure and then add it again. Libev tries to be fast, but it's 1832e3a38431SPaul Bohmstill not a constant-time operation. 1833e3a38431SPaul Bohm 1834e3a38431SPaul Bohm=item 2. Use a timer and re-start it with C<ev_timer_again> inactivity. 1835e3a38431SPaul Bohm 1836e3a38431SPaul BohmThis is the easiest way, and involves using C<ev_timer_again> instead of 1837e3a38431SPaul BohmC<ev_timer_start>. 1838e3a38431SPaul Bohm 1839e3a38431SPaul BohmTo implement this, configure an C<ev_timer> with a C<repeat> value 1840e3a38431SPaul Bohmof C<60> and then call C<ev_timer_again> at start and each time you 1841e3a38431SPaul Bohmsuccessfully read or write some data. If you go into an idle state where 1842e3a38431SPaul Bohmyou do not expect data to travel on the socket, you can C<ev_timer_stop> 1843e3a38431SPaul Bohmthe timer, and C<ev_timer_again> will automatically restart it if need be. 1844e3a38431SPaul Bohm 1845e3a38431SPaul BohmThat means you can ignore both the C<ev_timer_start> function and the 1846e3a38431SPaul BohmC<after> argument to C<ev_timer_set>, and only ever use the C<repeat> 1847e3a38431SPaul Bohmmember and C<ev_timer_again>. 1848e3a38431SPaul Bohm 1849e3a38431SPaul BohmAt start: 1850e3a38431SPaul Bohm 1851e3a38431SPaul Bohm ev_init (timer, callback); 1852e3a38431SPaul Bohm timer->repeat = 60.; 1853e3a38431SPaul Bohm ev_timer_again (loop, timer); 1854e3a38431SPaul Bohm 1855e3a38431SPaul BohmEach time there is some activity: 1856e3a38431SPaul Bohm 1857e3a38431SPaul Bohm ev_timer_again (loop, timer); 1858e3a38431SPaul Bohm 1859e3a38431SPaul BohmIt is even possible to change the time-out on the fly, regardless of 1860e3a38431SPaul Bohmwhether the watcher is active or not: 1861e3a38431SPaul Bohm 1862e3a38431SPaul Bohm timer->repeat = 30.; 1863e3a38431SPaul Bohm ev_timer_again (loop, timer); 1864e3a38431SPaul Bohm 1865e3a38431SPaul BohmThis is slightly more efficient then stopping/starting the timer each time 1866e3a38431SPaul Bohmyou want to modify its timeout value, as libev does not have to completely 1867e3a38431SPaul Bohmremove and re-insert the timer from/into its internal data structure. 1868e3a38431SPaul Bohm 1869e3a38431SPaul BohmIt is, however, even simpler than the "obvious" way to do it. 1870e3a38431SPaul Bohm 1871e3a38431SPaul Bohm=item 3. Let the timer time out, but then re-arm it as required. 1872e3a38431SPaul Bohm 1873e3a38431SPaul BohmThis method is more tricky, but usually most efficient: Most timeouts are 1874e3a38431SPaul Bohmrelatively long compared to the intervals between other activity - in 1875e3a38431SPaul Bohmour example, within 60 seconds, there are usually many I/O events with 1876e3a38431SPaul Bohmassociated activity resets. 1877e3a38431SPaul Bohm 1878e3a38431SPaul BohmIn this case, it would be more efficient to leave the C<ev_timer> alone, 1879e3a38431SPaul Bohmbut remember the time of last activity, and check for a real timeout only 1880e3a38431SPaul Bohmwithin the callback: 1881e3a38431SPaul Bohm 1882*93823e6cSPaul Bohm ev_tstamp timeout = 60.; 1883e3a38431SPaul Bohm ev_tstamp last_activity; // time of last activity 1884*93823e6cSPaul Bohm ev_timer timer; 1885e3a38431SPaul Bohm 1886e3a38431SPaul Bohm static void 1887e3a38431SPaul Bohm callback (EV_P_ ev_timer *w, int revents) 1888e3a38431SPaul Bohm { 1889*93823e6cSPaul Bohm // calculate when the timeout would happen 1890*93823e6cSPaul Bohm ev_tstamp after = last_activity - ev_now (EV_A) + timeout; 1891e3a38431SPaul Bohm 1892*93823e6cSPaul Bohm // if negative, it means we the timeout already occurred 1893*93823e6cSPaul Bohm if (after < 0.) 1894e3a38431SPaul Bohm { 1895e3a38431SPaul Bohm // timeout occurred, take action 1896e3a38431SPaul Bohm } 1897e3a38431SPaul Bohm else 1898e3a38431SPaul Bohm { 1899*93823e6cSPaul Bohm // callback was invoked, but there was some recent 1900*93823e6cSPaul Bohm // activity. simply restart the timer to time out 1901*93823e6cSPaul Bohm // after "after" seconds, which is the earliest time 1902*93823e6cSPaul Bohm // the timeout can occur. 1903*93823e6cSPaul Bohm ev_timer_set (w, after, 0.); 1904*93823e6cSPaul Bohm ev_timer_start (EV_A_ w); 1905e3a38431SPaul Bohm } 1906e3a38431SPaul Bohm } 1907e3a38431SPaul Bohm 1908*93823e6cSPaul BohmTo summarise the callback: first calculate in how many seconds the 1909*93823e6cSPaul Bohmtimeout will occur (by calculating the absolute time when it would occur, 1910*93823e6cSPaul BohmC<last_activity + timeout>, and subtracting the current time, C<ev_now 1911*93823e6cSPaul Bohm(EV_A)> from that). 1912e3a38431SPaul Bohm 1913*93823e6cSPaul BohmIf this value is negative, then we are already past the timeout, i.e. we 1914*93823e6cSPaul Bohmtimed out, and need to do whatever is needed in this case. 1915*93823e6cSPaul Bohm 1916*93823e6cSPaul BohmOtherwise, we now the earliest time at which the timeout would trigger, 1917*93823e6cSPaul Bohmand simply start the timer with this timeout value. 1918*93823e6cSPaul Bohm 1919*93823e6cSPaul BohmIn other words, each time the callback is invoked it will check whether 1920*93823e6cSPaul Bohmthe timeout occurred. If not, it will simply reschedule itself to check 1921*93823e6cSPaul Bohmagain at the earliest time it could time out. Rinse. Repeat. 1922e3a38431SPaul Bohm 1923e3a38431SPaul BohmThis scheme causes more callback invocations (about one every 60 seconds 1924e3a38431SPaul Bohmminus half the average time between activity), but virtually no calls to 1925e3a38431SPaul Bohmlibev to change the timeout. 1926e3a38431SPaul Bohm 1927*93823e6cSPaul BohmTo start the machinery, simply initialise the watcher and set 1928*93823e6cSPaul BohmC<last_activity> to the current time (meaning there was some activity just 1929*93823e6cSPaul Bohmnow), then call the callback, which will "do the right thing" and start 1930*93823e6cSPaul Bohmthe timer: 1931e3a38431SPaul Bohm 1932*93823e6cSPaul Bohm last_activity = ev_now (EV_A); 1933*93823e6cSPaul Bohm ev_init (&timer, callback); 1934*93823e6cSPaul Bohm callback (EV_A_ &timer, 0); 1935e3a38431SPaul Bohm 1936*93823e6cSPaul BohmWhen there is some activity, simply store the current time in 1937e3a38431SPaul BohmC<last_activity>, no libev calls at all: 1938e3a38431SPaul Bohm 1939*93823e6cSPaul Bohm if (activity detected) 1940*93823e6cSPaul Bohm last_activity = ev_now (EV_A); 1941*93823e6cSPaul Bohm 1942*93823e6cSPaul BohmWhen your timeout value changes, then the timeout can be changed by simply 1943*93823e6cSPaul Bohmproviding a new value, stopping the timer and calling the callback, which 1944*93823e6cSPaul Bohmwill again do the right thing (for example, time out immediately :). 1945*93823e6cSPaul Bohm 1946*93823e6cSPaul Bohm timeout = new_value; 1947*93823e6cSPaul Bohm ev_timer_stop (EV_A_ &timer); 1948*93823e6cSPaul Bohm callback (EV_A_ &timer, 0); 1949e3a38431SPaul Bohm 1950e3a38431SPaul BohmThis technique is slightly more complex, but in most cases where the 1951e3a38431SPaul Bohmtime-out is unlikely to be triggered, much more efficient. 1952e3a38431SPaul Bohm 1953e3a38431SPaul Bohm=item 4. Wee, just use a double-linked list for your timeouts. 1954e3a38431SPaul Bohm 1955e3a38431SPaul BohmIf there is not one request, but many thousands (millions...), all 1956e3a38431SPaul Bohmemploying some kind of timeout with the same timeout value, then one can 1957e3a38431SPaul Bohmdo even better: 1958e3a38431SPaul Bohm 1959e3a38431SPaul BohmWhen starting the timeout, calculate the timeout value and put the timeout 1960e3a38431SPaul Bohmat the I<end> of the list. 1961e3a38431SPaul Bohm 1962e3a38431SPaul BohmThen use an C<ev_timer> to fire when the timeout at the I<beginning> of 1963e3a38431SPaul Bohmthe list is expected to fire (for example, using the technique #3). 1964e3a38431SPaul Bohm 1965e3a38431SPaul BohmWhen there is some activity, remove the timer from the list, recalculate 1966e3a38431SPaul Bohmthe timeout, append it to the end of the list again, and make sure to 1967e3a38431SPaul Bohmupdate the C<ev_timer> if it was taken from the beginning of the list. 1968e3a38431SPaul Bohm 1969e3a38431SPaul BohmThis way, one can manage an unlimited number of timeouts in O(1) time for 1970e3a38431SPaul Bohmstarting, stopping and updating the timers, at the expense of a major 1971e3a38431SPaul Bohmcomplication, and having to use a constant timeout. The constant timeout 1972e3a38431SPaul Bohmensures that the list stays sorted. 1973e3a38431SPaul Bohm 1974e3a38431SPaul Bohm=back 1975e3a38431SPaul Bohm 1976e3a38431SPaul BohmSo which method the best? 1977e3a38431SPaul Bohm 1978e3a38431SPaul BohmMethod #2 is a simple no-brain-required solution that is adequate in most 1979e3a38431SPaul Bohmsituations. Method #3 requires a bit more thinking, but handles many cases 1980e3a38431SPaul Bohmbetter, and isn't very complicated either. In most case, choosing either 1981e3a38431SPaul Bohmone is fine, with #3 being better in typical situations. 1982e3a38431SPaul Bohm 1983e3a38431SPaul BohmMethod #1 is almost always a bad idea, and buys you nothing. Method #4 is 1984e3a38431SPaul Bohmrather complicated, but extremely efficient, something that really pays 1985e3a38431SPaul Bohmoff after the first million or so of active timers, i.e. it's usually 1986e3a38431SPaul Bohmoverkill :) 1987e3a38431SPaul Bohm 1988*93823e6cSPaul Bohm=head3 The special problem of being too early 1989*93823e6cSPaul Bohm 1990*93823e6cSPaul BohmIf you ask a timer to call your callback after three seconds, then 1991*93823e6cSPaul Bohmyou expect it to be invoked after three seconds - but of course, this 1992*93823e6cSPaul Bohmcannot be guaranteed to infinite precision. Less obviously, it cannot be 1993*93823e6cSPaul Bohmguaranteed to any precision by libev - imagine somebody suspending the 1994*93823e6cSPaul Bohmprocess with a STOP signal for a few hours for example. 1995*93823e6cSPaul Bohm 1996*93823e6cSPaul BohmSo, libev tries to invoke your callback as soon as possible I<after> the 1997*93823e6cSPaul Bohmdelay has occurred, but cannot guarantee this. 1998*93823e6cSPaul Bohm 1999*93823e6cSPaul BohmA less obvious failure mode is calling your callback too early: many event 2000*93823e6cSPaul Bohmloops compare timestamps with a "elapsed delay >= requested delay", but 2001*93823e6cSPaul Bohmthis can cause your callback to be invoked much earlier than you would 2002*93823e6cSPaul Bohmexpect. 2003*93823e6cSPaul Bohm 2004*93823e6cSPaul BohmTo see why, imagine a system with a clock that only offers full second 2005*93823e6cSPaul Bohmresolution (think windows if you can't come up with a broken enough OS 2006*93823e6cSPaul Bohmyourself). If you schedule a one-second timer at the time 500.9, then the 2007*93823e6cSPaul Bohmevent loop will schedule your timeout to elapse at a system time of 500 2008*93823e6cSPaul Bohm(500.9 truncated to the resolution) + 1, or 501. 2009*93823e6cSPaul Bohm 2010*93823e6cSPaul BohmIf an event library looks at the timeout 0.1s later, it will see "501 >= 2011*93823e6cSPaul Bohm501" and invoke the callback 0.1s after it was started, even though a 2012*93823e6cSPaul Bohmone-second delay was requested - this is being "too early", despite best 2013*93823e6cSPaul Bohmintentions. 2014*93823e6cSPaul Bohm 2015*93823e6cSPaul BohmThis is the reason why libev will never invoke the callback if the elapsed 2016*93823e6cSPaul Bohmdelay equals the requested delay, but only when the elapsed delay is 2017*93823e6cSPaul Bohmlarger than the requested delay. In the example above, libev would only invoke 2018*93823e6cSPaul Bohmthe callback at system time 502, or 1.1s after the timer was started. 2019*93823e6cSPaul Bohm 2020*93823e6cSPaul BohmSo, while libev cannot guarantee that your callback will be invoked 2021*93823e6cSPaul Bohmexactly when requested, it I<can> and I<does> guarantee that the requested 2022*93823e6cSPaul Bohmdelay has actually elapsed, or in other words, it always errs on the "too 2023*93823e6cSPaul Bohmlate" side of things. 2024*93823e6cSPaul Bohm 2025e3a38431SPaul Bohm=head3 The special problem of time updates 2026e3a38431SPaul Bohm 2027*93823e6cSPaul BohmEstablishing the current time is a costly operation (it usually takes 2028*93823e6cSPaul Bohmat least one system call): EV therefore updates its idea of the current 2029e3a38431SPaul Bohmtime only before and after C<ev_run> collects new events, which causes a 2030e3a38431SPaul Bohmgrowing difference between C<ev_now ()> and C<ev_time ()> when handling 2031e3a38431SPaul Bohmlots of events in one iteration. 2032e3a38431SPaul Bohm 2033e3a38431SPaul BohmThe relative timeouts are calculated relative to the C<ev_now ()> 2034e3a38431SPaul Bohmtime. This is usually the right thing as this timestamp refers to the time 2035e3a38431SPaul Bohmof the event triggering whatever timeout you are modifying/starting. If 2036e3a38431SPaul Bohmyou suspect event processing to be delayed and you I<need> to base the 2037*93823e6cSPaul Bohmtimeout on the current time, use something like the following to adjust 2038*93823e6cSPaul Bohmfor it: 2039e3a38431SPaul Bohm 2040*93823e6cSPaul Bohm ev_timer_set (&timer, after + (ev_time () - ev_now ()), 0.); 2041e3a38431SPaul Bohm 2042e3a38431SPaul BohmIf the event loop is suspended for a long time, you can also force an 2043e3a38431SPaul Bohmupdate of the time returned by C<ev_now ()> by calling C<ev_now_update 2044*93823e6cSPaul Bohm()>, although that will push the event time of all outstanding events 2045*93823e6cSPaul Bohmfurther into the future. 2046*93823e6cSPaul Bohm 2047*93823e6cSPaul Bohm=head3 The special problem of unsynchronised clocks 2048*93823e6cSPaul Bohm 2049*93823e6cSPaul BohmModern systems have a variety of clocks - libev itself uses the normal 2050*93823e6cSPaul Bohm"wall clock" clock and, if available, the monotonic clock (to avoid time 2051*93823e6cSPaul Bohmjumps). 2052*93823e6cSPaul Bohm 2053*93823e6cSPaul BohmNeither of these clocks is synchronised with each other or any other clock 2054*93823e6cSPaul Bohmon the system, so C<ev_time ()> might return a considerably different time 2055*93823e6cSPaul Bohmthan C<gettimeofday ()> or C<time ()>. On a GNU/Linux system, for example, 2056*93823e6cSPaul Bohma call to C<gettimeofday> might return a second count that is one higher 2057*93823e6cSPaul Bohmthan a directly following call to C<time>. 2058*93823e6cSPaul Bohm 2059*93823e6cSPaul BohmThe moral of this is to only compare libev-related timestamps with 2060*93823e6cSPaul BohmC<ev_time ()> and C<ev_now ()>, at least if you want better precision than 2061*93823e6cSPaul Bohma second or so. 2062*93823e6cSPaul Bohm 2063*93823e6cSPaul BohmOne more problem arises due to this lack of synchronisation: if libev uses 2064*93823e6cSPaul Bohmthe system monotonic clock and you compare timestamps from C<ev_time> 2065*93823e6cSPaul Bohmor C<ev_now> from when you started your timer and when your callback is 2066*93823e6cSPaul Bohminvoked, you will find that sometimes the callback is a bit "early". 2067*93823e6cSPaul Bohm 2068*93823e6cSPaul BohmThis is because C<ev_timer>s work in real time, not wall clock time, so 2069*93823e6cSPaul Bohmlibev makes sure your callback is not invoked before the delay happened, 2070*93823e6cSPaul BohmI<measured according to the real time>, not the system clock. 2071*93823e6cSPaul Bohm 2072*93823e6cSPaul BohmIf your timeouts are based on a physical timescale (e.g. "time out this 2073*93823e6cSPaul Bohmconnection after 100 seconds") then this shouldn't bother you as it is 2074*93823e6cSPaul Bohmexactly the right behaviour. 2075*93823e6cSPaul Bohm 2076*93823e6cSPaul BohmIf you want to compare wall clock/system timestamps to your timers, then 2077*93823e6cSPaul Bohmyou need to use C<ev_periodic>s, as these are based on the wall clock 2078*93823e6cSPaul Bohmtime, where your comparisons will always generate correct results. 2079e3a38431SPaul Bohm 2080e3a38431SPaul Bohm=head3 The special problems of suspended animation 2081e3a38431SPaul Bohm 2082e3a38431SPaul BohmWhen you leave the server world it is quite customary to hit machines that 2083e3a38431SPaul Bohmcan suspend/hibernate - what happens to the clocks during such a suspend? 2084e3a38431SPaul Bohm 2085e3a38431SPaul BohmSome quick tests made with a Linux 2.6.28 indicate that a suspend freezes 2086e3a38431SPaul Bohmall processes, while the clocks (C<times>, C<CLOCK_MONOTONIC>) continue 2087e3a38431SPaul Bohmto run until the system is suspended, but they will not advance while the 2088e3a38431SPaul Bohmsystem is suspended. That means, on resume, it will be as if the program 2089e3a38431SPaul Bohmwas frozen for a few seconds, but the suspend time will not be counted 2090e3a38431SPaul Bohmtowards C<ev_timer> when a monotonic clock source is used. The real time 2091e3a38431SPaul Bohmclock advanced as expected, but if it is used as sole clocksource, then a 2092e3a38431SPaul Bohmlong suspend would be detected as a time jump by libev, and timers would 2093e3a38431SPaul Bohmbe adjusted accordingly. 2094e3a38431SPaul Bohm 2095e3a38431SPaul BohmI would not be surprised to see different behaviour in different between 2096e3a38431SPaul Bohmoperating systems, OS versions or even different hardware. 2097e3a38431SPaul Bohm 2098e3a38431SPaul BohmThe other form of suspend (job control, or sending a SIGSTOP) will see a 2099e3a38431SPaul Bohmtime jump in the monotonic clocks and the realtime clock. If the program 2100e3a38431SPaul Bohmis suspended for a very long time, and monotonic clock sources are in use, 2101e3a38431SPaul Bohmthen you can expect C<ev_timer>s to expire as the full suspension time 2102e3a38431SPaul Bohmwill be counted towards the timers. When no monotonic clock source is in 2103e3a38431SPaul Bohmuse, then libev will again assume a timejump and adjust accordingly. 2104e3a38431SPaul Bohm 2105e3a38431SPaul BohmIt might be beneficial for this latter case to call C<ev_suspend> 2106e3a38431SPaul Bohmand C<ev_resume> in code that handles C<SIGTSTP>, to at least get 2107e3a38431SPaul Bohmdeterministic behaviour in this case (you can do nothing against 2108e3a38431SPaul BohmC<SIGSTOP>). 2109e3a38431SPaul Bohm 2110e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 2111e3a38431SPaul Bohm 2112e3a38431SPaul Bohm=over 4 2113e3a38431SPaul Bohm 2114e3a38431SPaul Bohm=item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) 2115e3a38431SPaul Bohm 2116e3a38431SPaul Bohm=item ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat) 2117e3a38431SPaul Bohm 2118e3a38431SPaul BohmConfigure the timer to trigger after C<after> seconds. If C<repeat> 2119e3a38431SPaul Bohmis C<0.>, then it will automatically be stopped once the timeout is 2120e3a38431SPaul Bohmreached. If it is positive, then the timer will automatically be 2121e3a38431SPaul Bohmconfigured to trigger again C<repeat> seconds later, again, and again, 2122e3a38431SPaul Bohmuntil stopped manually. 2123e3a38431SPaul Bohm 2124e3a38431SPaul BohmThe timer itself will do a best-effort at avoiding drift, that is, if 2125e3a38431SPaul Bohmyou configure a timer to trigger every 10 seconds, then it will normally 2126e3a38431SPaul Bohmtrigger at exactly 10 second intervals. If, however, your program cannot 2127e3a38431SPaul Bohmkeep up with the timer (because it takes longer than those 10 seconds to 2128e3a38431SPaul Bohmdo stuff) the timer will not fire more than once per event loop iteration. 2129e3a38431SPaul Bohm 2130e3a38431SPaul Bohm=item ev_timer_again (loop, ev_timer *) 2131e3a38431SPaul Bohm 2132*93823e6cSPaul BohmThis will act as if the timer timed out, and restarts it again if it is 2133*93823e6cSPaul Bohmrepeating. It basically works like calling C<ev_timer_stop>, updating the 2134*93823e6cSPaul Bohmtimeout to the C<repeat> value and calling C<ev_timer_start>. 2135e3a38431SPaul Bohm 2136*93823e6cSPaul BohmThe exact semantics are as in the following rules, all of which will be 2137*93823e6cSPaul Bohmapplied to the watcher: 2138e3a38431SPaul Bohm 2139*93823e6cSPaul Bohm=over 4 2140e3a38431SPaul Bohm 2141*93823e6cSPaul Bohm=item If the timer is pending, the pending status is always cleared. 2142e3a38431SPaul Bohm 2143*93823e6cSPaul Bohm=item If the timer is started but non-repeating, stop it (as if it timed 2144*93823e6cSPaul Bohmout, without invoking it). 2145*93823e6cSPaul Bohm 2146*93823e6cSPaul Bohm=item If the timer is repeating, make the C<repeat> value the new timeout 2147*93823e6cSPaul Bohmand start the timer, if necessary. 2148*93823e6cSPaul Bohm 2149*93823e6cSPaul Bohm=back 2150*93823e6cSPaul Bohm 2151*93823e6cSPaul BohmThis sounds a bit complicated, see L</Be smart about timeouts>, above, for a 2152e3a38431SPaul Bohmusage example. 2153e3a38431SPaul Bohm 2154e3a38431SPaul Bohm=item ev_tstamp ev_timer_remaining (loop, ev_timer *) 2155e3a38431SPaul Bohm 2156e3a38431SPaul BohmReturns the remaining time until a timer fires. If the timer is active, 2157e3a38431SPaul Bohmthen this time is relative to the current event loop time, otherwise it's 2158e3a38431SPaul Bohmthe timeout value currently configured. 2159e3a38431SPaul Bohm 2160e3a38431SPaul BohmThat is, after an C<ev_timer_set (w, 5, 7)>, C<ev_timer_remaining> returns 2161e3a38431SPaul BohmC<5>. When the timer is started and one second passes, C<ev_timer_remaining> 2162e3a38431SPaul Bohmwill return C<4>. When the timer expires and is restarted, it will return 2163e3a38431SPaul Bohmroughly C<7> (likely slightly less as callback invocation takes some time, 2164e3a38431SPaul Bohmtoo), and so on. 2165e3a38431SPaul Bohm 2166e3a38431SPaul Bohm=item ev_tstamp repeat [read-write] 2167e3a38431SPaul Bohm 2168e3a38431SPaul BohmThe current C<repeat> value. Will be used each time the watcher times out 2169e3a38431SPaul Bohmor C<ev_timer_again> is called, and determines the next timeout (if any), 2170e3a38431SPaul Bohmwhich is also when any modifications are taken into account. 2171e3a38431SPaul Bohm 2172e3a38431SPaul Bohm=back 2173e3a38431SPaul Bohm 2174e3a38431SPaul Bohm=head3 Examples 2175e3a38431SPaul Bohm 2176e3a38431SPaul BohmExample: Create a timer that fires after 60 seconds. 2177e3a38431SPaul Bohm 2178e3a38431SPaul Bohm static void 2179e3a38431SPaul Bohm one_minute_cb (struct ev_loop *loop, ev_timer *w, int revents) 2180e3a38431SPaul Bohm { 2181e3a38431SPaul Bohm .. one minute over, w is actually stopped right here 2182e3a38431SPaul Bohm } 2183e3a38431SPaul Bohm 2184e3a38431SPaul Bohm ev_timer mytimer; 2185e3a38431SPaul Bohm ev_timer_init (&mytimer, one_minute_cb, 60., 0.); 2186e3a38431SPaul Bohm ev_timer_start (loop, &mytimer); 2187e3a38431SPaul Bohm 2188e3a38431SPaul BohmExample: Create a timeout timer that times out after 10 seconds of 2189e3a38431SPaul Bohminactivity. 2190e3a38431SPaul Bohm 2191e3a38431SPaul Bohm static void 2192e3a38431SPaul Bohm timeout_cb (struct ev_loop *loop, ev_timer *w, int revents) 2193e3a38431SPaul Bohm { 2194e3a38431SPaul Bohm .. ten seconds without any activity 2195e3a38431SPaul Bohm } 2196e3a38431SPaul Bohm 2197e3a38431SPaul Bohm ev_timer mytimer; 2198e3a38431SPaul Bohm ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */ 2199e3a38431SPaul Bohm ev_timer_again (&mytimer); /* start timer */ 2200e3a38431SPaul Bohm ev_run (loop, 0); 2201e3a38431SPaul Bohm 2202e3a38431SPaul Bohm // and in some piece of code that gets executed on any "activity": 2203e3a38431SPaul Bohm // reset the timeout to start ticking again at 10 seconds 2204e3a38431SPaul Bohm ev_timer_again (&mytimer); 2205e3a38431SPaul Bohm 2206e3a38431SPaul Bohm 2207e3a38431SPaul Bohm=head2 C<ev_periodic> - to cron or not to cron? 2208e3a38431SPaul Bohm 2209e3a38431SPaul BohmPeriodic watchers are also timers of a kind, but they are very versatile 2210e3a38431SPaul Bohm(and unfortunately a bit complex). 2211e3a38431SPaul Bohm 2212e3a38431SPaul BohmUnlike C<ev_timer>, periodic watchers are not based on real time (or 2213e3a38431SPaul Bohmrelative time, the physical time that passes) but on wall clock time 2214e3a38431SPaul Bohm(absolute time, the thing you can read on your calender or clock). The 2215e3a38431SPaul Bohmdifference is that wall clock time can run faster or slower than real 2216e3a38431SPaul Bohmtime, and time jumps are not uncommon (e.g. when you adjust your 2217e3a38431SPaul Bohmwrist-watch). 2218e3a38431SPaul Bohm 2219e3a38431SPaul BohmYou can tell a periodic watcher to trigger after some specific point 2220e3a38431SPaul Bohmin time: for example, if you tell a periodic watcher to trigger "in 10 2221e3a38431SPaul Bohmseconds" (by specifying e.g. C<ev_now () + 10.>, that is, an absolute time 2222e3a38431SPaul Bohmnot a delay) and then reset your system clock to January of the previous 2223e3a38431SPaul Bohmyear, then it will take a year or more to trigger the event (unlike an 2224e3a38431SPaul BohmC<ev_timer>, which would still trigger roughly 10 seconds after starting 2225e3a38431SPaul Bohmit, as it uses a relative timeout). 2226e3a38431SPaul Bohm 2227e3a38431SPaul BohmC<ev_periodic> watchers can also be used to implement vastly more complex 2228e3a38431SPaul Bohmtimers, such as triggering an event on each "midnight, local time", or 2229e3a38431SPaul Bohmother complicated rules. This cannot be done with C<ev_timer> watchers, as 2230e3a38431SPaul Bohmthose cannot react to time jumps. 2231e3a38431SPaul Bohm 2232e3a38431SPaul BohmAs with timers, the callback is guaranteed to be invoked only when the 2233e3a38431SPaul Bohmpoint in time where it is supposed to trigger has passed. If multiple 2234e3a38431SPaul Bohmtimers become ready during the same loop iteration then the ones with 2235e3a38431SPaul Bohmearlier time-out values are invoked before ones with later time-out values 2236e3a38431SPaul Bohm(but this is no longer true when a callback calls C<ev_run> recursively). 2237e3a38431SPaul Bohm 2238e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 2239e3a38431SPaul Bohm 2240e3a38431SPaul Bohm=over 4 2241e3a38431SPaul Bohm 2242e3a38431SPaul Bohm=item ev_periodic_init (ev_periodic *, callback, ev_tstamp offset, ev_tstamp interval, reschedule_cb) 2243e3a38431SPaul Bohm 2244e3a38431SPaul Bohm=item ev_periodic_set (ev_periodic *, ev_tstamp offset, ev_tstamp interval, reschedule_cb) 2245e3a38431SPaul Bohm 2246e3a38431SPaul BohmLots of arguments, let's sort it out... There are basically three modes of 2247e3a38431SPaul Bohmoperation, and we will explain them from simplest to most complex: 2248e3a38431SPaul Bohm 2249e3a38431SPaul Bohm=over 4 2250e3a38431SPaul Bohm 2251e3a38431SPaul Bohm=item * absolute timer (offset = absolute time, interval = 0, reschedule_cb = 0) 2252e3a38431SPaul Bohm 2253e3a38431SPaul BohmIn this configuration the watcher triggers an event after the wall clock 2254e3a38431SPaul Bohmtime C<offset> has passed. It will not repeat and will not adjust when a 2255e3a38431SPaul Bohmtime jump occurs, that is, if it is to be run at January 1st 2011 then it 2256e3a38431SPaul Bohmwill be stopped and invoked when the system clock reaches or surpasses 2257e3a38431SPaul Bohmthis point in time. 2258e3a38431SPaul Bohm 2259e3a38431SPaul Bohm=item * repeating interval timer (offset = offset within interval, interval > 0, reschedule_cb = 0) 2260e3a38431SPaul Bohm 2261e3a38431SPaul BohmIn this mode the watcher will always be scheduled to time out at the next 2262e3a38431SPaul BohmC<offset + N * interval> time (for some integer N, which can also be 2263e3a38431SPaul Bohmnegative) and then repeat, regardless of any time jumps. The C<offset> 2264e3a38431SPaul Bohmargument is merely an offset into the C<interval> periods. 2265e3a38431SPaul Bohm 2266e3a38431SPaul BohmThis can be used to create timers that do not drift with respect to the 2267e3a38431SPaul Bohmsystem clock, for example, here is an C<ev_periodic> that triggers each 2268e3a38431SPaul Bohmhour, on the hour (with respect to UTC): 2269e3a38431SPaul Bohm 2270e3a38431SPaul Bohm ev_periodic_set (&periodic, 0., 3600., 0); 2271e3a38431SPaul Bohm 2272e3a38431SPaul BohmThis doesn't mean there will always be 3600 seconds in between triggers, 2273e3a38431SPaul Bohmbut only that the callback will be called when the system time shows a 2274e3a38431SPaul Bohmfull hour (UTC), or more correctly, when the system time is evenly divisible 2275e3a38431SPaul Bohmby 3600. 2276e3a38431SPaul Bohm 2277e3a38431SPaul BohmAnother way to think about it (for the mathematically inclined) is that 2278e3a38431SPaul BohmC<ev_periodic> will try to run the callback in this mode at the next possible 2279e3a38431SPaul Bohmtime where C<time = offset (mod interval)>, regardless of any time jumps. 2280e3a38431SPaul Bohm 2281*93823e6cSPaul BohmThe C<interval> I<MUST> be positive, and for numerical stability, the 2282*93823e6cSPaul Bohminterval value should be higher than C<1/8192> (which is around 100 2283*93823e6cSPaul Bohmmicroseconds) and C<offset> should be higher than C<0> and should have 2284*93823e6cSPaul Bohmat most a similar magnitude as the current time (say, within a factor of 2285*93823e6cSPaul Bohmten). Typical values for offset are, in fact, C<0> or something between 2286*93823e6cSPaul BohmC<0> and C<interval>, which is also the recommended range. 2287e3a38431SPaul Bohm 2288e3a38431SPaul BohmNote also that there is an upper limit to how often a timer can fire (CPU 2289e3a38431SPaul Bohmspeed for example), so if C<interval> is very small then timing stability 2290e3a38431SPaul Bohmwill of course deteriorate. Libev itself tries to be exact to be about one 2291e3a38431SPaul Bohmmillisecond (if the OS supports it and the machine is fast enough). 2292e3a38431SPaul Bohm 2293e3a38431SPaul Bohm=item * manual reschedule mode (offset ignored, interval ignored, reschedule_cb = callback) 2294e3a38431SPaul Bohm 2295e3a38431SPaul BohmIn this mode the values for C<interval> and C<offset> are both being 2296e3a38431SPaul Bohmignored. Instead, each time the periodic watcher gets scheduled, the 2297e3a38431SPaul Bohmreschedule callback will be called with the watcher as first, and the 2298e3a38431SPaul Bohmcurrent time as second argument. 2299e3a38431SPaul Bohm 2300e3a38431SPaul BohmNOTE: I<This callback MUST NOT stop or destroy any periodic watcher, ever, 2301e3a38431SPaul Bohmor make ANY other event loop modifications whatsoever, unless explicitly 2302e3a38431SPaul Bohmallowed by documentation here>. 2303e3a38431SPaul Bohm 2304e3a38431SPaul BohmIf you need to stop it, return C<now + 1e30> (or so, fudge fudge) and stop 2305e3a38431SPaul Bohmit afterwards (e.g. by starting an C<ev_prepare> watcher, which is the 2306e3a38431SPaul Bohmonly event loop modification you are allowed to do). 2307e3a38431SPaul Bohm 2308e3a38431SPaul BohmThe callback prototype is C<ev_tstamp (*reschedule_cb)(ev_periodic 2309e3a38431SPaul Bohm*w, ev_tstamp now)>, e.g.: 2310e3a38431SPaul Bohm 2311e3a38431SPaul Bohm static ev_tstamp 2312e3a38431SPaul Bohm my_rescheduler (ev_periodic *w, ev_tstamp now) 2313e3a38431SPaul Bohm { 2314e3a38431SPaul Bohm return now + 60.; 2315e3a38431SPaul Bohm } 2316e3a38431SPaul Bohm 2317e3a38431SPaul BohmIt must return the next time to trigger, based on the passed time value 2318e3a38431SPaul Bohm(that is, the lowest time value larger than to the second argument). It 2319e3a38431SPaul Bohmwill usually be called just before the callback will be triggered, but 2320e3a38431SPaul Bohmmight be called at other times, too. 2321e3a38431SPaul Bohm 2322e3a38431SPaul BohmNOTE: I<< This callback must always return a time that is higher than or 2323e3a38431SPaul Bohmequal to the passed C<now> value >>. 2324e3a38431SPaul Bohm 2325e3a38431SPaul BohmThis can be used to create very complex timers, such as a timer that 2326e3a38431SPaul Bohmtriggers on "next midnight, local time". To do this, you would calculate the 2327e3a38431SPaul Bohmnext midnight after C<now> and return the timestamp value for this. How 2328e3a38431SPaul Bohmyou do this is, again, up to you (but it is not trivial, which is the main 2329e3a38431SPaul Bohmreason I omitted it as an example). 2330e3a38431SPaul Bohm 2331e3a38431SPaul Bohm=back 2332e3a38431SPaul Bohm 2333e3a38431SPaul Bohm=item ev_periodic_again (loop, ev_periodic *) 2334e3a38431SPaul Bohm 2335e3a38431SPaul BohmSimply stops and restarts the periodic watcher again. This is only useful 2336e3a38431SPaul Bohmwhen you changed some parameters or the reschedule callback would return 2337e3a38431SPaul Bohma different time than the last time it was called (e.g. in a crond like 2338e3a38431SPaul Bohmprogram when the crontabs have changed). 2339e3a38431SPaul Bohm 2340e3a38431SPaul Bohm=item ev_tstamp ev_periodic_at (ev_periodic *) 2341e3a38431SPaul Bohm 2342e3a38431SPaul BohmWhen active, returns the absolute time that the watcher is supposed 2343e3a38431SPaul Bohmto trigger next. This is not the same as the C<offset> argument to 2344e3a38431SPaul BohmC<ev_periodic_set>, but indeed works even in interval and manual 2345e3a38431SPaul Bohmrescheduling modes. 2346e3a38431SPaul Bohm 2347e3a38431SPaul Bohm=item ev_tstamp offset [read-write] 2348e3a38431SPaul Bohm 2349e3a38431SPaul BohmWhen repeating, this contains the offset value, otherwise this is the 2350e3a38431SPaul Bohmabsolute point in time (the C<offset> value passed to C<ev_periodic_set>, 2351e3a38431SPaul Bohmalthough libev might modify this value for better numerical stability). 2352e3a38431SPaul Bohm 2353e3a38431SPaul BohmCan be modified any time, but changes only take effect when the periodic 2354e3a38431SPaul Bohmtimer fires or C<ev_periodic_again> is being called. 2355e3a38431SPaul Bohm 2356e3a38431SPaul Bohm=item ev_tstamp interval [read-write] 2357e3a38431SPaul Bohm 2358e3a38431SPaul BohmThe current interval value. Can be modified any time, but changes only 2359e3a38431SPaul Bohmtake effect when the periodic timer fires or C<ev_periodic_again> is being 2360e3a38431SPaul Bohmcalled. 2361e3a38431SPaul Bohm 2362e3a38431SPaul Bohm=item ev_tstamp (*reschedule_cb)(ev_periodic *w, ev_tstamp now) [read-write] 2363e3a38431SPaul Bohm 2364e3a38431SPaul BohmThe current reschedule callback, or C<0>, if this functionality is 2365e3a38431SPaul Bohmswitched off. Can be changed any time, but changes only take effect when 2366e3a38431SPaul Bohmthe periodic timer fires or C<ev_periodic_again> is being called. 2367e3a38431SPaul Bohm 2368e3a38431SPaul Bohm=back 2369e3a38431SPaul Bohm 2370e3a38431SPaul Bohm=head3 Examples 2371e3a38431SPaul Bohm 2372e3a38431SPaul BohmExample: Call a callback every hour, or, more precisely, whenever the 2373e3a38431SPaul Bohmsystem time is divisible by 3600. The callback invocation times have 2374e3a38431SPaul Bohmpotentially a lot of jitter, but good long-term stability. 2375e3a38431SPaul Bohm 2376e3a38431SPaul Bohm static void 2377e3a38431SPaul Bohm clock_cb (struct ev_loop *loop, ev_periodic *w, int revents) 2378e3a38431SPaul Bohm { 2379e3a38431SPaul Bohm ... its now a full hour (UTC, or TAI or whatever your clock follows) 2380e3a38431SPaul Bohm } 2381e3a38431SPaul Bohm 2382e3a38431SPaul Bohm ev_periodic hourly_tick; 2383e3a38431SPaul Bohm ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0); 2384e3a38431SPaul Bohm ev_periodic_start (loop, &hourly_tick); 2385e3a38431SPaul Bohm 2386e3a38431SPaul BohmExample: The same as above, but use a reschedule callback to do it: 2387e3a38431SPaul Bohm 2388e3a38431SPaul Bohm #include <math.h> 2389e3a38431SPaul Bohm 2390e3a38431SPaul Bohm static ev_tstamp 2391e3a38431SPaul Bohm my_scheduler_cb (ev_periodic *w, ev_tstamp now) 2392e3a38431SPaul Bohm { 2393e3a38431SPaul Bohm return now + (3600. - fmod (now, 3600.)); 2394e3a38431SPaul Bohm } 2395e3a38431SPaul Bohm 2396e3a38431SPaul Bohm ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); 2397e3a38431SPaul Bohm 2398e3a38431SPaul BohmExample: Call a callback every hour, starting now: 2399e3a38431SPaul Bohm 2400e3a38431SPaul Bohm ev_periodic hourly_tick; 2401e3a38431SPaul Bohm ev_periodic_init (&hourly_tick, clock_cb, 2402e3a38431SPaul Bohm fmod (ev_now (loop), 3600.), 3600., 0); 2403e3a38431SPaul Bohm ev_periodic_start (loop, &hourly_tick); 2404e3a38431SPaul Bohm 2405e3a38431SPaul Bohm 2406e3a38431SPaul Bohm=head2 C<ev_signal> - signal me when a signal gets signalled! 2407e3a38431SPaul Bohm 2408e3a38431SPaul BohmSignal watchers will trigger an event when the process receives a specific 2409e3a38431SPaul Bohmsignal one or more times. Even though signals are very asynchronous, libev 2410e3a38431SPaul Bohmwill try its best to deliver signals synchronously, i.e. as part of the 2411e3a38431SPaul Bohmnormal event processing, like any other event. 2412e3a38431SPaul Bohm 2413e3a38431SPaul BohmIf you want signals to be delivered truly asynchronously, just use 2414e3a38431SPaul BohmC<sigaction> as you would do without libev and forget about sharing 2415e3a38431SPaul Bohmthe signal. You can even use C<ev_async> from a signal handler to 2416e3a38431SPaul Bohmsynchronously wake up an event loop. 2417e3a38431SPaul Bohm 2418e3a38431SPaul BohmYou can configure as many watchers as you like for the same signal, but 2419e3a38431SPaul Bohmonly within the same loop, i.e. you can watch for C<SIGINT> in your 2420e3a38431SPaul Bohmdefault loop and for C<SIGIO> in another loop, but you cannot watch for 2421e3a38431SPaul BohmC<SIGINT> in both the default loop and another loop at the same time. At 2422e3a38431SPaul Bohmthe moment, C<SIGCHLD> is permanently tied to the default loop. 2423e3a38431SPaul Bohm 2424*93823e6cSPaul BohmOnly after the first watcher for a signal is started will libev actually 2425*93823e6cSPaul Bohmregister something with the kernel. It thus coexists with your own signal 2426*93823e6cSPaul Bohmhandlers as long as you don't register any with libev for the same signal. 2427e3a38431SPaul Bohm 2428e3a38431SPaul BohmIf possible and supported, libev will install its handlers with 2429e3a38431SPaul BohmC<SA_RESTART> (or equivalent) behaviour enabled, so system calls should 2430e3a38431SPaul Bohmnot be unduly interrupted. If you have a problem with system calls getting 2431e3a38431SPaul Bohminterrupted by signals you can block all signals in an C<ev_check> watcher 2432e3a38431SPaul Bohmand unblock them in an C<ev_prepare> watcher. 2433e3a38431SPaul Bohm 2434e3a38431SPaul Bohm=head3 The special problem of inheritance over fork/execve/pthread_create 2435e3a38431SPaul Bohm 2436e3a38431SPaul BohmBoth the signal mask (C<sigprocmask>) and the signal disposition 2437e3a38431SPaul Bohm(C<sigaction>) are unspecified after starting a signal watcher (and after 2438e3a38431SPaul Bohmstopping it again), that is, libev might or might not block the signal, 2439e3a38431SPaul Bohmand might or might not set or restore the installed signal handler (but 2440e3a38431SPaul Bohmsee C<EVFLAG_NOSIGMASK>). 2441e3a38431SPaul Bohm 2442e3a38431SPaul BohmWhile this does not matter for the signal disposition (libev never 2443e3a38431SPaul Bohmsets signals to C<SIG_IGN>, so handlers will be reset to C<SIG_DFL> on 2444e3a38431SPaul BohmC<execve>), this matters for the signal mask: many programs do not expect 2445e3a38431SPaul Bohmcertain signals to be blocked. 2446e3a38431SPaul Bohm 2447e3a38431SPaul BohmThis means that before calling C<exec> (from the child) you should reset 2448e3a38431SPaul Bohmthe signal mask to whatever "default" you expect (all clear is a good 2449e3a38431SPaul Bohmchoice usually). 2450e3a38431SPaul Bohm 2451e3a38431SPaul BohmThe simplest way to ensure that the signal mask is reset in the child is 2452e3a38431SPaul Bohmto install a fork handler with C<pthread_atfork> that resets it. That will 2453e3a38431SPaul Bohmcatch fork calls done by libraries (such as the libc) as well. 2454e3a38431SPaul Bohm 2455e3a38431SPaul BohmIn current versions of libev, the signal will not be blocked indefinitely 2456e3a38431SPaul Bohmunless you use the C<signalfd> API (C<EV_SIGNALFD>). While this reduces 2457e3a38431SPaul Bohmthe window of opportunity for problems, it will not go away, as libev 2458e3a38431SPaul BohmI<has> to modify the signal mask, at least temporarily. 2459e3a38431SPaul Bohm 2460e3a38431SPaul BohmSo I can't stress this enough: I<If you do not reset your signal mask when 2461e3a38431SPaul Bohmyou expect it to be empty, you have a race condition in your code>. This 2462e3a38431SPaul Bohmis not a libev-specific thing, this is true for most event libraries. 2463e3a38431SPaul Bohm 2464e3a38431SPaul Bohm=head3 The special problem of threads signal handling 2465e3a38431SPaul Bohm 2466e3a38431SPaul BohmPOSIX threads has problematic signal handling semantics, specifically, 2467e3a38431SPaul Bohma lot of functionality (sigfd, sigwait etc.) only really works if all 2468e3a38431SPaul Bohmthreads in a process block signals, which is hard to achieve. 2469e3a38431SPaul Bohm 2470e3a38431SPaul BohmWhen you want to use sigwait (or mix libev signal handling with your own 2471e3a38431SPaul Bohmfor the same signals), you can tackle this problem by globally blocking 2472e3a38431SPaul Bohmall signals before creating any threads (or creating them with a fully set 2473e3a38431SPaul Bohmsigprocmask) and also specifying the C<EVFLAG_NOSIGMASK> when creating 2474e3a38431SPaul Bohmloops. Then designate one thread as "signal receiver thread" which handles 2475e3a38431SPaul Bohmthese signals. You can pass on any signals that libev might be interested 2476e3a38431SPaul Bohmin by calling C<ev_feed_signal>. 2477e3a38431SPaul Bohm 2478e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 2479e3a38431SPaul Bohm 2480e3a38431SPaul Bohm=over 4 2481e3a38431SPaul Bohm 2482e3a38431SPaul Bohm=item ev_signal_init (ev_signal *, callback, int signum) 2483e3a38431SPaul Bohm 2484e3a38431SPaul Bohm=item ev_signal_set (ev_signal *, int signum) 2485e3a38431SPaul Bohm 2486e3a38431SPaul BohmConfigures the watcher to trigger on the given signal number (usually one 2487e3a38431SPaul Bohmof the C<SIGxxx> constants). 2488e3a38431SPaul Bohm 2489e3a38431SPaul Bohm=item int signum [read-only] 2490e3a38431SPaul Bohm 2491e3a38431SPaul BohmThe signal the watcher watches out for. 2492e3a38431SPaul Bohm 2493e3a38431SPaul Bohm=back 2494e3a38431SPaul Bohm 2495e3a38431SPaul Bohm=head3 Examples 2496e3a38431SPaul Bohm 2497e3a38431SPaul BohmExample: Try to exit cleanly on SIGINT. 2498e3a38431SPaul Bohm 2499e3a38431SPaul Bohm static void 2500e3a38431SPaul Bohm sigint_cb (struct ev_loop *loop, ev_signal *w, int revents) 2501e3a38431SPaul Bohm { 2502e3a38431SPaul Bohm ev_break (loop, EVBREAK_ALL); 2503e3a38431SPaul Bohm } 2504e3a38431SPaul Bohm 2505e3a38431SPaul Bohm ev_signal signal_watcher; 2506e3a38431SPaul Bohm ev_signal_init (&signal_watcher, sigint_cb, SIGINT); 2507e3a38431SPaul Bohm ev_signal_start (loop, &signal_watcher); 2508e3a38431SPaul Bohm 2509e3a38431SPaul Bohm 2510e3a38431SPaul Bohm=head2 C<ev_child> - watch out for process status changes 2511e3a38431SPaul Bohm 2512e3a38431SPaul BohmChild watchers trigger when your process receives a SIGCHLD in response to 2513e3a38431SPaul Bohmsome child status changes (most typically when a child of yours dies or 2514e3a38431SPaul Bohmexits). It is permissible to install a child watcher I<after> the child 2515e3a38431SPaul Bohmhas been forked (which implies it might have already exited), as long 2516e3a38431SPaul Bohmas the event loop isn't entered (or is continued from a watcher), i.e., 2517e3a38431SPaul Bohmforking and then immediately registering a watcher for the child is fine, 2518e3a38431SPaul Bohmbut forking and registering a watcher a few event loop iterations later or 2519e3a38431SPaul Bohmin the next callback invocation is not. 2520e3a38431SPaul Bohm 2521e3a38431SPaul BohmOnly the default event loop is capable of handling signals, and therefore 2522e3a38431SPaul Bohmyou can only register child watchers in the default event loop. 2523e3a38431SPaul Bohm 2524e3a38431SPaul BohmDue to some design glitches inside libev, child watchers will always be 2525e3a38431SPaul Bohmhandled at maximum priority (their priority is set to C<EV_MAXPRI> by 2526e3a38431SPaul Bohmlibev) 2527e3a38431SPaul Bohm 2528e3a38431SPaul Bohm=head3 Process Interaction 2529e3a38431SPaul Bohm 2530e3a38431SPaul BohmLibev grabs C<SIGCHLD> as soon as the default event loop is 2531e3a38431SPaul Bohminitialised. This is necessary to guarantee proper behaviour even if the 2532e3a38431SPaul Bohmfirst child watcher is started after the child exits. The occurrence 2533e3a38431SPaul Bohmof C<SIGCHLD> is recorded asynchronously, but child reaping is done 2534e3a38431SPaul Bohmsynchronously as part of the event loop processing. Libev always reaps all 2535e3a38431SPaul Bohmchildren, even ones not watched. 2536e3a38431SPaul Bohm 2537e3a38431SPaul Bohm=head3 Overriding the Built-In Processing 2538e3a38431SPaul Bohm 2539e3a38431SPaul BohmLibev offers no special support for overriding the built-in child 2540e3a38431SPaul Bohmprocessing, but if your application collides with libev's default child 2541e3a38431SPaul Bohmhandler, you can override it easily by installing your own handler for 2542e3a38431SPaul BohmC<SIGCHLD> after initialising the default loop, and making sure the 2543e3a38431SPaul Bohmdefault loop never gets destroyed. You are encouraged, however, to use an 2544e3a38431SPaul Bohmevent-based approach to child reaping and thus use libev's support for 2545e3a38431SPaul Bohmthat, so other libev users can use C<ev_child> watchers freely. 2546e3a38431SPaul Bohm 2547e3a38431SPaul Bohm=head3 Stopping the Child Watcher 2548e3a38431SPaul Bohm 2549e3a38431SPaul BohmCurrently, the child watcher never gets stopped, even when the 2550e3a38431SPaul Bohmchild terminates, so normally one needs to stop the watcher in the 2551e3a38431SPaul Bohmcallback. Future versions of libev might stop the watcher automatically 2552e3a38431SPaul Bohmwhen a child exit is detected (calling C<ev_child_stop> twice is not a 2553e3a38431SPaul Bohmproblem). 2554e3a38431SPaul Bohm 2555e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 2556e3a38431SPaul Bohm 2557e3a38431SPaul Bohm=over 4 2558e3a38431SPaul Bohm 2559e3a38431SPaul Bohm=item ev_child_init (ev_child *, callback, int pid, int trace) 2560e3a38431SPaul Bohm 2561e3a38431SPaul Bohm=item ev_child_set (ev_child *, int pid, int trace) 2562e3a38431SPaul Bohm 2563e3a38431SPaul BohmConfigures the watcher to wait for status changes of process C<pid> (or 2564e3a38431SPaul BohmI<any> process if C<pid> is specified as C<0>). The callback can look 2565e3a38431SPaul Bohmat the C<rstatus> member of the C<ev_child> watcher structure to see 2566e3a38431SPaul Bohmthe status word (use the macros from C<sys/wait.h> and see your systems 2567e3a38431SPaul BohmC<waitpid> documentation). The C<rpid> member contains the pid of the 2568e3a38431SPaul Bohmprocess causing the status change. C<trace> must be either C<0> (only 2569e3a38431SPaul Bohmactivate the watcher when the process terminates) or C<1> (additionally 2570e3a38431SPaul Bohmactivate the watcher when the process is stopped or continued). 2571e3a38431SPaul Bohm 2572e3a38431SPaul Bohm=item int pid [read-only] 2573e3a38431SPaul Bohm 2574e3a38431SPaul BohmThe process id this watcher watches out for, or C<0>, meaning any process id. 2575e3a38431SPaul Bohm 2576e3a38431SPaul Bohm=item int rpid [read-write] 2577e3a38431SPaul Bohm 2578e3a38431SPaul BohmThe process id that detected a status change. 2579e3a38431SPaul Bohm 2580e3a38431SPaul Bohm=item int rstatus [read-write] 2581e3a38431SPaul Bohm 2582e3a38431SPaul BohmThe process exit/trace status caused by C<rpid> (see your systems 2583e3a38431SPaul BohmC<waitpid> and C<sys/wait.h> documentation for details). 2584e3a38431SPaul Bohm 2585e3a38431SPaul Bohm=back 2586e3a38431SPaul Bohm 2587e3a38431SPaul Bohm=head3 Examples 2588e3a38431SPaul Bohm 2589e3a38431SPaul BohmExample: C<fork()> a new process and install a child handler to wait for 2590e3a38431SPaul Bohmits completion. 2591e3a38431SPaul Bohm 2592e3a38431SPaul Bohm ev_child cw; 2593e3a38431SPaul Bohm 2594e3a38431SPaul Bohm static void 2595e3a38431SPaul Bohm child_cb (EV_P_ ev_child *w, int revents) 2596e3a38431SPaul Bohm { 2597e3a38431SPaul Bohm ev_child_stop (EV_A_ w); 2598e3a38431SPaul Bohm printf ("process %d exited with status %x\n", w->rpid, w->rstatus); 2599e3a38431SPaul Bohm } 2600e3a38431SPaul Bohm 2601e3a38431SPaul Bohm pid_t pid = fork (); 2602e3a38431SPaul Bohm 2603e3a38431SPaul Bohm if (pid < 0) 2604e3a38431SPaul Bohm // error 2605e3a38431SPaul Bohm else if (pid == 0) 2606e3a38431SPaul Bohm { 2607e3a38431SPaul Bohm // the forked child executes here 2608e3a38431SPaul Bohm exit (1); 2609e3a38431SPaul Bohm } 2610e3a38431SPaul Bohm else 2611e3a38431SPaul Bohm { 2612e3a38431SPaul Bohm ev_child_init (&cw, child_cb, pid, 0); 2613e3a38431SPaul Bohm ev_child_start (EV_DEFAULT_ &cw); 2614e3a38431SPaul Bohm } 2615e3a38431SPaul Bohm 2616e3a38431SPaul Bohm 2617e3a38431SPaul Bohm=head2 C<ev_stat> - did the file attributes just change? 2618e3a38431SPaul Bohm 2619e3a38431SPaul BohmThis watches a file system path for attribute changes. That is, it calls 2620e3a38431SPaul BohmC<stat> on that path in regular intervals (or when the OS says it changed) 2621*93823e6cSPaul Bohmand sees if it changed compared to the last time, invoking the callback 2622*93823e6cSPaul Bohmif it did. Starting the watcher C<stat>'s the file, so only changes that 2623*93823e6cSPaul Bohmhappen after the watcher has been started will be reported. 2624e3a38431SPaul Bohm 2625e3a38431SPaul BohmThe path does not need to exist: changing from "path exists" to "path does 2626e3a38431SPaul Bohmnot exist" is a status change like any other. The condition "path does not 2627e3a38431SPaul Bohmexist" (or more correctly "path cannot be stat'ed") is signified by the 2628e3a38431SPaul BohmC<st_nlink> field being zero (which is otherwise always forced to be at 2629e3a38431SPaul Bohmleast one) and all the other fields of the stat buffer having unspecified 2630e3a38431SPaul Bohmcontents. 2631e3a38431SPaul Bohm 2632e3a38431SPaul BohmThe path I<must not> end in a slash or contain special components such as 2633e3a38431SPaul BohmC<.> or C<..>. The path I<should> be absolute: If it is relative and 2634e3a38431SPaul Bohmyour working directory changes, then the behaviour is undefined. 2635e3a38431SPaul Bohm 2636e3a38431SPaul BohmSince there is no portable change notification interface available, the 2637e3a38431SPaul Bohmportable implementation simply calls C<stat(2)> regularly on the path 2638e3a38431SPaul Bohmto see if it changed somehow. You can specify a recommended polling 2639e3a38431SPaul Bohminterval for this case. If you specify a polling interval of C<0> (highly 2640e3a38431SPaul Bohmrecommended!) then a I<suitable, unspecified default> value will be used 2641e3a38431SPaul Bohm(which you can expect to be around five seconds, although this might 2642e3a38431SPaul Bohmchange dynamically). Libev will also impose a minimum interval which is 2643e3a38431SPaul Bohmcurrently around C<0.1>, but that's usually overkill. 2644e3a38431SPaul Bohm 2645e3a38431SPaul BohmThis watcher type is not meant for massive numbers of stat watchers, 2646e3a38431SPaul Bohmas even with OS-supported change notifications, this can be 2647e3a38431SPaul Bohmresource-intensive. 2648e3a38431SPaul Bohm 2649e3a38431SPaul BohmAt the time of this writing, the only OS-specific interface implemented 2650e3a38431SPaul Bohmis the Linux inotify interface (implementing kqueue support is left as an 2651e3a38431SPaul Bohmexercise for the reader. Note, however, that the author sees no way of 2652e3a38431SPaul Bohmimplementing C<ev_stat> semantics with kqueue, except as a hint). 2653e3a38431SPaul Bohm 2654e3a38431SPaul Bohm=head3 ABI Issues (Largefile Support) 2655e3a38431SPaul Bohm 2656e3a38431SPaul BohmLibev by default (unless the user overrides this) uses the default 2657e3a38431SPaul Bohmcompilation environment, which means that on systems with large file 2658e3a38431SPaul Bohmsupport disabled by default, you get the 32 bit version of the stat 2659e3a38431SPaul Bohmstructure. When using the library from programs that change the ABI to 2660e3a38431SPaul Bohmuse 64 bit file offsets the programs will fail. In that case you have to 2661e3a38431SPaul Bohmcompile libev with the same flags to get binary compatibility. This is 2662e3a38431SPaul Bohmobviously the case with any flags that change the ABI, but the problem is 2663e3a38431SPaul Bohmmost noticeably displayed with ev_stat and large file support. 2664e3a38431SPaul Bohm 2665e3a38431SPaul BohmThe solution for this is to lobby your distribution maker to make large 2666e3a38431SPaul Bohmfile interfaces available by default (as e.g. FreeBSD does) and not 2667e3a38431SPaul Bohmoptional. Libev cannot simply switch on large file support because it has 2668e3a38431SPaul Bohmto exchange stat structures with application programs compiled using the 2669e3a38431SPaul Bohmdefault compilation environment. 2670e3a38431SPaul Bohm 2671e3a38431SPaul Bohm=head3 Inotify and Kqueue 2672e3a38431SPaul Bohm 2673e3a38431SPaul BohmWhen C<inotify (7)> support has been compiled into libev and present at 2674e3a38431SPaul Bohmruntime, it will be used to speed up change detection where possible. The 2675e3a38431SPaul Bohminotify descriptor will be created lazily when the first C<ev_stat> 2676e3a38431SPaul Bohmwatcher is being started. 2677e3a38431SPaul Bohm 2678e3a38431SPaul BohmInotify presence does not change the semantics of C<ev_stat> watchers 2679e3a38431SPaul Bohmexcept that changes might be detected earlier, and in some cases, to avoid 2680e3a38431SPaul Bohmmaking regular C<stat> calls. Even in the presence of inotify support 2681e3a38431SPaul Bohmthere are many cases where libev has to resort to regular C<stat> polling, 2682e3a38431SPaul Bohmbut as long as kernel 2.6.25 or newer is used (2.6.24 and older have too 2683e3a38431SPaul Bohmmany bugs), the path exists (i.e. stat succeeds), and the path resides on 2684e3a38431SPaul Bohma local filesystem (libev currently assumes only ext2/3, jfs, reiserfs and 2685e3a38431SPaul Bohmxfs are fully working) libev usually gets away without polling. 2686e3a38431SPaul Bohm 2687e3a38431SPaul BohmThere is no support for kqueue, as apparently it cannot be used to 2688e3a38431SPaul Bohmimplement this functionality, due to the requirement of having a file 2689e3a38431SPaul Bohmdescriptor open on the object at all times, and detecting renames, unlinks 2690e3a38431SPaul Bohmetc. is difficult. 2691e3a38431SPaul Bohm 2692e3a38431SPaul Bohm=head3 C<stat ()> is a synchronous operation 2693e3a38431SPaul Bohm 2694e3a38431SPaul BohmLibev doesn't normally do any kind of I/O itself, and so is not blocking 2695e3a38431SPaul Bohmthe process. The exception are C<ev_stat> watchers - those call C<stat 2696e3a38431SPaul Bohm()>, which is a synchronous operation. 2697e3a38431SPaul Bohm 2698e3a38431SPaul BohmFor local paths, this usually doesn't matter: unless the system is very 2699e3a38431SPaul Bohmbusy or the intervals between stat's are large, a stat call will be fast, 2700e3a38431SPaul Bohmas the path data is usually in memory already (except when starting the 2701e3a38431SPaul Bohmwatcher). 2702e3a38431SPaul Bohm 2703e3a38431SPaul BohmFor networked file systems, calling C<stat ()> can block an indefinite 2704e3a38431SPaul Bohmtime due to network issues, and even under good conditions, a stat call 2705e3a38431SPaul Bohmoften takes multiple milliseconds. 2706e3a38431SPaul Bohm 2707e3a38431SPaul BohmTherefore, it is best to avoid using C<ev_stat> watchers on networked 2708e3a38431SPaul Bohmpaths, although this is fully supported by libev. 2709e3a38431SPaul Bohm 2710e3a38431SPaul Bohm=head3 The special problem of stat time resolution 2711e3a38431SPaul Bohm 2712e3a38431SPaul BohmThe C<stat ()> system call only supports full-second resolution portably, 2713e3a38431SPaul Bohmand even on systems where the resolution is higher, most file systems 2714e3a38431SPaul Bohmstill only support whole seconds. 2715e3a38431SPaul Bohm 2716e3a38431SPaul BohmThat means that, if the time is the only thing that changes, you can 2717e3a38431SPaul Bohmeasily miss updates: on the first update, C<ev_stat> detects a change and 2718e3a38431SPaul Bohmcalls your callback, which does something. When there is another update 2719e3a38431SPaul Bohmwithin the same second, C<ev_stat> will be unable to detect unless the 2720e3a38431SPaul Bohmstat data does change in other ways (e.g. file size). 2721e3a38431SPaul Bohm 2722e3a38431SPaul BohmThe solution to this is to delay acting on a change for slightly more 2723e3a38431SPaul Bohmthan a second (or till slightly after the next full second boundary), using 2724e3a38431SPaul Bohma roughly one-second-delay C<ev_timer> (e.g. C<ev_timer_set (w, 0., 1.02); 2725e3a38431SPaul Bohmev_timer_again (loop, w)>). 2726e3a38431SPaul Bohm 2727e3a38431SPaul BohmThe C<.02> offset is added to work around small timing inconsistencies 2728e3a38431SPaul Bohmof some operating systems (where the second counter of the current time 2729e3a38431SPaul Bohmmight be be delayed. One such system is the Linux kernel, where a call to 2730e3a38431SPaul BohmC<gettimeofday> might return a timestamp with a full second later than 2731e3a38431SPaul Bohma subsequent C<time> call - if the equivalent of C<time ()> is used to 2732e3a38431SPaul Bohmupdate file times then there will be a small window where the kernel uses 2733e3a38431SPaul Bohmthe previous second to update file times but libev might already execute 2734e3a38431SPaul Bohmthe timer callback). 2735e3a38431SPaul Bohm 2736e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 2737e3a38431SPaul Bohm 2738e3a38431SPaul Bohm=over 4 2739e3a38431SPaul Bohm 2740e3a38431SPaul Bohm=item ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval) 2741e3a38431SPaul Bohm 2742e3a38431SPaul Bohm=item ev_stat_set (ev_stat *, const char *path, ev_tstamp interval) 2743e3a38431SPaul Bohm 2744e3a38431SPaul BohmConfigures the watcher to wait for status changes of the given 2745e3a38431SPaul BohmC<path>. The C<interval> is a hint on how quickly a change is expected to 2746e3a38431SPaul Bohmbe detected and should normally be specified as C<0> to let libev choose 2747e3a38431SPaul Bohma suitable value. The memory pointed to by C<path> must point to the same 2748e3a38431SPaul Bohmpath for as long as the watcher is active. 2749e3a38431SPaul Bohm 2750e3a38431SPaul BohmThe callback will receive an C<EV_STAT> event when a change was detected, 2751e3a38431SPaul Bohmrelative to the attributes at the time the watcher was started (or the 2752e3a38431SPaul Bohmlast change was detected). 2753e3a38431SPaul Bohm 2754e3a38431SPaul Bohm=item ev_stat_stat (loop, ev_stat *) 2755e3a38431SPaul Bohm 2756e3a38431SPaul BohmUpdates the stat buffer immediately with new values. If you change the 2757e3a38431SPaul Bohmwatched path in your callback, you could call this function to avoid 2758e3a38431SPaul Bohmdetecting this change (while introducing a race condition if you are not 2759e3a38431SPaul Bohmthe only one changing the path). Can also be useful simply to find out the 2760e3a38431SPaul Bohmnew values. 2761e3a38431SPaul Bohm 2762e3a38431SPaul Bohm=item ev_statdata attr [read-only] 2763e3a38431SPaul Bohm 2764e3a38431SPaul BohmThe most-recently detected attributes of the file. Although the type is 2765e3a38431SPaul BohmC<ev_statdata>, this is usually the (or one of the) C<struct stat> types 2766e3a38431SPaul Bohmsuitable for your system, but you can only rely on the POSIX-standardised 2767e3a38431SPaul Bohmmembers to be present. If the C<st_nlink> member is C<0>, then there was 2768e3a38431SPaul Bohmsome error while C<stat>ing the file. 2769e3a38431SPaul Bohm 2770e3a38431SPaul Bohm=item ev_statdata prev [read-only] 2771e3a38431SPaul Bohm 2772e3a38431SPaul BohmThe previous attributes of the file. The callback gets invoked whenever 2773e3a38431SPaul BohmC<prev> != C<attr>, or, more precisely, one or more of these members 2774e3a38431SPaul Bohmdiffer: C<st_dev>, C<st_ino>, C<st_mode>, C<st_nlink>, C<st_uid>, 2775e3a38431SPaul BohmC<st_gid>, C<st_rdev>, C<st_size>, C<st_atime>, C<st_mtime>, C<st_ctime>. 2776e3a38431SPaul Bohm 2777e3a38431SPaul Bohm=item ev_tstamp interval [read-only] 2778e3a38431SPaul Bohm 2779e3a38431SPaul BohmThe specified interval. 2780e3a38431SPaul Bohm 2781e3a38431SPaul Bohm=item const char *path [read-only] 2782e3a38431SPaul Bohm 2783e3a38431SPaul BohmThe file system path that is being watched. 2784e3a38431SPaul Bohm 2785e3a38431SPaul Bohm=back 2786e3a38431SPaul Bohm 2787e3a38431SPaul Bohm=head3 Examples 2788e3a38431SPaul Bohm 2789e3a38431SPaul BohmExample: Watch C</etc/passwd> for attribute changes. 2790e3a38431SPaul Bohm 2791e3a38431SPaul Bohm static void 2792e3a38431SPaul Bohm passwd_cb (struct ev_loop *loop, ev_stat *w, int revents) 2793e3a38431SPaul Bohm { 2794e3a38431SPaul Bohm /* /etc/passwd changed in some way */ 2795e3a38431SPaul Bohm if (w->attr.st_nlink) 2796e3a38431SPaul Bohm { 2797e3a38431SPaul Bohm printf ("passwd current size %ld\n", (long)w->attr.st_size); 2798e3a38431SPaul Bohm printf ("passwd current atime %ld\n", (long)w->attr.st_mtime); 2799e3a38431SPaul Bohm printf ("passwd current mtime %ld\n", (long)w->attr.st_mtime); 2800e3a38431SPaul Bohm } 2801e3a38431SPaul Bohm else 2802e3a38431SPaul Bohm /* you shalt not abuse printf for puts */ 2803e3a38431SPaul Bohm puts ("wow, /etc/passwd is not there, expect problems. " 2804e3a38431SPaul Bohm "if this is windows, they already arrived\n"); 2805e3a38431SPaul Bohm } 2806e3a38431SPaul Bohm 2807e3a38431SPaul Bohm ... 2808e3a38431SPaul Bohm ev_stat passwd; 2809e3a38431SPaul Bohm 2810e3a38431SPaul Bohm ev_stat_init (&passwd, passwd_cb, "/etc/passwd", 0.); 2811e3a38431SPaul Bohm ev_stat_start (loop, &passwd); 2812e3a38431SPaul Bohm 2813e3a38431SPaul BohmExample: Like above, but additionally use a one-second delay so we do not 2814e3a38431SPaul Bohmmiss updates (however, frequent updates will delay processing, too, so 2815e3a38431SPaul Bohmone might do the work both on C<ev_stat> callback invocation I<and> on 2816e3a38431SPaul BohmC<ev_timer> callback invocation). 2817e3a38431SPaul Bohm 2818e3a38431SPaul Bohm static ev_stat passwd; 2819e3a38431SPaul Bohm static ev_timer timer; 2820e3a38431SPaul Bohm 2821e3a38431SPaul Bohm static void 2822e3a38431SPaul Bohm timer_cb (EV_P_ ev_timer *w, int revents) 2823e3a38431SPaul Bohm { 2824e3a38431SPaul Bohm ev_timer_stop (EV_A_ w); 2825e3a38431SPaul Bohm 2826e3a38431SPaul Bohm /* now it's one second after the most recent passwd change */ 2827e3a38431SPaul Bohm } 2828e3a38431SPaul Bohm 2829e3a38431SPaul Bohm static void 2830e3a38431SPaul Bohm stat_cb (EV_P_ ev_stat *w, int revents) 2831e3a38431SPaul Bohm { 2832e3a38431SPaul Bohm /* reset the one-second timer */ 2833e3a38431SPaul Bohm ev_timer_again (EV_A_ &timer); 2834e3a38431SPaul Bohm } 2835e3a38431SPaul Bohm 2836e3a38431SPaul Bohm ... 2837e3a38431SPaul Bohm ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.); 2838e3a38431SPaul Bohm ev_stat_start (loop, &passwd); 2839e3a38431SPaul Bohm ev_timer_init (&timer, timer_cb, 0., 1.02); 2840e3a38431SPaul Bohm 2841e3a38431SPaul Bohm 2842e3a38431SPaul Bohm=head2 C<ev_idle> - when you've got nothing better to do... 2843e3a38431SPaul Bohm 2844e3a38431SPaul BohmIdle watchers trigger events when no other events of the same or higher 2845e3a38431SPaul Bohmpriority are pending (prepare, check and other idle watchers do not count 2846e3a38431SPaul Bohmas receiving "events"). 2847e3a38431SPaul Bohm 2848e3a38431SPaul BohmThat is, as long as your process is busy handling sockets or timeouts 2849e3a38431SPaul Bohm(or even signals, imagine) of the same or higher priority it will not be 2850e3a38431SPaul Bohmtriggered. But when your process is idle (or only lower-priority watchers 2851e3a38431SPaul Bohmare pending), the idle watchers are being called once per event loop 2852e3a38431SPaul Bohmiteration - until stopped, that is, or your process receives more events 2853e3a38431SPaul Bohmand becomes busy again with higher priority stuff. 2854e3a38431SPaul Bohm 2855e3a38431SPaul BohmThe most noteworthy effect is that as long as any idle watchers are 2856e3a38431SPaul Bohmactive, the process will not block when waiting for new events. 2857e3a38431SPaul Bohm 2858e3a38431SPaul BohmApart from keeping your process non-blocking (which is a useful 2859e3a38431SPaul Bohmeffect on its own sometimes), idle watchers are a good place to do 2860e3a38431SPaul Bohm"pseudo-background processing", or delay processing stuff to after the 2861e3a38431SPaul Bohmevent loop has handled all outstanding events. 2862e3a38431SPaul Bohm 2863*93823e6cSPaul Bohm=head3 Abusing an C<ev_idle> watcher for its side-effect 2864*93823e6cSPaul Bohm 2865*93823e6cSPaul BohmAs long as there is at least one active idle watcher, libev will never 2866*93823e6cSPaul Bohmsleep unnecessarily. Or in other words, it will loop as fast as possible. 2867*93823e6cSPaul BohmFor this to work, the idle watcher doesn't need to be invoked at all - the 2868*93823e6cSPaul Bohmlowest priority will do. 2869*93823e6cSPaul Bohm 2870*93823e6cSPaul BohmThis mode of operation can be useful together with an C<ev_check> watcher, 2871*93823e6cSPaul Bohmto do something on each event loop iteration - for example to balance load 2872*93823e6cSPaul Bohmbetween different connections. 2873*93823e6cSPaul Bohm 2874*93823e6cSPaul BohmSee L</Abusing an ev_check watcher for its side-effect> for a longer 2875*93823e6cSPaul Bohmexample. 2876*93823e6cSPaul Bohm 2877e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 2878e3a38431SPaul Bohm 2879e3a38431SPaul Bohm=over 4 2880e3a38431SPaul Bohm 2881e3a38431SPaul Bohm=item ev_idle_init (ev_idle *, callback) 2882e3a38431SPaul Bohm 2883e3a38431SPaul BohmInitialises and configures the idle watcher - it has no parameters of any 2884e3a38431SPaul Bohmkind. There is a C<ev_idle_set> macro, but using it is utterly pointless, 2885e3a38431SPaul Bohmbelieve me. 2886e3a38431SPaul Bohm 2887e3a38431SPaul Bohm=back 2888e3a38431SPaul Bohm 2889e3a38431SPaul Bohm=head3 Examples 2890e3a38431SPaul Bohm 2891e3a38431SPaul BohmExample: Dynamically allocate an C<ev_idle> watcher, start it, and in the 2892e3a38431SPaul Bohmcallback, free it. Also, use no error checking, as usual. 2893e3a38431SPaul Bohm 2894e3a38431SPaul Bohm static void 2895e3a38431SPaul Bohm idle_cb (struct ev_loop *loop, ev_idle *w, int revents) 2896e3a38431SPaul Bohm { 2897*93823e6cSPaul Bohm // stop the watcher 2898*93823e6cSPaul Bohm ev_idle_stop (loop, w); 2899*93823e6cSPaul Bohm 2900*93823e6cSPaul Bohm // now we can free it 2901e3a38431SPaul Bohm free (w); 2902*93823e6cSPaul Bohm 2903e3a38431SPaul Bohm // now do something you wanted to do when the program has 2904e3a38431SPaul Bohm // no longer anything immediate to do. 2905e3a38431SPaul Bohm } 2906e3a38431SPaul Bohm 2907e3a38431SPaul Bohm ev_idle *idle_watcher = malloc (sizeof (ev_idle)); 2908e3a38431SPaul Bohm ev_idle_init (idle_watcher, idle_cb); 2909e3a38431SPaul Bohm ev_idle_start (loop, idle_watcher); 2910e3a38431SPaul Bohm 2911e3a38431SPaul Bohm 2912e3a38431SPaul Bohm=head2 C<ev_prepare> and C<ev_check> - customise your event loop! 2913e3a38431SPaul Bohm 2914*93823e6cSPaul BohmPrepare and check watchers are often (but not always) used in pairs: 2915e3a38431SPaul Bohmprepare watchers get invoked before the process blocks and check watchers 2916e3a38431SPaul Bohmafterwards. 2917e3a38431SPaul Bohm 2918*93823e6cSPaul BohmYou I<must not> call C<ev_run> (or similar functions that enter the 2919*93823e6cSPaul Bohmcurrent event loop) or C<ev_loop_fork> from either C<ev_prepare> or 2920*93823e6cSPaul BohmC<ev_check> watchers. Other loops than the current one are fine, 2921*93823e6cSPaul Bohmhowever. The rationale behind this is that you do not need to check 2922*93823e6cSPaul Bohmfor recursion in those watchers, i.e. the sequence will always be 2923*93823e6cSPaul BohmC<ev_prepare>, blocking, C<ev_check> so if you have one watcher of each 2924*93823e6cSPaul Bohmkind they will always be called in pairs bracketing the blocking call. 2925e3a38431SPaul Bohm 2926e3a38431SPaul BohmTheir main purpose is to integrate other event mechanisms into libev and 2927e3a38431SPaul Bohmtheir use is somewhat advanced. They could be used, for example, to track 2928e3a38431SPaul Bohmvariable changes, implement your own watchers, integrate net-snmp or a 2929e3a38431SPaul Bohmcoroutine library and lots more. They are also occasionally useful if 2930e3a38431SPaul Bohmyou cache some data and want to flush it before blocking (for example, 2931e3a38431SPaul Bohmin X programs you might want to do an C<XFlush ()> in an C<ev_prepare> 2932e3a38431SPaul Bohmwatcher). 2933e3a38431SPaul Bohm 2934e3a38431SPaul BohmThis is done by examining in each prepare call which file descriptors 2935e3a38431SPaul Bohmneed to be watched by the other library, registering C<ev_io> watchers 2936e3a38431SPaul Bohmfor them and starting an C<ev_timer> watcher for any timeouts (many 2937e3a38431SPaul Bohmlibraries provide exactly this functionality). Then, in the check watcher, 2938e3a38431SPaul Bohmyou check for any events that occurred (by checking the pending status 2939e3a38431SPaul Bohmof all watchers and stopping them) and call back into the library. The 2940e3a38431SPaul BohmI/O and timer callbacks will never actually be called (but must be valid 2941e3a38431SPaul Bohmnevertheless, because you never know, you know?). 2942e3a38431SPaul Bohm 2943e3a38431SPaul BohmAs another example, the Perl Coro module uses these hooks to integrate 2944e3a38431SPaul Bohmcoroutines into libev programs, by yielding to other active coroutines 2945e3a38431SPaul Bohmduring each prepare and only letting the process block if no coroutines 2946e3a38431SPaul Bohmare ready to run (it's actually more complicated: it only runs coroutines 2947e3a38431SPaul Bohmwith priority higher than or equal to the event loop and one coroutine 2948e3a38431SPaul Bohmof lower priority, but only once, using idle watchers to keep the event 2949e3a38431SPaul Bohmloop from blocking if lower-priority coroutines are active, thus mapping 2950e3a38431SPaul Bohmlow-priority coroutines to idle/background tasks). 2951e3a38431SPaul Bohm 2952*93823e6cSPaul BohmWhen used for this purpose, it is recommended to give C<ev_check> watchers 2953*93823e6cSPaul Bohmhighest (C<EV_MAXPRI>) priority, to ensure that they are being run before 2954*93823e6cSPaul Bohmany other watchers after the poll (this doesn't matter for C<ev_prepare> 2955*93823e6cSPaul Bohmwatchers). 2956e3a38431SPaul Bohm 2957e3a38431SPaul BohmAlso, C<ev_check> watchers (and C<ev_prepare> watchers, too) should not 2958e3a38431SPaul Bohmactivate ("feed") events into libev. While libev fully supports this, they 2959e3a38431SPaul Bohmmight get executed before other C<ev_check> watchers did their job. As 2960e3a38431SPaul BohmC<ev_check> watchers are often used to embed other (non-libev) event 2961e3a38431SPaul Bohmloops those other event loops might be in an unusable state until their 2962e3a38431SPaul BohmC<ev_check> watcher ran (always remind yourself to coexist peacefully with 2963e3a38431SPaul Bohmothers). 2964e3a38431SPaul Bohm 2965*93823e6cSPaul Bohm=head3 Abusing an C<ev_check> watcher for its side-effect 2966*93823e6cSPaul Bohm 2967*93823e6cSPaul BohmC<ev_check> (and less often also C<ev_prepare>) watchers can also be 2968*93823e6cSPaul Bohmuseful because they are called once per event loop iteration. For 2969*93823e6cSPaul Bohmexample, if you want to handle a large number of connections fairly, you 2970*93823e6cSPaul Bohmnormally only do a bit of work for each active connection, and if there 2971*93823e6cSPaul Bohmis more work to do, you wait for the next event loop iteration, so other 2972*93823e6cSPaul Bohmconnections have a chance of making progress. 2973*93823e6cSPaul Bohm 2974*93823e6cSPaul BohmUsing an C<ev_check> watcher is almost enough: it will be called on the 2975*93823e6cSPaul Bohmnext event loop iteration. However, that isn't as soon as possible - 2976*93823e6cSPaul Bohmwithout external events, your C<ev_check> watcher will not be invoked. 2977*93823e6cSPaul Bohm 2978*93823e6cSPaul BohmThis is where C<ev_idle> watchers come in handy - all you need is a 2979*93823e6cSPaul Bohmsingle global idle watcher that is active as long as you have one active 2980*93823e6cSPaul BohmC<ev_check> watcher. The C<ev_idle> watcher makes sure the event loop 2981*93823e6cSPaul Bohmwill not sleep, and the C<ev_check> watcher makes sure a callback gets 2982*93823e6cSPaul Bohminvoked. Neither watcher alone can do that. 2983*93823e6cSPaul Bohm 2984e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 2985e3a38431SPaul Bohm 2986e3a38431SPaul Bohm=over 4 2987e3a38431SPaul Bohm 2988e3a38431SPaul Bohm=item ev_prepare_init (ev_prepare *, callback) 2989e3a38431SPaul Bohm 2990e3a38431SPaul Bohm=item ev_check_init (ev_check *, callback) 2991e3a38431SPaul Bohm 2992e3a38431SPaul BohmInitialises and configures the prepare or check watcher - they have no 2993e3a38431SPaul Bohmparameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> 2994e3a38431SPaul Bohmmacros, but using them is utterly, utterly, utterly and completely 2995e3a38431SPaul Bohmpointless. 2996e3a38431SPaul Bohm 2997e3a38431SPaul Bohm=back 2998e3a38431SPaul Bohm 2999e3a38431SPaul Bohm=head3 Examples 3000e3a38431SPaul Bohm 3001e3a38431SPaul BohmThere are a number of principal ways to embed other event loops or modules 3002e3a38431SPaul Bohminto libev. Here are some ideas on how to include libadns into libev 3003e3a38431SPaul Bohm(there is a Perl module named C<EV::ADNS> that does this, which you could 3004e3a38431SPaul Bohmuse as a working example. Another Perl module named C<EV::Glib> embeds a 3005e3a38431SPaul BohmGlib main context into libev, and finally, C<Glib::EV> embeds EV into the 3006e3a38431SPaul BohmGlib event loop). 3007e3a38431SPaul Bohm 3008e3a38431SPaul BohmMethod 1: Add IO watchers and a timeout watcher in a prepare handler, 3009e3a38431SPaul Bohmand in a check watcher, destroy them and call into libadns. What follows 3010e3a38431SPaul Bohmis pseudo-code only of course. This requires you to either use a low 3011e3a38431SPaul Bohmpriority for the check watcher or use C<ev_clear_pending> explicitly, as 3012e3a38431SPaul Bohmthe callbacks for the IO/timeout watchers might not have been called yet. 3013e3a38431SPaul Bohm 3014e3a38431SPaul Bohm static ev_io iow [nfd]; 3015e3a38431SPaul Bohm static ev_timer tw; 3016e3a38431SPaul Bohm 3017e3a38431SPaul Bohm static void 3018e3a38431SPaul Bohm io_cb (struct ev_loop *loop, ev_io *w, int revents) 3019e3a38431SPaul Bohm { 3020e3a38431SPaul Bohm } 3021e3a38431SPaul Bohm 3022e3a38431SPaul Bohm // create io watchers for each fd and a timer before blocking 3023e3a38431SPaul Bohm static void 3024e3a38431SPaul Bohm adns_prepare_cb (struct ev_loop *loop, ev_prepare *w, int revents) 3025e3a38431SPaul Bohm { 3026e3a38431SPaul Bohm int timeout = 3600000; 3027e3a38431SPaul Bohm struct pollfd fds [nfd]; 3028e3a38431SPaul Bohm // actual code will need to loop here and realloc etc. 3029e3a38431SPaul Bohm adns_beforepoll (ads, fds, &nfd, &timeout, timeval_from (ev_time ())); 3030e3a38431SPaul Bohm 3031e3a38431SPaul Bohm /* the callback is illegal, but won't be called as we stop during check */ 3032e3a38431SPaul Bohm ev_timer_init (&tw, 0, timeout * 1e-3, 0.); 3033e3a38431SPaul Bohm ev_timer_start (loop, &tw); 3034e3a38431SPaul Bohm 3035e3a38431SPaul Bohm // create one ev_io per pollfd 3036e3a38431SPaul Bohm for (int i = 0; i < nfd; ++i) 3037e3a38431SPaul Bohm { 3038e3a38431SPaul Bohm ev_io_init (iow + i, io_cb, fds [i].fd, 3039e3a38431SPaul Bohm ((fds [i].events & POLLIN ? EV_READ : 0) 3040e3a38431SPaul Bohm | (fds [i].events & POLLOUT ? EV_WRITE : 0))); 3041e3a38431SPaul Bohm 3042e3a38431SPaul Bohm fds [i].revents = 0; 3043e3a38431SPaul Bohm ev_io_start (loop, iow + i); 3044e3a38431SPaul Bohm } 3045e3a38431SPaul Bohm } 3046e3a38431SPaul Bohm 3047e3a38431SPaul Bohm // stop all watchers after blocking 3048e3a38431SPaul Bohm static void 3049e3a38431SPaul Bohm adns_check_cb (struct ev_loop *loop, ev_check *w, int revents) 3050e3a38431SPaul Bohm { 3051e3a38431SPaul Bohm ev_timer_stop (loop, &tw); 3052e3a38431SPaul Bohm 3053e3a38431SPaul Bohm for (int i = 0; i < nfd; ++i) 3054e3a38431SPaul Bohm { 3055e3a38431SPaul Bohm // set the relevant poll flags 3056e3a38431SPaul Bohm // could also call adns_processreadable etc. here 3057e3a38431SPaul Bohm struct pollfd *fd = fds + i; 3058e3a38431SPaul Bohm int revents = ev_clear_pending (iow + i); 3059e3a38431SPaul Bohm if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; 3060e3a38431SPaul Bohm if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; 3061e3a38431SPaul Bohm 3062e3a38431SPaul Bohm // now stop the watcher 3063e3a38431SPaul Bohm ev_io_stop (loop, iow + i); 3064e3a38431SPaul Bohm } 3065e3a38431SPaul Bohm 3066e3a38431SPaul Bohm adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); 3067e3a38431SPaul Bohm } 3068e3a38431SPaul Bohm 3069e3a38431SPaul BohmMethod 2: This would be just like method 1, but you run C<adns_afterpoll> 3070e3a38431SPaul Bohmin the prepare watcher and would dispose of the check watcher. 3071e3a38431SPaul Bohm 3072e3a38431SPaul BohmMethod 3: If the module to be embedded supports explicit event 3073e3a38431SPaul Bohmnotification (libadns does), you can also make use of the actual watcher 3074e3a38431SPaul Bohmcallbacks, and only destroy/create the watchers in the prepare watcher. 3075e3a38431SPaul Bohm 3076e3a38431SPaul Bohm static void 3077e3a38431SPaul Bohm timer_cb (EV_P_ ev_timer *w, int revents) 3078e3a38431SPaul Bohm { 3079e3a38431SPaul Bohm adns_state ads = (adns_state)w->data; 3080e3a38431SPaul Bohm update_now (EV_A); 3081e3a38431SPaul Bohm 3082e3a38431SPaul Bohm adns_processtimeouts (ads, &tv_now); 3083e3a38431SPaul Bohm } 3084e3a38431SPaul Bohm 3085e3a38431SPaul Bohm static void 3086e3a38431SPaul Bohm io_cb (EV_P_ ev_io *w, int revents) 3087e3a38431SPaul Bohm { 3088e3a38431SPaul Bohm adns_state ads = (adns_state)w->data; 3089e3a38431SPaul Bohm update_now (EV_A); 3090e3a38431SPaul Bohm 3091e3a38431SPaul Bohm if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now); 3092e3a38431SPaul Bohm if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now); 3093e3a38431SPaul Bohm } 3094e3a38431SPaul Bohm 3095e3a38431SPaul Bohm // do not ever call adns_afterpoll 3096e3a38431SPaul Bohm 3097e3a38431SPaul BohmMethod 4: Do not use a prepare or check watcher because the module you 3098e3a38431SPaul Bohmwant to embed is not flexible enough to support it. Instead, you can 3099e3a38431SPaul Bohmoverride their poll function. The drawback with this solution is that the 3100e3a38431SPaul Bohmmain loop is now no longer controllable by EV. The C<Glib::EV> module uses 3101e3a38431SPaul Bohmthis approach, effectively embedding EV as a client into the horrible 3102e3a38431SPaul Bohmlibglib event loop. 3103e3a38431SPaul Bohm 3104e3a38431SPaul Bohm static gint 3105e3a38431SPaul Bohm event_poll_func (GPollFD *fds, guint nfds, gint timeout) 3106e3a38431SPaul Bohm { 3107e3a38431SPaul Bohm int got_events = 0; 3108e3a38431SPaul Bohm 3109e3a38431SPaul Bohm for (n = 0; n < nfds; ++n) 3110e3a38431SPaul Bohm // create/start io watcher that sets the relevant bits in fds[n] and increment got_events 3111e3a38431SPaul Bohm 3112e3a38431SPaul Bohm if (timeout >= 0) 3113e3a38431SPaul Bohm // create/start timer 3114e3a38431SPaul Bohm 3115e3a38431SPaul Bohm // poll 3116e3a38431SPaul Bohm ev_run (EV_A_ 0); 3117e3a38431SPaul Bohm 3118e3a38431SPaul Bohm // stop timer again 3119e3a38431SPaul Bohm if (timeout >= 0) 3120e3a38431SPaul Bohm ev_timer_stop (EV_A_ &to); 3121e3a38431SPaul Bohm 3122e3a38431SPaul Bohm // stop io watchers again - their callbacks should have set 3123e3a38431SPaul Bohm for (n = 0; n < nfds; ++n) 3124e3a38431SPaul Bohm ev_io_stop (EV_A_ iow [n]); 3125e3a38431SPaul Bohm 3126e3a38431SPaul Bohm return got_events; 3127e3a38431SPaul Bohm } 3128e3a38431SPaul Bohm 3129e3a38431SPaul Bohm 3130e3a38431SPaul Bohm=head2 C<ev_embed> - when one backend isn't enough... 3131e3a38431SPaul Bohm 3132e3a38431SPaul BohmThis is a rather advanced watcher type that lets you embed one event loop 3133e3a38431SPaul Bohminto another (currently only C<ev_io> events are supported in the embedded 3134e3a38431SPaul Bohmloop, other types of watchers might be handled in a delayed or incorrect 3135e3a38431SPaul Bohmfashion and must not be used). 3136e3a38431SPaul Bohm 3137e3a38431SPaul BohmThere are primarily two reasons you would want that: work around bugs and 3138e3a38431SPaul Bohmprioritise I/O. 3139e3a38431SPaul Bohm 3140e3a38431SPaul BohmAs an example for a bug workaround, the kqueue backend might only support 3141e3a38431SPaul Bohmsockets on some platform, so it is unusable as generic backend, but you 3142e3a38431SPaul Bohmstill want to make use of it because you have many sockets and it scales 3143e3a38431SPaul Bohmso nicely. In this case, you would create a kqueue-based loop and embed 3144e3a38431SPaul Bohmit into your default loop (which might use e.g. poll). Overall operation 3145e3a38431SPaul Bohmwill be a bit slower because first libev has to call C<poll> and then 3146e3a38431SPaul BohmC<kevent>, but at least you can use both mechanisms for what they are 3147e3a38431SPaul Bohmbest: C<kqueue> for scalable sockets and C<poll> if you want it to work :) 3148e3a38431SPaul Bohm 3149e3a38431SPaul BohmAs for prioritising I/O: under rare circumstances you have the case where 3150e3a38431SPaul Bohmsome fds have to be watched and handled very quickly (with low latency), 3151e3a38431SPaul Bohmand even priorities and idle watchers might have too much overhead. In 3152e3a38431SPaul Bohmthis case you would put all the high priority stuff in one loop and all 3153e3a38431SPaul Bohmthe rest in a second one, and embed the second one in the first. 3154e3a38431SPaul Bohm 3155e3a38431SPaul BohmAs long as the watcher is active, the callback will be invoked every 3156e3a38431SPaul Bohmtime there might be events pending in the embedded loop. The callback 3157e3a38431SPaul Bohmmust then call C<ev_embed_sweep (mainloop, watcher)> to make a single 3158e3a38431SPaul Bohmsweep and invoke their callbacks (the callback doesn't need to invoke the 3159e3a38431SPaul BohmC<ev_embed_sweep> function directly, it could also start an idle watcher 3160e3a38431SPaul Bohmto give the embedded loop strictly lower priority for example). 3161e3a38431SPaul Bohm 3162e3a38431SPaul BohmYou can also set the callback to C<0>, in which case the embed watcher 3163e3a38431SPaul Bohmwill automatically execute the embedded loop sweep whenever necessary. 3164e3a38431SPaul Bohm 3165e3a38431SPaul BohmFork detection will be handled transparently while the C<ev_embed> watcher 3166e3a38431SPaul Bohmis active, i.e., the embedded loop will automatically be forked when the 3167e3a38431SPaul Bohmembedding loop forks. In other cases, the user is responsible for calling 3168e3a38431SPaul BohmC<ev_loop_fork> on the embedded loop. 3169e3a38431SPaul Bohm 3170e3a38431SPaul BohmUnfortunately, not all backends are embeddable: only the ones returned by 3171e3a38431SPaul BohmC<ev_embeddable_backends> are, which, unfortunately, does not include any 3172e3a38431SPaul Bohmportable one. 3173e3a38431SPaul Bohm 3174e3a38431SPaul BohmSo when you want to use this feature you will always have to be prepared 3175e3a38431SPaul Bohmthat you cannot get an embeddable loop. The recommended way to get around 3176e3a38431SPaul Bohmthis is to have a separate variables for your embeddable loop, try to 3177e3a38431SPaul Bohmcreate it, and if that fails, use the normal loop for everything. 3178e3a38431SPaul Bohm 3179e3a38431SPaul Bohm=head3 C<ev_embed> and fork 3180e3a38431SPaul Bohm 3181e3a38431SPaul BohmWhile the C<ev_embed> watcher is running, forks in the embedding loop will 3182e3a38431SPaul Bohmautomatically be applied to the embedded loop as well, so no special 3183e3a38431SPaul Bohmfork handling is required in that case. When the watcher is not running, 3184e3a38431SPaul Bohmhowever, it is still the task of the libev user to call C<ev_loop_fork ()> 3185e3a38431SPaul Bohmas applicable. 3186e3a38431SPaul Bohm 3187e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 3188e3a38431SPaul Bohm 3189e3a38431SPaul Bohm=over 4 3190e3a38431SPaul Bohm 3191e3a38431SPaul Bohm=item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) 3192e3a38431SPaul Bohm 3193*93823e6cSPaul Bohm=item ev_embed_set (ev_embed *, struct ev_loop *embedded_loop) 3194e3a38431SPaul Bohm 3195e3a38431SPaul BohmConfigures the watcher to embed the given loop, which must be 3196e3a38431SPaul Bohmembeddable. If the callback is C<0>, then C<ev_embed_sweep> will be 3197e3a38431SPaul Bohminvoked automatically, otherwise it is the responsibility of the callback 3198e3a38431SPaul Bohmto invoke it (it will continue to be called until the sweep has been done, 3199e3a38431SPaul Bohmif you do not want that, you need to temporarily stop the embed watcher). 3200e3a38431SPaul Bohm 3201e3a38431SPaul Bohm=item ev_embed_sweep (loop, ev_embed *) 3202e3a38431SPaul Bohm 3203e3a38431SPaul BohmMake a single, non-blocking sweep over the embedded loop. This works 3204e3a38431SPaul Bohmsimilarly to C<ev_run (embedded_loop, EVRUN_NOWAIT)>, but in the most 3205e3a38431SPaul Bohmappropriate way for embedded loops. 3206e3a38431SPaul Bohm 3207e3a38431SPaul Bohm=item struct ev_loop *other [read-only] 3208e3a38431SPaul Bohm 3209e3a38431SPaul BohmThe embedded event loop. 3210e3a38431SPaul Bohm 3211e3a38431SPaul Bohm=back 3212e3a38431SPaul Bohm 3213e3a38431SPaul Bohm=head3 Examples 3214e3a38431SPaul Bohm 3215e3a38431SPaul BohmExample: Try to get an embeddable event loop and embed it into the default 3216e3a38431SPaul Bohmevent loop. If that is not possible, use the default loop. The default 3217e3a38431SPaul Bohmloop is stored in C<loop_hi>, while the embeddable loop is stored in 3218e3a38431SPaul BohmC<loop_lo> (which is C<loop_hi> in the case no embeddable loop can be 3219e3a38431SPaul Bohmused). 3220e3a38431SPaul Bohm 3221e3a38431SPaul Bohm struct ev_loop *loop_hi = ev_default_init (0); 3222e3a38431SPaul Bohm struct ev_loop *loop_lo = 0; 3223e3a38431SPaul Bohm ev_embed embed; 3224e3a38431SPaul Bohm 3225e3a38431SPaul Bohm // see if there is a chance of getting one that works 3226e3a38431SPaul Bohm // (remember that a flags value of 0 means autodetection) 3227e3a38431SPaul Bohm loop_lo = ev_embeddable_backends () & ev_recommended_backends () 3228e3a38431SPaul Bohm ? ev_loop_new (ev_embeddable_backends () & ev_recommended_backends ()) 3229e3a38431SPaul Bohm : 0; 3230e3a38431SPaul Bohm 3231e3a38431SPaul Bohm // if we got one, then embed it, otherwise default to loop_hi 3232e3a38431SPaul Bohm if (loop_lo) 3233e3a38431SPaul Bohm { 3234e3a38431SPaul Bohm ev_embed_init (&embed, 0, loop_lo); 3235e3a38431SPaul Bohm ev_embed_start (loop_hi, &embed); 3236e3a38431SPaul Bohm } 3237e3a38431SPaul Bohm else 3238e3a38431SPaul Bohm loop_lo = loop_hi; 3239e3a38431SPaul Bohm 3240e3a38431SPaul BohmExample: Check if kqueue is available but not recommended and create 3241e3a38431SPaul Bohma kqueue backend for use with sockets (which usually work with any 3242e3a38431SPaul Bohmkqueue implementation). Store the kqueue/socket-only event loop in 3243e3a38431SPaul BohmC<loop_socket>. (One might optionally use C<EVFLAG_NOENV>, too). 3244e3a38431SPaul Bohm 3245e3a38431SPaul Bohm struct ev_loop *loop = ev_default_init (0); 3246e3a38431SPaul Bohm struct ev_loop *loop_socket = 0; 3247e3a38431SPaul Bohm ev_embed embed; 3248e3a38431SPaul Bohm 3249e3a38431SPaul Bohm if (ev_supported_backends () & ~ev_recommended_backends () & EVBACKEND_KQUEUE) 3250e3a38431SPaul Bohm if ((loop_socket = ev_loop_new (EVBACKEND_KQUEUE)) 3251e3a38431SPaul Bohm { 3252e3a38431SPaul Bohm ev_embed_init (&embed, 0, loop_socket); 3253e3a38431SPaul Bohm ev_embed_start (loop, &embed); 3254e3a38431SPaul Bohm } 3255e3a38431SPaul Bohm 3256e3a38431SPaul Bohm if (!loop_socket) 3257e3a38431SPaul Bohm loop_socket = loop; 3258e3a38431SPaul Bohm 3259e3a38431SPaul Bohm // now use loop_socket for all sockets, and loop for everything else 3260e3a38431SPaul Bohm 3261e3a38431SPaul Bohm 3262e3a38431SPaul Bohm=head2 C<ev_fork> - the audacity to resume the event loop after a fork 3263e3a38431SPaul Bohm 3264e3a38431SPaul BohmFork watchers are called when a C<fork ()> was detected (usually because 3265e3a38431SPaul Bohmwhoever is a good citizen cared to tell libev about it by calling 3266*93823e6cSPaul BohmC<ev_loop_fork>). The invocation is done before the event loop blocks next 3267*93823e6cSPaul Bohmand before C<ev_check> watchers are being called, and only in the child 3268*93823e6cSPaul Bohmafter the fork. If whoever good citizen calling C<ev_default_fork> cheats 3269*93823e6cSPaul Bohmand calls it in the wrong process, the fork handlers will be invoked, too, 3270*93823e6cSPaul Bohmof course. 3271e3a38431SPaul Bohm 3272e3a38431SPaul Bohm=head3 The special problem of life after fork - how is it possible? 3273e3a38431SPaul Bohm 3274e3a38431SPaul BohmMost uses of C<fork ()> consist of forking, then some simple calls to set 3275e3a38431SPaul Bohmup/change the process environment, followed by a call to C<exec()>. This 3276e3a38431SPaul Bohmsequence should be handled by libev without any problems. 3277e3a38431SPaul Bohm 3278e3a38431SPaul BohmThis changes when the application actually wants to do event handling 3279e3a38431SPaul Bohmin the child, or both parent in child, in effect "continuing" after the 3280e3a38431SPaul Bohmfork. 3281e3a38431SPaul Bohm 3282e3a38431SPaul BohmThe default mode of operation (for libev, with application help to detect 3283e3a38431SPaul Bohmforks) is to duplicate all the state in the child, as would be expected 3284e3a38431SPaul Bohmwhen I<either> the parent I<or> the child process continues. 3285e3a38431SPaul Bohm 3286e3a38431SPaul BohmWhen both processes want to continue using libev, then this is usually the 3287e3a38431SPaul Bohmwrong result. In that case, usually one process (typically the parent) is 3288e3a38431SPaul Bohmsupposed to continue with all watchers in place as before, while the other 3289e3a38431SPaul Bohmprocess typically wants to start fresh, i.e. without any active watchers. 3290e3a38431SPaul Bohm 3291e3a38431SPaul BohmThe cleanest and most efficient way to achieve that with libev is to 3292e3a38431SPaul Bohmsimply create a new event loop, which of course will be "empty", and 3293e3a38431SPaul Bohmuse that for new watchers. This has the advantage of not touching more 3294e3a38431SPaul Bohmmemory than necessary, and thus avoiding the copy-on-write, and the 3295e3a38431SPaul Bohmdisadvantage of having to use multiple event loops (which do not support 3296e3a38431SPaul Bohmsignal watchers). 3297e3a38431SPaul Bohm 3298e3a38431SPaul BohmWhen this is not possible, or you want to use the default loop for 3299e3a38431SPaul Bohmother reasons, then in the process that wants to start "fresh", call 3300e3a38431SPaul BohmC<ev_loop_destroy (EV_DEFAULT)> followed by C<ev_default_loop (...)>. 3301e3a38431SPaul BohmDestroying the default loop will "orphan" (not stop) all registered 3302e3a38431SPaul Bohmwatchers, so you have to be careful not to execute code that modifies 3303e3a38431SPaul Bohmthose watchers. Note also that in that case, you have to re-register any 3304e3a38431SPaul Bohmsignal watchers. 3305e3a38431SPaul Bohm 3306e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 3307e3a38431SPaul Bohm 3308e3a38431SPaul Bohm=over 4 3309e3a38431SPaul Bohm 3310e3a38431SPaul Bohm=item ev_fork_init (ev_fork *, callback) 3311e3a38431SPaul Bohm 3312e3a38431SPaul BohmInitialises and configures the fork watcher - it has no parameters of any 3313e3a38431SPaul Bohmkind. There is a C<ev_fork_set> macro, but using it is utterly pointless, 3314e3a38431SPaul Bohmreally. 3315e3a38431SPaul Bohm 3316e3a38431SPaul Bohm=back 3317e3a38431SPaul Bohm 3318e3a38431SPaul Bohm 3319e3a38431SPaul Bohm=head2 C<ev_cleanup> - even the best things end 3320e3a38431SPaul Bohm 3321e3a38431SPaul BohmCleanup watchers are called just before the event loop is being destroyed 3322e3a38431SPaul Bohmby a call to C<ev_loop_destroy>. 3323e3a38431SPaul Bohm 3324e3a38431SPaul BohmWhile there is no guarantee that the event loop gets destroyed, cleanup 3325e3a38431SPaul Bohmwatchers provide a convenient method to install cleanup hooks for your 3326e3a38431SPaul Bohmprogram, worker threads and so on - you just to make sure to destroy the 3327e3a38431SPaul Bohmloop when you want them to be invoked. 3328e3a38431SPaul Bohm 3329e3a38431SPaul BohmCleanup watchers are invoked in the same way as any other watcher. Unlike 3330e3a38431SPaul Bohmall other watchers, they do not keep a reference to the event loop (which 3331e3a38431SPaul Bohmmakes a lot of sense if you think about it). Like all other watchers, you 3332e3a38431SPaul Bohmcan call libev functions in the callback, except C<ev_cleanup_start>. 3333e3a38431SPaul Bohm 3334e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 3335e3a38431SPaul Bohm 3336e3a38431SPaul Bohm=over 4 3337e3a38431SPaul Bohm 3338e3a38431SPaul Bohm=item ev_cleanup_init (ev_cleanup *, callback) 3339e3a38431SPaul Bohm 3340e3a38431SPaul BohmInitialises and configures the cleanup watcher - it has no parameters of 3341e3a38431SPaul Bohmany kind. There is a C<ev_cleanup_set> macro, but using it is utterly 3342e3a38431SPaul Bohmpointless, I assure you. 3343e3a38431SPaul Bohm 3344e3a38431SPaul Bohm=back 3345e3a38431SPaul Bohm 3346e3a38431SPaul BohmExample: Register an atexit handler to destroy the default loop, so any 3347e3a38431SPaul Bohmcleanup functions are called. 3348e3a38431SPaul Bohm 3349e3a38431SPaul Bohm static void 3350e3a38431SPaul Bohm program_exits (void) 3351e3a38431SPaul Bohm { 3352e3a38431SPaul Bohm ev_loop_destroy (EV_DEFAULT_UC); 3353e3a38431SPaul Bohm } 3354e3a38431SPaul Bohm 3355e3a38431SPaul Bohm ... 3356e3a38431SPaul Bohm atexit (program_exits); 3357e3a38431SPaul Bohm 3358e3a38431SPaul Bohm 3359e3a38431SPaul Bohm=head2 C<ev_async> - how to wake up an event loop 3360e3a38431SPaul Bohm 3361e3a38431SPaul BohmIn general, you cannot use an C<ev_loop> from multiple threads or other 3362e3a38431SPaul Bohmasynchronous sources such as signal handlers (as opposed to multiple event 3363e3a38431SPaul Bohmloops - those are of course safe to use in different threads). 3364e3a38431SPaul Bohm 3365e3a38431SPaul BohmSometimes, however, you need to wake up an event loop you do not control, 3366e3a38431SPaul Bohmfor example because it belongs to another thread. This is what C<ev_async> 3367e3a38431SPaul Bohmwatchers do: as long as the C<ev_async> watcher is active, you can signal 3368e3a38431SPaul Bohmit by calling C<ev_async_send>, which is thread- and signal safe. 3369e3a38431SPaul Bohm 3370e3a38431SPaul BohmThis functionality is very similar to C<ev_signal> watchers, as signals, 3371e3a38431SPaul Bohmtoo, are asynchronous in nature, and signals, too, will be compressed 3372e3a38431SPaul Bohm(i.e. the number of callback invocations may be less than the number of 3373*93823e6cSPaul BohmC<ev_async_send> calls). In fact, you could use signal watchers as a kind 3374e3a38431SPaul Bohmof "global async watchers" by using a watcher on an otherwise unused 3375e3a38431SPaul Bohmsignal, and C<ev_feed_signal> to signal this watcher from another thread, 3376e3a38431SPaul Bohmeven without knowing which loop owns the signal. 3377e3a38431SPaul Bohm 3378e3a38431SPaul Bohm=head3 Queueing 3379e3a38431SPaul Bohm 3380e3a38431SPaul BohmC<ev_async> does not support queueing of data in any way. The reason 3381e3a38431SPaul Bohmis that the author does not know of a simple (or any) algorithm for a 3382e3a38431SPaul Bohmmultiple-writer-single-reader queue that works in all cases and doesn't 3383e3a38431SPaul Bohmneed elaborate support such as pthreads or unportable memory access 3384e3a38431SPaul Bohmsemantics. 3385e3a38431SPaul Bohm 3386e3a38431SPaul BohmThat means that if you want to queue data, you have to provide your own 3387e3a38431SPaul Bohmqueue. But at least I can tell you how to implement locking around your 3388e3a38431SPaul Bohmqueue: 3389e3a38431SPaul Bohm 3390e3a38431SPaul Bohm=over 4 3391e3a38431SPaul Bohm 3392e3a38431SPaul Bohm=item queueing from a signal handler context 3393e3a38431SPaul Bohm 3394e3a38431SPaul BohmTo implement race-free queueing, you simply add to the queue in the signal 3395e3a38431SPaul Bohmhandler but you block the signal handler in the watcher callback. Here is 3396e3a38431SPaul Bohman example that does that for some fictitious SIGUSR1 handler: 3397e3a38431SPaul Bohm 3398e3a38431SPaul Bohm static ev_async mysig; 3399e3a38431SPaul Bohm 3400e3a38431SPaul Bohm static void 3401e3a38431SPaul Bohm sigusr1_handler (void) 3402e3a38431SPaul Bohm { 3403e3a38431SPaul Bohm sometype data; 3404e3a38431SPaul Bohm 3405e3a38431SPaul Bohm // no locking etc. 3406e3a38431SPaul Bohm queue_put (data); 3407e3a38431SPaul Bohm ev_async_send (EV_DEFAULT_ &mysig); 3408e3a38431SPaul Bohm } 3409e3a38431SPaul Bohm 3410e3a38431SPaul Bohm static void 3411e3a38431SPaul Bohm mysig_cb (EV_P_ ev_async *w, int revents) 3412e3a38431SPaul Bohm { 3413e3a38431SPaul Bohm sometype data; 3414e3a38431SPaul Bohm sigset_t block, prev; 3415e3a38431SPaul Bohm 3416e3a38431SPaul Bohm sigemptyset (&block); 3417e3a38431SPaul Bohm sigaddset (&block, SIGUSR1); 3418e3a38431SPaul Bohm sigprocmask (SIG_BLOCK, &block, &prev); 3419e3a38431SPaul Bohm 3420e3a38431SPaul Bohm while (queue_get (&data)) 3421e3a38431SPaul Bohm process (data); 3422e3a38431SPaul Bohm 3423e3a38431SPaul Bohm if (sigismember (&prev, SIGUSR1) 3424e3a38431SPaul Bohm sigprocmask (SIG_UNBLOCK, &block, 0); 3425e3a38431SPaul Bohm } 3426e3a38431SPaul Bohm 3427e3a38431SPaul Bohm(Note: pthreads in theory requires you to use C<pthread_setmask> 3428e3a38431SPaul Bohminstead of C<sigprocmask> when you use threads, but libev doesn't do it 3429e3a38431SPaul Bohmeither...). 3430e3a38431SPaul Bohm 3431e3a38431SPaul Bohm=item queueing from a thread context 3432e3a38431SPaul Bohm 3433e3a38431SPaul BohmThe strategy for threads is different, as you cannot (easily) block 3434e3a38431SPaul Bohmthreads but you can easily preempt them, so to queue safely you need to 3435e3a38431SPaul Bohmemploy a traditional mutex lock, such as in this pthread example: 3436e3a38431SPaul Bohm 3437e3a38431SPaul Bohm static ev_async mysig; 3438e3a38431SPaul Bohm static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; 3439e3a38431SPaul Bohm 3440e3a38431SPaul Bohm static void 3441e3a38431SPaul Bohm otherthread (void) 3442e3a38431SPaul Bohm { 3443e3a38431SPaul Bohm // only need to lock the actual queueing operation 3444e3a38431SPaul Bohm pthread_mutex_lock (&mymutex); 3445e3a38431SPaul Bohm queue_put (data); 3446e3a38431SPaul Bohm pthread_mutex_unlock (&mymutex); 3447e3a38431SPaul Bohm 3448e3a38431SPaul Bohm ev_async_send (EV_DEFAULT_ &mysig); 3449e3a38431SPaul Bohm } 3450e3a38431SPaul Bohm 3451e3a38431SPaul Bohm static void 3452e3a38431SPaul Bohm mysig_cb (EV_P_ ev_async *w, int revents) 3453e3a38431SPaul Bohm { 3454e3a38431SPaul Bohm pthread_mutex_lock (&mymutex); 3455e3a38431SPaul Bohm 3456e3a38431SPaul Bohm while (queue_get (&data)) 3457e3a38431SPaul Bohm process (data); 3458e3a38431SPaul Bohm 3459e3a38431SPaul Bohm pthread_mutex_unlock (&mymutex); 3460e3a38431SPaul Bohm } 3461e3a38431SPaul Bohm 3462e3a38431SPaul Bohm=back 3463e3a38431SPaul Bohm 3464e3a38431SPaul Bohm 3465e3a38431SPaul Bohm=head3 Watcher-Specific Functions and Data Members 3466e3a38431SPaul Bohm 3467e3a38431SPaul Bohm=over 4 3468e3a38431SPaul Bohm 3469e3a38431SPaul Bohm=item ev_async_init (ev_async *, callback) 3470e3a38431SPaul Bohm 3471e3a38431SPaul BohmInitialises and configures the async watcher - it has no parameters of any 3472e3a38431SPaul Bohmkind. There is a C<ev_async_set> macro, but using it is utterly pointless, 3473e3a38431SPaul Bohmtrust me. 3474e3a38431SPaul Bohm 3475e3a38431SPaul Bohm=item ev_async_send (loop, ev_async *) 3476e3a38431SPaul Bohm 3477e3a38431SPaul BohmSends/signals/activates the given C<ev_async> watcher, that is, feeds 3478e3a38431SPaul Bohman C<EV_ASYNC> event on the watcher into the event loop, and instantly 3479e3a38431SPaul Bohmreturns. 3480e3a38431SPaul Bohm 3481e3a38431SPaul BohmUnlike C<ev_feed_event>, this call is safe to do from other threads, 3482e3a38431SPaul Bohmsignal or similar contexts (see the discussion of C<EV_ATOMIC_T> in the 3483e3a38431SPaul Bohmembedding section below on what exactly this means). 3484e3a38431SPaul Bohm 3485e3a38431SPaul BohmNote that, as with other watchers in libev, multiple events might get 3486*93823e6cSPaul Bohmcompressed into a single callback invocation (another way to look at 3487*93823e6cSPaul Bohmthis is that C<ev_async> watchers are level-triggered: they are set on 3488*93823e6cSPaul BohmC<ev_async_send>, reset when the event loop detects that). 3489e3a38431SPaul Bohm 3490*93823e6cSPaul BohmThis call incurs the overhead of at most one extra system call per event 3491*93823e6cSPaul Bohmloop iteration, if the event loop is blocked, and no syscall at all if 3492*93823e6cSPaul Bohmthe event loop (or your program) is processing events. That means that 3493*93823e6cSPaul Bohmrepeated calls are basically free (there is no need to avoid calls for 3494*93823e6cSPaul Bohmperformance reasons) and that the overhead becomes smaller (typically 3495*93823e6cSPaul Bohmzero) under load. 3496e3a38431SPaul Bohm 3497e3a38431SPaul Bohm=item bool = ev_async_pending (ev_async *) 3498e3a38431SPaul Bohm 3499e3a38431SPaul BohmReturns a non-zero value when C<ev_async_send> has been called on the 3500e3a38431SPaul Bohmwatcher but the event has not yet been processed (or even noted) by the 3501e3a38431SPaul Bohmevent loop. 3502e3a38431SPaul Bohm 3503e3a38431SPaul BohmC<ev_async_send> sets a flag in the watcher and wakes up the loop. When 3504e3a38431SPaul Bohmthe loop iterates next and checks for the watcher to have become active, 3505e3a38431SPaul Bohmit will reset the flag again. C<ev_async_pending> can be used to very 3506e3a38431SPaul Bohmquickly check whether invoking the loop might be a good idea. 3507e3a38431SPaul Bohm 3508e3a38431SPaul BohmNot that this does I<not> check whether the watcher itself is pending, 3509e3a38431SPaul Bohmonly whether it has been requested to make this watcher pending: there 3510e3a38431SPaul Bohmis a time window between the event loop checking and resetting the async 3511e3a38431SPaul Bohmnotification, and the callback being invoked. 3512e3a38431SPaul Bohm 3513e3a38431SPaul Bohm=back 3514e3a38431SPaul Bohm 3515e3a38431SPaul Bohm 3516e3a38431SPaul Bohm=head1 OTHER FUNCTIONS 3517e3a38431SPaul Bohm 3518e3a38431SPaul BohmThere are some other functions of possible interest. Described. Here. Now. 3519e3a38431SPaul Bohm 3520e3a38431SPaul Bohm=over 4 3521e3a38431SPaul Bohm 3522e3a38431SPaul Bohm=item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) 3523e3a38431SPaul Bohm 3524e3a38431SPaul BohmThis function combines a simple timer and an I/O watcher, calls your 3525e3a38431SPaul Bohmcallback on whichever event happens first and automatically stops both 3526e3a38431SPaul Bohmwatchers. This is useful if you want to wait for a single event on an fd 3527e3a38431SPaul Bohmor timeout without having to allocate/configure/start/stop/free one or 3528e3a38431SPaul Bohmmore watchers yourself. 3529e3a38431SPaul Bohm 3530e3a38431SPaul BohmIf C<fd> is less than 0, then no I/O watcher will be started and the 3531e3a38431SPaul BohmC<events> argument is being ignored. Otherwise, an C<ev_io> watcher for 3532e3a38431SPaul Bohmthe given C<fd> and C<events> set will be created and started. 3533e3a38431SPaul Bohm 3534e3a38431SPaul BohmIf C<timeout> is less than 0, then no timeout watcher will be 3535e3a38431SPaul Bohmstarted. Otherwise an C<ev_timer> watcher with after = C<timeout> (and 3536e3a38431SPaul Bohmrepeat = 0) will be started. C<0> is a valid timeout. 3537e3a38431SPaul Bohm 3538e3a38431SPaul BohmThe callback has the type C<void (*cb)(int revents, void *arg)> and is 3539e3a38431SPaul Bohmpassed an C<revents> set like normal event callbacks (a combination of 3540e3a38431SPaul BohmC<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMER>) and the C<arg> 3541e3a38431SPaul Bohmvalue passed to C<ev_once>. Note that it is possible to receive I<both> 3542e3a38431SPaul Bohma timeout and an io event at the same time - you probably should give io 3543e3a38431SPaul Bohmevents precedence. 3544e3a38431SPaul Bohm 3545e3a38431SPaul BohmExample: wait up to ten seconds for data to appear on STDIN_FILENO. 3546e3a38431SPaul Bohm 3547e3a38431SPaul Bohm static void stdin_ready (int revents, void *arg) 3548e3a38431SPaul Bohm { 3549e3a38431SPaul Bohm if (revents & EV_READ) 3550e3a38431SPaul Bohm /* stdin might have data for us, joy! */; 3551e3a38431SPaul Bohm else if (revents & EV_TIMER) 3552e3a38431SPaul Bohm /* doh, nothing entered */; 3553e3a38431SPaul Bohm } 3554e3a38431SPaul Bohm 3555e3a38431SPaul Bohm ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); 3556e3a38431SPaul Bohm 3557e3a38431SPaul Bohm=item ev_feed_fd_event (loop, int fd, int revents) 3558e3a38431SPaul Bohm 3559e3a38431SPaul BohmFeed an event on the given fd, as if a file descriptor backend detected 3560*93823e6cSPaul Bohmthe given events. 3561e3a38431SPaul Bohm 3562e3a38431SPaul Bohm=item ev_feed_signal_event (loop, int signum) 3563e3a38431SPaul Bohm 3564e3a38431SPaul BohmFeed an event as if the given signal occurred. See also C<ev_feed_signal>, 3565e3a38431SPaul Bohmwhich is async-safe. 3566e3a38431SPaul Bohm 3567e3a38431SPaul Bohm=back 3568e3a38431SPaul Bohm 3569e3a38431SPaul Bohm 3570e3a38431SPaul Bohm=head1 COMMON OR USEFUL IDIOMS (OR BOTH) 3571e3a38431SPaul Bohm 3572e3a38431SPaul BohmThis section explains some common idioms that are not immediately 3573e3a38431SPaul Bohmobvious. Note that examples are sprinkled over the whole manual, and this 3574e3a38431SPaul Bohmsection only contains stuff that wouldn't fit anywhere else. 3575e3a38431SPaul Bohm 3576e3a38431SPaul Bohm=head2 ASSOCIATING CUSTOM DATA WITH A WATCHER 3577e3a38431SPaul Bohm 3578e3a38431SPaul BohmEach watcher has, by default, a C<void *data> member that you can read 3579e3a38431SPaul Bohmor modify at any time: libev will completely ignore it. This can be used 3580e3a38431SPaul Bohmto associate arbitrary data with your watcher. If you need more data and 3581e3a38431SPaul Bohmdon't want to allocate memory separately and store a pointer to it in that 3582e3a38431SPaul Bohmdata member, you can also "subclass" the watcher type and provide your own 3583e3a38431SPaul Bohmdata: 3584e3a38431SPaul Bohm 3585e3a38431SPaul Bohm struct my_io 3586e3a38431SPaul Bohm { 3587e3a38431SPaul Bohm ev_io io; 3588e3a38431SPaul Bohm int otherfd; 3589e3a38431SPaul Bohm void *somedata; 3590e3a38431SPaul Bohm struct whatever *mostinteresting; 3591e3a38431SPaul Bohm }; 3592e3a38431SPaul Bohm 3593e3a38431SPaul Bohm ... 3594e3a38431SPaul Bohm struct my_io w; 3595e3a38431SPaul Bohm ev_io_init (&w.io, my_cb, fd, EV_READ); 3596e3a38431SPaul Bohm 3597e3a38431SPaul BohmAnd since your callback will be called with a pointer to the watcher, you 3598e3a38431SPaul Bohmcan cast it back to your own type: 3599e3a38431SPaul Bohm 3600e3a38431SPaul Bohm static void my_cb (struct ev_loop *loop, ev_io *w_, int revents) 3601e3a38431SPaul Bohm { 3602e3a38431SPaul Bohm struct my_io *w = (struct my_io *)w_; 3603e3a38431SPaul Bohm ... 3604e3a38431SPaul Bohm } 3605e3a38431SPaul Bohm 3606e3a38431SPaul BohmMore interesting and less C-conformant ways of casting your callback 3607e3a38431SPaul Bohmfunction type instead have been omitted. 3608e3a38431SPaul Bohm 3609e3a38431SPaul Bohm=head2 BUILDING YOUR OWN COMPOSITE WATCHERS 3610e3a38431SPaul Bohm 3611e3a38431SPaul BohmAnother common scenario is to use some data structure with multiple 3612e3a38431SPaul Bohmembedded watchers, in effect creating your own watcher that combines 3613e3a38431SPaul Bohmmultiple libev event sources into one "super-watcher": 3614e3a38431SPaul Bohm 3615e3a38431SPaul Bohm struct my_biggy 3616e3a38431SPaul Bohm { 3617e3a38431SPaul Bohm int some_data; 3618e3a38431SPaul Bohm ev_timer t1; 3619e3a38431SPaul Bohm ev_timer t2; 3620e3a38431SPaul Bohm } 3621e3a38431SPaul Bohm 3622e3a38431SPaul BohmIn this case getting the pointer to C<my_biggy> is a bit more 3623e3a38431SPaul Bohmcomplicated: Either you store the address of your C<my_biggy> struct in 3624e3a38431SPaul Bohmthe C<data> member of the watcher (for woozies or C++ coders), or you need 3625e3a38431SPaul Bohmto use some pointer arithmetic using C<offsetof> inside your watchers (for 3626e3a38431SPaul Bohmreal programmers): 3627e3a38431SPaul Bohm 3628e3a38431SPaul Bohm #include <stddef.h> 3629e3a38431SPaul Bohm 3630e3a38431SPaul Bohm static void 3631e3a38431SPaul Bohm t1_cb (EV_P_ ev_timer *w, int revents) 3632e3a38431SPaul Bohm { 3633e3a38431SPaul Bohm struct my_biggy big = (struct my_biggy *) 3634e3a38431SPaul Bohm (((char *)w) - offsetof (struct my_biggy, t1)); 3635e3a38431SPaul Bohm } 3636e3a38431SPaul Bohm 3637e3a38431SPaul Bohm static void 3638e3a38431SPaul Bohm t2_cb (EV_P_ ev_timer *w, int revents) 3639e3a38431SPaul Bohm { 3640e3a38431SPaul Bohm struct my_biggy big = (struct my_biggy *) 3641e3a38431SPaul Bohm (((char *)w) - offsetof (struct my_biggy, t2)); 3642e3a38431SPaul Bohm } 3643e3a38431SPaul Bohm 3644*93823e6cSPaul Bohm=head2 AVOIDING FINISHING BEFORE RETURNING 3645*93823e6cSPaul Bohm 3646*93823e6cSPaul BohmOften you have structures like this in event-based programs: 3647*93823e6cSPaul Bohm 3648*93823e6cSPaul Bohm callback () 3649*93823e6cSPaul Bohm { 3650*93823e6cSPaul Bohm free (request); 3651*93823e6cSPaul Bohm } 3652*93823e6cSPaul Bohm 3653*93823e6cSPaul Bohm request = start_new_request (..., callback); 3654*93823e6cSPaul Bohm 3655*93823e6cSPaul BohmThe intent is to start some "lengthy" operation. The C<request> could be 3656*93823e6cSPaul Bohmused to cancel the operation, or do other things with it. 3657*93823e6cSPaul Bohm 3658*93823e6cSPaul BohmIt's not uncommon to have code paths in C<start_new_request> that 3659*93823e6cSPaul Bohmimmediately invoke the callback, for example, to report errors. Or you add 3660*93823e6cSPaul Bohmsome caching layer that finds that it can skip the lengthy aspects of the 3661*93823e6cSPaul Bohmoperation and simply invoke the callback with the result. 3662*93823e6cSPaul Bohm 3663*93823e6cSPaul BohmThe problem here is that this will happen I<before> C<start_new_request> 3664*93823e6cSPaul Bohmhas returned, so C<request> is not set. 3665*93823e6cSPaul Bohm 3666*93823e6cSPaul BohmEven if you pass the request by some safer means to the callback, you 3667*93823e6cSPaul Bohmmight want to do something to the request after starting it, such as 3668*93823e6cSPaul Bohmcanceling it, which probably isn't working so well when the callback has 3669*93823e6cSPaul Bohmalready been invoked. 3670*93823e6cSPaul Bohm 3671*93823e6cSPaul BohmA common way around all these issues is to make sure that 3672*93823e6cSPaul BohmC<start_new_request> I<always> returns before the callback is invoked. If 3673*93823e6cSPaul BohmC<start_new_request> immediately knows the result, it can artificially 3674*93823e6cSPaul Bohmdelay invoking the callback by using a C<prepare> or C<idle> watcher for 3675*93823e6cSPaul Bohmexample, or more sneakily, by reusing an existing (stopped) watcher and 3676*93823e6cSPaul Bohmpushing it into the pending queue: 3677*93823e6cSPaul Bohm 3678*93823e6cSPaul Bohm ev_set_cb (watcher, callback); 3679*93823e6cSPaul Bohm ev_feed_event (EV_A_ watcher, 0); 3680*93823e6cSPaul Bohm 3681*93823e6cSPaul BohmThis way, C<start_new_request> can safely return before the callback is 3682*93823e6cSPaul Bohminvoked, while not delaying callback invocation too much. 3683*93823e6cSPaul Bohm 3684e3a38431SPaul Bohm=head2 MODEL/NESTED EVENT LOOP INVOCATIONS AND EXIT CONDITIONS 3685e3a38431SPaul Bohm 3686e3a38431SPaul BohmOften (especially in GUI toolkits) there are places where you have 3687e3a38431SPaul BohmI<modal> interaction, which is most easily implemented by recursively 3688e3a38431SPaul Bohminvoking C<ev_run>. 3689e3a38431SPaul Bohm 3690e3a38431SPaul BohmThis brings the problem of exiting - a callback might want to finish the 3691e3a38431SPaul Bohmmain C<ev_run> call, but not the nested one (e.g. user clicked "Quit", but 3692e3a38431SPaul Bohma modal "Are you sure?" dialog is still waiting), or just the nested one 3693e3a38431SPaul Bohmand not the main one (e.g. user clocked "Ok" in a modal dialog), or some 3694*93823e6cSPaul Bohmother combination: In these cases, a simple C<ev_break> will not work. 3695e3a38431SPaul Bohm 3696e3a38431SPaul BohmThe solution is to maintain "break this loop" variable for each C<ev_run> 3697e3a38431SPaul Bohminvocation, and use a loop around C<ev_run> until the condition is 3698e3a38431SPaul Bohmtriggered, using C<EVRUN_ONCE>: 3699e3a38431SPaul Bohm 3700e3a38431SPaul Bohm // main loop 3701e3a38431SPaul Bohm int exit_main_loop = 0; 3702e3a38431SPaul Bohm 3703e3a38431SPaul Bohm while (!exit_main_loop) 3704e3a38431SPaul Bohm ev_run (EV_DEFAULT_ EVRUN_ONCE); 3705e3a38431SPaul Bohm 3706*93823e6cSPaul Bohm // in a modal watcher 3707e3a38431SPaul Bohm int exit_nested_loop = 0; 3708e3a38431SPaul Bohm 3709e3a38431SPaul Bohm while (!exit_nested_loop) 3710e3a38431SPaul Bohm ev_run (EV_A_ EVRUN_ONCE); 3711e3a38431SPaul Bohm 3712e3a38431SPaul BohmTo exit from any of these loops, just set the corresponding exit variable: 3713e3a38431SPaul Bohm 3714e3a38431SPaul Bohm // exit modal loop 3715e3a38431SPaul Bohm exit_nested_loop = 1; 3716e3a38431SPaul Bohm 3717e3a38431SPaul Bohm // exit main program, after modal loop is finished 3718e3a38431SPaul Bohm exit_main_loop = 1; 3719e3a38431SPaul Bohm 3720e3a38431SPaul Bohm // exit both 3721e3a38431SPaul Bohm exit_main_loop = exit_nested_loop = 1; 3722e3a38431SPaul Bohm 3723e3a38431SPaul Bohm=head2 THREAD LOCKING EXAMPLE 3724e3a38431SPaul Bohm 3725e3a38431SPaul BohmHere is a fictitious example of how to run an event loop in a different 3726e3a38431SPaul Bohmthread from where callbacks are being invoked and watchers are 3727e3a38431SPaul Bohmcreated/added/removed. 3728e3a38431SPaul Bohm 3729e3a38431SPaul BohmFor a real-world example, see the C<EV::Loop::Async> perl module, 3730e3a38431SPaul Bohmwhich uses exactly this technique (which is suited for many high-level 3731e3a38431SPaul Bohmlanguages). 3732e3a38431SPaul Bohm 3733e3a38431SPaul BohmThe example uses a pthread mutex to protect the loop data, a condition 3734e3a38431SPaul Bohmvariable to wait for callback invocations, an async watcher to notify the 3735e3a38431SPaul Bohmevent loop thread and an unspecified mechanism to wake up the main thread. 3736e3a38431SPaul Bohm 3737e3a38431SPaul BohmFirst, you need to associate some data with the event loop: 3738e3a38431SPaul Bohm 3739e3a38431SPaul Bohm typedef struct { 3740e3a38431SPaul Bohm mutex_t lock; /* global loop lock */ 3741e3a38431SPaul Bohm ev_async async_w; 3742e3a38431SPaul Bohm thread_t tid; 3743e3a38431SPaul Bohm cond_t invoke_cv; 3744e3a38431SPaul Bohm } userdata; 3745e3a38431SPaul Bohm 3746e3a38431SPaul Bohm void prepare_loop (EV_P) 3747e3a38431SPaul Bohm { 3748e3a38431SPaul Bohm // for simplicity, we use a static userdata struct. 3749e3a38431SPaul Bohm static userdata u; 3750e3a38431SPaul Bohm 3751e3a38431SPaul Bohm ev_async_init (&u->async_w, async_cb); 3752e3a38431SPaul Bohm ev_async_start (EV_A_ &u->async_w); 3753e3a38431SPaul Bohm 3754e3a38431SPaul Bohm pthread_mutex_init (&u->lock, 0); 3755e3a38431SPaul Bohm pthread_cond_init (&u->invoke_cv, 0); 3756e3a38431SPaul Bohm 3757e3a38431SPaul Bohm // now associate this with the loop 3758e3a38431SPaul Bohm ev_set_userdata (EV_A_ u); 3759e3a38431SPaul Bohm ev_set_invoke_pending_cb (EV_A_ l_invoke); 3760e3a38431SPaul Bohm ev_set_loop_release_cb (EV_A_ l_release, l_acquire); 3761e3a38431SPaul Bohm 3762e3a38431SPaul Bohm // then create the thread running ev_run 3763e3a38431SPaul Bohm pthread_create (&u->tid, 0, l_run, EV_A); 3764e3a38431SPaul Bohm } 3765e3a38431SPaul Bohm 3766e3a38431SPaul BohmThe callback for the C<ev_async> watcher does nothing: the watcher is used 3767e3a38431SPaul Bohmsolely to wake up the event loop so it takes notice of any new watchers 3768e3a38431SPaul Bohmthat might have been added: 3769e3a38431SPaul Bohm 3770e3a38431SPaul Bohm static void 3771e3a38431SPaul Bohm async_cb (EV_P_ ev_async *w, int revents) 3772e3a38431SPaul Bohm { 3773e3a38431SPaul Bohm // just used for the side effects 3774e3a38431SPaul Bohm } 3775e3a38431SPaul Bohm 3776e3a38431SPaul BohmThe C<l_release> and C<l_acquire> callbacks simply unlock/lock the mutex 3777e3a38431SPaul Bohmprotecting the loop data, respectively. 3778e3a38431SPaul Bohm 3779e3a38431SPaul Bohm static void 3780e3a38431SPaul Bohm l_release (EV_P) 3781e3a38431SPaul Bohm { 3782e3a38431SPaul Bohm userdata *u = ev_userdata (EV_A); 3783e3a38431SPaul Bohm pthread_mutex_unlock (&u->lock); 3784e3a38431SPaul Bohm } 3785e3a38431SPaul Bohm 3786e3a38431SPaul Bohm static void 3787e3a38431SPaul Bohm l_acquire (EV_P) 3788e3a38431SPaul Bohm { 3789e3a38431SPaul Bohm userdata *u = ev_userdata (EV_A); 3790e3a38431SPaul Bohm pthread_mutex_lock (&u->lock); 3791e3a38431SPaul Bohm } 3792e3a38431SPaul Bohm 3793e3a38431SPaul BohmThe event loop thread first acquires the mutex, and then jumps straight 3794e3a38431SPaul Bohminto C<ev_run>: 3795e3a38431SPaul Bohm 3796e3a38431SPaul Bohm void * 3797e3a38431SPaul Bohm l_run (void *thr_arg) 3798e3a38431SPaul Bohm { 3799e3a38431SPaul Bohm struct ev_loop *loop = (struct ev_loop *)thr_arg; 3800e3a38431SPaul Bohm 3801e3a38431SPaul Bohm l_acquire (EV_A); 3802e3a38431SPaul Bohm pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0); 3803e3a38431SPaul Bohm ev_run (EV_A_ 0); 3804e3a38431SPaul Bohm l_release (EV_A); 3805e3a38431SPaul Bohm 3806e3a38431SPaul Bohm return 0; 3807e3a38431SPaul Bohm } 3808e3a38431SPaul Bohm 3809e3a38431SPaul BohmInstead of invoking all pending watchers, the C<l_invoke> callback will 3810e3a38431SPaul Bohmsignal the main thread via some unspecified mechanism (signals? pipe 3811e3a38431SPaul Bohmwrites? C<Async::Interrupt>?) and then waits until all pending watchers 3812e3a38431SPaul Bohmhave been called (in a while loop because a) spurious wakeups are possible 3813e3a38431SPaul Bohmand b) skipping inter-thread-communication when there are no pending 3814e3a38431SPaul Bohmwatchers is very beneficial): 3815e3a38431SPaul Bohm 3816e3a38431SPaul Bohm static void 3817e3a38431SPaul Bohm l_invoke (EV_P) 3818e3a38431SPaul Bohm { 3819e3a38431SPaul Bohm userdata *u = ev_userdata (EV_A); 3820e3a38431SPaul Bohm 3821e3a38431SPaul Bohm while (ev_pending_count (EV_A)) 3822e3a38431SPaul Bohm { 3823e3a38431SPaul Bohm wake_up_other_thread_in_some_magic_or_not_so_magic_way (); 3824e3a38431SPaul Bohm pthread_cond_wait (&u->invoke_cv, &u->lock); 3825e3a38431SPaul Bohm } 3826e3a38431SPaul Bohm } 3827e3a38431SPaul Bohm 3828e3a38431SPaul BohmNow, whenever the main thread gets told to invoke pending watchers, it 3829e3a38431SPaul Bohmwill grab the lock, call C<ev_invoke_pending> and then signal the loop 3830e3a38431SPaul Bohmthread to continue: 3831e3a38431SPaul Bohm 3832e3a38431SPaul Bohm static void 3833e3a38431SPaul Bohm real_invoke_pending (EV_P) 3834e3a38431SPaul Bohm { 3835e3a38431SPaul Bohm userdata *u = ev_userdata (EV_A); 3836e3a38431SPaul Bohm 3837e3a38431SPaul Bohm pthread_mutex_lock (&u->lock); 3838e3a38431SPaul Bohm ev_invoke_pending (EV_A); 3839e3a38431SPaul Bohm pthread_cond_signal (&u->invoke_cv); 3840e3a38431SPaul Bohm pthread_mutex_unlock (&u->lock); 3841e3a38431SPaul Bohm } 3842e3a38431SPaul Bohm 3843e3a38431SPaul BohmWhenever you want to start/stop a watcher or do other modifications to an 3844e3a38431SPaul Bohmevent loop, you will now have to lock: 3845e3a38431SPaul Bohm 3846e3a38431SPaul Bohm ev_timer timeout_watcher; 3847e3a38431SPaul Bohm userdata *u = ev_userdata (EV_A); 3848e3a38431SPaul Bohm 3849e3a38431SPaul Bohm ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); 3850e3a38431SPaul Bohm 3851e3a38431SPaul Bohm pthread_mutex_lock (&u->lock); 3852e3a38431SPaul Bohm ev_timer_start (EV_A_ &timeout_watcher); 3853e3a38431SPaul Bohm ev_async_send (EV_A_ &u->async_w); 3854e3a38431SPaul Bohm pthread_mutex_unlock (&u->lock); 3855e3a38431SPaul Bohm 3856e3a38431SPaul BohmNote that sending the C<ev_async> watcher is required because otherwise 3857e3a38431SPaul Bohman event loop currently blocking in the kernel will have no knowledge 3858e3a38431SPaul Bohmabout the newly added timer. By waking up the loop it will pick up any new 3859e3a38431SPaul Bohmwatchers in the next event loop iteration. 3860e3a38431SPaul Bohm 3861e3a38431SPaul Bohm=head2 THREADS, COROUTINES, CONTINUATIONS, QUEUES... INSTEAD OF CALLBACKS 3862e3a38431SPaul Bohm 3863e3a38431SPaul BohmWhile the overhead of a callback that e.g. schedules a thread is small, it 3864e3a38431SPaul Bohmis still an overhead. If you embed libev, and your main usage is with some 3865e3a38431SPaul Bohmkind of threads or coroutines, you might want to customise libev so that 3866e3a38431SPaul Bohmdoesn't need callbacks anymore. 3867e3a38431SPaul Bohm 3868e3a38431SPaul BohmImagine you have coroutines that you can switch to using a function 3869e3a38431SPaul BohmC<switch_to (coro)>, that libev runs in a coroutine called C<libev_coro> 3870e3a38431SPaul Bohmand that due to some magic, the currently active coroutine is stored in a 3871e3a38431SPaul Bohmglobal called C<current_coro>. Then you can build your own "wait for libev 3872e3a38431SPaul Bohmevent" primitive by changing C<EV_CB_DECLARE> and C<EV_CB_INVOKE> (note 3873e3a38431SPaul Bohmthe differing C<;> conventions): 3874e3a38431SPaul Bohm 3875e3a38431SPaul Bohm #define EV_CB_DECLARE(type) struct my_coro *cb; 3876e3a38431SPaul Bohm #define EV_CB_INVOKE(watcher) switch_to ((watcher)->cb) 3877e3a38431SPaul Bohm 3878e3a38431SPaul BohmThat means instead of having a C callback function, you store the 3879e3a38431SPaul Bohmcoroutine to switch to in each watcher, and instead of having libev call 3880e3a38431SPaul Bohmyour callback, you instead have it switch to that coroutine. 3881e3a38431SPaul Bohm 3882e3a38431SPaul BohmA coroutine might now wait for an event with a function called 3883e3a38431SPaul BohmC<wait_for_event>. (the watcher needs to be started, as always, but it doesn't 3884e3a38431SPaul Bohmmatter when, or whether the watcher is active or not when this function is 3885e3a38431SPaul Bohmcalled): 3886e3a38431SPaul Bohm 3887e3a38431SPaul Bohm void 3888e3a38431SPaul Bohm wait_for_event (ev_watcher *w) 3889e3a38431SPaul Bohm { 3890*93823e6cSPaul Bohm ev_set_cb (w, current_coro); 3891e3a38431SPaul Bohm switch_to (libev_coro); 3892e3a38431SPaul Bohm } 3893e3a38431SPaul Bohm 3894e3a38431SPaul BohmThat basically suspends the coroutine inside C<wait_for_event> and 3895e3a38431SPaul Bohmcontinues the libev coroutine, which, when appropriate, switches back to 3896*93823e6cSPaul Bohmthis or any other coroutine. 3897e3a38431SPaul Bohm 3898e3a38431SPaul BohmYou can do similar tricks if you have, say, threads with an event queue - 3899e3a38431SPaul Bohminstead of storing a coroutine, you store the queue object and instead of 3900e3a38431SPaul Bohmswitching to a coroutine, you push the watcher onto the queue and notify 3901e3a38431SPaul Bohmany waiters. 3902e3a38431SPaul Bohm 3903*93823e6cSPaul BohmTo embed libev, see L</EMBEDDING>, but in short, it's easiest to create two 3904e3a38431SPaul Bohmfiles, F<my_ev.h> and F<my_ev.c> that include the respective libev files: 3905e3a38431SPaul Bohm 3906e3a38431SPaul Bohm // my_ev.h 3907e3a38431SPaul Bohm #define EV_CB_DECLARE(type) struct my_coro *cb; 3908*93823e6cSPaul Bohm #define EV_CB_INVOKE(watcher) switch_to ((watcher)->cb) 3909e3a38431SPaul Bohm #include "../libev/ev.h" 3910e3a38431SPaul Bohm 3911e3a38431SPaul Bohm // my_ev.c 3912e3a38431SPaul Bohm #define EV_H "my_ev.h" 3913e3a38431SPaul Bohm #include "../libev/ev.c" 3914e3a38431SPaul Bohm 3915e3a38431SPaul BohmAnd then use F<my_ev.h> when you would normally use F<ev.h>, and compile 3916e3a38431SPaul BohmF<my_ev.c> into your project. When properly specifying include paths, you 3917e3a38431SPaul Bohmcan even use F<ev.h> as header file name directly. 3918e3a38431SPaul Bohm 3919e3a38431SPaul Bohm 3920e3a38431SPaul Bohm=head1 LIBEVENT EMULATION 3921e3a38431SPaul Bohm 3922e3a38431SPaul BohmLibev offers a compatibility emulation layer for libevent. It cannot 3923e3a38431SPaul Bohmemulate the internals of libevent, so here are some usage hints: 3924e3a38431SPaul Bohm 3925e3a38431SPaul Bohm=over 4 3926e3a38431SPaul Bohm 3927e3a38431SPaul Bohm=item * Only the libevent-1.4.1-beta API is being emulated. 3928e3a38431SPaul Bohm 3929e3a38431SPaul BohmThis was the newest libevent version available when libev was implemented, 3930e3a38431SPaul Bohmand is still mostly unchanged in 2010. 3931e3a38431SPaul Bohm 3932e3a38431SPaul Bohm=item * Use it by including <event.h>, as usual. 3933e3a38431SPaul Bohm 3934e3a38431SPaul Bohm=item * The following members are fully supported: ev_base, ev_callback, 3935e3a38431SPaul Bohmev_arg, ev_fd, ev_res, ev_events. 3936e3a38431SPaul Bohm 3937e3a38431SPaul Bohm=item * Avoid using ev_flags and the EVLIST_*-macros, while it is 3938e3a38431SPaul Bohmmaintained by libev, it does not work exactly the same way as in libevent (consider 3939e3a38431SPaul Bohmit a private API). 3940e3a38431SPaul Bohm 3941e3a38431SPaul Bohm=item * Priorities are not currently supported. Initialising priorities 3942e3a38431SPaul Bohmwill fail and all watchers will have the same priority, even though there 3943e3a38431SPaul Bohmis an ev_pri field. 3944e3a38431SPaul Bohm 3945e3a38431SPaul Bohm=item * In libevent, the last base created gets the signals, in libev, the 3946e3a38431SPaul Bohmbase that registered the signal gets the signals. 3947e3a38431SPaul Bohm 3948e3a38431SPaul Bohm=item * Other members are not supported. 3949e3a38431SPaul Bohm 3950e3a38431SPaul Bohm=item * The libev emulation is I<not> ABI compatible to libevent, you need 3951e3a38431SPaul Bohmto use the libev header file and library. 3952e3a38431SPaul Bohm 3953e3a38431SPaul Bohm=back 3954e3a38431SPaul Bohm 3955e3a38431SPaul Bohm=head1 C++ SUPPORT 3956e3a38431SPaul Bohm 3957*93823e6cSPaul Bohm=head2 C API 3958*93823e6cSPaul Bohm 3959*93823e6cSPaul BohmThe normal C API should work fine when used from C++: both ev.h and the 3960*93823e6cSPaul Bohmlibev sources can be compiled as C++. Therefore, code that uses the C API 3961*93823e6cSPaul Bohmwill work fine. 3962*93823e6cSPaul Bohm 3963*93823e6cSPaul BohmProper exception specifications might have to be added to callbacks passed 3964*93823e6cSPaul Bohmto libev: exceptions may be thrown only from watcher callbacks, all 3965*93823e6cSPaul Bohmother callbacks (allocator, syserr, loop acquire/release and periodic 3966*93823e6cSPaul Bohmreschedule callbacks) must not throw exceptions, and might need a C<throw 3967*93823e6cSPaul Bohm()> specification. If you have code that needs to be compiled as both C 3968*93823e6cSPaul Bohmand C++ you can use the C<EV_THROW> macro for this: 3969*93823e6cSPaul Bohm 3970*93823e6cSPaul Bohm static void 3971*93823e6cSPaul Bohm fatal_error (const char *msg) EV_THROW 3972*93823e6cSPaul Bohm { 3973*93823e6cSPaul Bohm perror (msg); 3974*93823e6cSPaul Bohm abort (); 3975*93823e6cSPaul Bohm } 3976*93823e6cSPaul Bohm 3977*93823e6cSPaul Bohm ... 3978*93823e6cSPaul Bohm ev_set_syserr_cb (fatal_error); 3979*93823e6cSPaul Bohm 3980*93823e6cSPaul BohmThe only API functions that can currently throw exceptions are C<ev_run>, 3981*93823e6cSPaul BohmC<ev_invoke>, C<ev_invoke_pending> and C<ev_loop_destroy> (the latter 3982*93823e6cSPaul Bohmbecause it runs cleanup watchers). 3983*93823e6cSPaul Bohm 3984*93823e6cSPaul BohmThrowing exceptions in watcher callbacks is only supported if libev itself 3985*93823e6cSPaul Bohmis compiled with a C++ compiler or your C and C++ environments allow 3986*93823e6cSPaul Bohmthrowing exceptions through C libraries (most do). 3987*93823e6cSPaul Bohm 3988*93823e6cSPaul Bohm=head2 C++ API 3989*93823e6cSPaul Bohm 3990e3a38431SPaul BohmLibev comes with some simplistic wrapper classes for C++ that mainly allow 3991e3a38431SPaul Bohmyou to use some convenience methods to start/stop watchers and also change 3992e3a38431SPaul Bohmthe callback model to a model using method callbacks on objects. 3993e3a38431SPaul Bohm 3994e3a38431SPaul BohmTo use it, 3995e3a38431SPaul Bohm 3996e3a38431SPaul Bohm #include <ev++.h> 3997e3a38431SPaul Bohm 3998e3a38431SPaul BohmThis automatically includes F<ev.h> and puts all of its definitions (many 3999e3a38431SPaul Bohmof them macros) into the global namespace. All C++ specific things are 4000e3a38431SPaul Bohmput into the C<ev> namespace. It should support all the same embedding 4001e3a38431SPaul Bohmoptions as F<ev.h>, most notably C<EV_MULTIPLICITY>. 4002e3a38431SPaul Bohm 4003e3a38431SPaul BohmCare has been taken to keep the overhead low. The only data member the C++ 4004e3a38431SPaul Bohmclasses add (compared to plain C-style watchers) is the event loop pointer 4005e3a38431SPaul Bohmthat the watcher is associated with (or no additional members at all if 4006e3a38431SPaul Bohmyou disable C<EV_MULTIPLICITY> when embedding libev). 4007e3a38431SPaul Bohm 4008e3a38431SPaul BohmCurrently, functions, static and non-static member functions and classes 4009e3a38431SPaul Bohmwith C<operator ()> can be used as callbacks. Other types should be easy 4010e3a38431SPaul Bohmto add as long as they only need one additional pointer for context. If 4011e3a38431SPaul Bohmyou need support for other types of functors please contact the author 4012e3a38431SPaul Bohm(preferably after implementing it). 4013e3a38431SPaul Bohm 4014*93823e6cSPaul BohmFor all this to work, your C++ compiler either has to use the same calling 4015*93823e6cSPaul Bohmconventions as your C compiler (for static member functions), or you have 4016*93823e6cSPaul Bohmto embed libev and compile libev itself as C++. 4017*93823e6cSPaul Bohm 4018e3a38431SPaul BohmHere is a list of things available in the C<ev> namespace: 4019e3a38431SPaul Bohm 4020e3a38431SPaul Bohm=over 4 4021e3a38431SPaul Bohm 4022e3a38431SPaul Bohm=item C<ev::READ>, C<ev::WRITE> etc. 4023e3a38431SPaul Bohm 4024e3a38431SPaul BohmThese are just enum values with the same values as the C<EV_READ> etc. 4025e3a38431SPaul Bohmmacros from F<ev.h>. 4026e3a38431SPaul Bohm 4027e3a38431SPaul Bohm=item C<ev::tstamp>, C<ev::now> 4028e3a38431SPaul Bohm 4029e3a38431SPaul BohmAliases to the same types/functions as with the C<ev_> prefix. 4030e3a38431SPaul Bohm 4031e3a38431SPaul Bohm=item C<ev::io>, C<ev::timer>, C<ev::periodic>, C<ev::idle>, C<ev::sig> etc. 4032e3a38431SPaul Bohm 4033e3a38431SPaul BohmFor each C<ev_TYPE> watcher in F<ev.h> there is a corresponding class of 4034e3a38431SPaul Bohmthe same name in the C<ev> namespace, with the exception of C<ev_signal> 4035e3a38431SPaul Bohmwhich is called C<ev::sig> to avoid clashes with the C<signal> macro 4036*93823e6cSPaul Bohmdefined by many implementations. 4037e3a38431SPaul Bohm 4038e3a38431SPaul BohmAll of those classes have these methods: 4039e3a38431SPaul Bohm 4040e3a38431SPaul Bohm=over 4 4041e3a38431SPaul Bohm 4042e3a38431SPaul Bohm=item ev::TYPE::TYPE () 4043e3a38431SPaul Bohm 4044e3a38431SPaul Bohm=item ev::TYPE::TYPE (loop) 4045e3a38431SPaul Bohm 4046e3a38431SPaul Bohm=item ev::TYPE::~TYPE 4047e3a38431SPaul Bohm 4048e3a38431SPaul BohmThe constructor (optionally) takes an event loop to associate the watcher 4049e3a38431SPaul Bohmwith. If it is omitted, it will use C<EV_DEFAULT>. 4050e3a38431SPaul Bohm 4051e3a38431SPaul BohmThe constructor calls C<ev_init> for you, which means you have to call the 4052e3a38431SPaul BohmC<set> method before starting it. 4053e3a38431SPaul Bohm 4054e3a38431SPaul BohmIt will not set a callback, however: You have to call the templated C<set> 4055e3a38431SPaul Bohmmethod to set a callback before you can start the watcher. 4056e3a38431SPaul Bohm 4057e3a38431SPaul Bohm(The reason why you have to use a method is a limitation in C++ which does 4058e3a38431SPaul Bohmnot allow explicit template arguments for constructors). 4059e3a38431SPaul Bohm 4060e3a38431SPaul BohmThe destructor automatically stops the watcher if it is active. 4061e3a38431SPaul Bohm 4062e3a38431SPaul Bohm=item w->set<class, &class::method> (object *) 4063e3a38431SPaul Bohm 4064e3a38431SPaul BohmThis method sets the callback method to call. The method has to have a 4065e3a38431SPaul Bohmsignature of C<void (*)(ev_TYPE &, int)>, it receives the watcher as 4066e3a38431SPaul Bohmfirst argument and the C<revents> as second. The object must be given as 4067e3a38431SPaul Bohmparameter and is stored in the C<data> member of the watcher. 4068e3a38431SPaul Bohm 4069e3a38431SPaul BohmThis method synthesizes efficient thunking code to call your method from 4070e3a38431SPaul Bohmthe C callback that libev requires. If your compiler can inline your 4071e3a38431SPaul Bohmcallback (i.e. it is visible to it at the place of the C<set> call and 4072e3a38431SPaul Bohmyour compiler is good :), then the method will be fully inlined into the 4073e3a38431SPaul Bohmthunking function, making it as fast as a direct C callback. 4074e3a38431SPaul Bohm 4075e3a38431SPaul BohmExample: simple class declaration and watcher initialisation 4076e3a38431SPaul Bohm 4077e3a38431SPaul Bohm struct myclass 4078e3a38431SPaul Bohm { 4079e3a38431SPaul Bohm void io_cb (ev::io &w, int revents) { } 4080e3a38431SPaul Bohm } 4081e3a38431SPaul Bohm 4082e3a38431SPaul Bohm myclass obj; 4083e3a38431SPaul Bohm ev::io iow; 4084e3a38431SPaul Bohm iow.set <myclass, &myclass::io_cb> (&obj); 4085e3a38431SPaul Bohm 4086e3a38431SPaul Bohm=item w->set (object *) 4087e3a38431SPaul Bohm 4088e3a38431SPaul BohmThis is a variation of a method callback - leaving out the method to call 4089e3a38431SPaul Bohmwill default the method to C<operator ()>, which makes it possible to use 4090e3a38431SPaul Bohmfunctor objects without having to manually specify the C<operator ()> all 4091e3a38431SPaul Bohmthe time. Incidentally, you can then also leave out the template argument 4092e3a38431SPaul Bohmlist. 4093e3a38431SPaul Bohm 4094e3a38431SPaul BohmThe C<operator ()> method prototype must be C<void operator ()(watcher &w, 4095e3a38431SPaul Bohmint revents)>. 4096e3a38431SPaul Bohm 4097e3a38431SPaul BohmSee the method-C<set> above for more details. 4098e3a38431SPaul Bohm 4099e3a38431SPaul BohmExample: use a functor object as callback. 4100e3a38431SPaul Bohm 4101e3a38431SPaul Bohm struct myfunctor 4102e3a38431SPaul Bohm { 4103e3a38431SPaul Bohm void operator() (ev::io &w, int revents) 4104e3a38431SPaul Bohm { 4105e3a38431SPaul Bohm ... 4106e3a38431SPaul Bohm } 4107e3a38431SPaul Bohm } 4108e3a38431SPaul Bohm 4109e3a38431SPaul Bohm myfunctor f; 4110e3a38431SPaul Bohm 4111e3a38431SPaul Bohm ev::io w; 4112e3a38431SPaul Bohm w.set (&f); 4113e3a38431SPaul Bohm 4114e3a38431SPaul Bohm=item w->set<function> (void *data = 0) 4115e3a38431SPaul Bohm 4116e3a38431SPaul BohmAlso sets a callback, but uses a static method or plain function as 4117e3a38431SPaul Bohmcallback. The optional C<data> argument will be stored in the watcher's 4118e3a38431SPaul BohmC<data> member and is free for you to use. 4119e3a38431SPaul Bohm 4120e3a38431SPaul BohmThe prototype of the C<function> must be C<void (*)(ev::TYPE &w, int)>. 4121e3a38431SPaul Bohm 4122e3a38431SPaul BohmSee the method-C<set> above for more details. 4123e3a38431SPaul Bohm 4124e3a38431SPaul BohmExample: Use a plain function as callback. 4125e3a38431SPaul Bohm 4126e3a38431SPaul Bohm static void io_cb (ev::io &w, int revents) { } 4127e3a38431SPaul Bohm iow.set <io_cb> (); 4128e3a38431SPaul Bohm 4129e3a38431SPaul Bohm=item w->set (loop) 4130e3a38431SPaul Bohm 4131e3a38431SPaul BohmAssociates a different C<struct ev_loop> with this watcher. You can only 4132e3a38431SPaul Bohmdo this when the watcher is inactive (and not pending either). 4133e3a38431SPaul Bohm 4134e3a38431SPaul Bohm=item w->set ([arguments]) 4135e3a38431SPaul Bohm 4136*93823e6cSPaul BohmBasically the same as C<ev_TYPE_set> (except for C<ev::embed> watchers>), 4137*93823e6cSPaul Bohmwith the same arguments. Either this method or a suitable start method 4138*93823e6cSPaul Bohmmust be called at least once. Unlike the C counterpart, an active watcher 4139*93823e6cSPaul Bohmgets automatically stopped and restarted when reconfiguring it with this 4140*93823e6cSPaul Bohmmethod. 4141*93823e6cSPaul Bohm 4142*93823e6cSPaul BohmFor C<ev::embed> watchers this method is called C<set_embed>, to avoid 4143*93823e6cSPaul Bohmclashing with the C<set (loop)> method. 4144e3a38431SPaul Bohm 4145e3a38431SPaul Bohm=item w->start () 4146e3a38431SPaul Bohm 4147e3a38431SPaul BohmStarts the watcher. Note that there is no C<loop> argument, as the 4148e3a38431SPaul Bohmconstructor already stores the event loop. 4149e3a38431SPaul Bohm 4150e3a38431SPaul Bohm=item w->start ([arguments]) 4151e3a38431SPaul Bohm 4152e3a38431SPaul BohmInstead of calling C<set> and C<start> methods separately, it is often 4153e3a38431SPaul Bohmconvenient to wrap them in one call. Uses the same type of arguments as 4154e3a38431SPaul Bohmthe configure C<set> method of the watcher. 4155e3a38431SPaul Bohm 4156e3a38431SPaul Bohm=item w->stop () 4157e3a38431SPaul Bohm 4158e3a38431SPaul BohmStops the watcher if it is active. Again, no C<loop> argument. 4159e3a38431SPaul Bohm 4160e3a38431SPaul Bohm=item w->again () (C<ev::timer>, C<ev::periodic> only) 4161e3a38431SPaul Bohm 4162e3a38431SPaul BohmFor C<ev::timer> and C<ev::periodic>, this invokes the corresponding 4163e3a38431SPaul BohmC<ev_TYPE_again> function. 4164e3a38431SPaul Bohm 4165e3a38431SPaul Bohm=item w->sweep () (C<ev::embed> only) 4166e3a38431SPaul Bohm 4167e3a38431SPaul BohmInvokes C<ev_embed_sweep>. 4168e3a38431SPaul Bohm 4169e3a38431SPaul Bohm=item w->update () (C<ev::stat> only) 4170e3a38431SPaul Bohm 4171e3a38431SPaul BohmInvokes C<ev_stat_stat>. 4172e3a38431SPaul Bohm 4173e3a38431SPaul Bohm=back 4174e3a38431SPaul Bohm 4175e3a38431SPaul Bohm=back 4176e3a38431SPaul Bohm 4177e3a38431SPaul BohmExample: Define a class with two I/O and idle watchers, start the I/O 4178e3a38431SPaul Bohmwatchers in the constructor. 4179e3a38431SPaul Bohm 4180e3a38431SPaul Bohm class myclass 4181e3a38431SPaul Bohm { 4182e3a38431SPaul Bohm ev::io io ; void io_cb (ev::io &w, int revents); 4183*93823e6cSPaul Bohm ev::io io2 ; void io2_cb (ev::io &w, int revents); 4184e3a38431SPaul Bohm ev::idle idle; void idle_cb (ev::idle &w, int revents); 4185e3a38431SPaul Bohm 4186e3a38431SPaul Bohm myclass (int fd) 4187e3a38431SPaul Bohm { 4188e3a38431SPaul Bohm io .set <myclass, &myclass::io_cb > (this); 4189e3a38431SPaul Bohm io2 .set <myclass, &myclass::io2_cb > (this); 4190e3a38431SPaul Bohm idle.set <myclass, &myclass::idle_cb> (this); 4191e3a38431SPaul Bohm 4192e3a38431SPaul Bohm io.set (fd, ev::WRITE); // configure the watcher 4193e3a38431SPaul Bohm io.start (); // start it whenever convenient 4194e3a38431SPaul Bohm 4195e3a38431SPaul Bohm io2.start (fd, ev::READ); // set + start in one call 4196e3a38431SPaul Bohm } 4197e3a38431SPaul Bohm }; 4198e3a38431SPaul Bohm 4199e3a38431SPaul Bohm 4200e3a38431SPaul Bohm=head1 OTHER LANGUAGE BINDINGS 4201e3a38431SPaul Bohm 4202e3a38431SPaul BohmLibev does not offer other language bindings itself, but bindings for a 4203e3a38431SPaul Bohmnumber of languages exist in the form of third-party packages. If you know 4204e3a38431SPaul Bohmany interesting language binding in addition to the ones listed here, drop 4205e3a38431SPaul Bohmme a note. 4206e3a38431SPaul Bohm 4207e3a38431SPaul Bohm=over 4 4208e3a38431SPaul Bohm 4209e3a38431SPaul Bohm=item Perl 4210e3a38431SPaul Bohm 4211e3a38431SPaul BohmThe EV module implements the full libev API and is actually used to test 4212e3a38431SPaul Bohmlibev. EV is developed together with libev. Apart from the EV core module, 4213e3a38431SPaul Bohmthere are additional modules that implement libev-compatible interfaces 4214e3a38431SPaul Bohmto C<libadns> (C<EV::ADNS>, but C<AnyEvent::DNS> is preferred nowadays), 4215e3a38431SPaul BohmC<Net::SNMP> (C<Net::SNMP::EV>) and the C<libglib> event core (C<Glib::EV> 4216e3a38431SPaul Bohmand C<EV::Glib>). 4217e3a38431SPaul Bohm 4218e3a38431SPaul BohmIt can be found and installed via CPAN, its homepage is at 4219e3a38431SPaul BohmL<http://software.schmorp.de/pkg/EV>. 4220e3a38431SPaul Bohm 4221e3a38431SPaul Bohm=item Python 4222e3a38431SPaul Bohm 4223e3a38431SPaul BohmPython bindings can be found at L<http://code.google.com/p/pyev/>. It 4224e3a38431SPaul Bohmseems to be quite complete and well-documented. 4225e3a38431SPaul Bohm 4226e3a38431SPaul Bohm=item Ruby 4227e3a38431SPaul Bohm 4228e3a38431SPaul BohmTony Arcieri has written a ruby extension that offers access to a subset 4229e3a38431SPaul Bohmof the libev API and adds file handle abstractions, asynchronous DNS and 4230e3a38431SPaul Bohmmore on top of it. It can be found via gem servers. Its homepage is at 4231e3a38431SPaul BohmL<http://rev.rubyforge.org/>. 4232e3a38431SPaul Bohm 4233e3a38431SPaul BohmRoger Pack reports that using the link order C<-lws2_32 -lmsvcrt-ruby-190> 4234e3a38431SPaul Bohmmakes rev work even on mingw. 4235e3a38431SPaul Bohm 4236e3a38431SPaul Bohm=item Haskell 4237e3a38431SPaul Bohm 4238e3a38431SPaul BohmA haskell binding to libev is available at 4239e3a38431SPaul BohmL<http://hackage.haskell.org/cgi-bin/hackage-scripts/package/hlibev>. 4240e3a38431SPaul Bohm 4241e3a38431SPaul Bohm=item D 4242e3a38431SPaul Bohm 4243e3a38431SPaul BohmLeandro Lucarella has written a D language binding (F<ev.d>) for libev, to 4244*93823e6cSPaul Bohmbe found at L<http://www.llucax.com.ar/proj/ev.d/index.html>. 4245e3a38431SPaul Bohm 4246e3a38431SPaul Bohm=item Ocaml 4247e3a38431SPaul Bohm 4248e3a38431SPaul BohmErkki Seppala has written Ocaml bindings for libev, to be found at 4249e3a38431SPaul BohmL<http://modeemi.cs.tut.fi/~flux/software/ocaml-ev/>. 4250e3a38431SPaul Bohm 4251e3a38431SPaul Bohm=item Lua 4252e3a38431SPaul Bohm 4253e3a38431SPaul BohmBrian Maher has written a partial interface to libev for lua (at the 4254e3a38431SPaul Bohmtime of this writing, only C<ev_io> and C<ev_timer>), to be found at 4255e3a38431SPaul BohmL<http://github.com/brimworks/lua-ev>. 4256e3a38431SPaul Bohm 4257*93823e6cSPaul Bohm=item Javascript 4258*93823e6cSPaul Bohm 4259*93823e6cSPaul BohmNode.js (L<http://nodejs.org>) uses libev as the underlying event library. 4260*93823e6cSPaul Bohm 4261*93823e6cSPaul Bohm=item Others 4262*93823e6cSPaul Bohm 4263*93823e6cSPaul BohmThere are others, and I stopped counting. 4264*93823e6cSPaul Bohm 4265e3a38431SPaul Bohm=back 4266e3a38431SPaul Bohm 4267e3a38431SPaul Bohm 4268e3a38431SPaul Bohm=head1 MACRO MAGIC 4269e3a38431SPaul Bohm 4270e3a38431SPaul BohmLibev can be compiled with a variety of options, the most fundamental 4271e3a38431SPaul Bohmof which is C<EV_MULTIPLICITY>. This option determines whether (most) 4272e3a38431SPaul Bohmfunctions and callbacks have an initial C<struct ev_loop *> argument. 4273e3a38431SPaul Bohm 4274e3a38431SPaul BohmTo make it easier to write programs that cope with either variant, the 4275e3a38431SPaul Bohmfollowing macros are defined: 4276e3a38431SPaul Bohm 4277e3a38431SPaul Bohm=over 4 4278e3a38431SPaul Bohm 4279e3a38431SPaul Bohm=item C<EV_A>, C<EV_A_> 4280e3a38431SPaul Bohm 4281e3a38431SPaul BohmThis provides the loop I<argument> for functions, if one is required ("ev 4282e3a38431SPaul Bohmloop argument"). The C<EV_A> form is used when this is the sole argument, 4283e3a38431SPaul BohmC<EV_A_> is used when other arguments are following. Example: 4284e3a38431SPaul Bohm 4285e3a38431SPaul Bohm ev_unref (EV_A); 4286e3a38431SPaul Bohm ev_timer_add (EV_A_ watcher); 4287e3a38431SPaul Bohm ev_run (EV_A_ 0); 4288e3a38431SPaul Bohm 4289e3a38431SPaul BohmIt assumes the variable C<loop> of type C<struct ev_loop *> is in scope, 4290e3a38431SPaul Bohmwhich is often provided by the following macro. 4291e3a38431SPaul Bohm 4292e3a38431SPaul Bohm=item C<EV_P>, C<EV_P_> 4293e3a38431SPaul Bohm 4294e3a38431SPaul BohmThis provides the loop I<parameter> for functions, if one is required ("ev 4295e3a38431SPaul Bohmloop parameter"). The C<EV_P> form is used when this is the sole parameter, 4296e3a38431SPaul BohmC<EV_P_> is used when other parameters are following. Example: 4297e3a38431SPaul Bohm 4298e3a38431SPaul Bohm // this is how ev_unref is being declared 4299e3a38431SPaul Bohm static void ev_unref (EV_P); 4300e3a38431SPaul Bohm 4301e3a38431SPaul Bohm // this is how you can declare your typical callback 4302e3a38431SPaul Bohm static void cb (EV_P_ ev_timer *w, int revents) 4303e3a38431SPaul Bohm 4304e3a38431SPaul BohmIt declares a parameter C<loop> of type C<struct ev_loop *>, quite 4305e3a38431SPaul Bohmsuitable for use with C<EV_A>. 4306e3a38431SPaul Bohm 4307e3a38431SPaul Bohm=item C<EV_DEFAULT>, C<EV_DEFAULT_> 4308e3a38431SPaul Bohm 4309e3a38431SPaul BohmSimilar to the other two macros, this gives you the value of the default 4310*93823e6cSPaul Bohmloop, if multiple loops are supported ("ev loop default"). The default loop 4311*93823e6cSPaul Bohmwill be initialised if it isn't already initialised. 4312*93823e6cSPaul Bohm 4313*93823e6cSPaul BohmFor non-multiplicity builds, these macros do nothing, so you always have 4314*93823e6cSPaul Bohmto initialise the loop somewhere. 4315e3a38431SPaul Bohm 4316e3a38431SPaul Bohm=item C<EV_DEFAULT_UC>, C<EV_DEFAULT_UC_> 4317e3a38431SPaul Bohm 4318e3a38431SPaul BohmUsage identical to C<EV_DEFAULT> and C<EV_DEFAULT_>, but requires that the 4319e3a38431SPaul Bohmdefault loop has been initialised (C<UC> == unchecked). Their behaviour 4320e3a38431SPaul Bohmis undefined when the default loop has not been initialised by a previous 4321e3a38431SPaul Bohmexecution of C<EV_DEFAULT>, C<EV_DEFAULT_> or C<ev_default_init (...)>. 4322e3a38431SPaul Bohm 4323e3a38431SPaul BohmIt is often prudent to use C<EV_DEFAULT> when initialising the first 4324e3a38431SPaul Bohmwatcher in a function but use C<EV_DEFAULT_UC> afterwards. 4325e3a38431SPaul Bohm 4326e3a38431SPaul Bohm=back 4327e3a38431SPaul Bohm 4328e3a38431SPaul BohmExample: Declare and initialise a check watcher, utilising the above 4329e3a38431SPaul Bohmmacros so it will work regardless of whether multiple loops are supported 4330e3a38431SPaul Bohmor not. 4331e3a38431SPaul Bohm 4332e3a38431SPaul Bohm static void 4333e3a38431SPaul Bohm check_cb (EV_P_ ev_timer *w, int revents) 4334e3a38431SPaul Bohm { 4335e3a38431SPaul Bohm ev_check_stop (EV_A_ w); 4336e3a38431SPaul Bohm } 4337e3a38431SPaul Bohm 4338e3a38431SPaul Bohm ev_check check; 4339e3a38431SPaul Bohm ev_check_init (&check, check_cb); 4340e3a38431SPaul Bohm ev_check_start (EV_DEFAULT_ &check); 4341e3a38431SPaul Bohm ev_run (EV_DEFAULT_ 0); 4342e3a38431SPaul Bohm 4343e3a38431SPaul Bohm=head1 EMBEDDING 4344e3a38431SPaul Bohm 4345e3a38431SPaul BohmLibev can (and often is) directly embedded into host 4346e3a38431SPaul Bohmapplications. Examples of applications that embed it include the Deliantra 4347e3a38431SPaul BohmGame Server, the EV perl module, the GNU Virtual Private Ethernet (gvpe) 4348e3a38431SPaul Bohmand rxvt-unicode. 4349e3a38431SPaul Bohm 4350e3a38431SPaul BohmThe goal is to enable you to just copy the necessary files into your 4351e3a38431SPaul Bohmsource directory without having to change even a single line in them, so 4352e3a38431SPaul Bohmyou can easily upgrade by simply copying (or having a checked-out copy of 4353e3a38431SPaul Bohmlibev somewhere in your source tree). 4354e3a38431SPaul Bohm 4355e3a38431SPaul Bohm=head2 FILESETS 4356e3a38431SPaul Bohm 4357e3a38431SPaul BohmDepending on what features you need you need to include one or more sets of files 4358e3a38431SPaul Bohmin your application. 4359e3a38431SPaul Bohm 4360e3a38431SPaul Bohm=head3 CORE EVENT LOOP 4361e3a38431SPaul Bohm 4362e3a38431SPaul BohmTo include only the libev core (all the C<ev_*> functions), with manual 4363e3a38431SPaul Bohmconfiguration (no autoconf): 4364e3a38431SPaul Bohm 4365e3a38431SPaul Bohm #define EV_STANDALONE 1 4366e3a38431SPaul Bohm #include "ev.c" 4367e3a38431SPaul Bohm 4368e3a38431SPaul BohmThis will automatically include F<ev.h>, too, and should be done in a 4369e3a38431SPaul Bohmsingle C source file only to provide the function implementations. To use 4370e3a38431SPaul Bohmit, do the same for F<ev.h> in all files wishing to use this API (best 4371e3a38431SPaul Bohmdone by writing a wrapper around F<ev.h> that you can include instead and 4372e3a38431SPaul Bohmwhere you can put other configuration options): 4373e3a38431SPaul Bohm 4374e3a38431SPaul Bohm #define EV_STANDALONE 1 4375e3a38431SPaul Bohm #include "ev.h" 4376e3a38431SPaul Bohm 4377e3a38431SPaul BohmBoth header files and implementation files can be compiled with a C++ 4378e3a38431SPaul Bohmcompiler (at least, that's a stated goal, and breakage will be treated 4379e3a38431SPaul Bohmas a bug). 4380e3a38431SPaul Bohm 4381e3a38431SPaul BohmYou need the following files in your source tree, or in a directory 4382e3a38431SPaul Bohmin your include path (e.g. in libev/ when using -Ilibev): 4383e3a38431SPaul Bohm 4384e3a38431SPaul Bohm ev.h 4385e3a38431SPaul Bohm ev.c 4386e3a38431SPaul Bohm ev_vars.h 4387e3a38431SPaul Bohm ev_wrap.h 4388e3a38431SPaul Bohm 4389e3a38431SPaul Bohm ev_win32.c required on win32 platforms only 4390e3a38431SPaul Bohm 4391e3a38431SPaul Bohm ev_select.c only when select backend is enabled (which is enabled by default) 4392e3a38431SPaul Bohm ev_poll.c only when poll backend is enabled (disabled by default) 4393e3a38431SPaul Bohm ev_epoll.c only when the epoll backend is enabled (disabled by default) 4394e3a38431SPaul Bohm ev_kqueue.c only when the kqueue backend is enabled (disabled by default) 4395e3a38431SPaul Bohm ev_port.c only when the solaris port backend is enabled (disabled by default) 4396e3a38431SPaul Bohm 4397e3a38431SPaul BohmF<ev.c> includes the backend files directly when enabled, so you only need 4398e3a38431SPaul Bohmto compile this single file. 4399e3a38431SPaul Bohm 4400e3a38431SPaul Bohm=head3 LIBEVENT COMPATIBILITY API 4401e3a38431SPaul Bohm 4402e3a38431SPaul BohmTo include the libevent compatibility API, also include: 4403e3a38431SPaul Bohm 4404e3a38431SPaul Bohm #include "event.c" 4405e3a38431SPaul Bohm 4406e3a38431SPaul Bohmin the file including F<ev.c>, and: 4407e3a38431SPaul Bohm 4408e3a38431SPaul Bohm #include "event.h" 4409e3a38431SPaul Bohm 4410e3a38431SPaul Bohmin the files that want to use the libevent API. This also includes F<ev.h>. 4411e3a38431SPaul Bohm 4412e3a38431SPaul BohmYou need the following additional files for this: 4413e3a38431SPaul Bohm 4414e3a38431SPaul Bohm event.h 4415e3a38431SPaul Bohm event.c 4416e3a38431SPaul Bohm 4417e3a38431SPaul Bohm=head3 AUTOCONF SUPPORT 4418e3a38431SPaul Bohm 4419e3a38431SPaul BohmInstead of using C<EV_STANDALONE=1> and providing your configuration in 4420e3a38431SPaul Bohmwhatever way you want, you can also C<m4_include([libev.m4])> in your 4421e3a38431SPaul BohmF<configure.ac> and leave C<EV_STANDALONE> undefined. F<ev.c> will then 4422e3a38431SPaul Bohminclude F<config.h> and configure itself accordingly. 4423e3a38431SPaul Bohm 4424e3a38431SPaul BohmFor this of course you need the m4 file: 4425e3a38431SPaul Bohm 4426e3a38431SPaul Bohm libev.m4 4427e3a38431SPaul Bohm 4428e3a38431SPaul Bohm=head2 PREPROCESSOR SYMBOLS/MACROS 4429e3a38431SPaul Bohm 4430e3a38431SPaul BohmLibev can be configured via a variety of preprocessor symbols you have to 4431e3a38431SPaul Bohmdefine before including (or compiling) any of its files. The default in 4432e3a38431SPaul Bohmthe absence of autoconf is documented for every option. 4433e3a38431SPaul Bohm 4434e3a38431SPaul BohmSymbols marked with "(h)" do not change the ABI, and can have different 4435e3a38431SPaul Bohmvalues when compiling libev vs. including F<ev.h>, so it is permissible 4436e3a38431SPaul Bohmto redefine them before including F<ev.h> without breaking compatibility 4437e3a38431SPaul Bohmto a compiled library. All other symbols change the ABI, which means all 4438e3a38431SPaul Bohmusers of libev and the libev code itself must be compiled with compatible 4439e3a38431SPaul Bohmsettings. 4440e3a38431SPaul Bohm 4441e3a38431SPaul Bohm=over 4 4442e3a38431SPaul Bohm 4443e3a38431SPaul Bohm=item EV_COMPAT3 (h) 4444e3a38431SPaul Bohm 4445e3a38431SPaul BohmBackwards compatibility is a major concern for libev. This is why this 4446e3a38431SPaul Bohmrelease of libev comes with wrappers for the functions and symbols that 4447e3a38431SPaul Bohmhave been renamed between libev version 3 and 4. 4448e3a38431SPaul Bohm 4449e3a38431SPaul BohmYou can disable these wrappers (to test compatibility with future 4450e3a38431SPaul Bohmversions) by defining C<EV_COMPAT3> to C<0> when compiling your 4451e3a38431SPaul Bohmsources. This has the additional advantage that you can drop the C<struct> 4452e3a38431SPaul Bohmfrom C<struct ev_loop> declarations, as libev will provide an C<ev_loop> 4453e3a38431SPaul Bohmtypedef in that case. 4454e3a38431SPaul Bohm 4455e3a38431SPaul BohmIn some future version, the default for C<EV_COMPAT3> will become C<0>, 4456e3a38431SPaul Bohmand in some even more future version the compatibility code will be 4457e3a38431SPaul Bohmremoved completely. 4458e3a38431SPaul Bohm 4459e3a38431SPaul Bohm=item EV_STANDALONE (h) 4460e3a38431SPaul Bohm 4461e3a38431SPaul BohmMust always be C<1> if you do not use autoconf configuration, which 4462e3a38431SPaul Bohmkeeps libev from including F<config.h>, and it also defines dummy 4463e3a38431SPaul Bohmimplementations for some libevent functions (such as logging, which is not 4464e3a38431SPaul Bohmsupported). It will also not define any of the structs usually found in 4465e3a38431SPaul BohmF<event.h> that are not directly supported by the libev core alone. 4466e3a38431SPaul Bohm 4467e3a38431SPaul BohmIn standalone mode, libev will still try to automatically deduce the 4468e3a38431SPaul Bohmconfiguration, but has to be more conservative. 4469e3a38431SPaul Bohm 4470*93823e6cSPaul Bohm=item EV_USE_FLOOR 4471*93823e6cSPaul Bohm 4472*93823e6cSPaul BohmIf defined to be C<1>, libev will use the C<floor ()> function for its 4473*93823e6cSPaul Bohmperiodic reschedule calculations, otherwise libev will fall back on a 4474*93823e6cSPaul Bohmportable (slower) implementation. If you enable this, you usually have to 4475*93823e6cSPaul Bohmlink against libm or something equivalent. Enabling this when the C<floor> 4476*93823e6cSPaul Bohmfunction is not available will fail, so the safe default is to not enable 4477*93823e6cSPaul Bohmthis. 4478*93823e6cSPaul Bohm 4479e3a38431SPaul Bohm=item EV_USE_MONOTONIC 4480e3a38431SPaul Bohm 4481e3a38431SPaul BohmIf defined to be C<1>, libev will try to detect the availability of the 4482e3a38431SPaul Bohmmonotonic clock option at both compile time and runtime. Otherwise no 4483e3a38431SPaul Bohmuse of the monotonic clock option will be attempted. If you enable this, 4484e3a38431SPaul Bohmyou usually have to link against librt or something similar. Enabling it 4485e3a38431SPaul Bohmwhen the functionality isn't available is safe, though, although you have 4486e3a38431SPaul Bohmto make sure you link against any libraries where the C<clock_gettime> 4487e3a38431SPaul Bohmfunction is hiding in (often F<-lrt>). See also C<EV_USE_CLOCK_SYSCALL>. 4488e3a38431SPaul Bohm 4489e3a38431SPaul Bohm=item EV_USE_REALTIME 4490e3a38431SPaul Bohm 4491e3a38431SPaul BohmIf defined to be C<1>, libev will try to detect the availability of the 4492e3a38431SPaul Bohmreal-time clock option at compile time (and assume its availability 4493e3a38431SPaul Bohmat runtime if successful). Otherwise no use of the real-time clock 4494e3a38431SPaul Bohmoption will be attempted. This effectively replaces C<gettimeofday> 4495e3a38431SPaul Bohmby C<clock_get (CLOCK_REALTIME, ...)> and will not normally affect 4496e3a38431SPaul Bohmcorrectness. See the note about libraries in the description of 4497e3a38431SPaul BohmC<EV_USE_MONOTONIC>, though. Defaults to the opposite value of 4498e3a38431SPaul BohmC<EV_USE_CLOCK_SYSCALL>. 4499e3a38431SPaul Bohm 4500e3a38431SPaul Bohm=item EV_USE_CLOCK_SYSCALL 4501e3a38431SPaul Bohm 4502e3a38431SPaul BohmIf defined to be C<1>, libev will try to use a direct syscall instead 4503e3a38431SPaul Bohmof calling the system-provided C<clock_gettime> function. This option 4504e3a38431SPaul Bohmexists because on GNU/Linux, C<clock_gettime> is in C<librt>, but C<librt> 4505e3a38431SPaul Bohmunconditionally pulls in C<libpthread>, slowing down single-threaded 4506e3a38431SPaul Bohmprograms needlessly. Using a direct syscall is slightly slower (in 4507e3a38431SPaul Bohmtheory), because no optimised vdso implementation can be used, but avoids 4508e3a38431SPaul Bohmthe pthread dependency. Defaults to C<1> on GNU/Linux with glibc 2.x or 4509e3a38431SPaul Bohmhigher, as it simplifies linking (no need for C<-lrt>). 4510e3a38431SPaul Bohm 4511e3a38431SPaul Bohm=item EV_USE_NANOSLEEP 4512e3a38431SPaul Bohm 4513e3a38431SPaul BohmIf defined to be C<1>, libev will assume that C<nanosleep ()> is available 4514e3a38431SPaul Bohmand will use it for delays. Otherwise it will use C<select ()>. 4515e3a38431SPaul Bohm 4516e3a38431SPaul Bohm=item EV_USE_EVENTFD 4517e3a38431SPaul Bohm 4518e3a38431SPaul BohmIf defined to be C<1>, then libev will assume that C<eventfd ()> is 4519e3a38431SPaul Bohmavailable and will probe for kernel support at runtime. This will improve 4520e3a38431SPaul BohmC<ev_signal> and C<ev_async> performance and reduce resource consumption. 4521e3a38431SPaul BohmIf undefined, it will be enabled if the headers indicate GNU/Linux + Glibc 4522e3a38431SPaul Bohm2.7 or newer, otherwise disabled. 4523e3a38431SPaul Bohm 4524e3a38431SPaul Bohm=item EV_USE_SELECT 4525e3a38431SPaul Bohm 4526e3a38431SPaul BohmIf undefined or defined to be C<1>, libev will compile in support for the 4527e3a38431SPaul BohmC<select>(2) backend. No attempt at auto-detection will be done: if no 4528e3a38431SPaul Bohmother method takes over, select will be it. Otherwise the select backend 4529e3a38431SPaul Bohmwill not be compiled in. 4530e3a38431SPaul Bohm 4531e3a38431SPaul Bohm=item EV_SELECT_USE_FD_SET 4532e3a38431SPaul Bohm 4533e3a38431SPaul BohmIf defined to C<1>, then the select backend will use the system C<fd_set> 4534e3a38431SPaul Bohmstructure. This is useful if libev doesn't compile due to a missing 4535e3a38431SPaul BohmC<NFDBITS> or C<fd_mask> definition or it mis-guesses the bitset layout 4536e3a38431SPaul Bohmon exotic systems. This usually limits the range of file descriptors to 4537e3a38431SPaul Bohmsome low limit such as 1024 or might have other limitations (winsocket 4538e3a38431SPaul Bohmonly allows 64 sockets). The C<FD_SETSIZE> macro, set before compilation, 4539e3a38431SPaul Bohmconfigures the maximum size of the C<fd_set>. 4540e3a38431SPaul Bohm 4541e3a38431SPaul Bohm=item EV_SELECT_IS_WINSOCKET 4542e3a38431SPaul Bohm 4543e3a38431SPaul BohmWhen defined to C<1>, the select backend will assume that 4544e3a38431SPaul Bohmselect/socket/connect etc. don't understand file descriptors but 4545e3a38431SPaul Bohmwants osf handles on win32 (this is the case when the select to 4546e3a38431SPaul Bohmbe used is the winsock select). This means that it will call 4547e3a38431SPaul BohmC<_get_osfhandle> on the fd to convert it to an OS handle. Otherwise, 4548e3a38431SPaul Bohmit is assumed that all these functions actually work on fds, even 4549e3a38431SPaul Bohmon win32. Should not be defined on non-win32 platforms. 4550e3a38431SPaul Bohm 4551e3a38431SPaul Bohm=item EV_FD_TO_WIN32_HANDLE(fd) 4552e3a38431SPaul Bohm 4553e3a38431SPaul BohmIf C<EV_SELECT_IS_WINSOCKET> is enabled, then libev needs a way to map 4554e3a38431SPaul Bohmfile descriptors to socket handles. When not defining this symbol (the 4555e3a38431SPaul Bohmdefault), then libev will call C<_get_osfhandle>, which is usually 4556e3a38431SPaul Bohmcorrect. In some cases, programs use their own file descriptor management, 4557e3a38431SPaul Bohmin which case they can provide this function to map fds to socket handles. 4558e3a38431SPaul Bohm 4559e3a38431SPaul Bohm=item EV_WIN32_HANDLE_TO_FD(handle) 4560e3a38431SPaul Bohm 4561e3a38431SPaul BohmIf C<EV_SELECT_IS_WINSOCKET> then libev maps handles to file descriptors 4562e3a38431SPaul Bohmusing the standard C<_open_osfhandle> function. For programs implementing 4563e3a38431SPaul Bohmtheir own fd to handle mapping, overwriting this function makes it easier 4564e3a38431SPaul Bohmto do so. This can be done by defining this macro to an appropriate value. 4565e3a38431SPaul Bohm 4566e3a38431SPaul Bohm=item EV_WIN32_CLOSE_FD(fd) 4567e3a38431SPaul Bohm 4568e3a38431SPaul BohmIf programs implement their own fd to handle mapping on win32, then this 4569e3a38431SPaul Bohmmacro can be used to override the C<close> function, useful to unregister 4570e3a38431SPaul Bohmfile descriptors again. Note that the replacement function has to close 4571e3a38431SPaul Bohmthe underlying OS handle. 4572e3a38431SPaul Bohm 4573*93823e6cSPaul Bohm=item EV_USE_WSASOCKET 4574*93823e6cSPaul Bohm 4575*93823e6cSPaul BohmIf defined to be C<1>, libev will use C<WSASocket> to create its internal 4576*93823e6cSPaul Bohmcommunication socket, which works better in some environments. Otherwise, 4577*93823e6cSPaul Bohmthe normal C<socket> function will be used, which works better in other 4578*93823e6cSPaul Bohmenvironments. 4579*93823e6cSPaul Bohm 4580e3a38431SPaul Bohm=item EV_USE_POLL 4581e3a38431SPaul Bohm 4582e3a38431SPaul BohmIf defined to be C<1>, libev will compile in support for the C<poll>(2) 4583e3a38431SPaul Bohmbackend. Otherwise it will be enabled on non-win32 platforms. It 4584e3a38431SPaul Bohmtakes precedence over select. 4585e3a38431SPaul Bohm 4586e3a38431SPaul Bohm=item EV_USE_EPOLL 4587e3a38431SPaul Bohm 4588e3a38431SPaul BohmIf defined to be C<1>, libev will compile in support for the Linux 4589e3a38431SPaul BohmC<epoll>(7) backend. Its availability will be detected at runtime, 4590e3a38431SPaul Bohmotherwise another method will be used as fallback. This is the preferred 4591e3a38431SPaul Bohmbackend for GNU/Linux systems. If undefined, it will be enabled if the 4592e3a38431SPaul Bohmheaders indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. 4593e3a38431SPaul Bohm 4594e3a38431SPaul Bohm=item EV_USE_KQUEUE 4595e3a38431SPaul Bohm 4596e3a38431SPaul BohmIf defined to be C<1>, libev will compile in support for the BSD style 4597e3a38431SPaul BohmC<kqueue>(2) backend. Its actual availability will be detected at runtime, 4598e3a38431SPaul Bohmotherwise another method will be used as fallback. This is the preferred 4599e3a38431SPaul Bohmbackend for BSD and BSD-like systems, although on most BSDs kqueue only 4600e3a38431SPaul Bohmsupports some types of fds correctly (the only platform we found that 4601e3a38431SPaul Bohmsupports ptys for example was NetBSD), so kqueue might be compiled in, but 4602e3a38431SPaul Bohmnot be used unless explicitly requested. The best way to use it is to find 4603e3a38431SPaul Bohmout whether kqueue supports your type of fd properly and use an embedded 4604e3a38431SPaul Bohmkqueue loop. 4605e3a38431SPaul Bohm 4606e3a38431SPaul Bohm=item EV_USE_PORT 4607e3a38431SPaul Bohm 4608e3a38431SPaul BohmIf defined to be C<1>, libev will compile in support for the Solaris 4609e3a38431SPaul Bohm10 port style backend. Its availability will be detected at runtime, 4610e3a38431SPaul Bohmotherwise another method will be used as fallback. This is the preferred 4611e3a38431SPaul Bohmbackend for Solaris 10 systems. 4612e3a38431SPaul Bohm 4613e3a38431SPaul Bohm=item EV_USE_DEVPOLL 4614e3a38431SPaul Bohm 4615e3a38431SPaul BohmReserved for future expansion, works like the USE symbols above. 4616e3a38431SPaul Bohm 4617e3a38431SPaul Bohm=item EV_USE_INOTIFY 4618e3a38431SPaul Bohm 4619e3a38431SPaul BohmIf defined to be C<1>, libev will compile in support for the Linux inotify 4620e3a38431SPaul Bohminterface to speed up C<ev_stat> watchers. Its actual availability will 4621e3a38431SPaul Bohmbe detected at runtime. If undefined, it will be enabled if the headers 4622e3a38431SPaul Bohmindicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. 4623e3a38431SPaul Bohm 4624*93823e6cSPaul Bohm=item EV_NO_SMP 4625*93823e6cSPaul Bohm 4626*93823e6cSPaul BohmIf defined to be C<1>, libev will assume that memory is always coherent 4627*93823e6cSPaul Bohmbetween threads, that is, threads can be used, but threads never run on 4628*93823e6cSPaul Bohmdifferent cpus (or different cpu cores). This reduces dependencies 4629*93823e6cSPaul Bohmand makes libev faster. 4630*93823e6cSPaul Bohm 4631*93823e6cSPaul Bohm=item EV_NO_THREADS 4632*93823e6cSPaul Bohm 4633*93823e6cSPaul BohmIf defined to be C<1>, libev will assume that it will never be called from 4634*93823e6cSPaul Bohmdifferent threads (that includes signal handlers), which is a stronger 4635*93823e6cSPaul Bohmassumption than C<EV_NO_SMP>, above. This reduces dependencies and makes 4636*93823e6cSPaul Bohmlibev faster. 4637*93823e6cSPaul Bohm 4638e3a38431SPaul Bohm=item EV_ATOMIC_T 4639e3a38431SPaul Bohm 4640e3a38431SPaul BohmLibev requires an integer type (suitable for storing C<0> or C<1>) whose 4641*93823e6cSPaul Bohmaccess is atomic with respect to other threads or signal contexts. No 4642*93823e6cSPaul Bohmsuch type is easily found in the C language, so you can provide your own 4643*93823e6cSPaul Bohmtype that you know is safe for your purposes. It is used both for signal 4644*93823e6cSPaul Bohmhandler "locking" as well as for signal and thread safety in C<ev_async> 4645*93823e6cSPaul Bohmwatchers. 4646e3a38431SPaul Bohm 4647e3a38431SPaul BohmIn the absence of this define, libev will use C<sig_atomic_t volatile> 4648e3a38431SPaul Bohm(from F<signal.h>), which is usually good enough on most platforms. 4649e3a38431SPaul Bohm 4650e3a38431SPaul Bohm=item EV_H (h) 4651e3a38431SPaul Bohm 4652e3a38431SPaul BohmThe name of the F<ev.h> header file used to include it. The default if 4653e3a38431SPaul Bohmundefined is C<"ev.h"> in F<event.h>, F<ev.c> and F<ev++.h>. This can be 4654e3a38431SPaul Bohmused to virtually rename the F<ev.h> header file in case of conflicts. 4655e3a38431SPaul Bohm 4656e3a38431SPaul Bohm=item EV_CONFIG_H (h) 4657e3a38431SPaul Bohm 4658e3a38431SPaul BohmIf C<EV_STANDALONE> isn't C<1>, this variable can be used to override 4659e3a38431SPaul BohmF<ev.c>'s idea of where to find the F<config.h> file, similarly to 4660e3a38431SPaul BohmC<EV_H>, above. 4661e3a38431SPaul Bohm 4662e3a38431SPaul Bohm=item EV_EVENT_H (h) 4663e3a38431SPaul Bohm 4664e3a38431SPaul BohmSimilarly to C<EV_H>, this macro can be used to override F<event.c>'s idea 4665e3a38431SPaul Bohmof how the F<event.h> header can be found, the default is C<"event.h">. 4666e3a38431SPaul Bohm 4667e3a38431SPaul Bohm=item EV_PROTOTYPES (h) 4668e3a38431SPaul Bohm 4669e3a38431SPaul BohmIf defined to be C<0>, then F<ev.h> will not define any function 4670e3a38431SPaul Bohmprototypes, but still define all the structs and other symbols. This is 4671e3a38431SPaul Bohmoccasionally useful if you want to provide your own wrapper functions 4672e3a38431SPaul Bohmaround libev functions. 4673e3a38431SPaul Bohm 4674e3a38431SPaul Bohm=item EV_MULTIPLICITY 4675e3a38431SPaul Bohm 4676e3a38431SPaul BohmIf undefined or defined to C<1>, then all event-loop-specific functions 4677e3a38431SPaul Bohmwill have the C<struct ev_loop *> as first argument, and you can create 4678e3a38431SPaul Bohmadditional independent event loops. Otherwise there will be no support 4679e3a38431SPaul Bohmfor multiple event loops and there is no first event loop pointer 4680e3a38431SPaul Bohmargument. Instead, all functions act on the single default loop. 4681e3a38431SPaul Bohm 4682*93823e6cSPaul BohmNote that C<EV_DEFAULT> and C<EV_DEFAULT_> will no longer provide a 4683*93823e6cSPaul Bohmdefault loop when multiplicity is switched off - you always have to 4684*93823e6cSPaul Bohminitialise the loop manually in this case. 4685*93823e6cSPaul Bohm 4686e3a38431SPaul Bohm=item EV_MINPRI 4687e3a38431SPaul Bohm 4688e3a38431SPaul Bohm=item EV_MAXPRI 4689e3a38431SPaul Bohm 4690e3a38431SPaul BohmThe range of allowed priorities. C<EV_MINPRI> must be smaller or equal to 4691e3a38431SPaul BohmC<EV_MAXPRI>, but otherwise there are no non-obvious limitations. You can 4692e3a38431SPaul Bohmprovide for more priorities by overriding those symbols (usually defined 4693e3a38431SPaul Bohmto be C<-2> and C<2>, respectively). 4694e3a38431SPaul Bohm 4695e3a38431SPaul BohmWhen doing priority-based operations, libev usually has to linearly search 4696e3a38431SPaul Bohmall the priorities, so having many of them (hundreds) uses a lot of space 4697e3a38431SPaul Bohmand time, so using the defaults of five priorities (-2 .. +2) is usually 4698e3a38431SPaul Bohmfine. 4699e3a38431SPaul Bohm 4700e3a38431SPaul BohmIf your embedding application does not need any priorities, defining these 4701e3a38431SPaul Bohmboth to C<0> will save some memory and CPU. 4702e3a38431SPaul Bohm 4703e3a38431SPaul Bohm=item EV_PERIODIC_ENABLE, EV_IDLE_ENABLE, EV_EMBED_ENABLE, EV_STAT_ENABLE, 4704e3a38431SPaul BohmEV_PREPARE_ENABLE, EV_CHECK_ENABLE, EV_FORK_ENABLE, EV_SIGNAL_ENABLE, 4705e3a38431SPaul BohmEV_ASYNC_ENABLE, EV_CHILD_ENABLE. 4706e3a38431SPaul Bohm 4707e3a38431SPaul BohmIf undefined or defined to be C<1> (and the platform supports it), then 4708e3a38431SPaul Bohmthe respective watcher type is supported. If defined to be C<0>, then it 4709e3a38431SPaul Bohmis not. Disabling watcher types mainly saves code size. 4710e3a38431SPaul Bohm 4711e3a38431SPaul Bohm=item EV_FEATURES 4712e3a38431SPaul Bohm 4713e3a38431SPaul BohmIf you need to shave off some kilobytes of code at the expense of some 4714e3a38431SPaul Bohmspeed (but with the full API), you can define this symbol to request 4715e3a38431SPaul Bohmcertain subsets of functionality. The default is to enable all features 4716e3a38431SPaul Bohmthat can be enabled on the platform. 4717e3a38431SPaul Bohm 4718e3a38431SPaul BohmA typical way to use this symbol is to define it to C<0> (or to a bitset 4719e3a38431SPaul Bohmwith some broad features you want) and then selectively re-enable 4720e3a38431SPaul Bohmadditional parts you want, for example if you want everything minimal, 4721e3a38431SPaul Bohmbut multiple event loop support, async and child watchers and the poll 4722e3a38431SPaul Bohmbackend, use this: 4723e3a38431SPaul Bohm 4724e3a38431SPaul Bohm #define EV_FEATURES 0 4725e3a38431SPaul Bohm #define EV_MULTIPLICITY 1 4726e3a38431SPaul Bohm #define EV_USE_POLL 1 4727e3a38431SPaul Bohm #define EV_CHILD_ENABLE 1 4728e3a38431SPaul Bohm #define EV_ASYNC_ENABLE 1 4729e3a38431SPaul Bohm 4730e3a38431SPaul BohmThe actual value is a bitset, it can be a combination of the following 4731*93823e6cSPaul Bohmvalues (by default, all of these are enabled): 4732e3a38431SPaul Bohm 4733e3a38431SPaul Bohm=over 4 4734e3a38431SPaul Bohm 4735e3a38431SPaul Bohm=item C<1> - faster/larger code 4736e3a38431SPaul Bohm 4737e3a38431SPaul BohmUse larger code to speed up some operations. 4738e3a38431SPaul Bohm 4739e3a38431SPaul BohmCurrently this is used to override some inlining decisions (enlarging the 4740e3a38431SPaul Bohmcode size by roughly 30% on amd64). 4741e3a38431SPaul Bohm 4742e3a38431SPaul BohmWhen optimising for size, use of compiler flags such as C<-Os> with 4743e3a38431SPaul Bohmgcc is recommended, as well as C<-DNDEBUG>, as libev contains a number of 4744e3a38431SPaul Bohmassertions. 4745e3a38431SPaul Bohm 4746*93823e6cSPaul BohmThe default is off when C<__OPTIMIZE_SIZE__> is defined by your compiler 4747*93823e6cSPaul Bohm(e.g. gcc with C<-Os>). 4748*93823e6cSPaul Bohm 4749e3a38431SPaul Bohm=item C<2> - faster/larger data structures 4750e3a38431SPaul Bohm 4751e3a38431SPaul BohmReplaces the small 2-heap for timer management by a faster 4-heap, larger 4752e3a38431SPaul Bohmhash table sizes and so on. This will usually further increase code size 4753e3a38431SPaul Bohmand can additionally have an effect on the size of data structures at 4754e3a38431SPaul Bohmruntime. 4755e3a38431SPaul Bohm 4756*93823e6cSPaul BohmThe default is off when C<__OPTIMIZE_SIZE__> is defined by your compiler 4757*93823e6cSPaul Bohm(e.g. gcc with C<-Os>). 4758*93823e6cSPaul Bohm 4759e3a38431SPaul Bohm=item C<4> - full API configuration 4760e3a38431SPaul Bohm 4761e3a38431SPaul BohmThis enables priorities (sets C<EV_MAXPRI>=2 and C<EV_MINPRI>=-2), and 4762e3a38431SPaul Bohmenables multiplicity (C<EV_MULTIPLICITY>=1). 4763e3a38431SPaul Bohm 4764e3a38431SPaul Bohm=item C<8> - full API 4765e3a38431SPaul Bohm 4766e3a38431SPaul BohmThis enables a lot of the "lesser used" API functions. See C<ev.h> for 4767e3a38431SPaul Bohmdetails on which parts of the API are still available without this 4768e3a38431SPaul Bohmfeature, and do not complain if this subset changes over time. 4769e3a38431SPaul Bohm 4770e3a38431SPaul Bohm=item C<16> - enable all optional watcher types 4771e3a38431SPaul Bohm 4772e3a38431SPaul BohmEnables all optional watcher types. If you want to selectively enable 4773e3a38431SPaul Bohmonly some watcher types other than I/O and timers (e.g. prepare, 4774e3a38431SPaul Bohmembed, async, child...) you can enable them manually by defining 4775e3a38431SPaul BohmC<EV_watchertype_ENABLE> to C<1> instead. 4776e3a38431SPaul Bohm 4777e3a38431SPaul Bohm=item C<32> - enable all backends 4778e3a38431SPaul Bohm 4779e3a38431SPaul BohmThis enables all backends - without this feature, you need to enable at 4780e3a38431SPaul Bohmleast one backend manually (C<EV_USE_SELECT> is a good choice). 4781e3a38431SPaul Bohm 4782e3a38431SPaul Bohm=item C<64> - enable OS-specific "helper" APIs 4783e3a38431SPaul Bohm 4784e3a38431SPaul BohmEnable inotify, eventfd, signalfd and similar OS-specific helper APIs by 4785e3a38431SPaul Bohmdefault. 4786e3a38431SPaul Bohm 4787e3a38431SPaul Bohm=back 4788e3a38431SPaul Bohm 4789e3a38431SPaul BohmCompiling with C<gcc -Os -DEV_STANDALONE -DEV_USE_EPOLL=1 -DEV_FEATURES=0> 4790e3a38431SPaul Bohmreduces the compiled size of libev from 24.7Kb code/2.8Kb data to 6.5Kb 4791e3a38431SPaul Bohmcode/0.3Kb data on my GNU/Linux amd64 system, while still giving you I/O 4792e3a38431SPaul Bohmwatchers, timers and monotonic clock support. 4793e3a38431SPaul Bohm 4794e3a38431SPaul BohmWith an intelligent-enough linker (gcc+binutils are intelligent enough 4795e3a38431SPaul Bohmwhen you use C<-Wl,--gc-sections -ffunction-sections>) functions unused by 4796e3a38431SPaul Bohmyour program might be left out as well - a binary starting a timer and an 4797e3a38431SPaul BohmI/O watcher then might come out at only 5Kb. 4798e3a38431SPaul Bohm 4799*93823e6cSPaul Bohm=item EV_API_STATIC 4800*93823e6cSPaul Bohm 4801*93823e6cSPaul BohmIf this symbol is defined (by default it is not), then all identifiers 4802*93823e6cSPaul Bohmwill have static linkage. This means that libev will not export any 4803*93823e6cSPaul Bohmidentifiers, and you cannot link against libev anymore. This can be useful 4804*93823e6cSPaul Bohmwhen you embed libev, only want to use libev functions in a single file, 4805*93823e6cSPaul Bohmand do not want its identifiers to be visible. 4806*93823e6cSPaul Bohm 4807*93823e6cSPaul BohmTo use this, define C<EV_API_STATIC> and include F<ev.c> in the file that 4808*93823e6cSPaul Bohmwants to use libev. 4809*93823e6cSPaul Bohm 4810*93823e6cSPaul BohmThis option only works when libev is compiled with a C compiler, as C++ 4811*93823e6cSPaul Bohmdoesn't support the required declaration syntax. 4812*93823e6cSPaul Bohm 4813e3a38431SPaul Bohm=item EV_AVOID_STDIO 4814e3a38431SPaul Bohm 4815e3a38431SPaul BohmIf this is set to C<1> at compiletime, then libev will avoid using stdio 4816e3a38431SPaul Bohmfunctions (printf, scanf, perror etc.). This will increase the code size 4817e3a38431SPaul Bohmsomewhat, but if your program doesn't otherwise depend on stdio and your 4818e3a38431SPaul Bohmlibc allows it, this avoids linking in the stdio library which is quite 4819e3a38431SPaul Bohmbig. 4820e3a38431SPaul Bohm 4821e3a38431SPaul BohmNote that error messages might become less precise when this option is 4822e3a38431SPaul Bohmenabled. 4823e3a38431SPaul Bohm 4824e3a38431SPaul Bohm=item EV_NSIG 4825e3a38431SPaul Bohm 4826e3a38431SPaul BohmThe highest supported signal number, +1 (or, the number of 4827e3a38431SPaul Bohmsignals): Normally, libev tries to deduce the maximum number of signals 4828e3a38431SPaul Bohmautomatically, but sometimes this fails, in which case it can be 4829e3a38431SPaul Bohmspecified. Also, using a lower number than detected (C<32> should be 4830e3a38431SPaul Bohmgood for about any system in existence) can save some memory, as libev 4831e3a38431SPaul Bohmstatically allocates some 12-24 bytes per signal number. 4832e3a38431SPaul Bohm 4833e3a38431SPaul Bohm=item EV_PID_HASHSIZE 4834e3a38431SPaul Bohm 4835e3a38431SPaul BohmC<ev_child> watchers use a small hash table to distribute workload by 4836e3a38431SPaul Bohmpid. The default size is C<16> (or C<1> with C<EV_FEATURES> disabled), 4837e3a38431SPaul Bohmusually more than enough. If you need to manage thousands of children you 4838e3a38431SPaul Bohmmight want to increase this value (I<must> be a power of two). 4839e3a38431SPaul Bohm 4840e3a38431SPaul Bohm=item EV_INOTIFY_HASHSIZE 4841e3a38431SPaul Bohm 4842e3a38431SPaul BohmC<ev_stat> watchers use a small hash table to distribute workload by 4843e3a38431SPaul Bohminotify watch id. The default size is C<16> (or C<1> with C<EV_FEATURES> 4844e3a38431SPaul Bohmdisabled), usually more than enough. If you need to manage thousands of 4845e3a38431SPaul BohmC<ev_stat> watchers you might want to increase this value (I<must> be a 4846e3a38431SPaul Bohmpower of two). 4847e3a38431SPaul Bohm 4848e3a38431SPaul Bohm=item EV_USE_4HEAP 4849e3a38431SPaul Bohm 4850e3a38431SPaul BohmHeaps are not very cache-efficient. To improve the cache-efficiency of the 4851e3a38431SPaul Bohmtimer and periodics heaps, libev uses a 4-heap when this symbol is defined 4852e3a38431SPaul Bohmto C<1>. The 4-heap uses more complicated (longer) code but has noticeably 4853e3a38431SPaul Bohmfaster performance with many (thousands) of watchers. 4854e3a38431SPaul Bohm 4855e3a38431SPaul BohmThe default is C<1>, unless C<EV_FEATURES> overrides it, in which case it 4856e3a38431SPaul Bohmwill be C<0>. 4857e3a38431SPaul Bohm 4858e3a38431SPaul Bohm=item EV_HEAP_CACHE_AT 4859e3a38431SPaul Bohm 4860e3a38431SPaul BohmHeaps are not very cache-efficient. To improve the cache-efficiency of the 4861e3a38431SPaul Bohmtimer and periodics heaps, libev can cache the timestamp (I<at>) within 4862e3a38431SPaul Bohmthe heap structure (selected by defining C<EV_HEAP_CACHE_AT> to C<1>), 4863e3a38431SPaul Bohmwhich uses 8-12 bytes more per watcher and a few hundred bytes more code, 4864e3a38431SPaul Bohmbut avoids random read accesses on heap changes. This improves performance 4865e3a38431SPaul Bohmnoticeably with many (hundreds) of watchers. 4866e3a38431SPaul Bohm 4867e3a38431SPaul BohmThe default is C<1>, unless C<EV_FEATURES> overrides it, in which case it 4868e3a38431SPaul Bohmwill be C<0>. 4869e3a38431SPaul Bohm 4870e3a38431SPaul Bohm=item EV_VERIFY 4871e3a38431SPaul Bohm 4872e3a38431SPaul BohmControls how much internal verification (see C<ev_verify ()>) will 4873e3a38431SPaul Bohmbe done: If set to C<0>, no internal verification code will be compiled 4874e3a38431SPaul Bohmin. If set to C<1>, then verification code will be compiled in, but not 4875e3a38431SPaul Bohmcalled. If set to C<2>, then the internal verification code will be 4876e3a38431SPaul Bohmcalled once per loop, which can slow down libev. If set to C<3>, then the 4877e3a38431SPaul Bohmverification code will be called very frequently, which will slow down 4878e3a38431SPaul Bohmlibev considerably. 4879e3a38431SPaul Bohm 4880e3a38431SPaul BohmThe default is C<1>, unless C<EV_FEATURES> overrides it, in which case it 4881e3a38431SPaul Bohmwill be C<0>. 4882e3a38431SPaul Bohm 4883e3a38431SPaul Bohm=item EV_COMMON 4884e3a38431SPaul Bohm 4885e3a38431SPaul BohmBy default, all watchers have a C<void *data> member. By redefining 4886e3a38431SPaul Bohmthis macro to something else you can include more and other types of 4887e3a38431SPaul Bohmmembers. You have to define it each time you include one of the files, 4888e3a38431SPaul Bohmthough, and it must be identical each time. 4889e3a38431SPaul Bohm 4890e3a38431SPaul BohmFor example, the perl EV module uses something like this: 4891e3a38431SPaul Bohm 4892e3a38431SPaul Bohm #define EV_COMMON \ 4893e3a38431SPaul Bohm SV *self; /* contains this struct */ \ 4894e3a38431SPaul Bohm SV *cb_sv, *fh /* note no trailing ";" */ 4895e3a38431SPaul Bohm 4896e3a38431SPaul Bohm=item EV_CB_DECLARE (type) 4897e3a38431SPaul Bohm 4898e3a38431SPaul Bohm=item EV_CB_INVOKE (watcher, revents) 4899e3a38431SPaul Bohm 4900e3a38431SPaul Bohm=item ev_set_cb (ev, cb) 4901e3a38431SPaul Bohm 4902e3a38431SPaul BohmCan be used to change the callback member declaration in each watcher, 4903e3a38431SPaul Bohmand the way callbacks are invoked and set. Must expand to a struct member 4904e3a38431SPaul Bohmdefinition and a statement, respectively. See the F<ev.h> header file for 4905e3a38431SPaul Bohmtheir default definitions. One possible use for overriding these is to 4906e3a38431SPaul Bohmavoid the C<struct ev_loop *> as first argument in all cases, or to use 4907e3a38431SPaul Bohmmethod calls instead of plain function calls in C++. 4908e3a38431SPaul Bohm 4909e3a38431SPaul Bohm=back 4910e3a38431SPaul Bohm 4911e3a38431SPaul Bohm=head2 EXPORTED API SYMBOLS 4912e3a38431SPaul Bohm 4913e3a38431SPaul BohmIf you need to re-export the API (e.g. via a DLL) and you need a list of 4914e3a38431SPaul Bohmexported symbols, you can use the provided F<Symbol.*> files which list 4915e3a38431SPaul Bohmall public symbols, one per line: 4916e3a38431SPaul Bohm 4917e3a38431SPaul Bohm Symbols.ev for libev proper 4918e3a38431SPaul Bohm Symbols.event for the libevent emulation 4919e3a38431SPaul Bohm 4920e3a38431SPaul BohmThis can also be used to rename all public symbols to avoid clashes with 4921e3a38431SPaul Bohmmultiple versions of libev linked together (which is obviously bad in 4922e3a38431SPaul Bohmitself, but sometimes it is inconvenient to avoid this). 4923e3a38431SPaul Bohm 4924e3a38431SPaul BohmA sed command like this will create wrapper C<#define>'s that you need to 4925e3a38431SPaul Bohminclude before including F<ev.h>: 4926e3a38431SPaul Bohm 4927e3a38431SPaul Bohm <Symbols.ev sed -e "s/.*/#define & myprefix_&/" >wrap.h 4928e3a38431SPaul Bohm 4929e3a38431SPaul BohmThis would create a file F<wrap.h> which essentially looks like this: 4930e3a38431SPaul Bohm 4931e3a38431SPaul Bohm #define ev_backend myprefix_ev_backend 4932e3a38431SPaul Bohm #define ev_check_start myprefix_ev_check_start 4933e3a38431SPaul Bohm #define ev_check_stop myprefix_ev_check_stop 4934e3a38431SPaul Bohm ... 4935e3a38431SPaul Bohm 4936e3a38431SPaul Bohm=head2 EXAMPLES 4937e3a38431SPaul Bohm 4938e3a38431SPaul BohmFor a real-world example of a program the includes libev 4939e3a38431SPaul Bohmverbatim, you can have a look at the EV perl module 4940e3a38431SPaul Bohm(L<http://software.schmorp.de/pkg/EV.html>). It has the libev files in 4941e3a38431SPaul Bohmthe F<libev/> subdirectory and includes them in the F<EV/EVAPI.h> (public 4942e3a38431SPaul Bohminterface) and F<EV.xs> (implementation) files. Only the F<EV.xs> file 4943e3a38431SPaul Bohmwill be compiled. It is pretty complex because it provides its own header 4944e3a38431SPaul Bohmfile. 4945e3a38431SPaul Bohm 4946e3a38431SPaul BohmThe usage in rxvt-unicode is simpler. It has a F<ev_cpp.h> header file 4947e3a38431SPaul Bohmthat everybody includes and which overrides some configure choices: 4948e3a38431SPaul Bohm 4949e3a38431SPaul Bohm #define EV_FEATURES 8 4950e3a38431SPaul Bohm #define EV_USE_SELECT 1 4951e3a38431SPaul Bohm #define EV_PREPARE_ENABLE 1 4952e3a38431SPaul Bohm #define EV_IDLE_ENABLE 1 4953e3a38431SPaul Bohm #define EV_SIGNAL_ENABLE 1 4954e3a38431SPaul Bohm #define EV_CHILD_ENABLE 1 4955e3a38431SPaul Bohm #define EV_USE_STDEXCEPT 0 4956e3a38431SPaul Bohm #define EV_CONFIG_H <config.h> 4957e3a38431SPaul Bohm 4958e3a38431SPaul Bohm #include "ev++.h" 4959e3a38431SPaul Bohm 4960e3a38431SPaul BohmAnd a F<ev_cpp.C> implementation file that contains libev proper and is compiled: 4961e3a38431SPaul Bohm 4962e3a38431SPaul Bohm #include "ev_cpp.h" 4963e3a38431SPaul Bohm #include "ev.c" 4964e3a38431SPaul Bohm 4965e3a38431SPaul Bohm=head1 INTERACTION WITH OTHER PROGRAMS, LIBRARIES OR THE ENVIRONMENT 4966e3a38431SPaul Bohm 4967e3a38431SPaul Bohm=head2 THREADS AND COROUTINES 4968e3a38431SPaul Bohm 4969e3a38431SPaul Bohm=head3 THREADS 4970e3a38431SPaul Bohm 4971e3a38431SPaul BohmAll libev functions are reentrant and thread-safe unless explicitly 4972e3a38431SPaul Bohmdocumented otherwise, but libev implements no locking itself. This means 4973e3a38431SPaul Bohmthat you can use as many loops as you want in parallel, as long as there 4974e3a38431SPaul Bohmare no concurrent calls into any libev function with the same loop 4975e3a38431SPaul Bohmparameter (C<ev_default_*> calls have an implicit default loop parameter, 4976e3a38431SPaul Bohmof course): libev guarantees that different event loops share no data 4977e3a38431SPaul Bohmstructures that need any locking. 4978e3a38431SPaul Bohm 4979e3a38431SPaul BohmOr to put it differently: calls with different loop parameters can be done 4980e3a38431SPaul Bohmconcurrently from multiple threads, calls with the same loop parameter 4981e3a38431SPaul Bohmmust be done serially (but can be done from different threads, as long as 4982e3a38431SPaul Bohmonly one thread ever is inside a call at any point in time, e.g. by using 4983e3a38431SPaul Bohma mutex per loop). 4984e3a38431SPaul Bohm 4985e3a38431SPaul BohmSpecifically to support threads (and signal handlers), libev implements 4986e3a38431SPaul Bohmso-called C<ev_async> watchers, which allow some limited form of 4987e3a38431SPaul Bohmconcurrency on the same event loop, namely waking it up "from the 4988e3a38431SPaul Bohmoutside". 4989e3a38431SPaul Bohm 4990e3a38431SPaul BohmIf you want to know which design (one loop, locking, or multiple loops 4991e3a38431SPaul Bohmwithout or something else still) is best for your problem, then I cannot 4992e3a38431SPaul Bohmhelp you, but here is some generic advice: 4993e3a38431SPaul Bohm 4994e3a38431SPaul Bohm=over 4 4995e3a38431SPaul Bohm 4996e3a38431SPaul Bohm=item * most applications have a main thread: use the default libev loop 4997e3a38431SPaul Bohmin that thread, or create a separate thread running only the default loop. 4998e3a38431SPaul Bohm 4999e3a38431SPaul BohmThis helps integrating other libraries or software modules that use libev 5000e3a38431SPaul Bohmthemselves and don't care/know about threading. 5001e3a38431SPaul Bohm 5002e3a38431SPaul Bohm=item * one loop per thread is usually a good model. 5003e3a38431SPaul Bohm 5004e3a38431SPaul BohmDoing this is almost never wrong, sometimes a better-performance model 5005e3a38431SPaul Bohmexists, but it is always a good start. 5006e3a38431SPaul Bohm 5007e3a38431SPaul Bohm=item * other models exist, such as the leader/follower pattern, where one 5008e3a38431SPaul Bohmloop is handed through multiple threads in a kind of round-robin fashion. 5009e3a38431SPaul Bohm 5010e3a38431SPaul BohmChoosing a model is hard - look around, learn, know that usually you can do 5011e3a38431SPaul Bohmbetter than you currently do :-) 5012e3a38431SPaul Bohm 5013e3a38431SPaul Bohm=item * often you need to talk to some other thread which blocks in the 5014e3a38431SPaul Bohmevent loop. 5015e3a38431SPaul Bohm 5016e3a38431SPaul BohmC<ev_async> watchers can be used to wake them up from other threads safely 5017e3a38431SPaul Bohm(or from signal contexts...). 5018e3a38431SPaul Bohm 5019e3a38431SPaul BohmAn example use would be to communicate signals or other events that only 5020e3a38431SPaul Bohmwork in the default loop by registering the signal watcher with the 5021e3a38431SPaul Bohmdefault loop and triggering an C<ev_async> watcher from the default loop 5022e3a38431SPaul Bohmwatcher callback into the event loop interested in the signal. 5023e3a38431SPaul Bohm 5024e3a38431SPaul Bohm=back 5025e3a38431SPaul Bohm 5026*93823e6cSPaul BohmSee also L</THREAD LOCKING EXAMPLE>. 5027e3a38431SPaul Bohm 5028e3a38431SPaul Bohm=head3 COROUTINES 5029e3a38431SPaul Bohm 5030e3a38431SPaul BohmLibev is very accommodating to coroutines ("cooperative threads"): 5031e3a38431SPaul Bohmlibev fully supports nesting calls to its functions from different 5032e3a38431SPaul Bohmcoroutines (e.g. you can call C<ev_run> on the same loop from two 5033e3a38431SPaul Bohmdifferent coroutines, and switch freely between both coroutines running 5034e3a38431SPaul Bohmthe loop, as long as you don't confuse yourself). The only exception is 5035e3a38431SPaul Bohmthat you must not do this from C<ev_periodic> reschedule callbacks. 5036e3a38431SPaul Bohm 5037e3a38431SPaul BohmCare has been taken to ensure that libev does not keep local state inside 5038e3a38431SPaul BohmC<ev_run>, and other calls do not usually allow for coroutine switches as 5039e3a38431SPaul Bohmthey do not call any callbacks. 5040e3a38431SPaul Bohm 5041e3a38431SPaul Bohm=head2 COMPILER WARNINGS 5042e3a38431SPaul Bohm 5043e3a38431SPaul BohmDepending on your compiler and compiler settings, you might get no or a 5044e3a38431SPaul Bohmlot of warnings when compiling libev code. Some people are apparently 5045e3a38431SPaul Bohmscared by this. 5046e3a38431SPaul Bohm 5047e3a38431SPaul BohmHowever, these are unavoidable for many reasons. For one, each compiler 5048e3a38431SPaul Bohmhas different warnings, and each user has different tastes regarding 5049e3a38431SPaul Bohmwarning options. "Warn-free" code therefore cannot be a goal except when 5050e3a38431SPaul Bohmtargeting a specific compiler and compiler-version. 5051e3a38431SPaul Bohm 5052e3a38431SPaul BohmAnother reason is that some compiler warnings require elaborate 5053e3a38431SPaul Bohmworkarounds, or other changes to the code that make it less clear and less 5054e3a38431SPaul Bohmmaintainable. 5055e3a38431SPaul Bohm 5056e3a38431SPaul BohmAnd of course, some compiler warnings are just plain stupid, or simply 5057e3a38431SPaul Bohmwrong (because they don't actually warn about the condition their message 5058e3a38431SPaul Bohmseems to warn about). For example, certain older gcc versions had some 5059e3a38431SPaul Bohmwarnings that resulted in an extreme number of false positives. These have 5060e3a38431SPaul Bohmbeen fixed, but some people still insist on making code warn-free with 5061e3a38431SPaul Bohmsuch buggy versions. 5062e3a38431SPaul Bohm 5063e3a38431SPaul BohmWhile libev is written to generate as few warnings as possible, 5064e3a38431SPaul Bohm"warn-free" code is not a goal, and it is recommended not to build libev 5065e3a38431SPaul Bohmwith any compiler warnings enabled unless you are prepared to cope with 5066e3a38431SPaul Bohmthem (e.g. by ignoring them). Remember that warnings are just that: 5067e3a38431SPaul Bohmwarnings, not errors, or proof of bugs. 5068e3a38431SPaul Bohm 5069e3a38431SPaul Bohm 5070e3a38431SPaul Bohm=head2 VALGRIND 5071e3a38431SPaul Bohm 5072e3a38431SPaul BohmValgrind has a special section here because it is a popular tool that is 5073e3a38431SPaul Bohmhighly useful. Unfortunately, valgrind reports are very hard to interpret. 5074e3a38431SPaul Bohm 5075e3a38431SPaul BohmIf you think you found a bug (memory leak, uninitialised data access etc.) 5076e3a38431SPaul Bohmin libev, then check twice: If valgrind reports something like: 5077e3a38431SPaul Bohm 5078e3a38431SPaul Bohm ==2274== definitely lost: 0 bytes in 0 blocks. 5079e3a38431SPaul Bohm ==2274== possibly lost: 0 bytes in 0 blocks. 5080e3a38431SPaul Bohm ==2274== still reachable: 256 bytes in 1 blocks. 5081e3a38431SPaul Bohm 5082e3a38431SPaul BohmThen there is no memory leak, just as memory accounted to global variables 5083e3a38431SPaul Bohmis not a memleak - the memory is still being referenced, and didn't leak. 5084e3a38431SPaul Bohm 5085e3a38431SPaul BohmSimilarly, under some circumstances, valgrind might report kernel bugs 5086e3a38431SPaul Bohmas if it were a bug in libev (e.g. in realloc or in the poll backend, 5087e3a38431SPaul Bohmalthough an acceptable workaround has been found here), or it might be 5088e3a38431SPaul Bohmconfused. 5089e3a38431SPaul Bohm 5090e3a38431SPaul BohmKeep in mind that valgrind is a very good tool, but only a tool. Don't 5091e3a38431SPaul Bohmmake it into some kind of religion. 5092e3a38431SPaul Bohm 5093e3a38431SPaul BohmIf you are unsure about something, feel free to contact the mailing list 5094e3a38431SPaul Bohmwith the full valgrind report and an explanation on why you think this 5095e3a38431SPaul Bohmis a bug in libev (best check the archives, too :). However, don't be 5096e3a38431SPaul Bohmannoyed when you get a brisk "this is no bug" answer and take the chance 5097e3a38431SPaul Bohmof learning how to interpret valgrind properly. 5098e3a38431SPaul Bohm 5099e3a38431SPaul BohmIf you need, for some reason, empty reports from valgrind for your project 5100e3a38431SPaul BohmI suggest using suppression lists. 5101e3a38431SPaul Bohm 5102e3a38431SPaul Bohm 5103e3a38431SPaul Bohm=head1 PORTABILITY NOTES 5104e3a38431SPaul Bohm 5105e3a38431SPaul Bohm=head2 GNU/LINUX 32 BIT LIMITATIONS 5106e3a38431SPaul Bohm 5107e3a38431SPaul BohmGNU/Linux is the only common platform that supports 64 bit file/large file 5108e3a38431SPaul Bohminterfaces but I<disables> them by default. 5109e3a38431SPaul Bohm 5110e3a38431SPaul BohmThat means that libev compiled in the default environment doesn't support 5111e3a38431SPaul Bohmfiles larger than 2GiB or so, which mainly affects C<ev_stat> watchers. 5112e3a38431SPaul Bohm 5113e3a38431SPaul BohmUnfortunately, many programs try to work around this GNU/Linux issue 5114e3a38431SPaul Bohmby enabling the large file API, which makes them incompatible with the 5115e3a38431SPaul Bohmstandard libev compiled for their system. 5116e3a38431SPaul Bohm 5117e3a38431SPaul BohmLikewise, libev cannot enable the large file API itself as this would 5118e3a38431SPaul Bohmsuddenly make it incompatible to the default compile time environment, 5119e3a38431SPaul Bohmi.e. all programs not using special compile switches. 5120e3a38431SPaul Bohm 5121e3a38431SPaul Bohm=head2 OS/X AND DARWIN BUGS 5122e3a38431SPaul Bohm 5123e3a38431SPaul BohmThe whole thing is a bug if you ask me - basically any system interface 5124e3a38431SPaul Bohmyou touch is broken, whether it is locales, poll, kqueue or even the 5125e3a38431SPaul BohmOpenGL drivers. 5126e3a38431SPaul Bohm 5127e3a38431SPaul Bohm=head3 C<kqueue> is buggy 5128e3a38431SPaul Bohm 5129e3a38431SPaul BohmThe kqueue syscall is broken in all known versions - most versions support 5130e3a38431SPaul Bohmonly sockets, many support pipes. 5131e3a38431SPaul Bohm 5132e3a38431SPaul BohmLibev tries to work around this by not using C<kqueue> by default on this 5133e3a38431SPaul Bohmrotten platform, but of course you can still ask for it when creating a 5134e3a38431SPaul Bohmloop - embedding a socket-only kqueue loop into a select-based one is 5135e3a38431SPaul Bohmprobably going to work well. 5136e3a38431SPaul Bohm 5137e3a38431SPaul Bohm=head3 C<poll> is buggy 5138e3a38431SPaul Bohm 5139e3a38431SPaul BohmInstead of fixing C<kqueue>, Apple replaced their (working) C<poll> 5140e3a38431SPaul Bohmimplementation by something calling C<kqueue> internally around the 10.5.6 5141e3a38431SPaul Bohmrelease, so now C<kqueue> I<and> C<poll> are broken. 5142e3a38431SPaul Bohm 5143e3a38431SPaul BohmLibev tries to work around this by not using C<poll> by default on 5144e3a38431SPaul Bohmthis rotten platform, but of course you can still ask for it when creating 5145e3a38431SPaul Bohma loop. 5146e3a38431SPaul Bohm 5147e3a38431SPaul Bohm=head3 C<select> is buggy 5148e3a38431SPaul Bohm 5149e3a38431SPaul BohmAll that's left is C<select>, and of course Apple found a way to fuck this 5150e3a38431SPaul Bohmone up as well: On OS/X, C<select> actively limits the number of file 5151e3a38431SPaul Bohmdescriptors you can pass in to 1024 - your program suddenly crashes when 5152e3a38431SPaul Bohmyou use more. 5153e3a38431SPaul Bohm 5154e3a38431SPaul BohmThere is an undocumented "workaround" for this - defining 5155e3a38431SPaul BohmC<_DARWIN_UNLIMITED_SELECT>, which libev tries to use, so select I<should> 5156e3a38431SPaul Bohmwork on OS/X. 5157e3a38431SPaul Bohm 5158e3a38431SPaul Bohm=head2 SOLARIS PROBLEMS AND WORKAROUNDS 5159e3a38431SPaul Bohm 5160e3a38431SPaul Bohm=head3 C<errno> reentrancy 5161e3a38431SPaul Bohm 5162e3a38431SPaul BohmThe default compile environment on Solaris is unfortunately so 5163e3a38431SPaul Bohmthread-unsafe that you can't even use components/libraries compiled 5164e3a38431SPaul Bohmwithout C<-D_REENTRANT> in a threaded program, which, of course, isn't 5165e3a38431SPaul Bohmdefined by default. A valid, if stupid, implementation choice. 5166e3a38431SPaul Bohm 5167e3a38431SPaul BohmIf you want to use libev in threaded environments you have to make sure 5168e3a38431SPaul Bohmit's compiled with C<_REENTRANT> defined. 5169e3a38431SPaul Bohm 5170e3a38431SPaul Bohm=head3 Event port backend 5171e3a38431SPaul Bohm 5172e3a38431SPaul BohmThe scalable event interface for Solaris is called "event 5173e3a38431SPaul Bohmports". Unfortunately, this mechanism is very buggy in all major 5174e3a38431SPaul Bohmreleases. If you run into high CPU usage, your program freezes or you get 5175e3a38431SPaul Bohma large number of spurious wakeups, make sure you have all the relevant 5176e3a38431SPaul Bohmand latest kernel patches applied. No, I don't know which ones, but there 5177e3a38431SPaul Bohmare multiple ones to apply, and afterwards, event ports actually work 5178e3a38431SPaul Bohmgreat. 5179e3a38431SPaul Bohm 5180e3a38431SPaul BohmIf you can't get it to work, you can try running the program by setting 5181e3a38431SPaul Bohmthe environment variable C<LIBEV_FLAGS=3> to only allow C<poll> and 5182e3a38431SPaul BohmC<select> backends. 5183e3a38431SPaul Bohm 5184e3a38431SPaul Bohm=head2 AIX POLL BUG 5185e3a38431SPaul Bohm 5186e3a38431SPaul BohmAIX unfortunately has a broken C<poll.h> header. Libev works around 5187e3a38431SPaul Bohmthis by trying to avoid the poll backend altogether (i.e. it's not even 5188e3a38431SPaul Bohmcompiled in), which normally isn't a big problem as C<select> works fine 5189e3a38431SPaul Bohmwith large bitsets on AIX, and AIX is dead anyway. 5190e3a38431SPaul Bohm 5191e3a38431SPaul Bohm=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS 5192e3a38431SPaul Bohm 5193e3a38431SPaul Bohm=head3 General issues 5194e3a38431SPaul Bohm 5195e3a38431SPaul BohmWin32 doesn't support any of the standards (e.g. POSIX) that libev 5196e3a38431SPaul Bohmrequires, and its I/O model is fundamentally incompatible with the POSIX 5197e3a38431SPaul Bohmmodel. Libev still offers limited functionality on this platform in 5198e3a38431SPaul Bohmthe form of the C<EVBACKEND_SELECT> backend, and only supports socket 5199e3a38431SPaul Bohmdescriptors. This only applies when using Win32 natively, not when using 5200e3a38431SPaul Bohme.g. cygwin. Actually, it only applies to the microsofts own compilers, 5201*93823e6cSPaul Bohmas every compiler comes with a slightly differently broken/incompatible 5202e3a38431SPaul Bohmenvironment. 5203e3a38431SPaul Bohm 5204e3a38431SPaul BohmLifting these limitations would basically require the full 5205e3a38431SPaul Bohmre-implementation of the I/O system. If you are into this kind of thing, 5206e3a38431SPaul Bohmthen note that glib does exactly that for you in a very portable way (note 5207e3a38431SPaul Bohmalso that glib is the slowest event library known to man). 5208e3a38431SPaul Bohm 5209e3a38431SPaul BohmThere is no supported compilation method available on windows except 5210e3a38431SPaul Bohmembedding it into other applications. 5211e3a38431SPaul Bohm 5212e3a38431SPaul BohmSensible signal handling is officially unsupported by Microsoft - libev 5213e3a38431SPaul Bohmtries its best, but under most conditions, signals will simply not work. 5214e3a38431SPaul Bohm 5215e3a38431SPaul BohmNot a libev limitation but worth mentioning: windows apparently doesn't 5216e3a38431SPaul Bohmaccept large writes: instead of resulting in a partial write, windows will 5217e3a38431SPaul Bohmeither accept everything or return C<ENOBUFS> if the buffer is too large, 5218e3a38431SPaul Bohmso make sure you only write small amounts into your sockets (less than a 5219e3a38431SPaul Bohmmegabyte seems safe, but this apparently depends on the amount of memory 5220e3a38431SPaul Bohmavailable). 5221e3a38431SPaul Bohm 5222e3a38431SPaul BohmDue to the many, low, and arbitrary limits on the win32 platform and 5223e3a38431SPaul Bohmthe abysmal performance of winsockets, using a large number of sockets 5224e3a38431SPaul Bohmis not recommended (and not reasonable). If your program needs to use 5225e3a38431SPaul Bohmmore than a hundred or so sockets, then likely it needs to use a totally 5226e3a38431SPaul Bohmdifferent implementation for windows, as libev offers the POSIX readiness 5227e3a38431SPaul Bohmnotification model, which cannot be implemented efficiently on windows 5228e3a38431SPaul Bohm(due to Microsoft monopoly games). 5229e3a38431SPaul Bohm 5230e3a38431SPaul BohmA typical way to use libev under windows is to embed it (see the embedding 5231e3a38431SPaul Bohmsection for details) and use the following F<evwrap.h> header file instead 5232e3a38431SPaul Bohmof F<ev.h>: 5233e3a38431SPaul Bohm 5234e3a38431SPaul Bohm #define EV_STANDALONE /* keeps ev from requiring config.h */ 5235e3a38431SPaul Bohm #define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */ 5236e3a38431SPaul Bohm 5237e3a38431SPaul Bohm #include "ev.h" 5238e3a38431SPaul Bohm 5239e3a38431SPaul BohmAnd compile the following F<evwrap.c> file into your project (make sure 5240e3a38431SPaul Bohmyou do I<not> compile the F<ev.c> or any other embedded source files!): 5241e3a38431SPaul Bohm 5242e3a38431SPaul Bohm #include "evwrap.h" 5243e3a38431SPaul Bohm #include "ev.c" 5244e3a38431SPaul Bohm 5245e3a38431SPaul Bohm=head3 The winsocket C<select> function 5246e3a38431SPaul Bohm 5247e3a38431SPaul BohmThe winsocket C<select> function doesn't follow POSIX in that it 5248e3a38431SPaul Bohmrequires socket I<handles> and not socket I<file descriptors> (it is 5249e3a38431SPaul Bohmalso extremely buggy). This makes select very inefficient, and also 5250e3a38431SPaul Bohmrequires a mapping from file descriptors to socket handles (the Microsoft 5251e3a38431SPaul BohmC runtime provides the function C<_open_osfhandle> for this). See the 5252e3a38431SPaul Bohmdiscussion of the C<EV_SELECT_USE_FD_SET>, C<EV_SELECT_IS_WINSOCKET> and 5253e3a38431SPaul BohmC<EV_FD_TO_WIN32_HANDLE> preprocessor symbols for more info. 5254e3a38431SPaul Bohm 5255e3a38431SPaul BohmThe configuration for a "naked" win32 using the Microsoft runtime 5256e3a38431SPaul Bohmlibraries and raw winsocket select is: 5257e3a38431SPaul Bohm 5258e3a38431SPaul Bohm #define EV_USE_SELECT 1 5259e3a38431SPaul Bohm #define EV_SELECT_IS_WINSOCKET 1 /* forces EV_SELECT_USE_FD_SET, too */ 5260e3a38431SPaul Bohm 5261e3a38431SPaul BohmNote that winsockets handling of fd sets is O(n), so you can easily get a 5262e3a38431SPaul Bohmcomplexity in the O(n²) range when using win32. 5263e3a38431SPaul Bohm 5264e3a38431SPaul Bohm=head3 Limited number of file descriptors 5265e3a38431SPaul Bohm 5266e3a38431SPaul BohmWindows has numerous arbitrary (and low) limits on things. 5267e3a38431SPaul Bohm 5268e3a38431SPaul BohmEarly versions of winsocket's select only supported waiting for a maximum 5269e3a38431SPaul Bohmof C<64> handles (probably owning to the fact that all windows kernels 5270e3a38431SPaul Bohmcan only wait for C<64> things at the same time internally; Microsoft 5271e3a38431SPaul Bohmrecommends spawning a chain of threads and wait for 63 handles and the 5272e3a38431SPaul Bohmprevious thread in each. Sounds great!). 5273e3a38431SPaul Bohm 5274e3a38431SPaul BohmNewer versions support more handles, but you need to define C<FD_SETSIZE> 5275e3a38431SPaul Bohmto some high number (e.g. C<2048>) before compiling the winsocket select 5276e3a38431SPaul Bohmcall (which might be in libev or elsewhere, for example, perl and many 5277e3a38431SPaul Bohmother interpreters do their own select emulation on windows). 5278e3a38431SPaul Bohm 5279e3a38431SPaul BohmAnother limit is the number of file descriptors in the Microsoft runtime 5280e3a38431SPaul Bohmlibraries, which by default is C<64> (there must be a hidden I<64> 5281e3a38431SPaul Bohmfetish or something like this inside Microsoft). You can increase this 5282e3a38431SPaul Bohmby calling C<_setmaxstdio>, which can increase this limit to C<2048> 5283e3a38431SPaul Bohm(another arbitrary limit), but is broken in many versions of the Microsoft 5284e3a38431SPaul Bohmruntime libraries. This might get you to about C<512> or C<2048> sockets 5285e3a38431SPaul Bohm(depending on windows version and/or the phase of the moon). To get more, 5286e3a38431SPaul Bohmyou need to wrap all I/O functions and provide your own fd management, but 5287e3a38431SPaul Bohmthe cost of calling select (O(n²)) will likely make this unworkable. 5288e3a38431SPaul Bohm 5289e3a38431SPaul Bohm=head2 PORTABILITY REQUIREMENTS 5290e3a38431SPaul Bohm 5291e3a38431SPaul BohmIn addition to a working ISO-C implementation and of course the 5292e3a38431SPaul Bohmbackend-specific APIs, libev relies on a few additional extensions: 5293e3a38431SPaul Bohm 5294e3a38431SPaul Bohm=over 4 5295e3a38431SPaul Bohm 5296e3a38431SPaul Bohm=item C<void (*)(ev_watcher_type *, int revents)> must have compatible 5297e3a38431SPaul Bohmcalling conventions regardless of C<ev_watcher_type *>. 5298e3a38431SPaul Bohm 5299e3a38431SPaul BohmLibev assumes not only that all watcher pointers have the same internal 5300e3a38431SPaul Bohmstructure (guaranteed by POSIX but not by ISO C for example), but it also 5301e3a38431SPaul Bohmassumes that the same (machine) code can be used to call any watcher 5302e3a38431SPaul Bohmcallback: The watcher callbacks have different type signatures, but libev 5303e3a38431SPaul Bohmcalls them using an C<ev_watcher *> internally. 5304e3a38431SPaul Bohm 5305e3a38431SPaul Bohm=item pointer accesses must be thread-atomic 5306e3a38431SPaul Bohm 5307e3a38431SPaul BohmAccessing a pointer value must be atomic, it must both be readable and 5308e3a38431SPaul Bohmwritable in one piece - this is the case on all current architectures. 5309e3a38431SPaul Bohm 5310e3a38431SPaul Bohm=item C<sig_atomic_t volatile> must be thread-atomic as well 5311e3a38431SPaul Bohm 5312e3a38431SPaul BohmThe type C<sig_atomic_t volatile> (or whatever is defined as 5313e3a38431SPaul BohmC<EV_ATOMIC_T>) must be atomic with respect to accesses from different 5314e3a38431SPaul Bohmthreads. This is not part of the specification for C<sig_atomic_t>, but is 5315e3a38431SPaul Bohmbelieved to be sufficiently portable. 5316e3a38431SPaul Bohm 5317e3a38431SPaul Bohm=item C<sigprocmask> must work in a threaded environment 5318e3a38431SPaul Bohm 5319e3a38431SPaul BohmLibev uses C<sigprocmask> to temporarily block signals. This is not 5320e3a38431SPaul Bohmallowed in a threaded program (C<pthread_sigmask> has to be used). Typical 5321e3a38431SPaul Bohmpthread implementations will either allow C<sigprocmask> in the "main 5322e3a38431SPaul Bohmthread" or will block signals process-wide, both behaviours would 5323e3a38431SPaul Bohmbe compatible with libev. Interaction between C<sigprocmask> and 5324e3a38431SPaul BohmC<pthread_sigmask> could complicate things, however. 5325e3a38431SPaul Bohm 5326e3a38431SPaul BohmThe most portable way to handle signals is to block signals in all threads 5327*93823e6cSPaul Bohmexcept the initial one, and run the signal handling loop in the initial 5328*93823e6cSPaul Bohmthread as well. 5329e3a38431SPaul Bohm 5330e3a38431SPaul Bohm=item C<long> must be large enough for common memory allocation sizes 5331e3a38431SPaul Bohm 5332e3a38431SPaul BohmTo improve portability and simplify its API, libev uses C<long> internally 5333e3a38431SPaul Bohminstead of C<size_t> when allocating its data structures. On non-POSIX 5334e3a38431SPaul Bohmsystems (Microsoft...) this might be unexpectedly low, but is still at 5335e3a38431SPaul Bohmleast 31 bits everywhere, which is enough for hundreds of millions of 5336e3a38431SPaul Bohmwatchers. 5337e3a38431SPaul Bohm 5338e3a38431SPaul Bohm=item C<double> must hold a time value in seconds with enough accuracy 5339e3a38431SPaul Bohm 5340e3a38431SPaul BohmThe type C<double> is used to represent timestamps. It is required to 5341e3a38431SPaul Bohmhave at least 51 bits of mantissa (and 9 bits of exponent), which is 5342e3a38431SPaul Bohmgood enough for at least into the year 4000 with millisecond accuracy 5343e3a38431SPaul Bohm(the design goal for libev). This requirement is overfulfilled by 5344*93823e6cSPaul Bohmimplementations using IEEE 754, which is basically all existing ones. 5345*93823e6cSPaul Bohm 5346*93823e6cSPaul BohmWith IEEE 754 doubles, you get microsecond accuracy until at least the 5347*93823e6cSPaul Bohmyear 2255 (and millisecond accuracy till the year 287396 - by then, libev 5348*93823e6cSPaul Bohmis either obsolete or somebody patched it to use C<long double> or 5349*93823e6cSPaul Bohmsomething like that, just kidding). 5350e3a38431SPaul Bohm 5351e3a38431SPaul Bohm=back 5352e3a38431SPaul Bohm 5353e3a38431SPaul BohmIf you know of other additional requirements drop me a note. 5354e3a38431SPaul Bohm 5355e3a38431SPaul Bohm 5356e3a38431SPaul Bohm=head1 ALGORITHMIC COMPLEXITIES 5357e3a38431SPaul Bohm 5358e3a38431SPaul BohmIn this section the complexities of (many of) the algorithms used inside 5359e3a38431SPaul Bohmlibev will be documented. For complexity discussions about backends see 5360e3a38431SPaul Bohmthe documentation for C<ev_default_init>. 5361e3a38431SPaul Bohm 5362e3a38431SPaul BohmAll of the following are about amortised time: If an array needs to be 5363e3a38431SPaul Bohmextended, libev needs to realloc and move the whole array, but this 5364e3a38431SPaul Bohmhappens asymptotically rarer with higher number of elements, so O(1) might 5365e3a38431SPaul Bohmmean that libev does a lengthy realloc operation in rare cases, but on 5366e3a38431SPaul Bohmaverage it is much faster and asymptotically approaches constant time. 5367e3a38431SPaul Bohm 5368e3a38431SPaul Bohm=over 4 5369e3a38431SPaul Bohm 5370e3a38431SPaul Bohm=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) 5371e3a38431SPaul Bohm 5372e3a38431SPaul BohmThis means that, when you have a watcher that triggers in one hour and 5373e3a38431SPaul Bohmthere are 100 watchers that would trigger before that, then inserting will 5374e3a38431SPaul Bohmhave to skip roughly seven (C<ld 100>) of these watchers. 5375e3a38431SPaul Bohm 5376e3a38431SPaul Bohm=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) 5377e3a38431SPaul Bohm 5378e3a38431SPaul BohmThat means that changing a timer costs less than removing/adding them, 5379e3a38431SPaul Bohmas only the relative motion in the event queue has to be paid for. 5380e3a38431SPaul Bohm 5381e3a38431SPaul Bohm=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) 5382e3a38431SPaul Bohm 5383e3a38431SPaul BohmThese just add the watcher into an array or at the head of a list. 5384e3a38431SPaul Bohm 5385e3a38431SPaul Bohm=item Stopping check/prepare/idle/fork/async watchers: O(1) 5386e3a38431SPaul Bohm 5387e3a38431SPaul Bohm=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) 5388e3a38431SPaul Bohm 5389e3a38431SPaul BohmThese watchers are stored in lists, so they need to be walked to find the 5390e3a38431SPaul Bohmcorrect watcher to remove. The lists are usually short (you don't usually 5391e3a38431SPaul Bohmhave many watchers waiting for the same fd or signal: one is typical, two 5392e3a38431SPaul Bohmis rare). 5393e3a38431SPaul Bohm 5394e3a38431SPaul Bohm=item Finding the next timer in each loop iteration: O(1) 5395e3a38431SPaul Bohm 5396e3a38431SPaul BohmBy virtue of using a binary or 4-heap, the next timer is always found at a 5397e3a38431SPaul Bohmfixed position in the storage array. 5398e3a38431SPaul Bohm 5399e3a38431SPaul Bohm=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) 5400e3a38431SPaul Bohm 5401e3a38431SPaul BohmA change means an I/O watcher gets started or stopped, which requires 5402e3a38431SPaul Bohmlibev to recalculate its status (and possibly tell the kernel, depending 5403e3a38431SPaul Bohmon backend and whether C<ev_io_set> was used). 5404e3a38431SPaul Bohm 5405e3a38431SPaul Bohm=item Activating one watcher (putting it into the pending state): O(1) 5406e3a38431SPaul Bohm 5407e3a38431SPaul Bohm=item Priority handling: O(number_of_priorities) 5408e3a38431SPaul Bohm 5409e3a38431SPaul BohmPriorities are implemented by allocating some space for each 5410e3a38431SPaul Bohmpriority. When doing priority-based operations, libev usually has to 5411e3a38431SPaul Bohmlinearly search all the priorities, but starting/stopping and activating 5412e3a38431SPaul Bohmwatchers becomes O(1) with respect to priority handling. 5413e3a38431SPaul Bohm 5414e3a38431SPaul Bohm=item Sending an ev_async: O(1) 5415e3a38431SPaul Bohm 5416e3a38431SPaul Bohm=item Processing ev_async_send: O(number_of_async_watchers) 5417e3a38431SPaul Bohm 5418e3a38431SPaul Bohm=item Processing signals: O(max_signal_number) 5419e3a38431SPaul Bohm 5420e3a38431SPaul BohmSending involves a system call I<iff> there were no other C<ev_async_send> 5421*93823e6cSPaul Bohmcalls in the current loop iteration and the loop is currently 5422*93823e6cSPaul Bohmblocked. Checking for async and signal events involves iterating over all 5423*93823e6cSPaul Bohmrunning async watchers or all signal numbers. 5424e3a38431SPaul Bohm 5425e3a38431SPaul Bohm=back 5426e3a38431SPaul Bohm 5427e3a38431SPaul Bohm 5428e3a38431SPaul Bohm=head1 PORTING FROM LIBEV 3.X TO 4.X 5429e3a38431SPaul Bohm 5430e3a38431SPaul BohmThe major version 4 introduced some incompatible changes to the API. 5431e3a38431SPaul Bohm 5432e3a38431SPaul BohmAt the moment, the C<ev.h> header file provides compatibility definitions 5433e3a38431SPaul Bohmfor all changes, so most programs should still compile. The compatibility 5434e3a38431SPaul Bohmlayer might be removed in later versions of libev, so better update to the 5435e3a38431SPaul Bohmnew API early than late. 5436e3a38431SPaul Bohm 5437e3a38431SPaul Bohm=over 4 5438e3a38431SPaul Bohm 5439e3a38431SPaul Bohm=item C<EV_COMPAT3> backwards compatibility mechanism 5440e3a38431SPaul Bohm 5441e3a38431SPaul BohmThe backward compatibility mechanism can be controlled by 5442*93823e6cSPaul BohmC<EV_COMPAT3>. See L</"PREPROCESSOR SYMBOLS/MACROS"> in the L</EMBEDDING> 5443e3a38431SPaul Bohmsection. 5444e3a38431SPaul Bohm 5445e3a38431SPaul Bohm=item C<ev_default_destroy> and C<ev_default_fork> have been removed 5446e3a38431SPaul Bohm 5447e3a38431SPaul BohmThese calls can be replaced easily by their C<ev_loop_xxx> counterparts: 5448e3a38431SPaul Bohm 5449e3a38431SPaul Bohm ev_loop_destroy (EV_DEFAULT_UC); 5450e3a38431SPaul Bohm ev_loop_fork (EV_DEFAULT); 5451e3a38431SPaul Bohm 5452e3a38431SPaul Bohm=item function/symbol renames 5453e3a38431SPaul Bohm 5454e3a38431SPaul BohmA number of functions and symbols have been renamed: 5455e3a38431SPaul Bohm 5456e3a38431SPaul Bohm ev_loop => ev_run 5457e3a38431SPaul Bohm EVLOOP_NONBLOCK => EVRUN_NOWAIT 5458e3a38431SPaul Bohm EVLOOP_ONESHOT => EVRUN_ONCE 5459e3a38431SPaul Bohm 5460e3a38431SPaul Bohm ev_unloop => ev_break 5461e3a38431SPaul Bohm EVUNLOOP_CANCEL => EVBREAK_CANCEL 5462e3a38431SPaul Bohm EVUNLOOP_ONE => EVBREAK_ONE 5463e3a38431SPaul Bohm EVUNLOOP_ALL => EVBREAK_ALL 5464e3a38431SPaul Bohm 5465e3a38431SPaul Bohm EV_TIMEOUT => EV_TIMER 5466e3a38431SPaul Bohm 5467e3a38431SPaul Bohm ev_loop_count => ev_iteration 5468e3a38431SPaul Bohm ev_loop_depth => ev_depth 5469e3a38431SPaul Bohm ev_loop_verify => ev_verify 5470e3a38431SPaul Bohm 5471e3a38431SPaul BohmMost functions working on C<struct ev_loop> objects don't have an 5472e3a38431SPaul BohmC<ev_loop_> prefix, so it was removed; C<ev_loop>, C<ev_unloop> and 5473e3a38431SPaul Bohmassociated constants have been renamed to not collide with the C<struct 5474e3a38431SPaul Bohmev_loop> anymore and C<EV_TIMER> now follows the same naming scheme 5475e3a38431SPaul Bohmas all other watcher types. Note that C<ev_loop_fork> is still called 5476e3a38431SPaul BohmC<ev_loop_fork> because it would otherwise clash with the C<ev_fork> 5477e3a38431SPaul Bohmtypedef. 5478e3a38431SPaul Bohm 5479e3a38431SPaul Bohm=item C<EV_MINIMAL> mechanism replaced by C<EV_FEATURES> 5480e3a38431SPaul Bohm 5481e3a38431SPaul BohmThe preprocessor symbol C<EV_MINIMAL> has been replaced by a different 5482e3a38431SPaul Bohmmechanism, C<EV_FEATURES>. Programs using C<EV_MINIMAL> usually compile 5483e3a38431SPaul Bohmand work, but the library code will of course be larger. 5484e3a38431SPaul Bohm 5485e3a38431SPaul Bohm=back 5486e3a38431SPaul Bohm 5487e3a38431SPaul Bohm 5488e3a38431SPaul Bohm=head1 GLOSSARY 5489e3a38431SPaul Bohm 5490e3a38431SPaul Bohm=over 4 5491e3a38431SPaul Bohm 5492e3a38431SPaul Bohm=item active 5493e3a38431SPaul Bohm 5494e3a38431SPaul BohmA watcher is active as long as it has been started and not yet stopped. 5495*93823e6cSPaul BohmSee L</WATCHER STATES> for details. 5496e3a38431SPaul Bohm 5497e3a38431SPaul Bohm=item application 5498e3a38431SPaul Bohm 5499e3a38431SPaul BohmIn this document, an application is whatever is using libev. 5500e3a38431SPaul Bohm 5501e3a38431SPaul Bohm=item backend 5502e3a38431SPaul Bohm 5503e3a38431SPaul BohmThe part of the code dealing with the operating system interfaces. 5504e3a38431SPaul Bohm 5505e3a38431SPaul Bohm=item callback 5506e3a38431SPaul Bohm 5507e3a38431SPaul BohmThe address of a function that is called when some event has been 5508e3a38431SPaul Bohmdetected. Callbacks are being passed the event loop, the watcher that 5509e3a38431SPaul Bohmreceived the event, and the actual event bitset. 5510e3a38431SPaul Bohm 5511e3a38431SPaul Bohm=item callback/watcher invocation 5512e3a38431SPaul Bohm 5513e3a38431SPaul BohmThe act of calling the callback associated with a watcher. 5514e3a38431SPaul Bohm 5515e3a38431SPaul Bohm=item event 5516e3a38431SPaul Bohm 5517e3a38431SPaul BohmA change of state of some external event, such as data now being available 5518e3a38431SPaul Bohmfor reading on a file descriptor, time having passed or simply not having 5519e3a38431SPaul Bohmany other events happening anymore. 5520e3a38431SPaul Bohm 5521e3a38431SPaul BohmIn libev, events are represented as single bits (such as C<EV_READ> or 5522e3a38431SPaul BohmC<EV_TIMER>). 5523e3a38431SPaul Bohm 5524e3a38431SPaul Bohm=item event library 5525e3a38431SPaul Bohm 5526e3a38431SPaul BohmA software package implementing an event model and loop. 5527e3a38431SPaul Bohm 5528e3a38431SPaul Bohm=item event loop 5529e3a38431SPaul Bohm 5530e3a38431SPaul BohmAn entity that handles and processes external events and converts them 5531e3a38431SPaul Bohminto callback invocations. 5532e3a38431SPaul Bohm 5533e3a38431SPaul Bohm=item event model 5534e3a38431SPaul Bohm 5535e3a38431SPaul BohmThe model used to describe how an event loop handles and processes 5536e3a38431SPaul Bohmwatchers and events. 5537e3a38431SPaul Bohm 5538e3a38431SPaul Bohm=item pending 5539e3a38431SPaul Bohm 5540e3a38431SPaul BohmA watcher is pending as soon as the corresponding event has been 5541*93823e6cSPaul Bohmdetected. See L</WATCHER STATES> for details. 5542e3a38431SPaul Bohm 5543e3a38431SPaul Bohm=item real time 5544e3a38431SPaul Bohm 5545e3a38431SPaul BohmThe physical time that is observed. It is apparently strictly monotonic :) 5546e3a38431SPaul Bohm 5547e3a38431SPaul Bohm=item wall-clock time 5548e3a38431SPaul Bohm 5549e3a38431SPaul BohmThe time and date as shown on clocks. Unlike real time, it can actually 5550e3a38431SPaul Bohmbe wrong and jump forwards and backwards, e.g. when you adjust your 5551e3a38431SPaul Bohmclock. 5552e3a38431SPaul Bohm 5553e3a38431SPaul Bohm=item watcher 5554e3a38431SPaul Bohm 5555e3a38431SPaul BohmA data structure that describes interest in certain events. Watchers need 5556e3a38431SPaul Bohmto be started (attached to an event loop) before they can receive events. 5557e3a38431SPaul Bohm 5558e3a38431SPaul Bohm=back 5559e3a38431SPaul Bohm 5560e3a38431SPaul Bohm=head1 AUTHOR 5561e3a38431SPaul Bohm 5562e3a38431SPaul BohmMarc Lehmann <[email protected]>, with repeated corrections by Mikael 5563e3a38431SPaul BohmMagnusson and Emanuele Giaquinta, and minor corrections by many others. 5564e3a38431SPaul Bohm 5565