1 /* 2 Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file 3 4 This file is part of libzmq, the ZeroMQ core engine in C++. 5 6 libzmq is free software; you can redistribute it and/or modify it under 7 the terms of the GNU Lesser General Public License (LGPL) as published 8 by the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 As a special exception, the Contributors give you permission to link 12 this library with independent modules to produce an executable, 13 regardless of the license terms of these independent modules, and to 14 copy and distribute the resulting executable under terms of your choice, 15 provided that you also meet, for each linked independent module, the 16 terms and conditions of the license of that module. An independent 17 module is a module which is not derived from or based on this library. 18 If you modify this library, you must extend this exception to your 19 version of the library. 20 21 libzmq is distributed in the hope that it will be useful, but WITHOUT 22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 24 License for more details. 25 26 You should have received a copy of the GNU Lesser General Public License 27 along with this program. If not, see <http://www.gnu.org/licenses/>. 28 */ 29 30 #ifndef __ZMQ_DBUFFER_HPP_INCLUDED__ 31 #define __ZMQ_DBUFFER_HPP_INCLUDED__ 32 33 #include <stdlib.h> 34 #include <stddef.h> 35 #include <algorithm> 36 37 #include "mutex.hpp" 38 #include "msg.hpp" 39 40 namespace zmq 41 { 42 // dbuffer is a single-producer single-consumer double-buffer 43 // implementation. 44 // 45 // The producer writes to a back buffer and then tries to swap 46 // pointers between the back and front buffers. If it fails, 47 // due to the consumer reading from the front buffer, it just 48 // gives up, which is ok since writes are many and redundant. 49 // 50 // The reader simply reads from the front buffer. 51 // 52 // has_msg keeps track of whether there has been a not yet read 53 // value written, it is used by ypipe_conflate to mimic ypipe 54 // functionality regarding a reader being asleep 55 56 template <typename T> class dbuffer_t; 57 58 template <> class dbuffer_t<msg_t> 59 { 60 public: dbuffer_t()61 dbuffer_t () : _back (&_storage[0]), _front (&_storage[1]), _has_msg (false) 62 { 63 _back->init (); 64 _front->init (); 65 } 66 ~dbuffer_t()67 ~dbuffer_t () 68 { 69 _back->close (); 70 _front->close (); 71 } 72 write(const msg_t & value_)73 void write (const msg_t &value_) 74 { 75 zmq_assert (value_.check ()); 76 *_back = value_; 77 78 zmq_assert (_back->check ()); 79 80 if (_sync.try_lock ()) { 81 _front->move (*_back); 82 _has_msg = true; 83 84 _sync.unlock (); 85 } 86 } 87 read(msg_t * value_)88 bool read (msg_t *value_) 89 { 90 if (!value_) 91 return false; 92 93 { 94 scoped_lock_t lock (_sync); 95 if (!_has_msg) 96 return false; 97 98 zmq_assert (_front->check ()); 99 100 *value_ = *_front; 101 _front->init (); // avoid double free 102 103 _has_msg = false; 104 return true; 105 } 106 } 107 108 check_read()109 bool check_read () 110 { 111 scoped_lock_t lock (_sync); 112 113 return _has_msg; 114 } 115 probe(bool (* fn_)(const msg_t &))116 bool probe (bool (*fn_) (const msg_t &)) 117 { 118 scoped_lock_t lock (_sync); 119 return (*fn_) (*_front); 120 } 121 122 123 private: 124 msg_t _storage[2]; 125 msg_t *_back, *_front; 126 127 mutex_t _sync; 128 bool _has_msg; 129 130 ZMQ_NON_COPYABLE_NOR_MOVABLE (dbuffer_t) 131 }; 132 } 133 134 #endif 135