dbuffer.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_DBUFFER_HPP_INCLUDED__
  25. #define __ZMQ_DBUFFER_HPP_INCLUDED__
  26. #include <stdlib.h>
  27. #include <stddef.h>
  28. #include <algorithm>
  29. #include "mutex.hpp"
  30. #include "msg.hpp"
  31. namespace zmq
  32. {
  33. // dbuffer is a single-producer single-consumer double-buffer
  34. // implementation.
  35. //
  36. // The producer writes to a back buffer and then tries to swap
  37. // pointers between the back and front buffers. If it fails,
  38. // due to the consumer reading from the front buffer, it just
  39. // gives up, which is ok since writes are many and redundant.
  40. //
  41. // The reader simply reads from the front buffer.
  42. //
  43. // has_msg keeps track of whether there has been a not yet read
  44. // value written, it is used by ypipe_conflate to mimic ypipe
  45. // functionality regarding a reader being asleep
  46. template <typename T> class dbuffer_t;
  47. template <> class dbuffer_t<msg_t>
  48. {
  49. public:
  50. dbuffer_t () : _back (&_storage[0]), _front (&_storage[1]), _has_msg (false)
  51. {
  52. _back->init ();
  53. _front->init ();
  54. }
  55. ~dbuffer_t ()
  56. {
  57. _back->close ();
  58. _front->close ();
  59. }
  60. void write (const msg_t &value_)
  61. {
  62. zmq_assert (value_.check ());
  63. *_back = value_;
  64. zmq_assert (_back->check ());
  65. if (_sync.try_lock ()) {
  66. _front->move (*_back);
  67. _has_msg = true;
  68. _sync.unlock ();
  69. }
  70. }
  71. bool read (msg_t *value_)
  72. {
  73. if (!value_)
  74. return false;
  75. {
  76. scoped_lock_t lock (_sync);
  77. if (!_has_msg)
  78. return false;
  79. zmq_assert (_front->check ());
  80. *value_ = *_front;
  81. _front->init (); // avoid double free
  82. _has_msg = false;
  83. return true;
  84. }
  85. }
  86. bool check_read ()
  87. {
  88. scoped_lock_t lock (_sync);
  89. return _has_msg;
  90. }
  91. bool probe (bool (*fn_) (const msg_t &))
  92. {
  93. scoped_lock_t lock (_sync);
  94. return (*fn_) (*_front);
  95. }
  96. private:
  97. msg_t _storage[2];
  98. msg_t *_back, *_front;
  99. mutex_t _sync;
  100. bool _has_msg;
  101. ZMQ_NON_COPYABLE_NOR_MOVABLE (dbuffer_t)
  102. };
  103. }
  104. #endif