kqueue.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef __ZMQ_KQUEUE_HPP_INCLUDED__
  25. #define __ZMQ_KQUEUE_HPP_INCLUDED__
  26. // poller.hpp decides which polling mechanism to use.
  27. #include "poller.hpp"
  28. #if defined ZMQ_IOTHREAD_POLLER_USE_KQUEUE
  29. #include <vector>
  30. #include <unistd.h>
  31. #include "ctx.hpp"
  32. #include "fd.hpp"
  33. #include "thread.hpp"
  34. #include "poller_base.hpp"
  35. namespace zmq
  36. {
  37. struct i_poll_events;
  38. // Implements socket polling mechanism using the BSD-specific
  39. // kqueue interface.
  40. class kqueue_t ZMQ_FINAL : public worker_poller_base_t
  41. {
  42. public:
  43. typedef void *handle_t;
  44. kqueue_t (const thread_ctx_t &ctx_);
  45. ~kqueue_t () ZMQ_FINAL;
  46. // "poller" concept.
  47. handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
  48. void rm_fd (handle_t handle_);
  49. void set_pollin (handle_t handle_);
  50. void reset_pollin (handle_t handle_);
  51. void set_pollout (handle_t handle_);
  52. void reset_pollout (handle_t handle_);
  53. void stop ();
  54. static int max_fds ();
  55. private:
  56. // Main event loop.
  57. void loop () ZMQ_FINAL;
  58. // File descriptor referring to the kernel event queue.
  59. fd_t kqueue_fd;
  60. // Adds the event to the kqueue.
  61. void kevent_add (fd_t fd_, short filter_, void *udata_);
  62. // Deletes the event from the kqueue.
  63. void kevent_delete (fd_t fd_, short filter_);
  64. struct poll_entry_t
  65. {
  66. fd_t fd;
  67. bool flag_pollin;
  68. bool flag_pollout;
  69. zmq::i_poll_events *reactor;
  70. };
  71. // List of retired event sources.
  72. typedef std::vector<poll_entry_t *> retired_t;
  73. retired_t retired;
  74. ZMQ_NON_COPYABLE_NOR_MOVABLE (kqueue_t)
  75. #ifdef HAVE_FORK
  76. // the process that created this context. Used to detect forking.
  77. pid_t pid;
  78. #endif
  79. };
  80. typedef kqueue_t poller_t;
  81. }
  82. #endif
  83. #endif