pollset.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "precompiled.hpp"
  25. #include "pollset.hpp"
  26. #if defined ZMQ_IOTHREAD_POLLER_USE_POLLSET
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <algorithm>
  31. #include <new>
  32. #include "macros.hpp"
  33. #include "err.hpp"
  34. #include "config.hpp"
  35. #include "i_poll_events.hpp"
  36. zmq::pollset_t::pollset_t (const zmq::thread_ctx_t &ctx_) :
  37. ctx (ctx_),
  38. stopping (false)
  39. {
  40. pollset_fd = pollset_create (-1);
  41. errno_assert (pollset_fd != -1);
  42. }
  43. zmq::pollset_t::~pollset_t ()
  44. {
  45. // Wait till the worker thread exits.
  46. worker.stop ();
  47. pollset_destroy (pollset_fd);
  48. for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it)
  49. LIBZMQ_DELETE (*it);
  50. }
  51. zmq::pollset_t::handle_t zmq::pollset_t::add_fd (fd_t fd_,
  52. i_poll_events *events_)
  53. {
  54. poll_entry_t *pe = new (std::nothrow) poll_entry_t;
  55. alloc_assert (pe);
  56. pe->fd = fd_;
  57. pe->flag_pollin = false;
  58. pe->flag_pollout = false;
  59. pe->events = events_;
  60. struct poll_ctl pc;
  61. pc.fd = fd_;
  62. pc.cmd = PS_ADD;
  63. pc.events = 0;
  64. int rc = pollset_ctl (pollset_fd, &pc, 1);
  65. errno_assert (rc != -1);
  66. // Increase the load metric of the thread.
  67. adjust_load (1);
  68. if (fd_ >= fd_table.size ()) {
  69. fd_table.resize (fd_ + 1, NULL);
  70. }
  71. fd_table[fd_] = pe;
  72. return pe;
  73. }
  74. void zmq::pollset_t::rm_fd (handle_t handle_)
  75. {
  76. poll_entry_t *pe = (poll_entry_t *) handle_;
  77. struct poll_ctl pc;
  78. pc.fd = pe->fd;
  79. pc.cmd = PS_DELETE;
  80. pc.events = 0;
  81. pollset_ctl (pollset_fd, &pc, 1);
  82. fd_table[pe->fd] = NULL;
  83. pe->fd = retired_fd;
  84. retired.push_back (pe);
  85. // Decrease the load metric of the thread.
  86. adjust_load (-1);
  87. }
  88. void zmq::pollset_t::set_pollin (handle_t handle_)
  89. {
  90. poll_entry_t *pe = (poll_entry_t *) handle_;
  91. if (likely (!pe->flag_pollin)) {
  92. struct poll_ctl pc;
  93. pc.fd = pe->fd;
  94. pc.cmd = PS_MOD;
  95. pc.events = POLLIN;
  96. const int rc = pollset_ctl (pollset_fd, &pc, 1);
  97. errno_assert (rc != -1);
  98. pe->flag_pollin = true;
  99. }
  100. }
  101. void zmq::pollset_t::reset_pollin (handle_t handle_)
  102. {
  103. poll_entry_t *pe = (poll_entry_t *) handle_;
  104. if (unlikely (!pe->flag_pollin)) {
  105. return;
  106. }
  107. struct poll_ctl pc;
  108. pc.fd = pe->fd;
  109. pc.events = 0;
  110. pc.cmd = PS_DELETE;
  111. int rc = pollset_ctl (pollset_fd, &pc, 1);
  112. if (pe->flag_pollout) {
  113. pc.events = POLLOUT;
  114. pc.cmd = PS_MOD;
  115. rc = pollset_ctl (pollset_fd, &pc, 1);
  116. errno_assert (rc != -1);
  117. }
  118. pe->flag_pollin = false;
  119. }
  120. void zmq::pollset_t::set_pollout (handle_t handle_)
  121. {
  122. poll_entry_t *pe = (poll_entry_t *) handle_;
  123. if (likely (!pe->flag_pollout)) {
  124. struct poll_ctl pc;
  125. pc.fd = pe->fd;
  126. pc.cmd = PS_MOD;
  127. pc.events = POLLOUT;
  128. const int rc = pollset_ctl (pollset_fd, &pc, 1);
  129. errno_assert (rc != -1);
  130. pe->flag_pollout = true;
  131. }
  132. }
  133. void zmq::pollset_t::reset_pollout (handle_t handle_)
  134. {
  135. poll_entry_t *pe = (poll_entry_t *) handle_;
  136. if (unlikely (!pe->flag_pollout)) {
  137. return;
  138. }
  139. struct poll_ctl pc;
  140. pc.fd = pe->fd;
  141. pc.events = 0;
  142. pc.cmd = PS_DELETE;
  143. int rc = pollset_ctl (pollset_fd, &pc, 1);
  144. errno_assert (rc != -1);
  145. if (pe->flag_pollin) {
  146. pc.cmd = PS_MOD;
  147. pc.events = POLLIN;
  148. rc = pollset_ctl (pollset_fd, &pc, 1);
  149. errno_assert (rc != -1);
  150. }
  151. pe->flag_pollout = false;
  152. }
  153. void zmq::pollset_t::start ()
  154. {
  155. ctx.start_thread (worker, worker_routine, this);
  156. }
  157. void zmq::pollset_t::stop ()
  158. {
  159. stopping = true;
  160. }
  161. int zmq::pollset_t::max_fds ()
  162. {
  163. return -1;
  164. }
  165. void zmq::pollset_t::loop ()
  166. {
  167. struct pollfd polldata_array[max_io_events];
  168. while (!stopping) {
  169. // Execute any due timers.
  170. int timeout = (int) execute_timers ();
  171. // Wait for events.
  172. int n = pollset_poll (pollset_fd, polldata_array, max_io_events,
  173. timeout ? timeout : -1);
  174. if (n == -1) {
  175. errno_assert (errno == EINTR);
  176. continue;
  177. }
  178. for (int i = 0; i < n; i++) {
  179. poll_entry_t *pe = fd_table[polldata_array[i].fd];
  180. if (!pe)
  181. continue;
  182. if (pe->fd == retired_fd)
  183. continue;
  184. if (polldata_array[i].revents & (POLLERR | POLLHUP))
  185. pe->events->in_event ();
  186. if (pe->fd == retired_fd)
  187. continue;
  188. if (polldata_array[i].revents & POLLOUT)
  189. pe->events->out_event ();
  190. if (pe->fd == retired_fd)
  191. continue;
  192. if (polldata_array[i].revents & POLLIN)
  193. pe->events->in_event ();
  194. }
  195. // Destroy retired event sources.
  196. for (retired_t::iterator it = retired.begin (); it != retired.end ();
  197. ++it)
  198. LIBZMQ_DELETE (*it);
  199. retired.clear ();
  200. }
  201. }
  202. void zmq::pollset_t::worker_routine (void *arg_)
  203. {
  204. ((pollset_t *) arg_)->loop ();
  205. }
  206. #endif