posix-poll.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Copyright libuv project contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "internal.h"
  23. /* POSIX defines poll() as a portable way to wait on file descriptors.
  24. * Here we maintain a dynamically sized array of file descriptors and
  25. * events to pass as the first argument to poll().
  26. */
  27. #include <assert.h>
  28. #include <stddef.h>
  29. #include <stdint.h>
  30. #include <errno.h>
  31. #include <unistd.h>
  32. int uv__platform_loop_init(uv_loop_t* loop) {
  33. loop->poll_fds = NULL;
  34. loop->poll_fds_used = 0;
  35. loop->poll_fds_size = 0;
  36. loop->poll_fds_iterating = 0;
  37. return 0;
  38. }
  39. void uv__platform_loop_delete(uv_loop_t* loop) {
  40. uv__free(loop->poll_fds);
  41. loop->poll_fds = NULL;
  42. }
  43. int uv__io_fork(uv_loop_t* loop) {
  44. uv__platform_loop_delete(loop);
  45. return uv__platform_loop_init(loop);
  46. }
  47. /* Allocate or dynamically resize our poll fds array. */
  48. static void uv__pollfds_maybe_resize(uv_loop_t* loop) {
  49. size_t i;
  50. size_t n;
  51. struct pollfd* p;
  52. if (loop->poll_fds_used < loop->poll_fds_size)
  53. return;
  54. n = loop->poll_fds_size ? loop->poll_fds_size * 2 : 64;
  55. p = uv__reallocf(loop->poll_fds, n * sizeof(*loop->poll_fds));
  56. if (p == NULL)
  57. abort();
  58. loop->poll_fds = p;
  59. for (i = loop->poll_fds_size; i < n; i++) {
  60. loop->poll_fds[i].fd = -1;
  61. loop->poll_fds[i].events = 0;
  62. loop->poll_fds[i].revents = 0;
  63. }
  64. loop->poll_fds_size = n;
  65. }
  66. /* Primitive swap operation on poll fds array elements. */
  67. static void uv__pollfds_swap(uv_loop_t* loop, size_t l, size_t r) {
  68. struct pollfd pfd;
  69. pfd = loop->poll_fds[l];
  70. loop->poll_fds[l] = loop->poll_fds[r];
  71. loop->poll_fds[r] = pfd;
  72. }
  73. /* Add a watcher's fd to our poll fds array with its pending events. */
  74. static void uv__pollfds_add(uv_loop_t* loop, uv__io_t* w) {
  75. size_t i;
  76. struct pollfd* pe;
  77. /* If the fd is already in the set just update its events. */
  78. assert(!loop->poll_fds_iterating);
  79. for (i = 0; i < loop->poll_fds_used; ++i) {
  80. if (loop->poll_fds[i].fd == w->fd) {
  81. loop->poll_fds[i].events = w->pevents;
  82. return;
  83. }
  84. }
  85. /* Otherwise, allocate a new slot in the set for the fd. */
  86. uv__pollfds_maybe_resize(loop);
  87. pe = &loop->poll_fds[loop->poll_fds_used++];
  88. pe->fd = w->fd;
  89. pe->events = w->pevents;
  90. }
  91. /* Remove a watcher's fd from our poll fds array. */
  92. static void uv__pollfds_del(uv_loop_t* loop, int fd) {
  93. size_t i;
  94. assert(!loop->poll_fds_iterating);
  95. for (i = 0; i < loop->poll_fds_used;) {
  96. if (loop->poll_fds[i].fd == fd) {
  97. /* swap to last position and remove */
  98. --loop->poll_fds_used;
  99. uv__pollfds_swap(loop, i, loop->poll_fds_used);
  100. loop->poll_fds[loop->poll_fds_used].fd = -1;
  101. loop->poll_fds[loop->poll_fds_used].events = 0;
  102. loop->poll_fds[loop->poll_fds_used].revents = 0;
  103. /* This method is called with an fd of -1 to purge the invalidated fds,
  104. * so we may possibly have multiples to remove.
  105. */
  106. if (-1 != fd)
  107. return;
  108. } else {
  109. /* We must only increment the loop counter when the fds do not match.
  110. * Otherwise, when we are purging an invalidated fd, the value just
  111. * swapped here from the previous end of the array will be skipped.
  112. */
  113. ++i;
  114. }
  115. }
  116. }
  117. void uv__io_poll(uv_loop_t* loop, int timeout) {
  118. sigset_t* pset;
  119. sigset_t set;
  120. uint64_t time_base;
  121. uint64_t time_diff;
  122. QUEUE* q;
  123. uv__io_t* w;
  124. size_t i;
  125. unsigned int nevents;
  126. int nfds;
  127. int have_signals;
  128. struct pollfd* pe;
  129. int fd;
  130. if (loop->nfds == 0) {
  131. assert(QUEUE_EMPTY(&loop->watcher_queue));
  132. return;
  133. }
  134. /* Take queued watchers and add their fds to our poll fds array. */
  135. while (!QUEUE_EMPTY(&loop->watcher_queue)) {
  136. q = QUEUE_HEAD(&loop->watcher_queue);
  137. QUEUE_REMOVE(q);
  138. QUEUE_INIT(q);
  139. w = QUEUE_DATA(q, uv__io_t, watcher_queue);
  140. assert(w->pevents != 0);
  141. assert(w->fd >= 0);
  142. assert(w->fd < (int) loop->nwatchers);
  143. uv__pollfds_add(loop, w);
  144. w->events = w->pevents;
  145. }
  146. /* Prepare a set of signals to block around poll(), if any. */
  147. pset = NULL;
  148. if (loop->flags & UV_LOOP_BLOCK_SIGPROF) {
  149. pset = &set;
  150. sigemptyset(pset);
  151. sigaddset(pset, SIGPROF);
  152. }
  153. assert(timeout >= -1);
  154. time_base = loop->time;
  155. /* Loop calls to poll() and processing of results. If we get some
  156. * results from poll() but they turn out not to be interesting to
  157. * our caller then we need to loop around and poll() again.
  158. */
  159. for (;;) {
  160. if (pset != NULL)
  161. if (pthread_sigmask(SIG_BLOCK, pset, NULL))
  162. abort();
  163. nfds = poll(loop->poll_fds, (nfds_t)loop->poll_fds_used, timeout);
  164. if (pset != NULL)
  165. if (pthread_sigmask(SIG_UNBLOCK, pset, NULL))
  166. abort();
  167. /* Update loop->time unconditionally. It's tempting to skip the update when
  168. * timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
  169. * operating system didn't reschedule our process while in the syscall.
  170. */
  171. SAVE_ERRNO(uv__update_time(loop));
  172. if (nfds == 0) {
  173. assert(timeout != -1);
  174. return;
  175. }
  176. if (nfds == -1) {
  177. if (errno != EINTR)
  178. abort();
  179. if (timeout == -1)
  180. continue;
  181. if (timeout == 0)
  182. return;
  183. /* Interrupted by a signal. Update timeout and poll again. */
  184. goto update_timeout;
  185. }
  186. /* Tell uv__platform_invalidate_fd not to manipulate our array
  187. * while we are iterating over it.
  188. */
  189. loop->poll_fds_iterating = 1;
  190. /* Initialize a count of events that we care about. */
  191. nevents = 0;
  192. have_signals = 0;
  193. /* Loop over the entire poll fds array looking for returned events. */
  194. for (i = 0; i < loop->poll_fds_used; i++) {
  195. pe = loop->poll_fds + i;
  196. fd = pe->fd;
  197. /* Skip invalidated events, see uv__platform_invalidate_fd. */
  198. if (fd == -1)
  199. continue;
  200. assert(fd >= 0);
  201. assert((unsigned) fd < loop->nwatchers);
  202. w = loop->watchers[fd];
  203. if (w == NULL) {
  204. /* File descriptor that we've stopped watching, ignore. */
  205. uv__platform_invalidate_fd(loop, fd);
  206. continue;
  207. }
  208. /* Filter out events that user has not requested us to watch
  209. * (e.g. POLLNVAL).
  210. */
  211. pe->revents &= w->pevents | POLLERR | POLLHUP;
  212. if (pe->revents != 0) {
  213. /* Run signal watchers last. */
  214. if (w == &loop->signal_io_watcher) {
  215. have_signals = 1;
  216. } else {
  217. w->cb(loop, w, pe->revents);
  218. }
  219. nevents++;
  220. }
  221. }
  222. if (have_signals != 0)
  223. loop->signal_io_watcher.cb(loop, &loop->signal_io_watcher, POLLIN);
  224. loop->poll_fds_iterating = 0;
  225. /* Purge invalidated fds from our poll fds array. */
  226. uv__pollfds_del(loop, -1);
  227. if (have_signals != 0)
  228. return; /* Event loop should cycle now so don't poll again. */
  229. if (nevents != 0)
  230. return;
  231. if (timeout == 0)
  232. return;
  233. if (timeout == -1)
  234. continue;
  235. update_timeout:
  236. assert(timeout > 0);
  237. time_diff = loop->time - time_base;
  238. if (time_diff >= (uint64_t) timeout)
  239. return;
  240. timeout -= time_diff;
  241. }
  242. }
  243. /* Remove the given fd from our poll fds array because no one
  244. * is interested in its events anymore.
  245. */
  246. void uv__platform_invalidate_fd(uv_loop_t* loop, int fd) {
  247. size_t i;
  248. assert(fd >= 0);
  249. if (loop->poll_fds_iterating) {
  250. /* uv__io_poll is currently iterating. Just invalidate fd. */
  251. for (i = 0; i < loop->poll_fds_used; i++)
  252. if (loop->poll_fds[i].fd == fd) {
  253. loop->poll_fds[i].fd = -1;
  254. loop->poll_fds[i].events = 0;
  255. loop->poll_fds[i].revents = 0;
  256. }
  257. } else {
  258. /* uv__io_poll is not iterating. Delete fd from the set. */
  259. uv__pollfds_del(loop, fd);
  260. }
  261. }
  262. /* Check whether the given fd is supported by poll(). */
  263. int uv__io_check_fd(uv_loop_t* loop, int fd) {
  264. struct pollfd p[1];
  265. int rv;
  266. p[0].fd = fd;
  267. p[0].events = POLLIN;
  268. do
  269. rv = poll(p, 1, 0);
  270. while (rv == -1 && (errno == EINTR || errno == EAGAIN));
  271. if (rv == -1)
  272. return UV__ERR(errno);
  273. if (p[0].revents & POLLNVAL)
  274. return UV_EINVAL;
  275. return 0;
  276. }