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