1 /* 2 * Copyright (c) 2024 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 #include "operation_queue.h" 17 #include "print_log.h" 18 19 namespace { 20 const int CHECK_OP_INTERVAL_MS = 1000; 21 } 22 using namespace OHOS::Print; 23 Run()24void OperationQueue::Run() 25 { 26 bool expectRunning = false; 27 if (!isRunning.compare_exchange_strong(expectRunning, true)) { 28 PRINT_HILOGW("Operation queue is running"); 29 return; 30 } 31 opThread = std::thread([this]() { 32 while (isRunning) { 33 std::function<void()> op = Pop(); 34 if (op != nullptr) { 35 op(); 36 } else { 37 syncWait.Wait(CHECK_OP_INTERVAL_MS); 38 } 39 } 40 }); 41 } Stop()42void OperationQueue::Stop() 43 { 44 bool expectRunning = true; 45 if (!isRunning.compare_exchange_strong(expectRunning, false)) { 46 PRINT_HILOGW("Operation queue is not running"); 47 return; 48 } 49 syncWait.Notify(); 50 if (opThread.joinable()) { 51 opThread.join(); 52 } 53 { 54 std::lock_guard<std::mutex> lock(listMutex); 55 opList.clear(); 56 } 57 } 58 Push(std::function<void ()> op)59bool OperationQueue::Push(std::function<void()> op) 60 { 61 if (!isRunning.load()) { 62 PRINT_HILOGW("Operation queue is not running"); 63 return false; 64 } 65 { 66 std::lock_guard<std::mutex> lock(listMutex); 67 if (opList.size() >= maxCount) { 68 PRINT_HILOGW("Operation queue full"); 69 opList.pop_back(); 70 } 71 opList.push_front(op); 72 } 73 syncWait.Notify(); 74 return true; 75 } 76 Pop()77std::function<void()> OperationQueue::Pop() 78 { 79 std::lock_guard<std::mutex> lock(listMutex); 80 if (opList.empty()) { 81 return nullptr; 82 } 83 std::function<void()> op = opList.back(); 84 opList.pop_back(); 85 return op; 86 } 87