Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 1 | #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 Huber | cc1cd95 | 2018-01-18 12:57:33 -0800 | [diff] [blame^] | 23 | #include <sys/uio.h> |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 24 | |
| 25 | #include "common/vsoc/shm/base.h" |
Greg Hartman | 8efdf09 | 2017-07-14 10:05:48 -0700 | [diff] [blame] | 26 | #include "common/vsoc/shm/lock.h" |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 27 | |
| 28 | namespace vsoc { |
Ping-Hao Wu | 9eed50a | 2017-10-17 16:57:27 -0700 | [diff] [blame] | 29 | class RegionSignalingInterface; |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 30 | namespace layout { |
| 31 | |
| 32 | /** |
| 33 | * Base classes for all spinlock protected circular queues. |
Jorge E. Moreira | c615b45 | 2017-08-07 17:22:44 -0700 | [diff] [blame] | 34 | * This class should be embedded in the per-region data structure that is used |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 35 | * as the parameter to TypedRegion. |
| 36 | */ |
| 37 | template <uint32_t SizeLog2> |
Greg Hartman | d792d2a | 2017-11-30 11:32:02 -0800 | [diff] [blame] | 38 | class CircularQueueBase { |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 39 | CircularQueueBase() = delete; |
| 40 | CircularQueueBase(const CircularQueueBase&) = delete; |
| 41 | CircularQueueBase& operator=(const CircularQueueBase&) = delete; |
Greg Hartman | 6454596 | 2017-12-13 19:37:03 -0800 | [diff] [blame] | 42 | |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 43 | 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 Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 58 | * 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 Wu | 9eed50a | 2017-10-17 16:57:27 -0700 | [diff] [blame] | 73 | void WaitForDataLocked(RegionSignalingInterface* r); |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 74 | |
| 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. Moreira | 1a51443 | 2017-10-06 17:49:51 -0700 | [diff] [blame] | 81 | * bytes > the queue size, -EWOULDBLOCK indicates that the call would block |
| 82 | * waiting for space but was requested non bloking. |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 83 | */ |
Greg Hartman | 6454596 | 2017-12-13 19:37:03 -0800 | [diff] [blame] | 84 | intptr_t WriteReserveLocked(RegionSignalingInterface* r, size_t bytes, |
| 85 | Range* t, bool non_blocking); |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 86 | |
Jorge E. Moreira | 3d77757 | 2017-10-06 17:49:51 -0700 | [diff] [blame] | 87 | // 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 Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 90 | // Advances when a reader has finished with buffer space |
Greg Hartman | 0249021 | 2017-12-13 19:49:10 -0800 | [diff] [blame] | 91 | std::atomic<uint32_t> r_released_; |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 92 | // Advances when buffer space is filled and ready for a reader |
Greg Hartman | 0249021 | 2017-12-13 19:49:10 -0800 | [diff] [blame] | 93 | std::atomic<uint32_t> w_pub_; |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 94 | // Spinlock that protects the region. 0 means unlocked |
Greg Hartman | 8efdf09 | 2017-07-14 10:05:48 -0700 | [diff] [blame] | 95 | SpinLock lock_; |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 96 | // The actual memory in the buffer |
| 97 | char buffer_[BufferSize]; |
| 98 | }; |
| 99 | using CircularQueueBase64k = CircularQueueBase<16>; |
| 100 | ASSERT_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 | */ |
| 107 | template <uint32_t SizeLog2> |
| 108 | class CircularByteQueue : public CircularQueueBase<SizeLog2> { |
| 109 | public: |
| 110 | /** |
| 111 | * Read at most max_size bytes from the qeueue, placing them in buffer_out |
| 112 | */ |
Greg Hartman | 6454596 | 2017-12-13 19:37:03 -0800 | [diff] [blame] | 113 | intptr_t Read(RegionSignalingInterface* r, char* buffer_out, |
| 114 | std::size_t max_size); |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 115 | /** |
Jorge E. Moreira | 1a51443 | 2017-10-06 17:49:51 -0700 | [diff] [blame] | 116 | * 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 Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 122 | */ |
Greg Hartman | 6454596 | 2017-12-13 19:37:03 -0800 | [diff] [blame] | 123 | intptr_t Write(RegionSignalingInterface* r, const char* buffer_in, |
| 124 | std::size_t bytes, bool non_blocking = false); |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 125 | |
| 126 | protected: |
| 127 | using Range = typename CircularQueueBase<SizeLog2>::Range; |
| 128 | }; |
| 129 | using CircularByteQueue64k = CircularByteQueue<16>; |
| 130 | ASSERT_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 | */ |
| 137 | template <uint32_t SizeLog2, uint32_t MaxPacketSize> |
| 138 | class 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 Hartman | 6454596 | 2017-12-13 19:37:03 -0800 | [diff] [blame] | 145 | intptr_t Read(RegionSignalingInterface* r, char* buffer_out, |
| 146 | std::size_t max_size); |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 147 | |
| 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. Moreira | 7510c96 | 2017-11-28 11:14:08 -0800 | [diff] [blame] | 152 | * If non_blocking is true and there is not enough free space on the queue to |
Jorge E. Moreira | 1a51443 | 2017-10-06 17:49:51 -0700 | [diff] [blame] | 153 | * write all the data -EWOULDBLOCK will be returned. |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 154 | */ |
Greg Hartman | 6454596 | 2017-12-13 19:37:03 -0800 | [diff] [blame] | 155 | intptr_t Write(RegionSignalingInterface* r, const char* buffer_in, |
| 156 | uint32_t bytes, bool non_blocking = false); |
Greg Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 157 | |
Andreas Huber | cc1cd95 | 2018-01-18 12:57:33 -0800 | [diff] [blame^] | 158 | /** |
| 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 Hartman | 7f3e54d | 2017-07-17 20:09:37 -0700 | [diff] [blame] | 172 | 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 | }; |
| 178 | using CircularPacketQueue64k = CircularPacketQueue<16, 1024>; |
| 179 | ASSERT_SHM_COMPATIBLE(CircularPacketQueue64k, multi_region); |
| 180 | |
| 181 | } // namespace layout |
| 182 | } // namespace vsoc |