dbuffer.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef __ZMQ_DBUFFER_HPP_INCLUDED__
4 #define __ZMQ_DBUFFER_HPP_INCLUDED__
5 
6 #include <stdlib.h>
7 #include <stddef.h>
8 #include <algorithm>
9 
10 #include "mutex.hpp"
11 #include "msg.hpp"
12 
13 namespace zmq
14 {
15 // dbuffer is a single-producer single-consumer double-buffer
16 // implementation.
17 //
18 // The producer writes to a back buffer and then tries to swap
19 // pointers between the back and front buffers. If it fails,
20 // due to the consumer reading from the front buffer, it just
21 // gives up, which is ok since writes are many and redundant.
22 //
23 // The reader simply reads from the front buffer.
24 //
25 // has_msg keeps track of whether there has been a not yet read
26 // value written, it is used by ypipe_conflate to mimic ypipe
27 // functionality regarding a reader being asleep
28 
29 template <typename T> class dbuffer_t;
30 
31 template <> class dbuffer_t<msg_t>
32 {
33  public:
34  dbuffer_t () : _back (&_storage[0]), _front (&_storage[1]), _has_msg (false)
35  {
36  _back->init ();
37  _front->init ();
38  }
39 
41  {
42  _back->close ();
43  _front->close ();
44  }
45 
46  void write (const msg_t &value_)
47  {
48  zmq_assert (value_.check ());
49  *_back = value_;
50 
51  zmq_assert (_back->check ());
52 
53  if (_sync.try_lock ()) {
54  _front->move (*_back);
55  _has_msg = true;
56 
57  _sync.unlock ();
58  }
59  }
60 
61  bool read (msg_t *value_)
62  {
63  if (!value_)
64  return false;
65 
66  {
67  scoped_lock_t lock (_sync);
68  if (!_has_msg)
69  return false;
70 
71  zmq_assert (_front->check ());
72 
73  *value_ = *_front;
74  _front->init (); // avoid double free
75 
76  _has_msg = false;
77  return true;
78  }
79  }
80 
81 
82  bool check_read ()
83  {
84  scoped_lock_t lock (_sync);
85 
86  return _has_msg;
87  }
88 
89  bool probe (bool (*fn_) (const msg_t &))
90  {
91  scoped_lock_t lock (_sync);
92  return (*fn_) (*_front);
93  }
94 
95 
96  private:
97  msg_t _storage[2];
98  msg_t *_back, *_front;
99 
101  bool _has_msg;
102 
104 };
105 }
106 
107 #endif
zmq::dbuffer_t< msg_t >::~dbuffer_t
~dbuffer_t()
Definition: dbuffer.hpp:40
zmq_assert
#define zmq_assert(x)
Definition: err.hpp:102
zmq::dbuffer_t< msg_t >::write
void write(const msg_t &value_)
Definition: dbuffer.hpp:46
zmq
Definition: zmq.hpp:229
zmq::dbuffer_t< msg_t >::_front
msg_t * _front
Definition: dbuffer.hpp:98
ZMQ_NON_COPYABLE_NOR_MOVABLE
#define ZMQ_NON_COPYABLE_NOR_MOVABLE(classname)
Definition: macros.hpp:58
zmq::dbuffer_t
Definition: dbuffer.hpp:29
zmq::dbuffer_t< msg_t >::check_read
bool check_read()
Definition: dbuffer.hpp:82
zmq::mutex_t
Definition: mutex.hpp:82
msg.hpp
value_
int value_
Definition: gmock-matchers_test.cc:571
zmq::dbuffer_t< msg_t >::_sync
mutex_t _sync
Definition: dbuffer.hpp:100
zmq::dbuffer_t< msg_t >::_has_msg
bool _has_msg
Definition: dbuffer.hpp:101
zmq::dbuffer_t< msg_t >::probe
bool probe(bool(*fn_)(const msg_t &))
Definition: dbuffer.hpp:89
zmq::dbuffer_t< msg_t >::dbuffer_t
dbuffer_t()
Definition: dbuffer.hpp:34
false
#define false
Definition: cJSON.c:70
zmq::msg_t
Definition: msg.hpp:33
zmq::scoped_lock_t
Definition: mutex.hpp:143
zmq::dbuffer_t< msg_t >::read
bool read(msg_t *value_)
Definition: dbuffer.hpp:61
mutex.hpp


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49