Ocean
BufferQueue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef FACEBOOK_NETWORK_BUFFER_QUEUE_H
9 #define FACEBOOK_NETWORK_BUFFER_QUEUE_H
10 
11 #include "ocean/network/Network.h"
12 
13 #include "ocean/base/Lock.h"
14 
15 #include <deque>
16 #include <vector>
17 
18 namespace Ocean
19 {
20 
21 namespace Network
22 {
23 
24 /**
25  * This class implements a thread-safe buffer queue.
26  * @ingroup network
27  */
29 {
30  protected:
31 
32  /**
33  * Definition of a vector holding bytes.
34  */
35  typedef std::vector<uint8_t> Buffer;
36 
37  /**
38  * Definition of a double-ended queue holding buffers.
39  */
40  typedef std::deque<Buffer> Queue;
41 
42  public:
43 
44  /**
45  * Pushes a new buffer to the queue.
46  * @param data The data to push, will be copied, can be nullptr if 'size == 0'
47  * @param size The number of bytes to copy, with range [0, infinity)
48  */
49  inline void push(const void* data, const size_t size);
50 
51  /**
52  * Pushes a new buffer to the queue.
53  * @param data The data to push, will be moved
54  */
55  inline void push(Buffer&& data);
56 
57  /**
58  * Pops a buffer from the queue.
59  * @return The buffer, if any
60  */
61  inline Buffer pop();
62 
63  /**
64  * Clears the buffer queue.
65  */
66  inline void clear();
67 
68  /**
69  * Returns the number of buffers in this queue.
70  * @return The number of buffers
71  */
72  inline size_t size() const;
73 
74  /**
75  * Returns whether this queue holds no buffers.
76  * @return True, if so
77  */
78  inline bool isEmpty() const;
79 
80  protected:
81 
82  /// The internal buffer queue.
84 
85  /// The buffer lock.
86  mutable Lock lock_;
87 };
88 
89 inline void BufferQueue::push(const void* data, const size_t size)
90 {
91  if (size == 0)
92  {
93  return;
94  }
95 
96  ocean_assert(data != nullptr);
97 
98  const ScopedLock scopedLock(lock_);
99 
100  Buffer buffer(size);
101  memcpy(buffer.data(), data, size);
102 
103  queue_.emplace_back(std::move(buffer));
104 }
105 
106 inline void BufferQueue::push(Buffer&& data)
107 {
108  ocean_assert(!data.empty());
109  if (data.empty())
110  {
111  return;
112  }
113 
114  const ScopedLock scopedLock(lock_);
115 
116  queue_.emplace_back(std::move(data));
117 }
118 
120 {
121  const ScopedLock scopedLock(lock_);
122 
123  if (queue_.empty())
124  {
125  return Buffer();
126  }
127 
128  Buffer buffer(std::move(queue_.front()));
129  queue_.pop_front();
130 
131  return buffer;
132 }
133 
134 inline void BufferQueue::clear()
135 {
136  const ScopedLock scopedLock(lock_);
137  queue_.clear();
138 }
139 
140 inline size_t BufferQueue::size() const
141 {
142  const ScopedLock scopedLock(lock_);
143  return queue_.size();
144 }
145 
146 inline bool BufferQueue::isEmpty() const
147 {
148  const ScopedLock scopedLock(lock_);
149  return queue_.empty();
150 }
151 
152 }
153 
154 }
155 
156 #endif // FACEBOOK_NETWORK_BUFFER_QUEUE_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a thread-safe buffer queue.
Definition: BufferQueue.h:29
Buffer pop()
Pops a buffer from the queue.
Definition: BufferQueue.h:119
std::vector< uint8_t > Buffer
Definition of a vector holding bytes.
Definition: BufferQueue.h:35
bool isEmpty() const
Returns whether this queue holds no buffers.
Definition: BufferQueue.h:146
std::deque< Buffer > Queue
Definition of a double-ended queue holding buffers.
Definition: BufferQueue.h:40
void push(const void *data, const size_t size)
Pushes a new buffer to the queue.
Definition: BufferQueue.h:89
Lock lock_
The buffer lock.
Definition: BufferQueue.h:86
Queue queue_
The internal buffer queue.
Definition: BufferQueue.h:83
size_t size() const
Returns the number of buffers in this queue.
Definition: BufferQueue.h:140
void clear()
Clears the buffer queue.
Definition: BufferQueue.h:134
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15