mailbox_safe.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "mailbox_safe.hpp"
  26. #include "clock.hpp"
  27. #include "err.hpp"
  28. #include <algorithm>
  29. zmq::mailbox_safe_t::mailbox_safe_t (mutex_t *sync_) : _sync (sync_)
  30. {
  31. // Get the pipe into passive state. That way, if the users starts by
  32. // polling on the associated file descriptor it will get woken up when
  33. // new command is posted.
  34. const bool ok = _cpipe.check_read ();
  35. zmq_assert (!ok);
  36. }
  37. zmq::mailbox_safe_t::~mailbox_safe_t ()
  38. {
  39. // TODO: Retrieve and deallocate commands inside the cpipe.
  40. // Work around problem that other threads might still be in our
  41. // send() method, by waiting on the mutex before disappearing.
  42. _sync->lock ();
  43. _sync->unlock ();
  44. }
  45. void zmq::mailbox_safe_t::add_signaler (signaler_t *signaler_)
  46. {
  47. _signalers.push_back (signaler_);
  48. }
  49. void zmq::mailbox_safe_t::remove_signaler (signaler_t *signaler_)
  50. {
  51. // TODO: make a copy of array and signal outside the lock
  52. const std::vector<zmq::signaler_t *>::iterator end = _signalers.end ();
  53. const std::vector<signaler_t *>::iterator it =
  54. std::find (_signalers.begin (), end, signaler_);
  55. if (it != end)
  56. _signalers.erase (it);
  57. }
  58. void zmq::mailbox_safe_t::clear_signalers ()
  59. {
  60. _signalers.clear ();
  61. }
  62. void zmq::mailbox_safe_t::send (const command_t &cmd_)
  63. {
  64. _sync->lock ();
  65. _cpipe.write (cmd_, false);
  66. const bool ok = _cpipe.flush ();
  67. if (!ok) {
  68. _cond_var.broadcast ();
  69. for (std::vector<signaler_t *>::iterator it = _signalers.begin (),
  70. end = _signalers.end ();
  71. it != end; ++it) {
  72. (*it)->send ();
  73. }
  74. }
  75. _sync->unlock ();
  76. }
  77. int zmq::mailbox_safe_t::recv (command_t *cmd_, int timeout_)
  78. {
  79. // Try to get the command straight away.
  80. if (_cpipe.read (cmd_))
  81. return 0;
  82. // If the timeout is zero, it will be quicker to release the lock, giving other a chance to send a command
  83. // and immediately relock it.
  84. if (timeout_ == 0) {
  85. _sync->unlock ();
  86. _sync->lock ();
  87. } else {
  88. // Wait for signal from the command sender.
  89. const int rc = _cond_var.wait (_sync, timeout_);
  90. if (rc == -1) {
  91. errno_assert (errno == EAGAIN || errno == EINTR);
  92. return -1;
  93. }
  94. }
  95. // Another thread may already fetch the command
  96. const bool ok = _cpipe.read (cmd_);
  97. if (!ok) {
  98. errno = EAGAIN;
  99. return -1;
  100. }
  101. return 0;
  102. }