Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include <chrono>
11 : #include "thread_manager.h"
12 : #include "log/adx_log.h"
13 :
14 : namespace Adx{
15 : constexpr uint32_t WAIT_THREAD_TIMEOUT = 60;
16 1 : ThreadManager::~ThreadManager() {
17 1 : WaitAll();
18 1 : }
19 :
20 32 : void ThreadManager::TaskAdd(int32_t tid) {
21 32 : std::lock_guard<std::mutex> lock(mtx_);
22 32 : threads_.insert(tid);
23 32 : IDE_LOGD("Task: %d Added! %zu tasks are running.", tid, threads_.size());
24 32 : cv_.notify_all();
25 32 : }
26 :
27 32 : void ThreadManager::TaskDone(int32_t tid) {
28 32 : std::lock_guard<std::mutex> lock(mtx_);
29 32 : threads_.erase(tid);
30 32 : IDE_LOGD( "Task: %d Done! %zu tasks remain.", tid, threads_.size());
31 32 : cv_.notify_all();
32 32 : }
33 :
34 27 : void ThreadManager::WaitAll() {
35 27 : std::unique_lock<std::mutex> lock(mtx_);
36 65 : cv_.wait_for(lock, std::chrono::seconds(WAIT_THREAD_TIMEOUT * threads_.size()), [this]() { return threads_.empty(); });
37 27 : }
38 : }
|