blob: 6e70c485e37c715698fd6c4daedbde834e4cb5d6 [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>
23
24#include "common/vsoc/shm/base.h"
Greg Hartman8efdf092017-07-14 10:05:48 -070025#include "common/vsoc/shm/lock.h"
Greg Hartman7f3e54d2017-07-17 20:09:37 -070026
27namespace vsoc {
Ping-Hao Wu9eed50a2017-10-17 16:57:27 -070028class RegionSignalingInterface;
Greg Hartman7f3e54d2017-07-17 20:09:37 -070029namespace layout {
30
31/**
32 * Base classes for all spinlock protected circular queues.
Jorge E. Moreirac615b452017-08-07 17:22:44 -070033 * This class should be embedded in the per-region data structure that is used
Greg Hartman7f3e54d2017-07-17 20:09:37 -070034 * as the parameter to TypedRegion.
35 */
36template <uint32_t SizeLog2>
Greg Hartmand792d2a2017-11-30 11:32:02 -080037class CircularQueueBase {
Greg Hartman7f3e54d2017-07-17 20:09:37 -070038 CircularQueueBase() = delete;
39 CircularQueueBase(const CircularQueueBase&) = delete;
40 CircularQueueBase& operator=(const CircularQueueBase&) = delete;
Greg Hartman64545962017-12-13 19:37:03 -080041
Greg Hartman7f3e54d2017-07-17 20:09:37 -070042 protected:
43 /**
44 * Specifies a part of the queue. Note, the given indexes must be masked
45 * before they can be used against buffer_
46 */
47 struct Range {
48 // Points to the first bytes that is part of the range
49 uint32_t start_idx;
50 // Points to the first byte that is not in the range. This is similar to
51 // the STL end iterator.
52 uint32_t end_idx;
53 };
54 static const uintptr_t BufferSize = (1 << SizeLog2);
55
56 /**
Greg Hartman7f3e54d2017-07-17 20:09:37 -070057 * Copy bytes from buffer_in into the part of the queue specified by Range.
58 */
59 void CopyInRange(const char* buffer_in, const Range& t);
60
61 /**
62 * Copy the bytes specified by range to the given buffer. They caller must
63 * ensure that the buffer is large enough to hold the content of the range.
64 */
65 void CopyOutRange(const Range& t, char* buffer_out);
66
67 /**
68 * Wait until data becomes available in the queue. The caller must have
69 * called Lock() before invoking this. The caller must call Unlock()
70 * after this returns.
71 */
Ping-Hao Wu9eed50a2017-10-17 16:57:27 -070072 void WaitForDataLocked(RegionSignalingInterface* r);
Greg Hartman7f3e54d2017-07-17 20:09:37 -070073
74 /**
75 * Reserve space in the queue for writing. The caller must have called Lock()
76 * before invoking this. The caller must call Unlock() after this returns.
77 * Indexes pointing to the reserved space will be placed in range.
78 * On success this returns bytes.
79 * On failure a negative errno indicates the problem. -ENOSPC indicates that
Jorge E. Moreira1a514432017-10-06 17:49:51 -070080 * bytes > the queue size, -EWOULDBLOCK indicates that the call would block
81 * waiting for space but was requested non bloking.
Greg Hartman7f3e54d2017-07-17 20:09:37 -070082 */
Greg Hartman64545962017-12-13 19:37:03 -080083 intptr_t WriteReserveLocked(RegionSignalingInterface* r, size_t bytes,
84 Range* t, bool non_blocking);
Greg Hartman7f3e54d2017-07-17 20:09:37 -070085
Jorge E. Moreira3d777572017-10-06 17:49:51 -070086 // Note: Both of these fields may hold values larger than the buffer size,
87 // they should be interpreted modulo the buffer size. This fact along with the
88 // buffer size being a power of two greatly simplyfies the index calculations.
Greg Hartman7f3e54d2017-07-17 20:09:37 -070089 // Advances when a reader has finished with buffer space
Greg Hartman02490212017-12-13 19:49:10 -080090 std::atomic<uint32_t> r_released_;
Greg Hartman7f3e54d2017-07-17 20:09:37 -070091 // Advances when buffer space is filled and ready for a reader
Greg Hartman02490212017-12-13 19:49:10 -080092 std::atomic<uint32_t> w_pub_;
Greg Hartman7f3e54d2017-07-17 20:09:37 -070093 // Spinlock that protects the region. 0 means unlocked
Greg Hartman8efdf092017-07-14 10:05:48 -070094 SpinLock lock_;
Greg Hartman7f3e54d2017-07-17 20:09:37 -070095 // The actual memory in the buffer
96 char buffer_[BufferSize];
97};
98using CircularQueueBase64k = CircularQueueBase<16>;
99ASSERT_SHM_COMPATIBLE(CircularQueueBase64k, multi_region);
100
101/**
102 * Byte oriented circular queue. Reads will always return some data, but
103 * may return less data than requested. Writes will always write all of the
104 * data or return an error.
105 */
106template <uint32_t SizeLog2>
107class CircularByteQueue : public CircularQueueBase<SizeLog2> {
108 public:
109 /**
110 * Read at most max_size bytes from the qeueue, placing them in buffer_out
111 */
Greg Hartman64545962017-12-13 19:37:03 -0800112 intptr_t Read(RegionSignalingInterface* r, char* buffer_out,
113 std::size_t max_size);
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700114 /**
Jorge E. Moreira1a514432017-10-06 17:49:51 -0700115 * Write all of the given bytes into the queue. If non_blocking isn't set the
116 * call may block until there is enough available space in the queue. On
117 * success the return value will match bytes. On failure a negative errno is
118 * returned. -ENOSPC: If the queue size is smaller than the number of bytes to
119 * write. -EWOULDBLOCK: If non_blocking is true and there is not enough free
120 * space.
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700121 */
Greg Hartman64545962017-12-13 19:37:03 -0800122 intptr_t Write(RegionSignalingInterface* r, const char* buffer_in,
123 std::size_t bytes, bool non_blocking = false);
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700124
125 protected:
126 using Range = typename CircularQueueBase<SizeLog2>::Range;
127};
128using CircularByteQueue64k = CircularByteQueue<16>;
129ASSERT_SHM_COMPATIBLE(CircularByteQueue64k, multi_region);
130
131/**
132 * Packet oriented circular queue. Reads will either return data or an error.
133 * Each return from read corresponds to a call to write and returns all of the
134 * data from that corresponding Write().
135 */
136template <uint32_t SizeLog2, uint32_t MaxPacketSize>
137class CircularPacketQueue : public CircularQueueBase<SizeLog2> {
138 public:
139 /**
140 * Read a single packet from the queue, placing its data into buffer_out.
141 * If max_size indicates that buffer_out cannot hold the entire packet
142 * this function will return -ENOSPC.
143 */
Greg Hartman64545962017-12-13 19:37:03 -0800144 intptr_t Read(RegionSignalingInterface* r, char* buffer_out,
145 std::size_t max_size);
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700146
147 /**
148 * Writes [buffer_in, buffer_in + bytes) to the queue.
149 * If the number of bytes to be written exceeds the size of the queue
150 * -ENOSPC will be returned.
Jorge E. Moreira7510c962017-11-28 11:14:08 -0800151 * If non_blocking is true and there is not enough free space on the queue to
Jorge E. Moreira1a514432017-10-06 17:49:51 -0700152 * write all the data -EWOULDBLOCK will be returned.
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700153 */
Greg Hartman64545962017-12-13 19:37:03 -0800154 intptr_t Write(RegionSignalingInterface* r, const char* buffer_in,
155 uint32_t bytes, bool non_blocking = false);
Greg Hartman7f3e54d2017-07-17 20:09:37 -0700156
157 protected:
158 static_assert(CircularQueueBase<SizeLog2>::BufferSize >= MaxPacketSize,
159 "Buffer is too small to hold the maximum sized packet");
160 using Range = typename CircularQueueBase<SizeLog2>::Range;
161 intptr_t CalculateBufferedSize(size_t payload);
162};
163using CircularPacketQueue64k = CircularPacketQueue<16, 1024>;
164ASSERT_SHM_COMPATIBLE(CircularPacketQueue64k, multi_region);
165
166} // namespace layout
167} // namespace vsoc