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.

ev_poll.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * libev poll fd activity backend
  3. *
  4. * Copyright (c) 2007,2008,2009,2010,2011,2016,2019 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 <poll.h>
  40. inline_size
  41. void
  42. array_needsize_pollidx (int *base, int offset, int count)
  43. {
  44. /* using memset (.., -1, ...) is tempting, we we try
  45. * to be ultraportable
  46. */
  47. base += offset;
  48. while (count--)
  49. *base++ = -1;
  50. }
  51. static void
  52. poll_modify (EV_P_ int fd, int oev, int nev)
  53. {
  54. int idx;
  55. if (oev == nev)
  56. return;
  57. array_needsize (int, pollidxs, pollidxmax, fd + 1, array_needsize_pollidx);
  58. idx = pollidxs [fd];
  59. if (idx < 0) /* need to allocate a new pollfd */
  60. {
  61. pollidxs [fd] = idx = pollcnt++;
  62. array_needsize (struct pollfd, polls, pollmax, pollcnt, array_needsize_noinit);
  63. polls [idx].fd = fd;
  64. }
  65. assert (polls [idx].fd == fd);
  66. if (nev)
  67. polls [idx].events =
  68. (nev & EV_READ ? POLLIN : 0)
  69. | (nev & EV_WRITE ? POLLOUT : 0);
  70. else /* remove pollfd */
  71. {
  72. pollidxs [fd] = -1;
  73. if (ecb_expect_true (idx < --pollcnt))
  74. {
  75. polls [idx] = polls [pollcnt];
  76. pollidxs [polls [idx].fd] = idx;
  77. }
  78. }
  79. }
  80. static void
  81. poll_poll (EV_P_ ev_tstamp timeout)
  82. {
  83. struct pollfd *p;
  84. int res;
  85. EV_RELEASE_CB;
  86. res = poll (polls, pollcnt, EV_TS_TO_MSEC (timeout));
  87. EV_ACQUIRE_CB;
  88. if (ecb_expect_false (res < 0))
  89. {
  90. if (errno == EBADF)
  91. fd_ebadf (EV_A);
  92. else if (errno == ENOMEM && !syserr_cb)
  93. fd_enomem (EV_A);
  94. else if (errno != EINTR)
  95. ev_syserr ("(libev) poll");
  96. }
  97. else
  98. for (p = polls; res; ++p)
  99. {
  100. assert (("libev: poll returned illegal result, broken BSD kernel?", p < polls + pollcnt));
  101. if (ecb_expect_false (p->revents)) /* this expect is debatable */
  102. {
  103. --res;
  104. if (ecb_expect_false (p->revents & POLLNVAL))
  105. {
  106. assert (("libev: poll found invalid fd in poll set", 0));
  107. fd_kill (EV_A_ p->fd);
  108. }
  109. else
  110. fd_event (
  111. EV_A_
  112. p->fd,
  113. (p->revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
  114. | (p->revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
  115. );
  116. }
  117. }
  118. }
  119. inline_size
  120. int
  121. poll_init (EV_P_ int flags)
  122. {
  123. backend_mintime = EV_TS_CONST (1e-3);
  124. backend_modify = poll_modify;
  125. backend_poll = poll_poll;
  126. pollidxs = 0; pollidxmax = 0;
  127. polls = 0; pollmax = 0; pollcnt = 0;
  128. return EVBACKEND_POLL;
  129. }
  130. inline_size
  131. void
  132. poll_destroy (EV_P)
  133. {
  134. ev_free (pollidxs);
  135. ev_free (polls);
  136. }