2
0

uloop-kqueue.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * uloop - event loop implementation
  3. *
  4. * Copyright (C) 2010-2016 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. static int uloop_init_pollfd(void)
  19. {
  20. struct timespec timeout = { 0, 0 };
  21. struct kevent ev = {};
  22. if (poll_fd >= 0)
  23. return 0;
  24. poll_fd = kqueue();
  25. if (poll_fd < 0)
  26. return -1;
  27. EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
  28. kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
  29. return 0;
  30. }
  31. static uint16_t get_flags(unsigned int flags, unsigned int mask)
  32. {
  33. uint16_t kflags = 0;
  34. if (!(flags & mask))
  35. return EV_DELETE;
  36. kflags = EV_ADD;
  37. if (flags & ULOOP_EDGE_TRIGGER)
  38. kflags |= EV_CLEAR;
  39. return kflags;
  40. }
  41. static struct kevent events[ULOOP_MAX_EVENTS];
  42. static int register_kevent(struct uloop_fd *fd, unsigned int flags)
  43. {
  44. struct timespec timeout = { 0, 0 };
  45. struct kevent ev[2];
  46. int nev = 0;
  47. unsigned int fl = 0;
  48. unsigned int changed;
  49. uint16_t kflags;
  50. if (flags & ULOOP_EDGE_DEFER)
  51. flags &= ~ULOOP_EDGE_TRIGGER;
  52. changed = flags ^ fd->flags;
  53. if (changed & ULOOP_EDGE_TRIGGER)
  54. changed |= flags;
  55. if (changed & ULOOP_READ) {
  56. kflags = get_flags(flags, ULOOP_READ);
  57. EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
  58. }
  59. if (changed & ULOOP_WRITE) {
  60. kflags = get_flags(flags, ULOOP_WRITE);
  61. EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
  62. }
  63. if (!flags)
  64. fl |= EV_DELETE;
  65. fd->flags = flags;
  66. if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
  67. return -1;
  68. return 0;
  69. }
  70. static int register_poll(struct uloop_fd *fd, unsigned int flags)
  71. {
  72. if (flags & ULOOP_EDGE_TRIGGER)
  73. flags |= ULOOP_EDGE_DEFER;
  74. else
  75. flags &= ~ULOOP_EDGE_DEFER;
  76. return register_kevent(fd, flags);
  77. }
  78. static int __uloop_fd_delete(struct uloop_fd *fd)
  79. {
  80. return register_poll(fd, 0);
  81. }
  82. static int uloop_fetch_events(int timeout)
  83. {
  84. struct timespec ts;
  85. int nfds, n;
  86. if (timeout >= 0) {
  87. ts.tv_sec = timeout / 1000;
  88. ts.tv_nsec = (timeout % 1000) * 1000000;
  89. }
  90. nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
  91. for (n = 0; n < nfds; n++) {
  92. struct uloop_fd_event *cur = &cur_fds[n];
  93. struct uloop_fd *u = events[n].udata;
  94. unsigned int ev = 0;
  95. cur->fd = u;
  96. if (!u)
  97. continue;
  98. if (events[n].flags & EV_ERROR) {
  99. u->error = true;
  100. if (!(u->flags & ULOOP_ERROR_CB))
  101. uloop_fd_delete(u);
  102. }
  103. if(events[n].filter == EVFILT_READ)
  104. ev |= ULOOP_READ;
  105. else if (events[n].filter == EVFILT_WRITE)
  106. ev |= ULOOP_WRITE;
  107. if (events[n].flags & EV_EOF)
  108. u->eof = true;
  109. else if (!ev)
  110. cur->fd = NULL;
  111. cur->events = ev;
  112. if (u->flags & ULOOP_EDGE_DEFER) {
  113. u->flags &= ~ULOOP_EDGE_DEFER;
  114. u->flags |= ULOOP_EDGE_TRIGGER;
  115. register_kevent(u, u->flags);
  116. }
  117. }
  118. return nfds;
  119. }