fq.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "fq.hpp"
  26. #include "pipe.hpp"
  27. #include "err.hpp"
  28. #include "msg.hpp"
  29. zmq::fq_t::fq_t () : _active (0), _last_in (NULL), _current (0), _more (false)
  30. {
  31. }
  32. zmq::fq_t::~fq_t ()
  33. {
  34. zmq_assert (_pipes.empty ());
  35. }
  36. void zmq::fq_t::attach (pipe_t *pipe_)
  37. {
  38. _pipes.push_back (pipe_);
  39. _pipes.swap (_active, _pipes.size () - 1);
  40. _active++;
  41. }
  42. void zmq::fq_t::pipe_terminated (pipe_t *pipe_)
  43. {
  44. const pipes_t::size_type index = _pipes.index (pipe_);
  45. // Remove the pipe from the list; adjust number of active pipes
  46. // accordingly.
  47. if (index < _active) {
  48. _active--;
  49. _pipes.swap (index, _active);
  50. if (_current == _active)
  51. _current = 0;
  52. }
  53. _pipes.erase (pipe_);
  54. if (_last_in == pipe_) {
  55. _last_in = NULL;
  56. }
  57. }
  58. void zmq::fq_t::activated (pipe_t *pipe_)
  59. {
  60. // Move the pipe to the list of active pipes.
  61. _pipes.swap (_pipes.index (pipe_), _active);
  62. _active++;
  63. }
  64. int zmq::fq_t::recv (msg_t *msg_)
  65. {
  66. return recvpipe (msg_, NULL);
  67. }
  68. int zmq::fq_t::recvpipe (msg_t *msg_, pipe_t **pipe_)
  69. {
  70. // Deallocate old content of the message.
  71. int rc = msg_->close ();
  72. errno_assert (rc == 0);
  73. // Round-robin over the pipes to get the next message.
  74. while (_active > 0) {
  75. // Try to fetch new message. If we've already read part of the message
  76. // subsequent part should be immediately available.
  77. const bool fetched = _pipes[_current]->read (msg_);
  78. // Note that when message is not fetched, current pipe is deactivated
  79. // and replaced by another active pipe. Thus we don't have to increase
  80. // the 'current' pointer.
  81. if (fetched) {
  82. if (pipe_)
  83. *pipe_ = _pipes[_current];
  84. _more = (msg_->flags () & msg_t::more) != 0;
  85. if (!_more) {
  86. _last_in = _pipes[_current];
  87. _current = (_current + 1) % _active;
  88. }
  89. return 0;
  90. }
  91. // Check the atomicity of the message.
  92. // If we've already received the first part of the message
  93. // we should get the remaining parts without blocking.
  94. zmq_assert (!_more);
  95. _active--;
  96. _pipes.swap (_current, _active);
  97. if (_current == _active)
  98. _current = 0;
  99. }
  100. // No message is available. Initialise the output parameter
  101. // to be a 0-byte message.
  102. rc = msg_->init ();
  103. errno_assert (rc == 0);
  104. errno = EAGAIN;
  105. return -1;
  106. }
  107. bool zmq::fq_t::has_in ()
  108. {
  109. // There are subsequent parts of the partly-read message available.
  110. if (_more)
  111. return true;
  112. // Note that messing with current doesn't break the fairness of fair
  113. // queueing algorithm. If there are no messages available current will
  114. // get back to its original value. Otherwise it'll point to the first
  115. // pipe holding messages, skipping only pipes with no messages available.
  116. while (_active > 0) {
  117. if (_pipes[_current]->check_read ())
  118. return true;
  119. // Deactivate the pipe.
  120. _active--;
  121. _pipes.swap (_current, _active);
  122. if (_current == _active)
  123. _current = 0;
  124. }
  125. return false;
  126. }