PageRenderTime 55ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/src/libeio/eio.3

http://github.com/jacksonh/manos
Unknown | 3428 lines | 3428 code | 0 blank | 0 comment | 0 complexity | c88e90ef82c5bf482c68de74b264d7ca MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05)
   2.\"
   3.\" Standard preamble:
   4.\" ========================================================================
   5.de Sh \" Subsection heading
   6.br
   7.if t .Sp
   8.ne 5
   9.PP
  10\fB\\$1\fR
  11.PP
  12..
  13.de Sp \" Vertical space (when we can't use .PP)
  14.if t .sp .5v
  15.if n .sp
  16..
  17.de Vb \" Begin verbatim text
  18.ft CW
  19.nf
  20.ne \\$1
  21..
  22.de Ve \" End verbatim text
  23.ft R
  24.fi
  25..
  26.\" Set up some character translations and predefined strings.  \*(-- will
  27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
  28.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
  29.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
  30.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
  31.\" nothing in troff, for use with C<>.
  32.tr \(*W-
  33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  34.ie n \{\
  35.    ds -- \(*W-
  36.    ds PI pi
  37.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
  38.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
  39.    ds L" ""
  40.    ds R" ""
  41.    ds C` ""
  42.    ds C' ""
  43'br\}
  44.el\{\
  45.    ds -- \|\(em\|
  46.    ds PI \(*p
  47.    ds L" ``
  48.    ds R" ''
  49'br\}
  50.\"
  51.\" Escape single quotes in literal strings from groff's Unicode transform.
  52.ie \n(.g .ds Aq \(aq
  53.el       .ds Aq '
  54.\"
  55.\" If the F register is turned on, we'll generate index entries on stderr for
  56.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
  57.\" entries marked with X<> in POD.  Of course, you'll have to process the
  58.\" output yourself in some meaningful fashion.
  59.ie \nF \{\
  60.    de IX
  61.    tm Index:\\$1\t\\n%\t"\\$2"
  62..
  63.    nr % 0
  64.    rr F
  65.\}
  66.el \{\
  67.    de IX
  68..
  69.\}
  70.\"
  71.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
  72.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
  73.    \" fudge factors for nroff and troff
  74.if n \{\
  75.    ds #H 0
  76.    ds #V .8m
  77.    ds #F .3m
  78.    ds #[ \f1
  79.    ds #] \fP
  80.\}
  81.if t \{\
  82.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  83.    ds #V .6m
  84.    ds #F 0
  85.    ds #[ \&
  86.    ds #] \&
  87.\}
  88.    \" simple accents for nroff and troff
  89.if n \{\
  90.    ds ' \&
  91.    ds ` \&
  92.    ds ^ \&
  93.    ds , \&
  94.    ds ~ ~
  95.    ds /
  96.\}
  97.if t \{\
  98.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  99.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
 100.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
 101.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
 102.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
 103.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
 104.\}
 105.    \" troff and (daisy-wheel) nroff accents
 106.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
 107.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
 108.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
 109.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
 110.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
 111.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
 112.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
 113.ds ae a\h'-(\w'a'u*4/10)'e
 114.ds Ae A\h'-(\w'A'u*4/10)'E
 115.    \" corrections for vroff
 116.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
 117.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
 118.    \" for low resolution devices (crt and lpr)
 119.if \n(.H>23 .if \n(.V>19 \
 120\{\
 121.    ds : e
 122.    ds 8 ss
 123.    ds o a
 124.    ds d- d\h'-1'\(ga
 125.    ds D- D\h'-1'\(hy
 126.    ds th \o'bp'
 127.    ds Th \o'LP'
 128.    ds ae ae
 129.    ds Ae AE
 130.\}
 131.rm #[ #] #H #V #F C
 132.\" ========================================================================
 133.\"
 134.IX Title "LIBEIO 3"
 135.TH LIBEIO 3 "2008-05-11" "libeio-1.0" "libeio - truly asynchronous POSIX I/O"
 136.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
 137.\" way too many mistakes in technical documents.
 138.if n .ad l
 139.nh
 140.SH "NAME"
 141libev \- a high performance full\-featured event loop written in C
 142.SH "SYNOPSIS"
 143.IX Header "SYNOPSIS"
 144.Vb 1
 145\&  #include <ev.h>
 146.Ve
 147.Sh "\s-1EXAMPLE\s0 \s-1PROGRAM\s0"
 148.IX Subsection "EXAMPLE PROGRAM"
 149.Vb 2
 150\&  // a single header file is required
 151\&  #include <ev.h>
 152\&
 153\&  // every watcher type has its own typedef\*(Aqd struct
 154\&  // with the name ev_<type>
 155\&  ev_io stdin_watcher;
 156\&  ev_timer timeout_watcher;
 157\&
 158\&  // all watcher callbacks have a similar signature
 159\&  // this callback is called when data is readable on stdin
 160\&  static void
 161\&  stdin_cb (EV_P_ struct ev_io *w, int revents)
 162\&  {
 163\&    puts ("stdin ready");
 164\&    // for one\-shot events, one must manually stop the watcher
 165\&    // with its corresponding stop function.
 166\&    ev_io_stop (EV_A_ w);
 167\&
 168\&    // this causes all nested ev_loop\*(Aqs to stop iterating
 169\&    ev_unloop (EV_A_ EVUNLOOP_ALL);
 170\&  }
 171\&
 172\&  // another callback, this time for a time\-out
 173\&  static void
 174\&  timeout_cb (EV_P_ struct ev_timer *w, int revents)
 175\&  {
 176\&    puts ("timeout");
 177\&    // this causes the innermost ev_loop to stop iterating
 178\&    ev_unloop (EV_A_ EVUNLOOP_ONE);
 179\&  }
 180\&
 181\&  int
 182\&  main (void)
 183\&  {
 184\&    // use the default event loop unless you have special needs
 185\&    struct ev_loop *loop = ev_default_loop (0);
 186\&
 187\&    // initialise an io watcher, then start it
 188\&    // this one will watch for stdin to become readable
 189\&    ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
 190\&    ev_io_start (loop, &stdin_watcher);
 191\&
 192\&    // initialise a timer watcher, then start it
 193\&    // simple non\-repeating 5.5 second timeout
 194\&    ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
 195\&    ev_timer_start (loop, &timeout_watcher);
 196\&
 197\&    // now wait for events to arrive
 198\&    ev_loop (loop, 0);
 199\&
 200\&    // unloop was called, so exit
 201\&    return 0;
 202\&  }
 203.Ve
 204.SH "DESCRIPTION"
 205.IX Header "DESCRIPTION"
 206The newest version of this document is also available as an html-formatted
 207web page you might find easier to navigate when reading it for the first
 208time: <http://cvs.schmorp.de/libev/ev.html>.
 209.PP
 210Libev is an event loop: you register interest in certain events (such as a
 211file descriptor being readable or a timeout occurring), and it will manage
 212these event sources and provide your program with events.
 213.PP
 214To do this, it must take more or less complete control over your process
 215(or thread) by executing the \fIevent loop\fR handler, and will then
 216communicate events via a callback mechanism.
 217.PP
 218You register interest in certain events by registering so-called \fIevent
 219watchers\fR, which are relatively small C structures you initialise with the
 220details of the event, and then hand it over to libev by \fIstarting\fR the
 221watcher.
 222.Sh "\s-1FEATURES\s0"
 223.IX Subsection "FEATURES"
 224Libev supports \f(CW\*(C`select\*(C'\fR, \f(CW\*(C`poll\*(C'\fR, the Linux-specific \f(CW\*(C`epoll\*(C'\fR, the
 225BSD-specific \f(CW\*(C`kqueue\*(C'\fR and the Solaris-specific event port mechanisms
 226for file descriptor events (\f(CW\*(C`ev_io\*(C'\fR), the Linux \f(CW\*(C`inotify\*(C'\fR interface
 227(for \f(CW\*(C`ev_stat\*(C'\fR), relative timers (\f(CW\*(C`ev_timer\*(C'\fR), absolute timers
 228with customised rescheduling (\f(CW\*(C`ev_periodic\*(C'\fR), synchronous signals
 229(\f(CW\*(C`ev_signal\*(C'\fR), process status change events (\f(CW\*(C`ev_child\*(C'\fR), and event
 230watchers dealing with the event loop mechanism itself (\f(CW\*(C`ev_idle\*(C'\fR,
 231\&\f(CW\*(C`ev_embed\*(C'\fR, \f(CW\*(C`ev_prepare\*(C'\fR and \f(CW\*(C`ev_check\*(C'\fR watchers) as well as
 232file watchers (\f(CW\*(C`ev_stat\*(C'\fR) and even limited support for fork events
 233(\f(CW\*(C`ev_fork\*(C'\fR).
 234.PP
 235It also is quite fast (see this
 236benchmark comparing it to libevent
 237for example).
 238.Sh "\s-1CONVENTIONS\s0"
 239.IX Subsection "CONVENTIONS"
 240Libev is very configurable. In this manual the default (and most common)
 241configuration will be described, which supports multiple event loops. For
 242more info about various configuration options please have a look at
 243\&\fB\s-1EMBED\s0\fR section in this manual. If libev was configured without support
 244for multiple event loops, then all functions taking an initial argument of
 245name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`struct ev_loop *\*(C'\fR) will not have
 246this argument.
 247.Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0"
 248.IX Subsection "TIME REPRESENTATION"
 249Libev represents time as a single floating point number, representing the
 250(fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near
 251the beginning of 1970, details are complicated, don't ask). This type is
 252called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases
 253to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on
 254it, you should treat it as some floatingpoint value. Unlike the name
 255component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences
 256throughout libev.
 257.SH "GLOBAL FUNCTIONS"
 258.IX Header "GLOBAL FUNCTIONS"
 259These functions can be called anytime, even before initialising the
 260library in any way.
 261.IP "ev_tstamp ev_time ()" 4
 262.IX Item "ev_tstamp ev_time ()"
 263Returns the current time as libev would use it. Please note that the
 264\&\f(CW\*(C`ev_now\*(C'\fR function is usually faster and also often returns the timestamp
 265you actually want to know.
 266.IP "ev_sleep (ev_tstamp interval)" 4
 267.IX Item "ev_sleep (ev_tstamp interval)"
 268Sleep for the given interval: The current thread will be blocked until
 269either it is interrupted or the given time interval has passed. Basically
 270this is a subsecond-resolution \f(CW\*(C`sleep ()\*(C'\fR.
 271.IP "int ev_version_major ()" 4
 272.IX Item "int ev_version_major ()"
 273.PD 0
 274.IP "int ev_version_minor ()" 4
 275.IX Item "int ev_version_minor ()"
 276.PD
 277You can find out the major and minor \s-1ABI\s0 version numbers of the library
 278you linked against by calling the functions \f(CW\*(C`ev_version_major\*(C'\fR and
 279\&\f(CW\*(C`ev_version_minor\*(C'\fR. If you want, you can compare against the global
 280symbols \f(CW\*(C`EV_VERSION_MAJOR\*(C'\fR and \f(CW\*(C`EV_VERSION_MINOR\*(C'\fR, which specify the
 281version of the library your program was compiled against.
 282.Sp
 283These version numbers refer to the \s-1ABI\s0 version of the library, not the
 284release version.
 285.Sp
 286Usually, it's a good idea to terminate if the major versions mismatch,
 287as this indicates an incompatible change. Minor versions are usually
 288compatible to older versions, so a larger minor version alone is usually
 289not a problem.
 290.Sp
 291Example: Make sure we haven't accidentally been linked against the wrong
 292version.
 293.Sp
 294.Vb 3
 295\&  assert (("libev version mismatch",
 296\&           ev_version_major () == EV_VERSION_MAJOR
 297\&           && ev_version_minor () >= EV_VERSION_MINOR));
 298.Ve
 299.IP "unsigned int ev_supported_backends ()" 4
 300.IX Item "unsigned int ev_supported_backends ()"
 301Return the set of all backends (i.e. their corresponding \f(CW\*(C`EV_BACKEND_*\*(C'\fR
 302value) compiled into this binary of libev (independent of their
 303availability on the system you are running on). See \f(CW\*(C`ev_default_loop\*(C'\fR for
 304a description of the set values.
 305.Sp
 306Example: make sure we have the epoll method, because yeah this is cool and
 307a must have and can we have a torrent of it please!!!11
 308.Sp
 309.Vb 2
 310\&  assert (("sorry, no epoll, no sex",
 311\&           ev_supported_backends () & EVBACKEND_EPOLL));
 312.Ve
 313.IP "unsigned int ev_recommended_backends ()" 4
 314.IX Item "unsigned int ev_recommended_backends ()"
 315Return the set of all backends compiled into this binary of libev and also
 316recommended for this platform. This set is often smaller than the one
 317returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on
 318most BSDs and will not be autodetected unless you explicitly request it
 319(assuming you know what you are doing). This is the set of backends that
 320libev will probe for if you specify no backends explicitly.
 321.IP "unsigned int ev_embeddable_backends ()" 4
 322.IX Item "unsigned int ev_embeddable_backends ()"
 323Returns the set of backends that are embeddable in other event loops. This
 324is the theoretical, all-platform, value. To find which backends
 325might be supported on the current system, you would need to look at
 326\&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for
 327recommended ones.
 328.Sp
 329See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
 330.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4
 331.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))"
 332Sets the allocation function to use (the prototype is similar \- the
 333semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is
 334used to allocate and free memory (no surprises here). If it returns zero
 335when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort
 336or take some potentially destructive action.
 337.Sp
 338Since some systems (at least OpenBSD and Darwin) fail to implement
 339correct \f(CW\*(C`realloc\*(C'\fR semantics, libev will use a wrapper around the system
 340\&\f(CW\*(C`realloc\*(C'\fR and \f(CW\*(C`free\*(C'\fR functions by default.
 341.Sp
 342You could override this function in high-availability programs to, say,
 343free some memory if it cannot allocate memory, to use a special allocator,
 344or even to sleep a while and retry until some memory is available.
 345.Sp
 346Example: Replace the libev allocator with one that waits a bit and then
 347retries (example requires a standards-compliant \f(CW\*(C`realloc\*(C'\fR).
 348.Sp
 349.Vb 6
 350\&   static void *
 351\&   persistent_realloc (void *ptr, size_t size)
 352\&   {
 353\&     for (;;)
 354\&       {
 355\&         void *newptr = realloc (ptr, size);
 356\&
 357\&         if (newptr)
 358\&           return newptr;
 359\&
 360\&         sleep (60);
 361\&       }
 362\&   }
 363\&
 364\&   ...
 365\&   ev_set_allocator (persistent_realloc);
 366.Ve
 367.IP "ev_set_syserr_cb (void (*cb)(const char *msg));" 4
 368.IX Item "ev_set_syserr_cb (void (*cb)(const char *msg));"
 369Set the callback function to call on a retryable syscall error (such
 370as failed select, poll, epoll_wait). The message is a printable string
 371indicating the system call or subsystem causing the problem. If this
 372callback is set, then libev will expect it to remedy the sitution, no
 373matter what, when it returns. That is, libev will generally retry the
 374requested operation, or, if the condition doesn't go away, do bad stuff
 375(such as abort).
 376.Sp
 377Example: This is basically the same thing that libev does internally, too.
 378.Sp
 379.Vb 6
 380\&   static void
 381\&   fatal_error (const char *msg)
 382\&   {
 383\&     perror (msg);
 384\&     abort ();
 385\&   }
 386\&
 387\&   ...
 388\&   ev_set_syserr_cb (fatal_error);
 389.Ve
 390.SH "FUNCTIONS CONTROLLING THE EVENT LOOP"
 391.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP"
 392An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two
 393types of such loops, the \fIdefault\fR loop, which supports signals and child
 394events, and dynamically created loops which do not.
 395.IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4
 396.IX Item "struct ev_loop *ev_default_loop (unsigned int flags)"
 397This will initialise the default event loop if it hasn't been initialised
 398yet and return it. If the default loop could not be initialised, returns
 399false. If it already was initialised it simply returns it (and ignores the
 400flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards).
 401.Sp
 402If you don't know what event loop to use, use the one returned from this
 403function.
 404.Sp
 405Note that this function is \fInot\fR thread-safe, so if you want to use it
 406from multiple threads, you have to lock (note also that this is unlikely,
 407as loops cannot bes hared easily between threads anyway).
 408.Sp
 409The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and
 410\&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler
 411for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your app you can either
 412create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you
 413can simply overwrite the \f(CW\*(C`SIGCHLD\*(C'\fR signal handler \fIafter\fR calling
 414\&\f(CW\*(C`ev_default_init\*(C'\fR.
 415.Sp
 416The flags argument can be used to specify special behaviour or specific
 417backends to use, and is usually specified as \f(CW0\fR (or \f(CW\*(C`EVFLAG_AUTO\*(C'\fR).
 418.Sp
 419The following flags are supported:
 420.RS 4
 421.ie n .IP """EVFLAG_AUTO""" 4
 422.el .IP "\f(CWEVFLAG_AUTO\fR" 4
 423.IX Item "EVFLAG_AUTO"
 424The default flags value. Use this if you have no clue (it's the right
 425thing, believe me).
 426.ie n .IP """EVFLAG_NOENV""" 4
 427.el .IP "\f(CWEVFLAG_NOENV\fR" 4
 428.IX Item "EVFLAG_NOENV"
 429If this flag bit is ored into the flag value (or the program runs setuid
 430or setgid) then libev will \fInot\fR look at the environment variable
 431\&\f(CW\*(C`LIBEV_FLAGS\*(C'\fR. Otherwise (the default), this environment variable will
 432override the flags completely if it is found in the environment. This is
 433useful to try out specific backends to test their performance, or to work
 434around bugs.
 435.ie n .IP """EVFLAG_FORKCHECK""" 4
 436.el .IP "\f(CWEVFLAG_FORKCHECK\fR" 4
 437.IX Item "EVFLAG_FORKCHECK"
 438Instead of calling \f(CW\*(C`ev_default_fork\*(C'\fR or \f(CW\*(C`ev_loop_fork\*(C'\fR manually after
 439a fork, you can also make libev check for a fork in each iteration by
 440enabling this flag.
 441.Sp
 442This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop,
 443and thus this might slow down your event loop if you do a lot of loop
 444iterations and little real work, but is usually not noticeable (on my
 445GNU/Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence
 446without a syscall and thus \fIvery\fR fast, but my GNU/Linux system also has
 447\&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster).
 448.Sp
 449The big advantage of this flag is that you can forget about fork (and
 450forget about forgetting to tell libev about forking) when you use this
 451flag.
 452.Sp
 453This flag setting cannot be overriden or specified in the \f(CW\*(C`LIBEV_FLAGS\*(C'\fR
 454environment variable.
 455.ie n .IP """EVBACKEND_SELECT""  (value 1, portable select backend)" 4
 456.el .IP "\f(CWEVBACKEND_SELECT\fR  (value 1, portable select backend)" 4
 457.IX Item "EVBACKEND_SELECT  (value 1, portable select backend)"
 458This is your standard \fIselect\fR\|(2) backend. Not \fIcompletely\fR standard, as
 459libev tries to roll its own fd_set with no limits on the number of fds,
 460but if that fails, expect a fairly low limit on the number of fds when
 461using this backend. It doesn't scale too well (O(highest_fd)), but its
 462usually the fastest backend for a low number of (low-numbered :) fds.
 463.Sp
 464To get good performance out of this backend you need a high amount of
 465parallelity (most of the file descriptors should be busy). If you are
 466writing a server, you should \f(CW\*(C`accept ()\*(C'\fR in a loop to accept as many
 467connections as possible during one iteration. You might also want to have
 468a look at \f(CW\*(C`ev_set_io_collect_interval ()\*(C'\fR to increase the amount of
 469readyness notifications you get per iteration.
 470.ie n .IP """EVBACKEND_POLL""    (value 2, poll backend, available everywhere except on windows)" 4
 471.el .IP "\f(CWEVBACKEND_POLL\fR    (value 2, poll backend, available everywhere except on windows)" 4
 472.IX Item "EVBACKEND_POLL    (value 2, poll backend, available everywhere except on windows)"
 473And this is your standard \fIpoll\fR\|(2) backend. It's more complicated
 474than select, but handles sparse fds better and has no artificial
 475limit on the number of fds you can use (except it will slow down
 476considerably with a lot of inactive fds). It scales similarly to select,
 477i.e. O(total_fds). See the entry for \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR, above, for
 478performance tips.
 479.ie n .IP """EVBACKEND_EPOLL""   (value 4, Linux)" 4
 480.el .IP "\f(CWEVBACKEND_EPOLL\fR   (value 4, Linux)" 4
 481.IX Item "EVBACKEND_EPOLL   (value 4, Linux)"
 482For few fds, this backend is a bit little slower than poll and select,
 483but it scales phenomenally better. While poll and select usually scale
 484like O(total_fds) where n is the total number of fds (or the highest fd),
 485epoll scales either O(1) or O(active_fds). The epoll design has a number
 486of shortcomings, such as silently dropping events in some hard-to-detect
 487cases and requiring a syscall per fd change, no fork support and bad
 488support for dup.
 489.Sp
 490While stopping, setting and starting an I/O watcher in the same iteration
 491will result in some caching, there is still a syscall per such incident
 492(because the fd could point to a different file description now), so its
 493best to avoid that. Also, \f(CW\*(C`dup ()\*(C'\fR'ed file descriptors might not work
 494very well if you register events for both fds.
 495.Sp
 496Please note that epoll sometimes generates spurious notifications, so you
 497need to use non-blocking I/O or other means to avoid blocking when no data
 498(or space) is available.
 499.Sp
 500Best performance from this backend is achieved by not unregistering all
 501watchers for a file descriptor until it has been closed, if possible, i.e.
 502keep at least one watcher active per fd at all times.
 503.Sp
 504While nominally embeddeble in other event loops, this feature is broken in
 505all kernel versions tested so far.
 506.ie n .IP """EVBACKEND_KQUEUE""  (value 8, most \s-1BSD\s0 clones)" 4
 507.el .IP "\f(CWEVBACKEND_KQUEUE\fR  (value 8, most \s-1BSD\s0 clones)" 4
 508.IX Item "EVBACKEND_KQUEUE  (value 8, most BSD clones)"
 509Kqueue deserves special mention, as at the time of this writing, it
 510was broken on all BSDs except NetBSD (usually it doesn't work reliably
 511with anything but sockets and pipes, except on Darwin, where of course
 512it's completely useless). For this reason it's not being \*(L"autodetected\*(R"
 513unless you explicitly specify it explicitly in the flags (i.e. using
 514\&\f(CW\*(C`EVBACKEND_KQUEUE\*(C'\fR) or libev was compiled on a known-to-be-good (\-enough)
 515system like NetBSD.
 516.Sp
 517You still can embed kqueue into a normal poll or select backend and use it
 518only for sockets (after having made sure that sockets work with kqueue on
 519the target platform). See \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
 520.Sp
 521It scales in the same way as the epoll backend, but the interface to the
 522kernel is more efficient (which says nothing about its actual speed, of
 523course). While stopping, setting and starting an I/O watcher does never
 524cause an extra syscall as with \f(CW\*(C`EVBACKEND_EPOLL\*(C'\fR, it still adds up to
 525two event changes per incident, support for \f(CW\*(C`fork ()\*(C'\fR is very bad and it
 526drops fds silently in similarly hard-to-detect cases.
 527.Sp
 528This backend usually performs well under most conditions.
 529.Sp
 530While nominally embeddable in other event loops, this doesn't work
 531everywhere, so you might need to test for this. And since it is broken
 532almost everywhere, you should only use it when you have a lot of sockets
 533(for which it usually works), by embedding it into another event loop
 534(e.g. \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR) and using it only for
 535sockets.
 536.ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4
 537.el .IP "\f(CWEVBACKEND_DEVPOLL\fR (value 16, Solaris 8)" 4
 538.IX Item "EVBACKEND_DEVPOLL (value 16, Solaris 8)"
 539This is not implemented yet (and might never be, unless you send me an
 540implementation). According to reports, \f(CW\*(C`/dev/poll\*(C'\fR only supports sockets
 541and is not embeddable, which would limit the usefulness of this backend
 542immensely.
 543.ie n .IP """EVBACKEND_PORT""    (value 32, Solaris 10)" 4
 544.el .IP "\f(CWEVBACKEND_PORT\fR    (value 32, Solaris 10)" 4
 545.IX Item "EVBACKEND_PORT    (value 32, Solaris 10)"
 546This uses the Solaris 10 event port mechanism. As with everything on Solaris,
 547it's really slow, but it still scales very well (O(active_fds)).
 548.Sp
 549Please note that solaris event ports can deliver a lot of spurious
 550notifications, so you need to use non-blocking I/O or other means to avoid
 551blocking when no data (or space) is available.
 552.Sp
 553While this backend scales well, it requires one system call per active
 554file descriptor per loop iteration. For small and medium numbers of file
 555descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend
 556might perform better.
 557.Sp
 558On the positive side, ignoring the spurious readyness notifications, this
 559backend actually performed to specification in all tests and is fully
 560embeddable, which is a rare feat among the OS-specific backends.
 561.ie n .IP """EVBACKEND_ALL""" 4
 562.el .IP "\f(CWEVBACKEND_ALL\fR" 4
 563.IX Item "EVBACKEND_ALL"
 564Try all backends (even potentially broken ones that wouldn't be tried
 565with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as
 566\&\f(CW\*(C`EVBACKEND_ALL & ~EVBACKEND_KQUEUE\*(C'\fR.
 567.Sp
 568It is definitely not recommended to use this flag.
 569.RE
 570.RS 4
 571.Sp
 572If one or more of these are ored into the flags value, then only these
 573backends will be tried (in the reverse order as listed here). If none are
 574specified, all backends in \f(CW\*(C`ev_recommended_backends ()\*(C'\fR will be tried.
 575.Sp
 576The most typical usage is like this:
 577.Sp
 578.Vb 2
 579\&  if (!ev_default_loop (0))
 580\&    fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?");
 581.Ve
 582.Sp
 583Restrict libev to the select and poll backends, and do not allow
 584environment settings to be taken into account:
 585.Sp
 586.Vb 1
 587\&  ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV);
 588.Ve
 589.Sp
 590Use whatever libev has to offer, but make sure that kqueue is used if
 591available (warning, breaks stuff, best use only with your own private
 592event loop and only if you know the \s-1OS\s0 supports your types of fds):
 593.Sp
 594.Vb 1
 595\&  ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE);
 596.Ve
 597.RE
 598.IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4
 599.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)"
 600Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is
 601always distinct from the default loop. Unlike the default loop, it cannot
 602handle signal and child watchers, and attempts to do so will be greeted by
 603undefined behaviour (or a failed assertion if assertions are enabled).
 604.Sp
 605Note that this function \fIis\fR thread-safe, and the recommended way to use
 606libev with threads is indeed to create one loop per thread, and using the
 607default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread.
 608.Sp
 609Example: Try to create a event loop that uses epoll and nothing else.
 610.Sp
 611.Vb 3
 612\&  struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
 613\&  if (!epoller)
 614\&    fatal ("no epoll found here, maybe it hides under your chair");
 615.Ve
 616.IP "ev_default_destroy ()" 4
 617.IX Item "ev_default_destroy ()"
 618Destroys the default loop again (frees all memory and kernel state
 619etc.). None of the active event watchers will be stopped in the normal
 620sense, so e.g. \f(CW\*(C`ev_is_active\*(C'\fR might still return true. It is your
 621responsibility to either stop all watchers cleanly yoursef \fIbefore\fR
 622calling this function, or cope with the fact afterwards (which is usually
 623the easiest thing, you can just ignore the watchers and/or \f(CW\*(C`free ()\*(C'\fR them
 624for example).
 625.Sp
 626Note that certain global state, such as signal state, will not be freed by
 627this function, and related watchers (such as signal and child watchers)
 628would need to be stopped manually.
 629.Sp
 630In general it is not advisable to call this function except in the
 631rare occasion where you really need to free e.g. the signal handling
 632pipe fds. If you need dynamically allocated loops it is better to use
 633\&\f(CW\*(C`ev_loop_new\*(C'\fR and \f(CW\*(C`ev_loop_destroy\*(C'\fR).
 634.IP "ev_loop_destroy (loop)" 4
 635.IX Item "ev_loop_destroy (loop)"
 636Like \f(CW\*(C`ev_default_destroy\*(C'\fR, but destroys an event loop created by an
 637earlier call to \f(CW\*(C`ev_loop_new\*(C'\fR.
 638.IP "ev_default_fork ()" 4
 639.IX Item "ev_default_fork ()"
 640This function sets a flag that causes subsequent \f(CW\*(C`ev_loop\*(C'\fR iterations
 641to reinitialise the kernel state for backends that have one. Despite the
 642name, you can call it anytime, but it makes most sense after forking, in
 643the child process (or both child and parent, but that again makes little
 644sense). You \fImust\fR call it in the child before using any of the libev
 645functions, and it will only take effect at the next \f(CW\*(C`ev_loop\*(C'\fR iteration.
 646.Sp
 647On the other hand, you only need to call this function in the child
 648process if and only if you want to use the event library in the child. If
 649you just fork+exec, you don't have to call it at all.
 650.Sp
 651The function itself is quite fast and it's usually not a problem to call
 652it just in case after a fork. To make this easy, the function will fit in
 653quite nicely into a call to \f(CW\*(C`pthread_atfork\*(C'\fR:
 654.Sp
 655.Vb 1
 656\&    pthread_atfork (0, 0, ev_default_fork);
 657.Ve
 658.IP "ev_loop_fork (loop)" 4
 659.IX Item "ev_loop_fork (loop)"
 660Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by
 661\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop
 662after fork, and how you do this is entirely your own problem.
 663.IP "int ev_is_default_loop (loop)" 4
 664.IX Item "int ev_is_default_loop (loop)"
 665Returns true when the given loop actually is the default loop, false otherwise.
 666.IP "unsigned int ev_loop_count (loop)" 4
 667.IX Item "unsigned int ev_loop_count (loop)"
 668Returns the count of loop iterations for the loop, which is identical to
 669the number of times libev did poll for new events. It starts at \f(CW0\fR and
 670happily wraps around with enough iterations.
 671.Sp
 672This value can sometimes be useful as a generation counter of sorts (it
 673\&\*(L"ticks\*(R" the number of loop iterations), as it roughly corresponds with
 674\&\f(CW\*(C`ev_prepare\*(C'\fR and \f(CW\*(C`ev_check\*(C'\fR calls.
 675.IP "unsigned int ev_backend (loop)" 4
 676.IX Item "unsigned int ev_backend (loop)"
 677Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in
 678use.
 679.IP "ev_tstamp ev_now (loop)" 4
 680.IX Item "ev_tstamp ev_now (loop)"
 681Returns the current \*(L"event loop time\*(R", which is the time the event loop
 682received events and started processing them. This timestamp does not
 683change as long as callbacks are being processed, and this is also the base
 684time used for relative timers. You can treat it as the timestamp of the
 685event occurring (or more correctly, libev finding out about it).
 686.IP "ev_loop (loop, int flags)" 4
 687.IX Item "ev_loop (loop, int flags)"
 688Finally, this is it, the event handler. This function usually is called
 689after you initialised all your watchers and you want to start handling
 690events.
 691.Sp
 692If the flags argument is specified as \f(CW0\fR, it will not return until
 693either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called.
 694.Sp
 695Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than
 696relying on all watchers to be stopped when deciding when a program has
 697finished (especially in interactive programs), but having a program that
 698automatically loops as long as it has to and no longer by virtue of
 699relying on its watchers stopping correctly is a thing of beauty.
 700.Sp
 701A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle
 702those events and any outstanding ones, but will not block your process in
 703case there are no events and will return after one iteration of the loop.
 704.Sp
 705A flags value of \f(CW\*(C`EVLOOP_ONESHOT\*(C'\fR will look for new events (waiting if
 706neccessary) and will handle those and any outstanding ones. It will block
 707your process until at least one new event arrives, and will return after
 708one iteration of the loop. This is useful if you are waiting for some
 709external event in conjunction with something not expressible using other
 710libev watchers. However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is
 711usually a better approach for this kind of thing.
 712.Sp
 713Here are the gory details of what \f(CW\*(C`ev_loop\*(C'\fR does:
 714.Sp
 715.Vb 10
 716\&   \- Before the first iteration, call any pending watchers.
 717\&   * If EVFLAG_FORKCHECK was used, check for a fork.
 718\&   \- If a fork was detected, queue and call all fork watchers.
 719\&   \- Queue and call all prepare watchers.
 720\&   \- If we have been forked, recreate the kernel state.
 721\&   \- Update the kernel state with all outstanding changes.
 722\&   \- Update the "event loop time".
 723\&   \- Calculate for how long to sleep or block, if at all
 724\&     (active idle watchers, EVLOOP_NONBLOCK or not having
 725\&     any active watchers at all will result in not sleeping).
 726\&   \- Sleep if the I/O and timer collect interval say so.
 727\&   \- Block the process, waiting for any events.
 728\&   \- Queue all outstanding I/O (fd) events.
 729\&   \- Update the "event loop time" and do time jump handling.
 730\&   \- Queue all outstanding timers.
 731\&   \- Queue all outstanding periodics.
 732\&   \- If no events are pending now, queue all idle watchers.
 733\&   \- Queue all check watchers.
 734\&   \- Call all queued watchers in reverse order (i.e. check watchers first).
 735\&     Signals and child watchers are implemented as I/O watchers, and will
 736\&     be handled here by queueing them when their watcher gets executed.
 737\&   \- If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK
 738\&     were used, or there are no active watchers, return, otherwise
 739\&     continue with step *.
 740.Ve
 741.Sp
 742Example: Queue some jobs and then loop until no events are outstanding
 743anymore.
 744.Sp
 745.Vb 4
 746\&   ... queue jobs here, make sure they register event watchers as long
 747\&   ... as they still have work to do (even an idle watcher will do..)
 748\&   ev_loop (my_loop, 0);
 749\&   ... jobs done. yeah!
 750.Ve
 751.IP "ev_unloop (loop, how)" 4
 752.IX Item "ev_unloop (loop, how)"
 753Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it
 754has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either
 755\&\f(CW\*(C`EVUNLOOP_ONE\*(C'\fR, which will make the innermost \f(CW\*(C`ev_loop\*(C'\fR call return, or
 756\&\f(CW\*(C`EVUNLOOP_ALL\*(C'\fR, which will make all nested \f(CW\*(C`ev_loop\*(C'\fR calls return.
 757.Sp
 758This \*(L"unloop state\*(R" will be cleared when entering \f(CW\*(C`ev_loop\*(C'\fR again.
 759.IP "ev_ref (loop)" 4
 760.IX Item "ev_ref (loop)"
 761.PD 0
 762.IP "ev_unref (loop)" 4
 763.IX Item "ev_unref (loop)"
 764.PD
 765Ref/unref can be used to add or remove a reference count on the event
 766loop: Every watcher keeps one reference, and as long as the reference
 767count is nonzero, \f(CW\*(C`ev_loop\*(C'\fR will not return on its own. If you have
 768a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR from
 769returning, \fIev_unref()\fR after starting, and \fIev_ref()\fR before stopping it. For
 770example, libev itself uses this for its internal signal pipe: It is not
 771visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting if
 772no event watchers registered by it are active. It is also an excellent
 773way to do this for generic recurring timers or from within third-party
 774libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR
 775(but only if the watcher wasn't active before, or was active before,
 776respectively).
 777.Sp
 778Example: Create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR
 779running when nothing else is active.
 780.Sp
 781.Vb 4
 782\&  struct ev_signal exitsig;
 783\&  ev_signal_init (&exitsig, sig_cb, SIGINT);
 784\&  ev_signal_start (loop, &exitsig);
 785\&  evf_unref (loop);
 786.Ve
 787.Sp
 788Example: For some weird reason, unregister the above signal handler again.
 789.Sp
 790.Vb 2
 791\&  ev_ref (loop);
 792\&  ev_signal_stop (loop, &exitsig);
 793.Ve
 794.IP "ev_set_io_collect_interval (loop, ev_tstamp interval)" 4
 795.IX Item "ev_set_io_collect_interval (loop, ev_tstamp interval)"
 796.PD 0
 797.IP "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" 4
 798.IX Item "ev_set_timeout_collect_interval (loop, ev_tstamp interval)"
 799.PD
 800These advanced functions influence the time that libev will spend waiting
 801for events. Both are by default \f(CW0\fR, meaning that libev will try to
 802invoke timer/periodic callbacks and I/O callbacks with minimum latency.
 803.Sp
 804Setting these to a higher value (the \f(CW\*(C`interval\*(C'\fR \fImust\fR be >= \f(CW0\fR)
 805allows libev to delay invocation of I/O and timer/periodic callbacks to
 806increase efficiency of loop iterations.
 807.Sp
 808The background is that sometimes your program runs just fast enough to
 809handle one (or very few) event(s) per loop iteration. While this makes
 810the program responsive, it also wastes a lot of \s-1CPU\s0 time to poll for new
 811events, especially with backends like \f(CW\*(C`select ()\*(C'\fR which have a high
 812overhead for the actual polling but can deliver many events at once.
 813.Sp
 814By setting a higher \fIio collect interval\fR you allow libev to spend more
 815time collecting I/O events, so you can handle more events per iteration,
 816at the cost of increasing latency. Timeouts (both \f(CW\*(C`ev_periodic\*(C'\fR and
 817\&\f(CW\*(C`ev_timer\*(C'\fR) will be not affected. Setting this to a non-null value will
 818introduce an additional \f(CW\*(C`ev_sleep ()\*(C'\fR call into most loop iterations.
 819.Sp
 820Likewise, by setting a higher \fItimeout collect interval\fR you allow libev
 821to spend more time collecting timeouts, at the expense of increased
 822latency (the watcher callback will be called later). \f(CW\*(C`ev_io\*(C'\fR watchers
 823will not be affected. Setting this to a non-null value will not introduce
 824any overhead in libev.
 825.Sp
 826Many (busy) programs can usually benefit by setting the io collect
 827interval to a value near \f(CW0.1\fR or so, which is often enough for
 828interactive servers (of course not for games), likewise for timeouts. It
 829usually doesn't make much sense to set it to a lower value than \f(CW0.01\fR,
 830as this approsaches the timing granularity of most systems.
 831.SH "ANATOMY OF A WATCHER"
 832.IX Header "ANATOMY OF A WATCHER"
 833A watcher is a structure that you create and register to record your
 834interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to
 835become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that:
 836.PP
 837.Vb 5
 838\&  static void my_cb (struct ev_loop *loop, struct ev_io *w, int revents)
 839\&  {
 840\&    ev_io_stop (w);
 841\&    ev_unloop (loop, EVUNLOOP_ALL);
 842\&  }
 843\&
 844\&  struct ev_loop *loop = ev_default_loop (0);
 845\&  struct ev_io stdin_watcher;
 846\&  ev_init (&stdin_watcher, my_cb);
 847\&  ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ);
 848\&  ev_io_start (loop, &stdin_watcher);
 849\&  ev_loop (loop, 0);
 850.Ve
 851.PP
 852As you can see, you are responsible for allocating the memory for your
 853watcher structures (and it is usually a bad idea to do this on the stack,
 854although this can sometimes be quite valid).
 855.PP
 856Each watcher structure must be initialised by a call to \f(CW\*(C`ev_init
 857(watcher *, callback)\*(C'\fR, which expects a callback to be provided. This
 858callback gets invoked each time the event occurs (or, in the case of io
 859watchers, each time the event loop detects that the file descriptor given
 860is readable and/or writable).
 861.PP
 862Each watcher type has its own \f(CW\*(C`ev_<type>_set (watcher *, ...)\*(C'\fR macro
 863with arguments specific to this watcher type. There is also a macro
 864to combine initialisation and setting in one call: \f(CW\*(C`ev_<type>_init
 865(watcher *, callback, ...)\*(C'\fR.
 866.PP
 867To make the watcher actually watch out for events, you have to start it
 868with a watcher-specific start function (\f(CW\*(C`ev_<type>_start (loop, watcher
 869*)\*(C'\fR), and you can stop watching for events at any time by calling the
 870corresponding stop function (\f(CW\*(C`ev_<type>_stop (loop, watcher *)\*(C'\fR.
 871.PP
 872As long as your watcher is active (has been started but not stopped) you
 873must not touch the values stored in it. Most specifically you must never
 874reinitialise it or call its \f(CW\*(C`set\*(C'\fR macro.
 875.PP
 876Each and every callback receives the event loop pointer as first, the
 877registered watcher structure as second, and a bitset of received events as
 878third argument.
 879.PP
 880The received events usually include a single bit per event type received
 881(you can receive multiple events at the same time). The possible bit masks
 882are:
 883.ie n .IP """EV_READ""" 4
 884.el .IP "\f(CWEV_READ\fR" 4
 885.IX Item "EV_READ"
 886.PD 0
 887.ie n .IP """EV_WRITE""" 4
 888.el .IP "\f(CWEV_WRITE\fR" 4
 889.IX Item "EV_WRITE"
 890.PD
 891The file descriptor in the \f(CW\*(C`ev_io\*(C'\fR watcher has become readable and/or
 892writable.
 893.ie n .IP """EV_TIMEOUT""" 4
 894.el .IP "\f(CWEV_TIMEOUT\fR" 4
 895.IX Item "EV_TIMEOUT"
 896The \f(CW\*(C`ev_timer\*(C'\fR watcher has timed out.
 897.ie n .IP """EV_PERIODIC""" 4
 898.el .IP "\f(CWEV_PERIODIC\fR" 4
 899.IX Item "EV_PERIODIC"
 900The \f(CW\*(C`ev_periodic\*(C'\fR watcher has timed out.
 901.ie n .IP """EV_SIGNAL""" 4
 902.el .IP "\f(CWEV_SIGNAL\fR" 4
 903.IX Item "EV_SIGNAL"
 904The signal specified in the \f(CW\*(C`ev_signal\*(C'\fR watcher has been received by a thread.
 905.ie n .IP """EV_CHILD""" 4
 906.el .IP "\f(CWEV_CHILD\fR" 4
 907.IX Item "EV_CHILD"
 908The pid specified in the \f(CW\*(C`ev_child\*(C'\fR watcher has received a status change.
 909.ie n .IP """EV_STAT""" 4
 910.el .IP "\f(CWEV_STAT\fR" 4
 911.IX Item "EV_STAT"
 912The path specified in the \f(CW\*(C`ev_stat\*(C'\fR watcher changed its attributes somehow.
 913.ie n .IP """EV_IDLE""" 4
 914.el .IP "\f(CWEV_IDLE\fR" 4
 915.IX Item "EV_IDLE"
 916The \f(CW\*(C`ev_idle\*(C'\fR watcher has determined that you have nothing better to do.
 917.ie n .IP """EV_PREPARE""" 4
 918.el .IP "\f(CWEV_PREPARE\fR" 4
 919.IX Item "EV_PREPARE"
 920.PD 0
 921.ie n .IP """EV_CHECK""" 4
 922.el .IP "\f(CWEV_CHECK\fR" 4
 923.IX Item "EV_CHECK"
 924.PD
 925All \f(CW\*(C`ev_prepare\*(C'\fR watchers are invoked just \fIbefore\fR \f(CW\*(C`ev_loop\*(C'\fR starts
 926to gather new events, and all \f(CW\*(C`ev_check\*(C'\fR watchers are invoked just after
 927\&\f(CW\*(C`ev_loop\*(C'\fR has gathered them, but before it invokes any callbacks for any
 928received events. Callbacks of both watcher types can start and stop as
 929many watchers as they want, and all of them will be taken into account
 930(for example, a \f(CW\*(C`ev_prepare\*(C'\fR watcher might start an idle watcher to keep
 931\&\f(CW\*(C`ev_loop\*(C'\fR from blocking).
 932.ie n .IP """EV_EMBED""" 4
 933.el .IP "\f(CWEV_EMBED\fR" 4
 934.IX Item "EV_EMBED"
 935The embedded event loop specified in the \f(CW\*(C`ev_embed\*(C'\fR watcher needs attention.
 936.ie n .IP """EV_FORK""" 4
 937.el .IP "\f(CWEV_FORK\fR" 4
 938.IX Item "EV_FORK"
 939The event loop has been resumed in the child process after fork (see
 940\&\f(CW\*(C`ev_fork\*(C'\fR).
 941.ie n .IP """EV_ASYNC""" 4
 942.el .IP "\f(CWEV_ASYNC\fR" 4
 943.IX Item "EV_ASYNC"
 944The given async watcher has been asynchronously notified (see \f(CW\*(C`ev_async\*(C'\fR).
 945.ie n .IP """EV_ERROR""" 4
 946.el .IP "\f(CWEV_ERROR\fR" 4
 947.IX Item "EV_ERROR"
 948An unspecified error has occured, the watcher has been stopped. This might
 949happen because the watcher could not be properly started because libev
 950ran out of memory, a file descriptor was found to be closed or any other
 951problem. You best act on it by reporting the problem and somehow coping
 952with the watcher being stopped.
 953.Sp
 954Libev will usually signal a few \*(L"dummy\*(R" events together with an error,
 955for example it might indicate that a fd is readable or writable, and if
 956your callbacks is well-written it can just attempt the operation and cope
 957with the error from \fIread()\fR or \fIwrite()\fR. This will not work in multithreaded
 958programs, though, so beware.
 959.Sh "\s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0"
 960.IX Subsection "GENERIC WATCHER FUNCTIONS"
 961In the following description, \f(CW\*(C`TYPE\*(C'\fR stands for the watcher type,
 962e.g. \f(CW\*(C`timer\*(C'\fR for \f(CW\*(C`ev_timer\*(C'\fR watchers and \f(CW\*(C`io\*(C'\fR for \f(CW\*(C`ev_io\*(C'\fR watchers.
 963.ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4
 964.el .IP "\f(CWev_init\fR (ev_TYPE *watcher, callback)" 4
 965.IX Item "ev_init (ev_TYPE *watcher, callback)"
 966This macro initialises the generic portion of a watcher. The contents
 967of the watcher object can be arbitrary (so \f(CW\*(C`malloc\*(C'\fR will do). Only
 968the generic parts of the watcher are initialised, you \fIneed\fR to call
 969the type-specific \f(CW\*(C`ev_TYPE_set\*(C'\fR macro afterwards to initialise the
 970type-specific parts. For each type there is also a \f(CW\*(C`ev_TYPE_init\*(C'\fR macro
 971which rolls both calls into one.
 972.Sp
 973You can reinitialise a watcher at any time as long as it has been stopped
 974(or never started) and there are no pending events outstanding.
 975.Sp
 976The callback is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher,
 977int revents)\*(C'\fR.
 978.ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4
 979.el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4
 980.IX Item "ev_TYPE_set (ev_TYPE *, [args])"
 981This macro initialises the type-specific parts of a watcher. You need to
 982call \f(CW\*(C`ev_init\*(C'\fR at least once before you call this macro, but you can
 983call \f(CW\*(C`ev_TYPE_set\*(C'\fR any number of times. You must not, however, call this
 984macro on a watcher that is active (it can be pending, however, which is a
 985difference to the \f(CW\*(C`ev_init\*(C'\fR macro).
 986.Sp
 987Although some watcher types do not have type-specific arguments
 988(e.g. \f(CW\*(C`ev_prepare\*(C'\fR) you still need to call its \f(CW\*(C`set\*(C'\fR macro.
 989.ie n .IP """ev_TYPE_init"" (ev_TYPE *watcher, callback, [args])" 4
 990.el .IP "\f(CWev_TYPE_init\fR (ev_TYPE *watcher, callback, [args])" 4
 991.IX Item "ev_TYPE_init (ev_TYPE *watcher, callback, [args])"
 992This convinience macro rolls both \f(CW\*(C`ev_init\*(C'\fR and \f(CW\*(C`ev_TYPE_set\*(C'\fR macro
 993calls into a single call. This is the most convinient method to initialise
 994a watcher. The same limitations apply, of course.
 995.ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4
 996.el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4
 997.IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)"
 998Starts (activates) the given watcher. Only active watchers will receive
 999events. If the watcher is already active nothing will happen.
1000.ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4
1001.el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4
1002.IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)"
1003Stops the given watcher again (if active) and clears the pending
1004status. It is possible that stopped watchers are pending (for example,
1005non-repeating timers are being stopped when they become pending), but
1006\&\f(CW\*(C`ev_TYPE_stop\*(C'\fR ensures that the watcher is neither active nor pending. If
1007you want to free or reuse the memory used by the watcher it is therefore a
1008good idea to always call its \f(CW\*(C`ev_TYPE_stop\*(C'\fR function.
1009.IP "bool ev_is_active (ev_TYPE *watcher)" 4
1010.IX Item "bool ev_is_active (ev_TYPE *watcher)"
1011Returns a true value iff the watcher is active (i.e. it has been started
1012and not yet been stopped). As long as a watcher is active you must not modify
1013it.
1014.IP "bool ev_is_pending (ev_TYPE *watcher)" 4
1015.IX Item "bool ev_is_pending (ev_TYPE *watcher)"
1016Returns a true value iff the watcher is pending, (i.e. it has outstanding
1017events but its callback has not yet been invoked). As long as a watcher
1018is pending (but not active) you must not call an init function on it (but
1019\&\f(CW\*(C`ev_TYPE_set\*(C'\fR is safe), you must not change its priority, and you must
1020make sure the watcher is available to libev (e.g. you cannot \f(CW\*(C`free ()\*(C'\fR
1021it).
1022.IP "callback ev_cb (ev_TYPE *watcher)" 4
1023.IX Item "callback ev_cb (ev_TYPE *watcher)"
1024Returns the callback currently set on the watcher.
1025.IP "ev_cb_set (ev_TYPE *watcher, callback)" 4
1026.IX Item "ev_cb_set (ev_TYPE *watcher, callback)"
1027Change the callback. You can change the callback at virtually any time
1028(modulo threads).
1029.IP "ev_set_priority (ev_TYPE *watcher, priority)" 4
1030.IX Item "ev_set_priority (ev_TYPE *watcher, priority)"
1031.PD 0
1032.IP "int ev_priority (ev_TYPE *watcher)" 4
1033.IX Item "int ev_priority (ev_TYPE *watcher)"
1034.PD
1035Set and query the priority of the watcher. The priority is a small
1036integer between \f(CW\*(C`EV_MAXPRI\*(C'\fR (default: \f(CW2\fR) and \f(CW\*(C`EV_MINPRI\*(C'\fR
1037(default: \f(CW\*(C`\-2\*(C'\fR). Pending watchers with higher priority will be invoked
1038before watchers with lower priority, but priority will not keep watchers
1039from being executed (except for \f(CW\*(C`ev_idle\*(C'\fR watchers).
1040.Sp
1041This means that priorities are \fIonly\fR used for ordering callback
1042invocation after new events have been received. This is useful, for
1043example, to reduce latency after idling, or more often, to bind two
1044watchers on the same event and make sure one is called first.
1045.Sp
1046If you need to suppress invocation when higher priority events are pending
1047you need to look at \f(CW\*(C`ev_idle\*(C'\fR watchers, which provide this functionality.
1048.Sp
1049You \fImust not\fR change the priority of a watcher as long as it is active or
1050pending.
1051.Sp
1052The default priority used by watchers when no priority has been set is
1053always \f(CW0\fR, which is supposed to not be too high and not be too low :).
1054.Sp
1055Setting a priority outside the range of \f(CW\*(C`EV_MINPRI\*(C'\fR to \f(CW\*(C`EV_MAXPRI\*(C'\fR is
1056fine, as long as you do not mind that the priority value you query might
1057or might not have been adjusted to be within valid range.
1058.IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4
1059.IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)"
1060Invoke the \f(CW\*(C`watcher\*(C'\fR with the given \f(CW\*(C`loop\*(C'\fR and \f(CW\*(C`revents\*(C'\fR. Neither
1061\&\f(CW\*(C`loop\*(C'\fR nor \f(CW\*(C`revents\*(C'\fR need to be valid as long as the watcher callback
1062can deal with that fact.
1063.IP "int ev_clear_pending (loop, ev_TYPE *watcher)" 4
1064.IX Item "int ev_clear_pending (loop, ev_TYPE *watcher)"
1065If the watcher is pending, this function returns clears its pending status
1066and returns its \f(CW\*(C`revents\*(C'\fR bitset (as if its callback was invoked). If the
1067watcher isn't pending it does nothing and returns \f(CW0\fR.
1068.Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0"
1069.IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER"
1070Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change
1071and read at any time, libev will completely ignore it. This can be used
1072to associate arbitrary data with your watcher. If you need more data and
1073don't want to allocate memory and store a pointer to it in that data
1074member, you can also \*(L"subclass\*(R" the watcher type and provide …

Large files files are truncated, but you can click here to view the full file