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.

219 lines
7.1 KiB

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
14 years ago
  1. /*
  2. * libev epoll fd activity backend
  3. *
  4. * Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modifica-
  8. * tion, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
  19. * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  20. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
  21. * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
  25. * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  26. * OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * the GNU General Public License ("GPL") version 2 or any later version,
  30. * in which case the provisions of the GPL are applicable instead of
  31. * the above. If you wish to allow the use of your version of this file
  32. * only under the terms of the GPL and not to allow others to use your
  33. * version of this file under the BSD license, indicate your decision
  34. * by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL. If you do not delete the
  36. * provisions above, a recipient may use your version of this file under
  37. * either the BSD or the GPL.
  38. */
  39. /*
  40. * general notes about epoll:
  41. *
  42. * a) epoll silently removes fds from the fd set. as nothing tells us
  43. * that an fd has been removed otherwise, we have to continually
  44. * "rearm" fds that we suspect *might* have changed (same
  45. * problem with kqueue, but much less costly there).
  46. * b) the fact that ADD != MOD creates a lot of extra syscalls due to a)
  47. * and seems not to have any advantage.
  48. * c) the inability to handle fork or file descriptors (think dup)
  49. * limits the applicability over poll, so this is not a generic
  50. * poll replacement.
  51. *
  52. * lots of "weird code" and complication handling in this file is due
  53. * to these design problems with epoll, as we try very hard to avoid
  54. * epoll_ctl syscalls for common usage patterns and handle the breakage
  55. * ensuing from receiving events for closed and otherwise long gone
  56. * file descriptors.
  57. */
  58. #include <sys/epoll.h>
  59. static void
  60. epoll_modify (EV_P_ int fd, int oev, int nev)
  61. {
  62. struct epoll_event ev;
  63. unsigned char oldmask;
  64. /*
  65. * we handle EPOLL_CTL_DEL by ignoring it here
  66. * on the assumption that the fd is gone anyways
  67. * if that is wrong, we have to handle the spurious
  68. * event in epoll_poll.
  69. * if the fd is added again, we try to ADD it, and, if that
  70. * fails, we assume it still has the same eventmask.
  71. */
  72. if (!nev)
  73. return;
  74. oldmask = anfds [fd].emask;
  75. anfds [fd].emask = nev;
  76. /* store the generation counter in the upper 32 bits, the fd in the lower 32 bits */
  77. ev.data.u64 = (uint64_t)(uint32_t)fd
  78. | ((uint64_t)(uint32_t)++anfds [fd].egen << 32);
  79. ev.events = (nev & EV_READ ? EPOLLIN : 0)
  80. | (nev & EV_WRITE ? EPOLLOUT : 0);
  81. if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev)))
  82. return;
  83. if (expect_true (errno == ENOENT))
  84. {
  85. /* if ENOENT then the fd went away, so try to do the right thing */
  86. if (!nev)
  87. goto dec_egen;
  88. if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev))
  89. return;
  90. }
  91. else if (expect_true (errno == EEXIST))
  92. {
  93. /* EEXIST means we ignored a previous DEL, but the fd is still active */
  94. /* if the kernel mask is the same as the new mask, we assume it hasn't changed */
  95. if (oldmask == nev)
  96. goto dec_egen;
  97. if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev))
  98. return;
  99. }
  100. fd_kill (EV_A_ fd);
  101. dec_egen:
  102. /* we didn't successfully call epoll_ctl, so decrement the generation counter again */
  103. --anfds [fd].egen;
  104. }
  105. static void
  106. epoll_poll (EV_P_ ev_tstamp timeout)
  107. {
  108. int i;
  109. int eventcnt;
  110. EV_RELEASE_CB;
  111. eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.));
  112. EV_ACQUIRE_CB;
  113. if (expect_false (eventcnt < 0))
  114. {
  115. if (errno != EINTR)
  116. ev_syserr ("(libev) epoll_wait");
  117. return;
  118. }
  119. for (i = 0; i < eventcnt; ++i)
  120. {
  121. struct epoll_event *ev = epoll_events + i;
  122. int fd = (uint32_t)ev->data.u64; /* mask out the lower 32 bits */
  123. int want = anfds [fd].events;
  124. int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
  125. | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0);
  126. /* check for spurious notification */
  127. if (expect_false ((uint32_t)anfds [fd].egen != (uint32_t)(ev->data.u64 >> 32)))
  128. {
  129. /* recreate kernel state */
  130. postfork = 1;
  131. continue;
  132. }
  133. if (expect_false (got & ~want))
  134. {
  135. anfds [fd].emask = want;
  136. /* we received an event but are not interested in it, try mod or del */
  137. /* I don't think we ever need MOD, but let's handle it anyways */
  138. ev->events = (want & EV_READ ? EPOLLIN : 0)
  139. | (want & EV_WRITE ? EPOLLOUT : 0);
  140. if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev))
  141. {
  142. postfork = 1; /* an error occured, recreate kernel state */
  143. continue;
  144. }
  145. }
  146. fd_event (EV_A_ fd, got);
  147. }
  148. /* if the receive array was full, increase its size */
  149. if (expect_false (eventcnt == epoll_eventmax))
  150. {
  151. ev_free (epoll_events);
  152. epoll_eventmax = array_nextsize (sizeof (struct epoll_event), epoll_eventmax, epoll_eventmax + 1);
  153. epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
  154. }
  155. }
  156. int inline_size
  157. epoll_init (EV_P_ int flags)
  158. {
  159. backend_fd = epoll_create (256);
  160. if (backend_fd < 0)
  161. return 0;
  162. fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
  163. backend_fudge = 0.; /* kernel sources seem to indicate this to be zero */
  164. backend_modify = epoll_modify;
  165. backend_poll = epoll_poll;
  166. epoll_eventmax = 64; /* initial number of events receivable per poll */
  167. epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
  168. return EVBACKEND_EPOLL;
  169. }
  170. void inline_size
  171. epoll_destroy (EV_P)
  172. {
  173. ev_free (epoll_events);
  174. }
  175. void inline_size
  176. epoll_fork (EV_P)
  177. {
  178. close (backend_fd);
  179. while ((backend_fd = epoll_create (256)) < 0)
  180. ev_syserr ("(libev) epoll_create");
  181. fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
  182. fd_rearm_all (EV_A);
  183. }