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.

203 lines
5.5 KiB

14 years ago
14 years ago
  1. /*
  2. * Copyright 2007 Marc Alexander Lehmann <libev@schmorp.de>
  3. * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <sys/types.h>
  30. #include <sys/time.h>
  31. #include <sys/queue.h>
  32. #include <sys/event.h>
  33. #include <string.h>
  34. #include <errno.h>
  35. static void
  36. kqueue_change (EV_P_ int fd, int filter, int flags, int fflags)
  37. {
  38. struct kevent *ke;
  39. array_needsize (kqueue_changes, kqueue_changemax, ++kqueue_changecnt, );
  40. ke = &kqueue_changes [kqueue_changecnt - 1];
  41. memset (ke, 0, sizeof (struct kevent));
  42. ke->ident = fd;
  43. ke->filter = filter;
  44. ke->flags = flags;
  45. ke->fflags = fflags;
  46. }
  47. #ifndef NOTE_EOF
  48. # define NOTE_EOF 0
  49. #endif
  50. static void
  51. kqueue_modify (EV_P_ int fd, int oev, int nev)
  52. {
  53. if ((oev ^ nev) & EV_READ)
  54. {
  55. if (nev & EV_READ)
  56. kqueue_change (fd, EVFILT_READ, EV_ADD, NOTE_EOF);
  57. else
  58. kqueue_change (fd, EVFILT_READ, EV_DELETE, 0);
  59. }
  60. if ((oev ^ nev) & EV_WRITE)
  61. {
  62. if (nev & EV_WRITE)
  63. kqueue_change (fd, EVFILT_WRITE, EV_ADD, NOTE_EOF);
  64. else
  65. kqueue_change (fd, EVFILT_WRITE, EV_DELETE, 0);
  66. }
  67. }
  68. static void
  69. kqueue_poll (EV_P_ ev_tstamp timeout)
  70. {
  71. int res, i;
  72. struct timespec ts;
  73. ts.tv_sec = (time_t)timeout;
  74. ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9;
  75. res = kevent (kqueue_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts);
  76. kqueue_changecnt = 0;
  77. if (res < 0)
  78. {
  79. if (errno != EINTR)
  80. syserr ("(libev) kevent");
  81. return;
  82. }
  83. for (i = 0; i < res; ++i)
  84. {
  85. if (kqueue_events [i].flags & EV_ERROR)
  86. {
  87. /*
  88. * Error messages that can happen, when a delete fails.
  89. * EBADF happens when the file discriptor has been
  90. * closed,
  91. * ENOENT when the file discriptor was closed and
  92. * then reopened.
  93. * EINVAL for some reasons not understood; EINVAL
  94. * should not be returned ever; but FreeBSD does :-\
  95. * An error is also indicated when a callback deletes
  96. * an event we are still processing. In that case
  97. * the data field is set to ENOENT.
  98. */
  99. if (kqueue_events [i].data == EBADF)
  100. fd_kill (EV_A_ kqueue_events [i].ident);
  101. }
  102. else
  103. fd_event (
  104. EV_A_
  105. kqueue_events [i].ident,
  106. kqueue_events [i].filter == EVFILT_READ ? EV_READ
  107. : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE
  108. : 0
  109. );
  110. }
  111. if (expect_false (res == kqueue_eventmax))
  112. {
  113. ev_free (kqueue_events);
  114. kqueue_eventmax = array_roundsize (kqueue_events, kqueue_eventmax << 1);
  115. kqueue_events = ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  116. }
  117. }
  118. static int
  119. kqueue_init (EV_P_ int flags)
  120. {
  121. struct kevent ch, ev;
  122. /* Initalize the kernel queue */
  123. if ((kqueue_fd = kqueue ()) < 0)
  124. return 0;
  125. fcntl (kqueue_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
  126. /* Check for Mac OS X kqueue bug. */
  127. ch.ident = -1;
  128. ch.filter = EVFILT_READ;
  129. ch.flags = EV_ADD;
  130. /*
  131. * If kqueue works, then kevent will succeed, and it will
  132. * stick an error in ev. If kqueue is broken, then
  133. * kevent will fail.
  134. */
  135. if (kevent (kqueue_fd, &ch, 1, &ev, 1, 0) != 1
  136. || ev.ident != -1
  137. || ev.flags != EV_ERROR)
  138. {
  139. /* detected broken kqueue */
  140. close (kqueue_fd);
  141. return 0;
  142. }
  143. method_fudge = 1e-3; /* needed to compensate for kevent returning early */
  144. method_modify = kqueue_modify;
  145. method_poll = kqueue_poll;
  146. kqueue_eventmax = 64; /* intiial number of events receivable per poll */
  147. kqueue_events = ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  148. kqueue_changes = 0;
  149. kqueue_changemax = 0;
  150. kqueue_changecnt = 0;
  151. return EVMETHOD_KQUEUE;
  152. }
  153. static void
  154. kqueue_destroy (EV_P)
  155. {
  156. close (kqueue_fd);
  157. ev_free (kqueue_events);
  158. ev_free (kqueue_changes);
  159. }
  160. static void
  161. kqueue_fork (EV_P)
  162. {
  163. for (;;)
  164. {
  165. kqueue_fd = kqueue ();
  166. if (kqueue_fd >= 0)
  167. break;
  168. syserr ("(libev) kqueue");
  169. }
  170. fcntl (kqueue_fd, F_SETFD, FD_CLOEXEC);
  171. /* re-register interest in fds */
  172. fd_rearm_all (EV_A);
  173. }