1 /** 2 * Copyright (c) 2021-2022 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 PANDA_RUNTIME_THREAD_POOL_QUEUE_H_ 17 #define PANDA_RUNTIME_THREAD_POOL_QUEUE_H_ 18 19 static constexpr size_t QUEUE_SIZE_MAX_SIZE = 1000; 20 21 namespace panda { 22 23 class TaskInterface { 24 public: 25 bool IsEmpty(); 26 }; 27 28 template <typename Task> 29 class TaskQueueInterface { 30 public: 31 // All methods (except for Finalize) require an acquired lock from a thread pool. queue_max_size_(queue_max_size)32 explicit TaskQueueInterface(size_t queue_max_size = QUEUE_SIZE_MAX_SIZE) : queue_max_size_(queue_max_size) {} 33 virtual ~TaskQueueInterface() = default; 34 35 NO_COPY_SEMANTIC(TaskQueueInterface); 36 NO_MOVE_SEMANTIC(TaskQueueInterface); 37 38 virtual Task GetTask() = 0; 39 40 // NOLINTNEXTLINE(google-default-arguments) 41 virtual void AddTask(Task task, size_t priority = 0) = 0; 42 virtual void Finalize() = 0; 43 44 bool TryAddTask(Task task, size_t priority = 0) 45 { 46 if (IsFull()) { 47 return false; 48 } 49 AddTask(task, priority); 50 return true; 51 } 52 IsEmpty()53 bool IsEmpty() 54 { 55 return GetQueueSize() == 0; 56 } 57 IsFull()58 bool IsFull() 59 { 60 return GetQueueSize() >= queue_max_size_; 61 } 62 63 protected: 64 virtual size_t GetQueueSize() = 0; 65 66 private: 67 const size_t queue_max_size_; 68 }; 69 70 } // namespace panda 71 72 #endif // PANDA_RUNTIME_THREAD_POOL_QUEUE_H_ 73