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
40namespace 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
56template <typename T> class dbuffer_t;
57
58template <> class dbuffer_t<msg_t>
59{
60 public:
61 inline dbuffer_t () :
62 _back (&_storage[0]),
63 _front (&_storage[1]),
64 _has_msg (false)
65 {
66 _back->init ();
67 _front->init ();
68 }
69
70 inline ~dbuffer_t ()
71 {
72 _back->close ();
73 _front->close ();
74 }
75
76 inline void write (const msg_t &value_)
77 {
78 msg_t &xvalue = const_cast<msg_t &> (value_);
79
80 zmq_assert (xvalue.check ());
81 *_back = value_;
82
83 zmq_assert (_back->check ());
84
85 if (_sync.try_lock ()) {
86 _front->move (*_back);
87 _has_msg = true;
88
89 _sync.unlock ();
90 }
91 }
92
93 inline bool read (msg_t *value_)
94 {
95 if (!value_)
96 return false;
97
98 {
99 scoped_lock_t lock (_sync);
100 if (!_has_msg)
101 return false;
102
103 zmq_assert (_front->check ());
104
105 *value_ = *_front;
106 _front->init (); // avoid double free
107
108 _has_msg = false;
109 return true;
110 }
111 }
112
113
114 inline bool check_read ()
115 {
116 scoped_lock_t lock (_sync);
117
118 return _has_msg;
119 }
120
121 inline bool probe (bool (*fn_) (const msg_t &))
122 {
123 scoped_lock_t lock (_sync);
124 return (*fn_) (*_front);
125 }
126
127
128 private:
129 msg_t _storage[2];
130 msg_t *_back, *_front;
131
132 mutex_t _sync;
133 bool _has_msg;
134
135 ZMQ_NON_COPYABLE_NOR_MOVABLE (dbuffer_t)
136};
137}
138
139#endif
140