Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020-2021 Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * 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, WITHOUT |
| 14 | * 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 | #ifndef MESSAGE_QUEUE_H |
| 20 | #define MESSAGE_QUEUE_H |
| 21 | |
| 22 | #include <cstddef> |
| 23 | #include <ethosu_core_interface.h> |
| 24 | |
| 25 | namespace MessageQueue { |
| 26 | |
| 27 | template <uint32_t SIZE> |
| 28 | struct Queue { |
| 29 | EthosU::ethosu_core_queue_header header; |
| 30 | uint8_t data[SIZE]; |
| 31 | |
| 32 | constexpr Queue() : header({SIZE, 0, 0}) {} |
| 33 | |
| 34 | constexpr EthosU::ethosu_core_queue *toQueue() { |
| 35 | return reinterpret_cast<EthosU::ethosu_core_queue *>(&header); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | class QueueImpl { |
| 40 | public: |
| 41 | struct Vec { |
| 42 | const void *base; |
| 43 | size_t length; |
| 44 | }; |
| 45 | |
| 46 | QueueImpl(EthosU::ethosu_core_queue &queue); |
| 47 | |
| 48 | bool empty() const; |
| 49 | size_t available() const; |
| 50 | size_t capacity() const; |
| 51 | void reset(); |
| 52 | bool read(uint8_t *dst, uint32_t length); |
| 53 | template <typename T> |
| 54 | bool read(T &dst) { |
| 55 | return read(reinterpret_cast<uint8_t *>(&dst), sizeof(dst)); |
| 56 | } |
| 57 | bool write(const Vec *vec, size_t length); |
| 58 | bool write(const uint32_t type, const void *src = nullptr, uint32_t length = 0); |
| 59 | template <typename T> |
| 60 | bool write(const uint32_t type, const T &src) { |
| 61 | return write(type, reinterpret_cast<const void *>(&src), sizeof(src)); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | void cleanHeader() const; |
| 66 | void cleanHeaderData() const; |
| 67 | void invalidateHeader() const; |
| 68 | void invalidateHeaderData() const; |
| 69 | |
| 70 | EthosU::ethosu_core_queue &queue; |
| 71 | }; |
| 72 | } // namespace MessageQueue |
| 73 | |
| 74 | #endif |