1 /* 2 * Copyright (c) 2025 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 #ifndef NOLOCK_REQUEST_QUEUE_H 16 #define NOLOCK_REQUEST_QUEUE_H 17 #include <vector> 18 #include <atomic> 19 #include <functional> 20 #include <cstdint> 21 22 namespace OHOS { 23 namespace AudioStandard { 24 namespace HPAE { 25 const size_t CURRENT_REQUEST_COUNT = 10000; 26 using Request = std::function<void()>; 27 struct RequestNode { 28 RequestNode() = default; RequestNodeRequestNode29 RequestNode(const RequestNode &requestNode) : nextRequestIndex() 30 {} 31 RequestNode& operator=(const RequestNode& requestNode) = delete; 32 Request request; 33 std::atomic<uint64_t> nextRequestIndex; 34 }; 35 class HpaeNoLockQueue { 36 public: 37 explicit HpaeNoLockQueue(size_t maxRequestCount); 38 ~HpaeNoLockQueue(); 39 40 void PushRequest(Request &&request); 41 void HandleRequests(); 42 void Reset(); 43 bool IsFinishProcess(); 44 45 private: 46 void InitQueue(size_t maxRequestCount); 47 uint64_t IncRequsetIndex(uint64_t requestIndex); 48 uint64_t GetRequsetIndex(uint64_t requestIndex); 49 uint64_t GetRequsetFlag(uint64_t requestFlag); 50 51 void PushRequestNode(std::atomic<uint64_t> *pRequestHeadIndex, uint64_t index); 52 uint64_t GetRequestNode(std::atomic<uint64_t> *pRequestHeadIndex); 53 void ProcessRequests(uint64_t requestHeadIndex, bool isProcess); 54 55 private: 56 std::atomic<uint64_t> freeRequestHeadIndex_; 57 std::atomic<uint64_t> requestHeadIndex_; 58 std::vector<RequestNode> requestQueue_; 59 std::vector<Request> tempRequestQueue_; 60 }; 61 } // namespace HPAE 62 } // namespace AudioStandard 63 } // namespace OHOS 64 #endif 65