1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef QUEUE_H 17 #define QUEUE_H 18 19 #include <atomic> 20 21 #include "protocol/retcode_inner/aie_retcode_inner.h" 22 #include "utils/aie_macros.h" 23 24 namespace OHOS { 25 namespace AI { 26 template<class TYPE> 27 class Queue { 28 FORBID_COPY_AND_ASSIGN(Queue); 29 public: 30 explicit Queue(size_t maxQueueSize); 31 32 virtual ~Queue(); 33 34 public: 35 /** 36 * Check if the queue is empty. 37 * 38 * @return true if empty or false if not empty. 39 */ 40 bool IsEmpty() const; 41 42 /** 43 * Check if the queue is full. 44 * 45 * @return true if full or false if not full. 46 */ 47 bool IsFull() const; 48 49 /** 50 * Push a message at the rear. 51 * 52 * @param [in] msgBlock message to push. 53 * @return 1011 if the queue is full or 0 if success. 54 */ 55 int PushBack(TYPE &msgBlock); 56 57 /** 58 * Pop a message from the head. 59 * 60 * @param [out] msgBlock message from the head. 61 * @return 1012 if the queue is empty or 0 if success. 62 */ 63 int PopFront(TYPE &msgBlock); 64 65 /** 66 * Query the number of elements in the queue. 67 * 68 * @return the number of elements in the queue. 69 */ 70 size_t Count() const; 71 72 /** 73 * Reset the queue. 74 */ 75 void Reset(); 76 77 private: 78 /** 79 * Next position to push elements to. 80 */ 81 std::atomic<size_t> pushPos_; 82 83 /** 84 * Next position to pop message from. 85 */ 86 std::atomic<size_t> popPos_; 87 88 /** 89 * Maximum message of the queue. 90 */ 91 size_t totalNum_; 92 93 /** 94 * Writable message number. 95 */ 96 std::atomic<size_t> writeAbleCount_; 97 98 /** 99 * Readable message number. 100 */ 101 std::atomic<size_t> readAbleCount_; 102 103 struct QueueNode { 104 bool empty = true; 105 TYPE node; 106 }; 107 108 QueueNode *queue_; 109 }; 110 } // namespace AI 111 } // namespace OHOS 112 113 #include "platform/queuepool/queue.inl" 114 115 #endif // QUEUE_H