unix-service.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #if !defined(_GNU_SOURCE)
  25. #define _GNU_SOURCE
  26. #endif
  27. #include "private-lib-core.h"
  28. int
  29. lws_poll_listen_fd(struct lws_pollfd *fd)
  30. {
  31. return poll(fd, 1, 0);
  32. }
  33. int
  34. _lws_plat_service_forced_tsi(struct lws_context *context, int tsi)
  35. {
  36. struct lws_context_per_thread *pt = &context->pt[tsi];
  37. int m, n, r;
  38. r = lws_service_flag_pending(context, tsi);
  39. /* any socket with events to service? */
  40. for (n = 0; n < (int)pt->fds_count; n++) {
  41. if (!pt->fds[n].revents)
  42. continue;
  43. m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
  44. if (m < 0) {
  45. lwsl_err("%s: lws_service_fd_tsi returned %d\n",
  46. __func__, m);
  47. return -1;
  48. }
  49. /* if something closed, retry this slot */
  50. if (m)
  51. n--;
  52. }
  53. lws_service_do_ripe_rxflow(pt);
  54. return r;
  55. }
  56. #define LWS_POLL_WAIT_LIMIT 2000000000
  57. int
  58. _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
  59. {
  60. volatile struct lws_foreign_thread_pollfd *ftp, *next;
  61. volatile struct lws_context_per_thread *vpt;
  62. struct lws_context_per_thread *pt;
  63. lws_usec_t timeout_us, us;
  64. int n;
  65. #if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
  66. int m;
  67. #endif
  68. /* stay dead once we are dead */
  69. if (!context)
  70. return 1;
  71. pt = &context->pt[tsi];
  72. vpt = (volatile struct lws_context_per_thread *)pt;
  73. lws_stats_bump(pt, LWSSTATS_C_SERVICE_ENTRY, 1);
  74. if (timeout_ms < 0)
  75. timeout_ms = 0;
  76. else
  77. /* force a default timeout of 23 days */
  78. timeout_ms = LWS_POLL_WAIT_LIMIT;
  79. timeout_us = ((lws_usec_t)timeout_ms) * LWS_US_PER_MS;
  80. if (context->event_loop_ops->run_pt)
  81. context->event_loop_ops->run_pt(context, tsi);
  82. if (!pt->service_tid_detected && context->vhost_list) {
  83. lws_fakewsi_def_plwsa(pt);
  84. lws_fakewsi_prep_plwsa_ctx(context);
  85. pt->service_tid = context->vhost_list->protocols[0].callback(
  86. (struct lws *)plwsa,
  87. LWS_CALLBACK_GET_THREAD_ID,
  88. NULL, NULL, 0);
  89. pt->service_tid_detected = 1;
  90. }
  91. us = lws_now_usecs();
  92. lws_pt_lock(pt, __func__);
  93. /*
  94. * service ripe scheduled events, and limit wait to next expected one
  95. */
  96. us = __lws_sul_service_ripe(pt->pt_sul_owner, LWS_COUNT_PT_SUL_OWNERS, us);
  97. if (us && us < timeout_us)
  98. timeout_us = us;
  99. lws_pt_unlock(pt);
  100. /*
  101. * is there anybody with pending stuff that needs service forcing?
  102. */
  103. if (!lws_service_adjust_timeout(context, 1, tsi))
  104. timeout_us = 0;
  105. /* ensure we don't wrap at 2^31 with poll()'s signed int ms */
  106. timeout_us /= LWS_US_PER_MS; /* ms now */
  107. vpt->inside_poll = 1;
  108. lws_memory_barrier();
  109. n = poll(pt->fds, pt->fds_count, timeout_us /* ms now */ );
  110. vpt->inside_poll = 0;
  111. lws_memory_barrier();
  112. #if defined(LWS_WITH_DETAILED_LATENCY)
  113. /*
  114. * so we can track how long it took before we actually read a
  115. * POLLIN that was signalled when we last exited poll()
  116. */
  117. if (context->detailed_latency_cb)
  118. pt->ust_left_poll = lws_now_usecs();
  119. #endif
  120. /* Collision will be rare and brief. Spin until it completes */
  121. while (vpt->foreign_spinlock)
  122. ;
  123. /*
  124. * At this point we are not inside a foreign thread pollfd
  125. * change, and we have marked ourselves as outside the poll()
  126. * wait. So we are the only guys that can modify the
  127. * lws_foreign_thread_pollfd list on the pt. Drain the list
  128. * and apply the changes to the affected pollfds in the correct
  129. * order.
  130. */
  131. lws_pt_lock(pt, __func__);
  132. ftp = vpt->foreign_pfd_list;
  133. //lwsl_notice("cleared list %p\n", ftp);
  134. while (ftp) {
  135. struct lws *wsi;
  136. struct lws_pollfd *pfd;
  137. next = ftp->next;
  138. pfd = &vpt->fds[ftp->fd_index];
  139. if (lws_socket_is_valid(pfd->fd)) {
  140. wsi = wsi_from_fd(context, pfd->fd);
  141. if (wsi)
  142. __lws_change_pollfd(wsi, ftp->_and,
  143. ftp->_or);
  144. }
  145. lws_free((void *)ftp);
  146. ftp = next;
  147. }
  148. vpt->foreign_pfd_list = NULL;
  149. lws_memory_barrier();
  150. lws_pt_unlock(pt);
  151. #if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
  152. m = 0;
  153. #endif
  154. #if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
  155. m |= !!pt->ws.rx_draining_ext_list;
  156. #endif
  157. #if defined(LWS_WITH_TLS)
  158. if (pt->context->tls_ops &&
  159. pt->context->tls_ops->fake_POLLIN_for_buffered)
  160. m |= pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
  161. #endif
  162. if (
  163. #if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
  164. !m &&
  165. #endif
  166. !n) { /* nothing to do */
  167. lws_service_do_ripe_rxflow(pt);
  168. return 0;
  169. }
  170. if (_lws_plat_service_forced_tsi(context, tsi) < 0)
  171. return -1;
  172. if (pt->destroy_self) {
  173. lws_context_destroy(pt->context);
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. int
  179. lws_plat_service(struct lws_context *context, int timeout_ms)
  180. {
  181. return _lws_plat_service_tsi(context, timeout_ms, 0);
  182. }