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.

191 lines
5.5 KiB

14 years ago
14 years ago
14 years ago
14 years ago
  1. /*
  2. * libev kqueue backend
  3. *
  4. * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials provided
  17. * with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <sys/types.h>
  32. #include <sys/time.h>
  33. #include <sys/queue.h>
  34. #include <sys/event.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. void inline_speed
  38. kqueue_change (EV_P_ int fd, int filter, int flags, int fflags)
  39. {
  40. struct kevent *ke;
  41. ++kqueue_changecnt;
  42. array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2);
  43. EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0);
  44. }
  45. #ifndef NOTE_EOF
  46. # define NOTE_EOF 0
  47. #endif
  48. static void
  49. kqueue_modify (EV_P_ int fd, int oev, int nev)
  50. {
  51. if (oev != nev)
  52. {
  53. if (oev & EV_READ)
  54. kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0);
  55. if (oev & EV_WRITE)
  56. kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0);
  57. }
  58. /* to detect close/reopen reliably, we have to re-add */
  59. /* event requests even when oev == nev */
  60. if (nev & EV_READ)
  61. kqueue_change (EV_A_ fd, EVFILT_READ , EV_ADD, NOTE_EOF);
  62. if (nev & EV_WRITE)
  63. kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD, NOTE_EOF);
  64. }
  65. static void
  66. kqueue_poll (EV_P_ ev_tstamp timeout)
  67. {
  68. int res, i;
  69. struct timespec ts;
  70. /* need to resize so there is enough space for errors */
  71. if (kqueue_changecnt > kqueue_eventmax)
  72. {
  73. ev_free (kqueue_events);
  74. kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_changecnt);
  75. kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  76. }
  77. ts.tv_sec = (time_t)timeout;
  78. ts.tv_nsec = (long)((timeout - (ev_tstamp)ts.tv_sec) * 1e9);
  79. res = kevent (backend_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts);
  80. kqueue_changecnt = 0;
  81. if (res < 0)
  82. {
  83. if (errno != EINTR)
  84. syserr ("(libev) kevent");
  85. return;
  86. }
  87. for (i = 0; i < res; ++i)
  88. {
  89. int fd = kqueue_events [i].ident;
  90. if (expect_false (kqueue_events [i].flags & EV_ERROR))
  91. {
  92. int err = kqueue_events [i].data;
  93. /* we are only interested in errors for fds that we are interested in :) */
  94. if (anfds [fd].events)
  95. {
  96. if (err == ENOENT) /* resubmit changes on ENOENT */
  97. kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
  98. else if (err == EBADF) /* on EBADF, we re-check the fd */
  99. {
  100. if (fd_valid (fd))
  101. kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
  102. else
  103. fd_kill (EV_A_ fd);
  104. }
  105. else /* on all other errors, we error out on the fd */
  106. fd_kill (EV_A_ fd);
  107. }
  108. }
  109. else
  110. fd_event (
  111. EV_A_
  112. fd,
  113. kqueue_events [i].filter == EVFILT_READ ? EV_READ
  114. : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE
  115. : 0
  116. );
  117. }
  118. if (expect_false (res == kqueue_eventmax))
  119. {
  120. ev_free (kqueue_events);
  121. kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_eventmax + 1);
  122. kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  123. }
  124. }
  125. int inline_size
  126. kqueue_init (EV_P_ int flags)
  127. {
  128. struct kevent ch, ev;
  129. /* Initalize the kernel queue */
  130. if ((backend_fd = kqueue ()) < 0)
  131. return 0;
  132. fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
  133. /* fudge *might* be zero from the documentation, but bsd docs are notoriously wrong */
  134. backend_fudge = 1e-3; /* needed to compensate for kevent returning early */
  135. backend_modify = kqueue_modify;
  136. backend_poll = kqueue_poll;
  137. kqueue_eventmax = 64; /* initial number of events receivable per poll */
  138. kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  139. kqueue_changes = 0;
  140. kqueue_changemax = 0;
  141. kqueue_changecnt = 0;
  142. return EVBACKEND_KQUEUE;
  143. }
  144. void inline_size
  145. kqueue_destroy (EV_P)
  146. {
  147. ev_free (kqueue_events);
  148. ev_free (kqueue_changes);
  149. }
  150. void inline_size
  151. kqueue_fork (EV_P)
  152. {
  153. close (backend_fd);
  154. while ((backend_fd = kqueue ()) < 0)
  155. syserr ("(libev) kqueue");
  156. fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
  157. /* re-register interest in fds */
  158. fd_rearm_all (EV_A);
  159. }