blob: e841bbf1821ce95089e48c92f9bdc5e73866f25b [file] [log] [blame]
Greg Hartman7f3e54d2017-07-17 20:09:37 -07001#pragma once
2
3/*
4 * Copyright (C) 2017 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19// Memory layout for byte-oriented circular queues
20
21#include <atomic>
22#include <cstdint>
Andreas Hubercc1cd952018-01-18 12:57:33 -080023#include <sys/uio.h>
Greg Hartman7f3e54d2017-07-17 20:09:37 -070024
25#include "common/vsoc/shm/base.h"
Greg Hartman8efdf092017-07-14 10:05:48 -070026#include "common/vsoc/shm/lock.h"
Greg Hartman7f3e54d2017-07-17 20:09:37 -070027
28namespace vsoc {
Ping-Hao Wu9eed50a2017-10-17 16:57:27 -070029class RegionSignalingInterface;
Greg Hartman7f3e54d2017-07-17 20:09:37 -070030namespace layout {
31
32/**
33 * Base classes for all spinlock protected circular queues.
Jorge E. Moreirac615b452017-08-07 17:22:44 -070034 * This class should be embedded in the per-region data structure that is used
Greg Hartman7f3e54d2017-07-17 20:09:37 -070035 * as the parameter to TypedRegion.
36 */
37template <uint32_t SizeLog2>
Greg Hartmand792d2a2017-11-30 11:32:02 -080038class CircularQueueBase {
Greg Hartman7f3e54d2017-07-17 20:09:37 -070039 CircularQueueBase() = delete;
40 CircularQueueBase(const CircularQueueBase&) = delete;
41 CircularQueueBase& operator=(const CircularQueueBase&) = delete;
Greg Hartman64545962017-12-13 19:37:03 -080042
Greg Hartman7f3e54d2017-07-17 20:09:37 -070043 protected:
44 /**
45 * Specifies a part of the queue. Note, the given indexes must be masked
46 * before they can be used against buffer_
47 */
48 struct Range {
49 // Points to the first bytes that is part of the range
50 uint32_t start_idx;
51 // Points to the first byte that is not in the range. This is similar to
52 // the STL end iterator.
53 uint32_t end_idx;
54 };
55 static const uintptr_t BufferSize = (1 << SizeLog2);
56
57 /**
Greg Hartman7f3e54d2017-07-17 20:09:37 -070058 * Copy bytes from buffer_in into the part of the queue specified by Range.
59 */
60 void CopyInRange(const char* buffer_in, const Range& t);
61
62 /**
63 * Copy the bytes specified by range to the given buffer. They caller must
64 * ensure that the buffer is large enough to hold the content of the range.
65 */
66 void CopyOutRange(const Range& t, char* buffer_out);
67
68 /**
69 * Wait until data becomes available in the queue. The caller must have
70 * called Lock() before invoking this. The caller must call Unlock()
71 * after this returns.
72 */
Ping-Hao Wu9eed50a2017-10-17 16:57:27 -070073 void WaitForDataLocked(RegionSignalingInterface* r);
Greg Hartman7f3e54d2017-07-17 20:09:37 -070074
75 /**
76 * Reserve space in the queue for writing. The caller must have called Lock()
77 * before invoking this. The caller must call Unlock() after this returns.
78 * Indexes pointing to the reserved space will be placed in range.
79 * On success this returns bytes.
80 * On failure a negative errno indicates the problem. -ENOSPC indicates that
Jorge E. Moreira1a514432017-10-06 17:49:51 -070081 * bytes > the queue size, -EWOULDBLOCK indicates that the call would block
82 * waiting for space but was requested non bloking.
Greg Hartman7f3e54d2017-07-17 20:09:37 -070083 */
Greg Hartman64545962017-12-13 19:37:03 -080084 intptr_t WriteReserveLocked(RegionSignalingInterface* r, size_t bytes,
85 Range* t, bool non_blocking);
Greg Hartman7f3e54d2017-07-17 20:09:37 -070086
Jorge E. Moreira3d777572017-10-06 17:49:51 -070087 // Note: Both of these fields may hold values larger than the buffer size,
88 // they should be interpreted modulo the buffer size. This fact along with the
89 // buffer size being a power of two greatly simplyfies the index calculations.
Greg Hartman7f3e54d2017-07-17 20:09:37 -070090 // Advances when a reader has finished with buffer space
Greg Hartman02490212017-12-13 19:49:10 -080091 std::atomic<uint32_t> r_released_;
Greg Hartman7f3e54d2017-07-17 20:09:37 -070092 // Advances when buffer space is filled and ready for a reader
Greg Hartman02490212017-12-13 19:49:10 -080093 std::atomic<uint32_t> w_pub_;
Greg Hartman7f3e54d2017-07-17 20:09:37 -070094 // Spinlock that protects the region. 0 means unlocked
Greg Hartman8efdf092017-07-14 10:05:48 -070095 SpinLock lock_;
Greg Hartman7f3e54d2017-07-17 20:09:37 -070096 // The actual memory in the buffer
97 char buffer_[BufferSize];
98};
99using CircularQueueBase64k = CircularQueueBase<16>;
100ASSERT_SHM_COMPATIBLE(CircularQueueBase64k, multi_region);
101
102/**
103 * Byte oriented circular queue. Reads will always return some data, but
104 * may return less data than requested. Writes will always write all of the
105 * data or return an error.
106 */
107template <uint32_t SizeLog2>
108class CircularByteQueue : public CircularQueueBase<SizeLog2> {
109 public:
110 /**
111 * Read at most max_size bytes from the qeueue, placing them in buffer_out
112 */
Greg Hartman64545962017-12-13 19:37:03 -0800113 intptr_t Read(RegionSignalingInterface* r, char* buffer_out,
114 std::size_t max_size);
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700115 /**
Jorge E. Moreira1a514432017-10-06 17:49:51 -0700116 * Write all of the given bytes into the queue. If non_blocking isn't set the
117 * call may block until there is enough available space in the queue. On
118 * success the return value will match bytes. On failure a negative errno is
119 * returned. -ENOSPC: If the queue size is smaller than the number of bytes to
120 * write. -EWOULDBLOCK: If non_blocking is true and there is not enough free
121 * space.
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700122 */
Greg Hartman64545962017-12-13 19:37:03 -0800123 intptr_t Write(RegionSignalingInterface* r, const char* buffer_in,
124 std::size_t bytes, bool non_blocking = false);
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700125
126 protected:
127 using Range = typename CircularQueueBase<SizeLog2>::Range;
128};
129using CircularByteQueue64k = CircularByteQueue<16>;
130ASSERT_SHM_COMPATIBLE(CircularByteQueue64k, multi_region);
131
132/**
133 * Packet oriented circular queue. Reads will either return data or an error.
134 * Each return from read corresponds to a call to write and returns all of the
135 * data from that corresponding Write().
136 */
137template <uint32_t SizeLog2, uint32_t MaxPacketSize>
138class CircularPacketQueue : public CircularQueueBase<SizeLog2> {
139 public:
140 /**
141 * Read a single packet from the queue, placing its data into buffer_out.
142 * If max_size indicates that buffer_out cannot hold the entire packet
143 * this function will return -ENOSPC.
144 */
Greg Hartman64545962017-12-13 19:37:03 -0800145 intptr_t Read(RegionSignalingInterface* r, char* buffer_out,
146 std::size_t max_size);
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700147
148 /**
149 * Writes [buffer_in, buffer_in + bytes) to the queue.
150 * If the number of bytes to be written exceeds the size of the queue
151 * -ENOSPC will be returned.
Jorge E. Moreira7510c962017-11-28 11:14:08 -0800152 * If non_blocking is true and there is not enough free space on the queue to
Jorge E. Moreira1a514432017-10-06 17:49:51 -0700153 * write all the data -EWOULDBLOCK will be returned.
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700154 */
Greg Hartman64545962017-12-13 19:37:03 -0800155 intptr_t Write(RegionSignalingInterface* r, const char* buffer_in,
156 uint32_t bytes, bool non_blocking = false);
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700157
Andreas Hubercc1cd952018-01-18 12:57:33 -0800158 /**
159 * Writes the data referenced by the given iov scatter/gather array to the
160 * queue.
161 * If the number of bytes to be written exceeds the size of the queue
162 * -ENOSPC will be returned.
163 * If non_blocking is true and there is not enough free space on the queue to
164 * write all the data -EWOULDBLOCK will be returned.
165 */
166 intptr_t Writev(
167 RegionSignalingInterface *r,
168 const iovec *iov,
169 size_t iov_count,
170 bool non_blocking = false);
171
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700172 protected:
173 static_assert(CircularQueueBase<SizeLog2>::BufferSize >= MaxPacketSize,
174 "Buffer is too small to hold the maximum sized packet");
175 using Range = typename CircularQueueBase<SizeLog2>::Range;
176 intptr_t CalculateBufferedSize(size_t payload);
177};
178using CircularPacketQueue64k = CircularPacketQueue<16, 1024>;
179ASSERT_SHM_COMPATIBLE(CircularPacketQueue64k, multi_region);
180
181} // namespace layout
182} // namespace vsoc