/src/libeio/eio.pod

http://github.com/jacksonh/manos · Unknown · 303 lines · 198 code · 105 blank · 0 comment · 0 complexity · d08766f08d6f1e176c215cf0a7e6eccc MD5 · raw file

  1. =head1 NAME
  2. libeio - truly asynchronous POSIX I/O
  3. =head1 SYNOPSIS
  4. #include <eio.h>
  5. =head1 DESCRIPTION
  6. The newest version of this document is also available as an html-formatted
  7. web page you might find easier to navigate when reading it for the first
  8. time: L<http://pod.tst.eu/http://cvs.schmorp.de/libeio/eio.pod>.
  9. Note that this library is a by-product of the C<IO::AIO> perl
  10. module, and many of the subtler points regarding requets lifetime
  11. and so on are only documented in its documentation at the
  12. moment: L<http://pod.tst.eu/http://cvs.schmorp.de/IO-AIO/AIO.pm>.
  13. =head2 FEATURES
  14. This library provides fully asynchronous versions of most POSIX functions
  15. dealign with I/O. Unlike most asynchronous libraries, this not only
  16. includes C<read> and C<write>, but also C<open>, C<stat>, C<unlink> and
  17. similar functions, as well as less rarely ones such as C<mknod>, C<futime>
  18. or C<readlink>.
  19. It also offers wrappers around C<sendfile> (Solaris, Linux, HP-UX and
  20. FreeBSD, with emulation on other platforms) and C<readahead> (Linux, with
  21. emulation elsewhere>).
  22. The goal is to enable you to write fully non-blocking programs. For
  23. example, in a game server, you would not want to freeze for a few seconds
  24. just because the server is running a backup and you happen to call
  25. C<readdir>.
  26. =head2 TIME REPRESENTATION
  27. Libeio represents time as a single floating point number, representing the
  28. (fractional) number of seconds since the (POSIX) epoch (somewhere near
  29. the beginning of 1970, details are complicated, don't ask). This type is
  30. called C<eio_tstamp>, but it is guarenteed to be of type C<double> (or
  31. better), so you can freely use C<double> yourself.
  32. Unlike the name component C<stamp> might indicate, it is also used for
  33. time differences throughout libeio.
  34. =head2 FORK SUPPORT
  35. Calling C<fork ()> is fully supported by this module. It is implemented in these steps:
  36. 1. wait till all requests in "execute" state have been handled
  37. (basically requests that are already handed over to the kernel).
  38. 2. fork
  39. 3. in the parent, continue business as usual, done
  40. 4. in the child, destroy all ready and pending requests and free the
  41. memory used by the worker threads. This gives you a fully empty
  42. libeio queue.
  43. =head1 INITIALISATION/INTEGRATION
  44. Before you can call any eio functions you first have to initialise the
  45. library. The library integrates into any event loop, but can also be used
  46. without one, including in polling mode.
  47. You have to provide the necessary glue yourself, however.
  48. =over 4
  49. =item int eio_init (void (*want_poll)(void), void (*done_poll)(void))
  50. This function initialises the library. On success it returns C<0>, on
  51. failure it returns C<-1> and sets C<errno> appropriately.
  52. It accepts two function pointers specifying callbacks as argument, both of
  53. which can be C<0>, in which case the callback isn't called.
  54. =item want_poll callback
  55. The C<want_poll> callback is invoked whenever libeio wants attention (i.e.
  56. it wants to be polled by calling C<eio_poll>). It is "edge-triggered",
  57. that is, it will only be called once when eio wants attention, until all
  58. pending requests have been handled.
  59. This callback is called while locks are being held, so I<you must
  60. not call any libeio functions inside this callback>. That includes
  61. C<eio_poll>. What you should do is notify some other thread, or wake up
  62. your event loop, and then call C<eio_poll>.
  63. =item done_poll callback
  64. This callback is invoked when libeio detects that all pending requests
  65. have been handled. It is "edge-triggered", that is, it will only be
  66. called once after C<want_poll>. To put it differently, C<want_poll> and
  67. C<done_poll> are invoked in pairs: after C<want_poll> you have to call
  68. C<eio_poll ()> until either C<eio_poll> indicates that everything has been
  69. handled or C<done_poll> has been called, which signals the same.
  70. Note that C<eio_poll> might return after C<done_poll> and C<want_poll>
  71. have been called again, so watch out for races in your code.
  72. As with C<want_poll>, this callback is called while lcoks are being held,
  73. so you I<must not call any libeio functions form within this callback>.
  74. =item int eio_poll ()
  75. This function has to be called whenever there are pending requests that
  76. need finishing. You usually call this after C<want_poll> has indicated
  77. that you should do so, but you can also call this function regularly to
  78. poll for new results.
  79. If any request invocation returns a non-zero value, then C<eio_poll ()>
  80. immediately returns with that value as return value.
  81. Otherwise, if all requests could be handled, it returns C<0>. If for some
  82. reason not all requests have been handled, i.e. some are still pending, it
  83. returns C<-1>.
  84. =back
  85. For libev, you would typically use an C<ev_async> watcher: the
  86. C<want_poll> callback would invoke C<ev_async_send> to wake up the event
  87. loop. Inside the callback set for the watcher, one would call C<eio_poll
  88. ()> (followed by C<ev_async_send> again if C<eio_poll> indicates that not
  89. all requests have been handled yet). The race is taken care of because
  90. libev resets/rearms the async watcher before calling your callback,
  91. and therefore, before calling C<eio_poll>. This might result in (some)
  92. spurious wake-ups, but is generally harmless.
  93. For most other event loops, you would typically use a pipe - the event
  94. loop should be told to wait for read readyness on the read end. In
  95. C<want_poll> you would write a single byte, in C<done_poll> you would try
  96. to read that byte, and in the callback for the read end, you would call
  97. C<eio_poll>. The race is avoided here because the event loop should invoke
  98. your callback again and again until the byte has been read (as the pipe
  99. read callback does not read it, only C<done_poll>).
  100. =head2 CONFIGURATION
  101. The functions in this section can sometimes be useful, but the default
  102. configuration will do in most case, so you should skip this section on
  103. first reading.
  104. =over 4
  105. =item eio_set_max_poll_time (eio_tstamp nseconds)
  106. This causes C<eio_poll ()> to return after it has detected that it was
  107. running for C<nsecond> seconds or longer (this number can be fractional).
  108. This can be used to limit the amount of time spent handling eio requests,
  109. for example, in interactive programs, you might want to limit this time to
  110. C<0.01> seconds or so.
  111. Note that:
  112. a) libeio doesn't know how long your request callbacks take, so the time
  113. spent in C<eio_poll> is up to one callback invocation longer then this
  114. interval.
  115. b) this is implemented by calling C<gettimeofday> after each request,
  116. which can be costly.
  117. c) at least one request will be handled.
  118. =item eio_set_max_poll_reqs (unsigned int nreqs)
  119. When C<nreqs> is non-zero, then C<eio_poll> will not handle more than
  120. C<nreqs> requests per invocation. This is a less costly way to limit the
  121. amount of work done by C<eio_poll> then setting a time limit.
  122. If you know your callbacks are generally fast, you could use this to
  123. encourage interactiveness in your programs by setting it to C<10>, C<100>
  124. or even C<1000>.
  125. =item eio_set_min_parallel (unsigned int nthreads)
  126. Make sure libeio can handle at least this many requests in parallel. It
  127. might be able handle more.
  128. =item eio_set_max_parallel (unsigned int nthreads)
  129. Set the maximum number of threads that libeio will spawn.
  130. =item eio_set_max_idle (unsigned int nthreads)
  131. Libeio uses threads internally to handle most requests, and will start and stop threads on demand.
  132. This call can be used to limit the number of idle threads (threads without
  133. work to do): libeio will keep some threads idle in preperation for more
  134. requests, but never longer than C<nthreads> threads.
  135. In addition to this, libeio will also stop threads when they are idle for
  136. a few seconds, regardless of this setting.
  137. =item unsigned int eio_nthreads ()
  138. Return the number of worker threads currently running.
  139. =item unsigned int eio_nreqs ()
  140. Return the number of requests currently handled by libeio. This is the
  141. total number of requests that have been submitted to libeio, but not yet
  142. destroyed.
  143. =item unsigned int eio_nready ()
  144. Returns the number of ready requests, i.e. requests that have been
  145. submitted but have not yet entered the execution phase.
  146. =item unsigned int eio_npending ()
  147. Returns the number of pending requests, i.e. requests that have been
  148. executed and have results, but have not been finished yet by a call to
  149. C<eio_poll>).
  150. =back
  151. =head1 ANATOMY OF AN EIO REQUEST
  152. #TODO
  153. =head1 HIGH LEVEL REQUEST API
  154. #TODO
  155. =back
  156. =head1 LOW LEVEL REQUEST API
  157. #TODO
  158. =head1 EMBEDDING
  159. Libeio can be embedded directly into programs. This functionality is not
  160. documented and not (yet) officially supported.
  161. Note that, when including C<libeio.m4>, you are responsible for defining
  162. the compilation environment (C<_LARGEFILE_SOURCE>, C<_GNU_SOURCE> etc.).
  163. If you need to know how, check the C<IO::AIO> perl module, which does
  164. exactly that.
  165. =head1 COMPILETIME CONFIGURATION
  166. These symbols, if used, must be defined when compiling F<eio.c>.
  167. =over 4
  168. =item EIO_STACKSIZE
  169. This symbol governs the stack size for each eio thread. Libeio itself
  170. was written to use very little stackspace, but when using C<EIO_CUSTOM>
  171. requests, you might want to increase this.
  172. If this symbol is undefined (the default) then libeio will use its default
  173. stack size (C<sizeof (long) * 4096> currently). If it is defined, but
  174. C<0>, then the default operating system stack size will be used. In all
  175. other cases, the value must be an expression that evaluates to the desired
  176. stack size.
  177. =back
  178. =head1 PORTABILITY REQUIREMENTS
  179. In addition to a working ISO-C implementation, libeio relies on a few
  180. additional extensions:
  181. =over 4
  182. =item POSIX threads
  183. To be portable, this module uses threads, specifically, the POSIX threads
  184. library must be available (and working, which partially excludes many xBSD
  185. systems, where C<fork ()> is buggy).
  186. =item POSIX-compatible filesystem API
  187. This is actually a harder portability requirement: The libeio API is quite
  188. demanding regarding POSIX API calls (symlinks, user/group management
  189. etc.).
  190. =item C<double> must hold a time value in seconds with enough accuracy
  191. The type C<double> is used to represent timestamps. It is required to
  192. have at least 51 bits of mantissa (and 9 bits of exponent), which is good
  193. enough for at least into the year 4000. This requirement is fulfilled by
  194. implementations implementing IEEE 754 (basically all existing ones).
  195. =back
  196. If you know of other additional requirements drop me a note.
  197. =head1 AUTHOR
  198. Marc Lehmann <libeio@schmorp.de>.