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.

194 lines
6.0 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /*
  2. * libev kqueue 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. #include <sys/types.h>
  40. #include <sys/time.h>
  41. #include <sys/queue.h>
  42. #include <sys/event.h>
  43. #include <string.h>
  44. #include <errno.h>
  45. void inline_speed
  46. kqueue_change (EV_P_ int fd, int filter, int flags, int fflags)
  47. {
  48. ++kqueue_changecnt;
  49. array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2);
  50. EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0);
  51. }
  52. #ifndef NOTE_EOF
  53. # define NOTE_EOF 0
  54. #endif
  55. static void
  56. kqueue_modify (EV_P_ int fd, int oev, int nev)
  57. {
  58. if (oev != nev)
  59. {
  60. if (oev & EV_READ)
  61. kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0);
  62. if (oev & EV_WRITE)
  63. kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0);
  64. }
  65. /* to detect close/reopen reliably, we have to re-add */
  66. /* event requests even when oev == nev */
  67. if (nev & EV_READ)
  68. kqueue_change (EV_A_ fd, EVFILT_READ , EV_ADD, NOTE_EOF);
  69. if (nev & EV_WRITE)
  70. kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD, NOTE_EOF);
  71. }
  72. static void
  73. kqueue_poll (EV_P_ ev_tstamp timeout)
  74. {
  75. int res, i;
  76. struct timespec ts;
  77. /* need to resize so there is enough space for errors */
  78. if (kqueue_changecnt > kqueue_eventmax)
  79. {
  80. ev_free (kqueue_events);
  81. kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_changecnt);
  82. kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  83. }
  84. ts.tv_sec = (time_t)timeout;
  85. ts.tv_nsec = (long)((timeout - (ev_tstamp)ts.tv_sec) * 1e9);
  86. res = kevent (backend_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts);
  87. kqueue_changecnt = 0;
  88. if (expect_false (res < 0))
  89. {
  90. if (errno != EINTR)
  91. syserr ("(libev) kevent");
  92. return;
  93. }
  94. for (i = 0; i < res; ++i)
  95. {
  96. int fd = kqueue_events [i].ident;
  97. if (expect_false (kqueue_events [i].flags & EV_ERROR))
  98. {
  99. int err = kqueue_events [i].data;
  100. /* we are only interested in errors for fds that we are interested in :) */
  101. if (anfds [fd].events)
  102. {
  103. if (err == ENOENT) /* resubmit changes on ENOENT */
  104. kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
  105. else if (err == EBADF) /* on EBADF, we re-check the fd */
  106. {
  107. if (fd_valid (fd))
  108. kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
  109. else
  110. fd_kill (EV_A_ fd);
  111. }
  112. else /* on all other errors, we error out on the fd */
  113. fd_kill (EV_A_ fd);
  114. }
  115. }
  116. else
  117. fd_event (
  118. EV_A_
  119. fd,
  120. kqueue_events [i].filter == EVFILT_READ ? EV_READ
  121. : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE
  122. : 0
  123. );
  124. }
  125. if (expect_false (res == kqueue_eventmax))
  126. {
  127. ev_free (kqueue_events);
  128. kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_eventmax + 1);
  129. kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  130. }
  131. }
  132. int inline_size
  133. kqueue_init (EV_P_ int flags)
  134. {
  135. /* Initalize the kernel queue */
  136. if ((backend_fd = kqueue ()) < 0)
  137. return 0;
  138. fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
  139. backend_fudge = 0.;
  140. backend_modify = kqueue_modify;
  141. backend_poll = kqueue_poll;
  142. kqueue_eventmax = 64; /* initial number of events receivable per poll */
  143. kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  144. kqueue_changes = 0;
  145. kqueue_changemax = 0;
  146. kqueue_changecnt = 0;
  147. return EVBACKEND_KQUEUE;
  148. }
  149. void inline_size
  150. kqueue_destroy (EV_P)
  151. {
  152. ev_free (kqueue_events);
  153. ev_free (kqueue_changes);
  154. }
  155. void inline_size
  156. kqueue_fork (EV_P)
  157. {
  158. close (backend_fd);
  159. while ((backend_fd = kqueue ()) < 0)
  160. syserr ("(libev) kqueue");
  161. fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
  162. /* re-register interest in fds */
  163. fd_rearm_all (EV_A);
  164. }