/* * Copyright (c) 2021 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. */ namespace OHOS { namespace AI { template std::mutex QueuePool::mutex_; template QueuePool *QueuePool::instance_ = nullptr; template size_t QueuePool::singleQueueCapacity_ = MAX_QUEUE_LENGTH; template QueuePool *QueuePool::GetInstance(size_t singleQueueCapacity) { CHK_RET(instance_ != nullptr, instance_); std::lock_guard lock(mutex_); CHK_RET(instance_ != nullptr, instance_); singleQueueCapacity_ = singleQueueCapacity; AIE_NEW(instance_, QueuePool); return instance_; } template void QueuePool::ReleaseInstance() { std::lock_guard lock(mutex_); AIE_DELETE(instance_); } template QueuePool::QueuePool() : busyQueueNum_(0) { } template QueuePool::~QueuePool() = default; template std::shared_ptr> QueuePool::Pop() { std::shared_ptr > queue = nullptr; if (busyQueueNum_ >= MAX_QUEUE_COUNT) { return queue; } ++busyQueueNum_; if (queues_.empty()) { Queue *ptr = nullptr; AIE_NEW(ptr, Queue(singleQueueCapacity_)); CHK_RET(ptr == nullptr, nullptr); queue.reset(ptr); return queue; } std::lock_guard guard(mutex4Inner_); queue = queues_.front(); queues_.pop_front(); return queue; } template void QueuePool::Push(std::shared_ptr> &queue) { if (busyQueueNum_ <= 0) { return; } busyQueueNum_--; std::lock_guard guard(mutex4Inner_); queue->Reset(); queues_.push_back(queue); } template size_t QueuePool::BusyQueueNum() const { return busyQueueNum_; } } // namespace AI } // namespace OHOS