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.

122 lines
3.6 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
  1. /*
  2. * libev epoll fd activity 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/epoll.h>
  32. static void
  33. epoll_modify (EV_P_ int fd, int oev, int nev)
  34. {
  35. int mode = nev ? oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL;
  36. struct epoll_event ev;
  37. ev.data.u64 = fd; /* use u64 to fully initialise the struct, for nicer strace etc. */
  38. ev.events =
  39. (nev & EV_READ ? EPOLLIN : 0)
  40. | (nev & EV_WRITE ? EPOLLOUT : 0);
  41. if (epoll_ctl (backend_fd, mode, fd, &ev))
  42. if (errno != ENOENT /* on ENOENT the fd went away, so try to do the right thing */
  43. || (nev && epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev)))
  44. fd_kill (EV_A_ fd);
  45. }
  46. static void
  47. epoll_poll (EV_P_ ev_tstamp timeout)
  48. {
  49. int i;
  50. int eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.));
  51. if (eventcnt < 0)
  52. {
  53. if (errno != EINTR)
  54. syserr ("(libev) epoll_wait");
  55. return;
  56. }
  57. for (i = 0; i < eventcnt; ++i)
  58. fd_event (
  59. EV_A_
  60. epoll_events [i].data.u64,
  61. (epoll_events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
  62. | (epoll_events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0)
  63. );
  64. /* if the receive array was full, increase its size */
  65. if (expect_false (eventcnt == epoll_eventmax))
  66. {
  67. ev_free (epoll_events);
  68. epoll_eventmax = array_roundsize (epoll_events, epoll_eventmax << 1);
  69. epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
  70. }
  71. }
  72. static int
  73. epoll_init (EV_P_ int flags)
  74. {
  75. backend_fd = epoll_create (256);
  76. if (backend_fd < 0)
  77. return 0;
  78. fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
  79. backend_fudge = 1e-3; /* needed to compensate for epoll returning early */
  80. backend_modify = epoll_modify;
  81. backend_poll = epoll_poll;
  82. epoll_eventmax = 64; /* intiial number of events receivable per poll */
  83. epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
  84. return EVBACKEND_EPOLL;
  85. }
  86. static void
  87. epoll_destroy (EV_P)
  88. {
  89. close (backend_fd);
  90. ev_free (epoll_events);
  91. }
  92. static void
  93. epoll_fork (EV_P)
  94. {
  95. close (backend_fd);
  96. while ((backend_fd = epoll_create (256)) < 0)
  97. syserr ("(libev) epoll_create");
  98. fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
  99. fd_rearm_all (EV_A);
  100. }