Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4289 lines
194 KiB

12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
12 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
14 years ago
13 years ago
13 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
13 years ago
12 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
14 years ago
13 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
14 years ago
13 years ago
13 years ago
14 years ago
12 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
14 years ago
14 years ago
14 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
14 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
  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 "LIBEV 3"
  135. .TH LIBEV 3 "2009-04-25" "libev-3.6" "libev - high performance full featured event loop"
  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"
  141. libev \- 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. \& #include <stdio.h> // for puts
  154. \&
  155. \& // every watcher type has its own typedef\*(Aqd struct
  156. \& // with the name ev_TYPE
  157. \& ev_io stdin_watcher;
  158. \& ev_timer timeout_watcher;
  159. \&
  160. \& // all watcher callbacks have a similar signature
  161. \& // this callback is called when data is readable on stdin
  162. \& static void
  163. \& stdin_cb (EV_P_ ev_io *w, int revents)
  164. \& {
  165. \& puts ("stdin ready");
  166. \& // for one\-shot events, one must manually stop the watcher
  167. \& // with its corresponding stop function.
  168. \& ev_io_stop (EV_A_ w);
  169. \&
  170. \& // this causes all nested ev_loop\*(Aqs to stop iterating
  171. \& ev_unloop (EV_A_ EVUNLOOP_ALL);
  172. \& }
  173. \&
  174. \& // another callback, this time for a time\-out
  175. \& static void
  176. \& timeout_cb (EV_P_ ev_timer *w, int revents)
  177. \& {
  178. \& puts ("timeout");
  179. \& // this causes the innermost ev_loop to stop iterating
  180. \& ev_unloop (EV_A_ EVUNLOOP_ONE);
  181. \& }
  182. \&
  183. \& int
  184. \& main (void)
  185. \& {
  186. \& // use the default event loop unless you have special needs
  187. \& struct ev_loop *loop = ev_default_loop (0);
  188. \&
  189. \& // initialise an io watcher, then start it
  190. \& // this one will watch for stdin to become readable
  191. \& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
  192. \& ev_io_start (loop, &stdin_watcher);
  193. \&
  194. \& // initialise a timer watcher, then start it
  195. \& // simple non\-repeating 5.5 second timeout
  196. \& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
  197. \& ev_timer_start (loop, &timeout_watcher);
  198. \&
  199. \& // now wait for events to arrive
  200. \& ev_loop (loop, 0);
  201. \&
  202. \& // unloop was called, so exit
  203. \& return 0;
  204. \& }
  205. .Ve
  206. .SH "ABOUT THIS DOCUMENT"
  207. .IX Header "ABOUT THIS DOCUMENT"
  208. This document documents the libev software package.
  209. .PP
  210. The newest version of this document is also available as an html-formatted
  211. web page you might find easier to navigate when reading it for the first
  212. time: <http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>.
  213. .PP
  214. While this document tries to be as complete as possible in documenting
  215. libev, its usage and the rationale behind its design, it is not a tutorial
  216. on event-based programming, nor will it introduce event-based programming
  217. with libev.
  218. .PP
  219. Familarity with event based programming techniques in general is assumed
  220. throughout this document.
  221. .SH "ABOUT LIBEV"
  222. .IX Header "ABOUT LIBEV"
  223. Libev is an event loop: you register interest in certain events (such as a
  224. file descriptor being readable or a timeout occurring), and it will manage
  225. these event sources and provide your program with events.
  226. .PP
  227. To do this, it must take more or less complete control over your process
  228. (or thread) by executing the \fIevent loop\fR handler, and will then
  229. communicate events via a callback mechanism.
  230. .PP
  231. You register interest in certain events by registering so-called \fIevent
  232. watchers\fR, which are relatively small C structures you initialise with the
  233. details of the event, and then hand it over to libev by \fIstarting\fR the
  234. watcher.
  235. .Sh "\s-1FEATURES\s0"
  236. .IX Subsection "FEATURES"
  237. Libev supports \f(CW\*(C`select\*(C'\fR, \f(CW\*(C`poll\*(C'\fR, the Linux-specific \f(CW\*(C`epoll\*(C'\fR, the
  238. BSD-specific \f(CW\*(C`kqueue\*(C'\fR and the Solaris-specific event port mechanisms
  239. for file descriptor events (\f(CW\*(C`ev_io\*(C'\fR), the Linux \f(CW\*(C`inotify\*(C'\fR interface
  240. (for \f(CW\*(C`ev_stat\*(C'\fR), relative timers (\f(CW\*(C`ev_timer\*(C'\fR), absolute timers
  241. with customised rescheduling (\f(CW\*(C`ev_periodic\*(C'\fR), synchronous signals
  242. (\f(CW\*(C`ev_signal\*(C'\fR), process status change events (\f(CW\*(C`ev_child\*(C'\fR), and event
  243. watchers dealing with the event loop mechanism itself (\f(CW\*(C`ev_idle\*(C'\fR,
  244. \&\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
  245. file watchers (\f(CW\*(C`ev_stat\*(C'\fR) and even limited support for fork events
  246. (\f(CW\*(C`ev_fork\*(C'\fR).
  247. .PP
  248. It also is quite fast (see this
  249. benchmark comparing it to libevent
  250. for example).
  251. .Sh "\s-1CONVENTIONS\s0"
  252. .IX Subsection "CONVENTIONS"
  253. Libev is very configurable. In this manual the default (and most common)
  254. configuration will be described, which supports multiple event loops. For
  255. more info about various configuration options please have a look at
  256. \&\fB\s-1EMBED\s0\fR section in this manual. If libev was configured without support
  257. for multiple event loops, then all functions taking an initial argument of
  258. name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`ev_loop *\*(C'\fR) will not have
  259. this argument.
  260. .Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0"
  261. .IX Subsection "TIME REPRESENTATION"
  262. Libev represents time as a single floating point number, representing
  263. the (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere
  264. near the beginning of 1970, details are complicated, don't ask). This
  265. type is called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually
  266. aliases to the \f(CW\*(C`double\*(C'\fR type in C. When you need to do any calculations
  267. on it, you should treat it as some floating point value. Unlike the name
  268. component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences
  269. throughout libev.
  270. .SH "ERROR HANDLING"
  271. .IX Header "ERROR HANDLING"
  272. Libev knows three classes of errors: operating system errors, usage errors
  273. and internal errors (bugs).
  274. .PP
  275. When libev catches an operating system error it cannot handle (for example
  276. a system call indicating a condition libev cannot fix), it calls the callback
  277. set via \f(CW\*(C`ev_set_syserr_cb\*(C'\fR, which is supposed to fix the problem or
  278. abort. The default is to print a diagnostic message and to call \f(CW\*(C`abort
  279. ()\*(C'\fR.
  280. .PP
  281. When libev detects a usage error such as a negative timer interval, then
  282. it will print a diagnostic message and abort (via the \f(CW\*(C`assert\*(C'\fR mechanism,
  283. so \f(CW\*(C`NDEBUG\*(C'\fR will disable this checking): these are programming errors in
  284. the libev caller and need to be fixed there.
  285. .PP
  286. Libev also has a few internal error-checking \f(CW\*(C`assert\*(C'\fRions, and also has
  287. extensive consistency checking code. These do not trigger under normal
  288. circumstances, as they indicate either a bug in libev or worse.
  289. .SH "GLOBAL FUNCTIONS"
  290. .IX Header "GLOBAL FUNCTIONS"
  291. These functions can be called anytime, even before initialising the
  292. library in any way.
  293. .IP "ev_tstamp ev_time ()" 4
  294. .IX Item "ev_tstamp ev_time ()"
  295. Returns the current time as libev would use it. Please note that the
  296. \&\f(CW\*(C`ev_now\*(C'\fR function is usually faster and also often returns the timestamp
  297. you actually want to know.
  298. .IP "ev_sleep (ev_tstamp interval)" 4
  299. .IX Item "ev_sleep (ev_tstamp interval)"
  300. Sleep for the given interval: The current thread will be blocked until
  301. either it is interrupted or the given time interval has passed. Basically
  302. this is a sub-second-resolution \f(CW\*(C`sleep ()\*(C'\fR.
  303. .IP "int ev_version_major ()" 4
  304. .IX Item "int ev_version_major ()"
  305. .PD 0
  306. .IP "int ev_version_minor ()" 4
  307. .IX Item "int ev_version_minor ()"
  308. .PD
  309. You can find out the major and minor \s-1ABI\s0 version numbers of the library
  310. you linked against by calling the functions \f(CW\*(C`ev_version_major\*(C'\fR and
  311. \&\f(CW\*(C`ev_version_minor\*(C'\fR. If you want, you can compare against the global
  312. symbols \f(CW\*(C`EV_VERSION_MAJOR\*(C'\fR and \f(CW\*(C`EV_VERSION_MINOR\*(C'\fR, which specify the
  313. version of the library your program was compiled against.
  314. .Sp
  315. These version numbers refer to the \s-1ABI\s0 version of the library, not the
  316. release version.
  317. .Sp
  318. Usually, it's a good idea to terminate if the major versions mismatch,
  319. as this indicates an incompatible change. Minor versions are usually
  320. compatible to older versions, so a larger minor version alone is usually
  321. not a problem.
  322. .Sp
  323. Example: Make sure we haven't accidentally been linked against the wrong
  324. version.
  325. .Sp
  326. .Vb 3
  327. \& assert (("libev version mismatch",
  328. \& ev_version_major () == EV_VERSION_MAJOR
  329. \& && ev_version_minor () >= EV_VERSION_MINOR));
  330. .Ve
  331. .IP "unsigned int ev_supported_backends ()" 4
  332. .IX Item "unsigned int ev_supported_backends ()"
  333. Return the set of all backends (i.e. their corresponding \f(CW\*(C`EV_BACKEND_*\*(C'\fR
  334. value) compiled into this binary of libev (independent of their
  335. availability on the system you are running on). See \f(CW\*(C`ev_default_loop\*(C'\fR for
  336. a description of the set values.
  337. .Sp
  338. Example: make sure we have the epoll method, because yeah this is cool and
  339. a must have and can we have a torrent of it please!!!11
  340. .Sp
  341. .Vb 2
  342. \& assert (("sorry, no epoll, no sex",
  343. \& ev_supported_backends () & EVBACKEND_EPOLL));
  344. .Ve
  345. .IP "unsigned int ev_recommended_backends ()" 4
  346. .IX Item "unsigned int ev_recommended_backends ()"
  347. Return the set of all backends compiled into this binary of libev and also
  348. recommended for this platform. This set is often smaller than the one
  349. returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on
  350. most BSDs and will not be auto-detected unless you explicitly request it
  351. (assuming you know what you are doing). This is the set of backends that
  352. libev will probe for if you specify no backends explicitly.
  353. .IP "unsigned int ev_embeddable_backends ()" 4
  354. .IX Item "unsigned int ev_embeddable_backends ()"
  355. Returns the set of backends that are embeddable in other event loops. This
  356. is the theoretical, all-platform, value. To find which backends
  357. might be supported on the current system, you would need to look at
  358. \&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for
  359. recommended ones.
  360. .Sp
  361. See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
  362. .IP "ev_set_allocator (void *(*cb)(void *ptr, long size)) [\s-1NOT\s0 \s-1REENTRANT\s0]" 4
  363. .IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size)) [NOT REENTRANT]"
  364. Sets the allocation function to use (the prototype is similar \- the
  365. semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is
  366. used to allocate and free memory (no surprises here). If it returns zero
  367. when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort
  368. or take some potentially destructive action.
  369. .Sp
  370. Since some systems (at least OpenBSD and Darwin) fail to implement
  371. correct \f(CW\*(C`realloc\*(C'\fR semantics, libev will use a wrapper around the system
  372. \&\f(CW\*(C`realloc\*(C'\fR and \f(CW\*(C`free\*(C'\fR functions by default.
  373. .Sp
  374. You could override this function in high-availability programs to, say,
  375. free some memory if it cannot allocate memory, to use a special allocator,
  376. or even to sleep a while and retry until some memory is available.
  377. .Sp
  378. Example: Replace the libev allocator with one that waits a bit and then
  379. retries (example requires a standards-compliant \f(CW\*(C`realloc\*(C'\fR).
  380. .Sp
  381. .Vb 6
  382. \& static void *
  383. \& persistent_realloc (void *ptr, size_t size)
  384. \& {
  385. \& for (;;)
  386. \& {
  387. \& void *newptr = realloc (ptr, size);
  388. \&
  389. \& if (newptr)
  390. \& return newptr;
  391. \&
  392. \& sleep (60);
  393. \& }
  394. \& }
  395. \&
  396. \& ...
  397. \& ev_set_allocator (persistent_realloc);
  398. .Ve
  399. .IP "ev_set_syserr_cb (void (*cb)(const char *msg)); [\s-1NOT\s0 \s-1REENTRANT\s0]" 4
  400. .IX Item "ev_set_syserr_cb (void (*cb)(const char *msg)); [NOT REENTRANT]"
  401. Set the callback function to call on a retryable system call error (such
  402. as failed select, poll, epoll_wait). The message is a printable string
  403. indicating the system call or subsystem causing the problem. If this
  404. callback is set, then libev will expect it to remedy the situation, no
  405. matter what, when it returns. That is, libev will generally retry the
  406. requested operation, or, if the condition doesn't go away, do bad stuff
  407. (such as abort).
  408. .Sp
  409. Example: This is basically the same thing that libev does internally, too.
  410. .Sp
  411. .Vb 6
  412. \& static void
  413. \& fatal_error (const char *msg)
  414. \& {
  415. \& perror (msg);
  416. \& abort ();
  417. \& }
  418. \&
  419. \& ...
  420. \& ev_set_syserr_cb (fatal_error);
  421. .Ve
  422. .SH "FUNCTIONS CONTROLLING THE EVENT LOOP"
  423. .IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP"
  424. An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR (the \f(CW\*(C`struct\*(C'\fR
  425. is \fInot\fR optional in this case, as there is also an \f(CW\*(C`ev_loop\*(C'\fR
  426. \&\fIfunction\fR).
  427. .PP
  428. The library knows two types of such loops, the \fIdefault\fR loop, which
  429. supports signals and child events, and dynamically created loops which do
  430. not.
  431. .IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4
  432. .IX Item "struct ev_loop *ev_default_loop (unsigned int flags)"
  433. This will initialise the default event loop if it hasn't been initialised
  434. yet and return it. If the default loop could not be initialised, returns
  435. false. If it already was initialised it simply returns it (and ignores the
  436. flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards).
  437. .Sp
  438. If you don't know what event loop to use, use the one returned from this
  439. function.
  440. .Sp
  441. Note that this function is \fInot\fR thread-safe, so if you want to use it
  442. from multiple threads, you have to lock (note also that this is unlikely,
  443. as loops cannot be shared easily between threads anyway).
  444. .Sp
  445. The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and
  446. \&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler
  447. for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your application you can either
  448. create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you
  449. can simply overwrite the \f(CW\*(C`SIGCHLD\*(C'\fR signal handler \fIafter\fR calling
  450. \&\f(CW\*(C`ev_default_init\*(C'\fR.
  451. .Sp
  452. The flags argument can be used to specify special behaviour or specific
  453. backends to use, and is usually specified as \f(CW0\fR (or \f(CW\*(C`EVFLAG_AUTO\*(C'\fR).
  454. .Sp
  455. The following flags are supported:
  456. .RS 4
  457. .ie n .IP """EVFLAG_AUTO""" 4
  458. .el .IP "\f(CWEVFLAG_AUTO\fR" 4
  459. .IX Item "EVFLAG_AUTO"
  460. The default flags value. Use this if you have no clue (it's the right
  461. thing, believe me).
  462. .ie n .IP """EVFLAG_NOENV""" 4
  463. .el .IP "\f(CWEVFLAG_NOENV\fR" 4
  464. .IX Item "EVFLAG_NOENV"
  465. If this flag bit is or'ed into the flag value (or the program runs setuid
  466. or setgid) then libev will \fInot\fR look at the environment variable
  467. \&\f(CW\*(C`LIBEV_FLAGS\*(C'\fR. Otherwise (the default), this environment variable will
  468. override the flags completely if it is found in the environment. This is
  469. useful to try out specific backends to test their performance, or to work
  470. around bugs.
  471. .ie n .IP """EVFLAG_FORKCHECK""" 4
  472. .el .IP "\f(CWEVFLAG_FORKCHECK\fR" 4
  473. .IX Item "EVFLAG_FORKCHECK"
  474. Instead of calling \f(CW\*(C`ev_default_fork\*(C'\fR or \f(CW\*(C`ev_loop_fork\*(C'\fR manually after
  475. a fork, you can also make libev check for a fork in each iteration by
  476. enabling this flag.
  477. .Sp
  478. This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop,
  479. and thus this might slow down your event loop if you do a lot of loop
  480. iterations and little real work, but is usually not noticeable (on my
  481. GNU/Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence
  482. without a system call and thus \fIvery\fR fast, but my GNU/Linux system also has
  483. \&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster).
  484. .Sp
  485. The big advantage of this flag is that you can forget about fork (and
  486. forget about forgetting to tell libev about forking) when you use this
  487. flag.
  488. .Sp
  489. This flag setting cannot be overridden or specified in the \f(CW\*(C`LIBEV_FLAGS\*(C'\fR
  490. environment variable.
  491. .ie n .IP """EVBACKEND_SELECT"" (value 1, portable select backend)" 4
  492. .el .IP "\f(CWEVBACKEND_SELECT\fR (value 1, portable select backend)" 4
  493. .IX Item "EVBACKEND_SELECT (value 1, portable select backend)"
  494. This is your standard \fIselect\fR\|(2) backend. Not \fIcompletely\fR standard, as
  495. libev tries to roll its own fd_set with no limits on the number of fds,
  496. but if that fails, expect a fairly low limit on the number of fds when
  497. using this backend. It doesn't scale too well (O(highest_fd)), but its
  498. usually the fastest backend for a low number of (low-numbered :) fds.
  499. .Sp
  500. To get good performance out of this backend you need a high amount of
  501. parallelism (most of the file descriptors should be busy). If you are
  502. writing a server, you should \f(CW\*(C`accept ()\*(C'\fR in a loop to accept as many
  503. connections as possible during one iteration. You might also want to have
  504. a look at \f(CW\*(C`ev_set_io_collect_interval ()\*(C'\fR to increase the amount of
  505. readiness notifications you get per iteration.
  506. .Sp
  507. This backend maps \f(CW\*(C`EV_READ\*(C'\fR to the \f(CW\*(C`readfds\*(C'\fR set and \f(CW\*(C`EV_WRITE\*(C'\fR to the
  508. \&\f(CW\*(C`writefds\*(C'\fR set (and to work around Microsoft Windows bugs, also onto the
  509. \&\f(CW\*(C`exceptfds\*(C'\fR set on that platform).
  510. .ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4
  511. .el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4
  512. .IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)"
  513. And this is your standard \fIpoll\fR\|(2) backend. It's more complicated
  514. than select, but handles sparse fds better and has no artificial
  515. limit on the number of fds you can use (except it will slow down
  516. considerably with a lot of inactive fds). It scales similarly to select,
  517. i.e. O(total_fds). See the entry for \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR, above, for
  518. performance tips.
  519. .Sp
  520. This backend maps \f(CW\*(C`EV_READ\*(C'\fR to \f(CW\*(C`POLLIN | POLLERR | POLLHUP\*(C'\fR, and
  521. \&\f(CW\*(C`EV_WRITE\*(C'\fR to \f(CW\*(C`POLLOUT | POLLERR | POLLHUP\*(C'\fR.
  522. .ie n .IP """EVBACKEND_EPOLL"" (value 4, Linux)" 4
  523. .el .IP "\f(CWEVBACKEND_EPOLL\fR (value 4, Linux)" 4
  524. .IX Item "EVBACKEND_EPOLL (value 4, Linux)"
  525. For few fds, this backend is a bit little slower than poll and select,
  526. but it scales phenomenally better. While poll and select usually scale
  527. like O(total_fds) where n is the total number of fds (or the highest fd),
  528. epoll scales either O(1) or O(active_fds).
  529. .Sp
  530. The epoll mechanism deserves honorable mention as the most misdesigned
  531. of the more advanced event mechanisms: mere annoyances include silently
  532. dropping file descriptors, requiring a system call per change per file
  533. descriptor (and unnecessary guessing of parameters), problems with dup and
  534. so on. The biggest issue is fork races, however \- if a program forks then
  535. \&\fIboth\fR parent and child process have to recreate the epoll set, which can
  536. take considerable time (one syscall per file descriptor) and is of course
  537. hard to detect.
  538. .Sp
  539. Epoll is also notoriously buggy \- embedding epoll fds \fIshould\fR work, but
  540. of course \fIdoesn't\fR, and epoll just loves to report events for totally
  541. \&\fIdifferent\fR file descriptors (even already closed ones, so one cannot
  542. even remove them from the set) than registered in the set (especially
  543. on \s-1SMP\s0 systems). Libev tries to counter these spurious notifications by
  544. employing an additional generation counter and comparing that against the
  545. events to filter out spurious ones, recreating the set when required.
  546. .Sp
  547. While stopping, setting and starting an I/O watcher in the same iteration
  548. will result in some caching, there is still a system call per such
  549. incident (because the same \fIfile descriptor\fR could point to a different
  550. \&\fIfile description\fR now), so its best to avoid that. Also, \f(CW\*(C`dup ()\*(C'\fR'ed
  551. file descriptors might not work very well if you register events for both
  552. file descriptors.
  553. .Sp
  554. Best performance from this backend is achieved by not unregistering all
  555. watchers for a file descriptor until it has been closed, if possible,
  556. i.e. keep at least one watcher active per fd at all times. Stopping and
  557. starting a watcher (without re-setting it) also usually doesn't cause
  558. extra overhead. A fork can both result in spurious notifications as well
  559. as in libev having to destroy and recreate the epoll object, which can
  560. take considerable time and thus should be avoided.
  561. .Sp
  562. All this means that, in practice, \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR can be as fast or
  563. faster than epoll for maybe up to a hundred file descriptors, depending on
  564. the usage. So sad.
  565. .Sp
  566. While nominally embeddable in other event loops, this feature is broken in
  567. all kernel versions tested so far.
  568. .Sp
  569. This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as
  570. \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
  571. .ie n .IP """EVBACKEND_KQUEUE"" (value 8, most \s-1BSD\s0 clones)" 4
  572. .el .IP "\f(CWEVBACKEND_KQUEUE\fR (value 8, most \s-1BSD\s0 clones)" 4
  573. .IX Item "EVBACKEND_KQUEUE (value 8, most BSD clones)"
  574. Kqueue deserves special mention, as at the time of this writing, it
  575. was broken on all BSDs except NetBSD (usually it doesn't work reliably
  576. with anything but sockets and pipes, except on Darwin, where of course
  577. it's completely useless). Unlike epoll, however, whose brokenness
  578. is by design, these kqueue bugs can (and eventually will) be fixed
  579. without \s-1API\s0 changes to existing programs. For this reason it's not being
  580. \&\*(L"auto-detected\*(R" unless you explicitly specify it in the flags (i.e. using
  581. \&\f(CW\*(C`EVBACKEND_KQUEUE\*(C'\fR) or libev was compiled on a known-to-be-good (\-enough)
  582. system like NetBSD.
  583. .Sp
  584. You still can embed kqueue into a normal poll or select backend and use it
  585. only for sockets (after having made sure that sockets work with kqueue on
  586. the target platform). See \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
  587. .Sp
  588. It scales in the same way as the epoll backend, but the interface to the
  589. kernel is more efficient (which says nothing about its actual speed, of
  590. course). While stopping, setting and starting an I/O watcher does never
  591. cause an extra system call as with \f(CW\*(C`EVBACKEND_EPOLL\*(C'\fR, it still adds up to
  592. two event changes per incident. Support for \f(CW\*(C`fork ()\*(C'\fR is very bad (but
  593. sane, unlike epoll) and it drops fds silently in similarly hard-to-detect
  594. cases
  595. .Sp
  596. This backend usually performs well under most conditions.
  597. .Sp
  598. While nominally embeddable in other event loops, this doesn't work
  599. everywhere, so you might need to test for this. And since it is broken
  600. almost everywhere, you should only use it when you have a lot of sockets
  601. (for which it usually works), by embedding it into another event loop
  602. (e.g. \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR (but \f(CW\*(C`poll\*(C'\fR is of course
  603. also broken on \s-1OS\s0 X)) and, did I mention it, using it only for sockets.
  604. .Sp
  605. This backend maps \f(CW\*(C`EV_READ\*(C'\fR into an \f(CW\*(C`EVFILT_READ\*(C'\fR kevent with
  606. \&\f(CW\*(C`NOTE_EOF\*(C'\fR, and \f(CW\*(C`EV_WRITE\*(C'\fR into an \f(CW\*(C`EVFILT_WRITE\*(C'\fR kevent with
  607. \&\f(CW\*(C`NOTE_EOF\*(C'\fR.
  608. .ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4
  609. .el .IP "\f(CWEVBACKEND_DEVPOLL\fR (value 16, Solaris 8)" 4
  610. .IX Item "EVBACKEND_DEVPOLL (value 16, Solaris 8)"
  611. This is not implemented yet (and might never be, unless you send me an
  612. implementation). According to reports, \f(CW\*(C`/dev/poll\*(C'\fR only supports sockets
  613. and is not embeddable, which would limit the usefulness of this backend
  614. immensely.
  615. .ie n .IP """EVBACKEND_PORT"" (value 32, Solaris 10)" 4
  616. .el .IP "\f(CWEVBACKEND_PORT\fR (value 32, Solaris 10)" 4
  617. .IX Item "EVBACKEND_PORT (value 32, Solaris 10)"
  618. This uses the Solaris 10 event port mechanism. As with everything on Solaris,
  619. it's really slow, but it still scales very well (O(active_fds)).
  620. .Sp
  621. Please note that Solaris event ports can deliver a lot of spurious
  622. notifications, so you need to use non-blocking I/O or other means to avoid
  623. blocking when no data (or space) is available.
  624. .Sp
  625. While this backend scales well, it requires one system call per active
  626. file descriptor per loop iteration. For small and medium numbers of file
  627. descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend
  628. might perform better.
  629. .Sp
  630. On the positive side, with the exception of the spurious readiness
  631. notifications, this backend actually performed fully to specification
  632. in all tests and is fully embeddable, which is a rare feat among the
  633. OS-specific backends (I vastly prefer correctness over speed hacks).
  634. .Sp
  635. This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as
  636. \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
  637. .ie n .IP """EVBACKEND_ALL""" 4
  638. .el .IP "\f(CWEVBACKEND_ALL\fR" 4
  639. .IX Item "EVBACKEND_ALL"
  640. Try all backends (even potentially broken ones that wouldn't be tried
  641. with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as
  642. \&\f(CW\*(C`EVBACKEND_ALL & ~EVBACKEND_KQUEUE\*(C'\fR.
  643. .Sp
  644. It is definitely not recommended to use this flag.
  645. .RE
  646. .RS 4
  647. .Sp
  648. If one or more of these are or'ed into the flags value, then only these
  649. backends will be tried (in the reverse order as listed here). If none are
  650. specified, all backends in \f(CW\*(C`ev_recommended_backends ()\*(C'\fR will be tried.
  651. .Sp
  652. Example: This is the most typical usage.
  653. .Sp
  654. .Vb 2
  655. \& if (!ev_default_loop (0))
  656. \& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?");
  657. .Ve
  658. .Sp
  659. Example: Restrict libev to the select and poll backends, and do not allow
  660. environment settings to be taken into account:
  661. .Sp
  662. .Vb 1
  663. \& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV);
  664. .Ve
  665. .Sp
  666. Example: Use whatever libev has to offer, but make sure that kqueue is
  667. used if available (warning, breaks stuff, best use only with your own
  668. private event loop and only if you know the \s-1OS\s0 supports your types of
  669. fds):
  670. .Sp
  671. .Vb 1
  672. \& ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE);
  673. .Ve
  674. .RE
  675. .IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4
  676. .IX Item "struct ev_loop *ev_loop_new (unsigned int flags)"
  677. Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is
  678. always distinct from the default loop. Unlike the default loop, it cannot
  679. handle signal and child watchers, and attempts to do so will be greeted by
  680. undefined behaviour (or a failed assertion if assertions are enabled).
  681. .Sp
  682. Note that this function \fIis\fR thread-safe, and the recommended way to use
  683. libev with threads is indeed to create one loop per thread, and using the
  684. default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread.
  685. .Sp
  686. Example: Try to create a event loop that uses epoll and nothing else.
  687. .Sp
  688. .Vb 3
  689. \& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
  690. \& if (!epoller)
  691. \& fatal ("no epoll found here, maybe it hides under your chair");
  692. .Ve
  693. .IP "ev_default_destroy ()" 4
  694. .IX Item "ev_default_destroy ()"
  695. Destroys the default loop again (frees all memory and kernel state
  696. etc.). None of the active event watchers will be stopped in the normal
  697. sense, so e.g. \f(CW\*(C`ev_is_active\*(C'\fR might still return true. It is your