/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "operation_queue.h" #include "print_log.h" namespace { const int CHECK_OP_INTERVAL_MS = 1000; } using namespace OHOS::Print; void OperationQueue::Run() { bool expectRunning = false; if (!isRunning.compare_exchange_strong(expectRunning, true)) { PRINT_HILOGW("Operation queue is running"); return; } opThread = std::thread([this]() { while (isRunning) { std::function op = Pop(); if (op != nullptr) { op(); } else { syncWait.Wait(CHECK_OP_INTERVAL_MS); } } }); } void OperationQueue::Stop() { bool expectRunning = true; if (!isRunning.compare_exchange_strong(expectRunning, false)) { PRINT_HILOGW("Operation queue is not running"); return; } syncWait.Notify(); if (opThread.joinable()) { opThread.join(); } { std::lock_guard lock(listMutex); opList.clear(); } } bool OperationQueue::Push(std::function op) { if (!isRunning.load()) { PRINT_HILOGW("Operation queue is not running"); return false; } { std::lock_guard lock(listMutex); if (opList.size() >= maxCount) { PRINT_HILOGW("Operation queue full"); opList.pop_back(); } opList.push_front(op); } syncWait.Notify(); return true; } std::function OperationQueue::Pop() { std::lock_guard lock(listMutex); if (opList.empty()) { return nullptr; } std::function op = opList.back(); opList.pop_back(); return op; }