kqueue.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_arch_poll_private.h"
  17. #ifdef POLLSET_USES_KQUEUE
  18. static fspr_int16_t get_kqueue_revent(fspr_int16_t event, fspr_int16_t flags)
  19. {
  20. fspr_int16_t rv = 0;
  21. if (event == EVFILT_READ)
  22. rv |= APR_POLLIN;
  23. if (event == EVFILT_WRITE)
  24. rv |= APR_POLLOUT;
  25. if (flags & EV_EOF)
  26. rv |= APR_POLLHUP;
  27. if (flags & EV_ERROR)
  28. rv |= APR_POLLERR;
  29. return rv;
  30. }
  31. struct fspr_pollset_t
  32. {
  33. fspr_pool_t *pool;
  34. fspr_uint32_t nelts;
  35. fspr_uint32_t nalloc;
  36. int kqueue_fd;
  37. struct kevent kevent;
  38. struct kevent *ke_set;
  39. fspr_pollfd_t *result_set;
  40. fspr_uint32_t flags;
  41. #if APR_HAS_THREADS
  42. /* A thread mutex to protect operations on the rings */
  43. fspr_thread_mutex_t *ring_lock;
  44. #endif
  45. /* A ring containing all of the pollfd_t that are active */
  46. APR_RING_HEAD(pfd_query_ring_t, pfd_elem_t) query_ring;
  47. /* A ring of pollfd_t that have been used, and then _remove'd */
  48. APR_RING_HEAD(pfd_free_ring_t, pfd_elem_t) free_ring;
  49. /* A ring of pollfd_t where rings that have been _remove'd but
  50. might still be inside a _poll */
  51. APR_RING_HEAD(pfd_dead_ring_t, pfd_elem_t) dead_ring;
  52. };
  53. static fspr_status_t backend_cleanup(void *p_)
  54. {
  55. fspr_pollset_t *pollset = (fspr_pollset_t *) p_;
  56. close(pollset->kqueue_fd);
  57. return APR_SUCCESS;
  58. }
  59. APR_DECLARE(fspr_status_t) fspr_pollset_create(fspr_pollset_t **pollset,
  60. fspr_uint32_t size,
  61. fspr_pool_t *p,
  62. fspr_uint32_t flags)
  63. {
  64. fspr_status_t rv = APR_SUCCESS;
  65. *pollset = fspr_palloc(p, sizeof(**pollset));
  66. #if APR_HAS_THREADS
  67. if (flags & APR_POLLSET_THREADSAFE &&
  68. ((rv = fspr_thread_mutex_create(&(*pollset)->ring_lock,
  69. APR_THREAD_MUTEX_DEFAULT,
  70. p) != APR_SUCCESS))) {
  71. *pollset = NULL;
  72. return rv;
  73. }
  74. #else
  75. if (flags & APR_POLLSET_THREADSAFE) {
  76. *pollset = NULL;
  77. return APR_ENOTIMPL;
  78. }
  79. #endif
  80. (*pollset)->nelts = 0;
  81. (*pollset)->nalloc = size;
  82. (*pollset)->flags = flags;
  83. (*pollset)->pool = p;
  84. (*pollset)->ke_set =
  85. (struct kevent *) fspr_palloc(p, size * sizeof(struct kevent));
  86. memset((*pollset)->ke_set, 0, size * sizeof(struct kevent));
  87. (*pollset)->kqueue_fd = kqueue();
  88. if ((*pollset)->kqueue_fd == -1) {
  89. return APR_ENOMEM;
  90. }
  91. fspr_pool_cleanup_register(p, (void *) (*pollset), backend_cleanup,
  92. fspr_pool_cleanup_null);
  93. (*pollset)->result_set = fspr_palloc(p, size * sizeof(fspr_pollfd_t));
  94. APR_RING_INIT(&(*pollset)->query_ring, pfd_elem_t, link);
  95. APR_RING_INIT(&(*pollset)->free_ring, pfd_elem_t, link);
  96. APR_RING_INIT(&(*pollset)->dead_ring, pfd_elem_t, link);
  97. return rv;
  98. }
  99. APR_DECLARE(fspr_status_t) fspr_pollset_destroy(fspr_pollset_t * pollset)
  100. {
  101. return fspr_pool_cleanup_run(pollset->pool, pollset, backend_cleanup);
  102. }
  103. APR_DECLARE(fspr_status_t) fspr_pollset_add(fspr_pollset_t *pollset,
  104. const fspr_pollfd_t *descriptor)
  105. {
  106. fspr_os_sock_t fd;
  107. pfd_elem_t *elem;
  108. fspr_status_t rv = APR_SUCCESS;
  109. pollset_lock_rings();
  110. if (!APR_RING_EMPTY(&(pollset->free_ring), pfd_elem_t, link)) {
  111. elem = APR_RING_FIRST(&(pollset->free_ring));
  112. APR_RING_REMOVE(elem, link);
  113. }
  114. else {
  115. elem = (pfd_elem_t *) fspr_palloc(pollset->pool, sizeof(pfd_elem_t));
  116. APR_RING_ELEM_INIT(elem, link);
  117. }
  118. elem->pfd = *descriptor;
  119. if (descriptor->desc_type == APR_POLL_SOCKET) {
  120. fd = descriptor->desc.s->socketdes;
  121. }
  122. else {
  123. fd = descriptor->desc.f->filedes;
  124. }
  125. if (descriptor->reqevents & APR_POLLIN) {
  126. EV_SET(&pollset->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, elem);
  127. if (kevent(pollset->kqueue_fd, &pollset->kevent, 1, NULL, 0,
  128. NULL) == -1) {
  129. rv = APR_ENOMEM;
  130. }
  131. }
  132. if (descriptor->reqevents & APR_POLLOUT && rv == APR_SUCCESS) {
  133. EV_SET(&pollset->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, elem);
  134. if (kevent(pollset->kqueue_fd, &pollset->kevent, 1, NULL, 0,
  135. NULL) == -1) {
  136. rv = APR_ENOMEM;
  137. }
  138. }
  139. if (rv == APR_SUCCESS) {
  140. pollset->nelts++;
  141. APR_RING_INSERT_TAIL(&(pollset->query_ring), elem, pfd_elem_t, link);
  142. }
  143. else {
  144. APR_RING_INSERT_TAIL(&(pollset->free_ring), elem, pfd_elem_t, link);
  145. }
  146. pollset_unlock_rings();
  147. return rv;
  148. }
  149. APR_DECLARE(fspr_status_t) fspr_pollset_remove(fspr_pollset_t *pollset,
  150. const fspr_pollfd_t *descriptor)
  151. {
  152. pfd_elem_t *ep;
  153. fspr_status_t rv = APR_SUCCESS;
  154. fspr_os_sock_t fd;
  155. pollset_lock_rings();
  156. if (descriptor->desc_type == APR_POLL_SOCKET) {
  157. fd = descriptor->desc.s->socketdes;
  158. }
  159. else {
  160. fd = descriptor->desc.f->filedes;
  161. }
  162. if (descriptor->reqevents & APR_POLLIN) {
  163. EV_SET(&pollset->kevent, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
  164. if (kevent(pollset->kqueue_fd, &pollset->kevent, 1, NULL, 0,
  165. NULL) == -1) {
  166. rv = APR_NOTFOUND;
  167. }
  168. }
  169. if (descriptor->reqevents & APR_POLLOUT && rv == APR_SUCCESS) {
  170. EV_SET(&pollset->kevent, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
  171. if (kevent(pollset->kqueue_fd, &pollset->kevent, 1, NULL, 0,
  172. NULL) == -1) {
  173. rv = APR_NOTFOUND;
  174. }
  175. }
  176. if (!APR_RING_EMPTY(&(pollset->query_ring), pfd_elem_t, link)) {
  177. for (ep = APR_RING_FIRST(&(pollset->query_ring));
  178. ep != APR_RING_SENTINEL(&(pollset->query_ring),
  179. pfd_elem_t, link);
  180. ep = APR_RING_NEXT(ep, link)) {
  181. if (descriptor->desc.s == ep->pfd.desc.s) {
  182. APR_RING_REMOVE(ep, link);
  183. APR_RING_INSERT_TAIL(&(pollset->dead_ring),
  184. ep, pfd_elem_t, link);
  185. break;
  186. }
  187. }
  188. }
  189. pollset_unlock_rings();
  190. return rv;
  191. }
  192. APR_DECLARE(fspr_status_t) fspr_pollset_poll(fspr_pollset_t *pollset,
  193. fspr_interval_time_t timeout,
  194. fspr_int32_t *num,
  195. const fspr_pollfd_t **descriptors)
  196. {
  197. int ret, i;
  198. struct timespec tv, *tvptr;
  199. fspr_status_t rv = APR_SUCCESS;
  200. if (timeout < 0) {
  201. tvptr = NULL;
  202. }
  203. else {
  204. tv.tv_sec = (long) fspr_time_sec(timeout);
  205. tv.tv_nsec = (long) fspr_time_msec(timeout);
  206. tvptr = &tv;
  207. }
  208. ret = kevent(pollset->kqueue_fd, NULL, 0, pollset->ke_set, pollset->nalloc,
  209. tvptr);
  210. (*num) = ret;
  211. if (ret < 0) {
  212. rv = fspr_get_netos_error();
  213. }
  214. else if (ret == 0) {
  215. rv = APR_TIMEUP;
  216. }
  217. else {
  218. for (i = 0; i < ret; i++) {
  219. pollset->result_set[i] =
  220. (((pfd_elem_t*)(pollset->ke_set[i].udata))->pfd);
  221. pollset->result_set[i].rtnevents =
  222. get_kqueue_revent(pollset->ke_set[i].filter,
  223. pollset->ke_set[i].flags);
  224. }
  225. if (descriptors) {
  226. *descriptors = pollset->result_set;
  227. }
  228. }
  229. pollset_lock_rings();
  230. /* Shift all PFDs in the Dead Ring to be Free Ring */
  231. APR_RING_CONCAT(&(pollset->free_ring), &(pollset->dead_ring), pfd_elem_t, link);
  232. pollset_unlock_rings();
  233. return rv;
  234. }
  235. #endif /* POLLSET_USES_KQUEUE */