1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_DATA_QUEUE_H 17 #define OHOS_SHARING_DATA_QUEUE_H 18 #include <atomic> 19 #include <condition_variable> 20 #include <functional> 21 #include <memory> 22 #include <mutex> 23 #include <queue> 24 25 namespace OHOS { 26 namespace Sharing { 27 28 template <class T> 29 class DataQueue { 30 public: 31 explicit DataQueue(const size_t sizeMax); 32 DataQueue(const DataQueue &) = delete; 33 DataQueue &operator=(const DataQueue &) = delete; 34 ~DataQueue(); 35 36 void Quit(); 37 void Finished(); 38 bool Clear(); 39 bool IsEmpty(); 40 bool Pop(T &data); 41 bool Push(const T &data); 42 uint16_t Size(); 43 44 private: 45 std::shared_ptr<T> Pop(void); 46 47 protected: 48 std::queue<T> queue_; 49 50 private: 51 std::mutex mutex_; 52 std::atomic_bool quitFlag_; 53 std::atomic_bool finishFlag_; 54 std::condition_variable fullQueue_; 55 std::condition_variable emptyQueue_; 56 typename std::queue<T>::size_type sizeMax_; 57 }; 58 59 } // namespace Sharing 60 } // namespace OHOS 61 #endif