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 :
11 : #include "aicpu_sharder.h"
12 : #include "aicpu_context.h"
13 : #include "aicpu_sharder_log.h"
14 : #include "aicpu_timer.h"
15 :
16 : namespace aicpu {
17 :
18 115 : AicpuTimer &AicpuTimer::GetInstance()
19 : {
20 115 : static AicpuTimer instance;
21 115 : return instance;
22 : }
23 :
24 49 : void AicpuTimer::SetSupportTimer(const bool flag)
25 : {
26 49 : isSupportTimer_ = flag;
27 49 : }
28 :
29 40 : void AicpuTimer::RegistMonitorFunc(const StartMonitorFunc &startFunc, const StopMonitorFunc &stopFunc)
30 : {
31 40 : startTimerFunc_ = startFunc;
32 40 : stopTimerFunc_ = stopFunc;
33 :
34 40 : return;
35 : }
36 :
37 12 : TimerStatus AicpuTimer::StartTimer(TimerHandle &timerHandle, const TimeoutCallback &callback, const uint32_t timeInS)
38 : {
39 12 : if (!isSupportTimer_) {
40 1 : AICPUE_LOGI("No need start timer");
41 1 : return TimerStatus::AICPU_TIMER_SUCCESS;
42 : }
43 :
44 11 : if (callback == nullptr) {
45 1 : AICPUE_LOGE("Async timer callback is null.");
46 1 : return TimerStatus::AICPU_TIMER_FAILED;
47 : }
48 :
49 : {
50 10 : const std::lock_guard<std::mutex> lk(timerHandleMutex_);
51 10 : ++timerHandleCnt_;
52 10 : timerHandle = timerHandleCnt_;
53 10 : }
54 :
55 10 : TimerStatus ret = RegistTimeoutCallback(timerHandle, callback);
56 10 : if (ret != TimerStatus::AICPU_TIMER_SUCCESS) {
57 1 : AICPUE_LOGE("Register op timeout callback func failed, ret=%d, TimerHandle=%lu.", static_cast<int32_t>(ret),
58 : timerHandle);
59 1 : return ret;
60 : }
61 :
62 9 : ret = StartTimerInMonitor(timerHandle, timeInS);
63 9 : if (ret != TimerStatus::AICPU_TIMER_SUCCESS) {
64 1 : AICPUE_LOGE("Start op timer failed, ret=%d, TimerHandle=%lu.", static_cast<int32_t>(ret), timerHandle);
65 1 : return ret;
66 : }
67 :
68 8 : AICPUE_LOGI("Start op timer success, TimerHandle=%lu.", timerHandle);
69 :
70 8 : return ret;
71 : }
72 :
73 6 : TimerStatus AicpuTimer::StopTimer(const TimerHandle timerHandle)
74 : {
75 6 : if ((!isSupportTimer_) && (timeoutCbkMap_.empty())) {
76 1 : AICPUE_LOGI("No need stop timer");
77 1 : return TimerStatus::AICPU_TIMER_SUCCESS;
78 : }
79 :
80 5 : TimerStatus ret = StopTimerInMonitor(timerHandle);
81 5 : if (ret != TimerStatus::AICPU_TIMER_SUCCESS) {
82 2 : AICPUE_LOGE("Stop op timer failed, ret=%d, TimerHandle=%lu.", static_cast<int32_t>(ret), timerHandle);
83 2 : return ret;
84 : }
85 :
86 3 : ret = UnregistTimeoutCallback(timerHandle);
87 3 : if (ret != TimerStatus::AICPU_TIMER_SUCCESS) {
88 2 : AICPUE_LOGE("Unregister op timeout callback func failed, ret=%d, TimerHandle=%lu.", static_cast<int32_t>(ret),
89 : timerHandle);
90 2 : return ret;
91 : }
92 :
93 1 : AICPUE_LOGI("Stop op timer success, TimerHandle=%lu.", timerHandle);
94 :
95 1 : return ret;
96 : }
97 :
98 2 : void AicpuTimer::CallTimeoutCallback(const TimerHandle timerHandle)
99 : {
100 : {
101 2 : const std::lock_guard<std::mutex> lk(timeoutCbkMapMutex_);
102 2 : const auto iter = timeoutCbkMap_.find(timerHandle);
103 2 : if (iter == timeoutCbkMap_.end()) {
104 2 : AICPUE_LOGE("Call timeout callback func, TimerHandle=%lu.", timerHandle);
105 2 : return;
106 : }
107 :
108 0 : iter->second();
109 2 : }
110 :
111 0 : return;
112 : }
113 :
114 10 : TimerStatus AicpuTimer::RegistTimeoutCallback(const TimerHandle timerHandle, const TimeoutCallback &callback)
115 : {
116 : {
117 10 : const std::lock_guard<std::mutex> lk(timeoutCbkMapMutex_);
118 10 : const auto ret = timeoutCbkMap_.emplace(timerHandle, callback);
119 10 : if (!ret.second) {
120 1 : AICPUE_LOGE("Register timeout callback func in map failed, TimerHandle=%lu.", timerHandle);
121 1 : return TimerStatus::AICPU_TIMER_FAILED;
122 : }
123 10 : }
124 :
125 9 : return TimerStatus::AICPU_TIMER_SUCCESS;
126 : }
127 :
128 1 : TimerStatus AicpuTimer::UnregistTimeoutCallback(const TimerHandle timerHandle)
129 : {
130 : {
131 1 : const std::lock_guard<std::mutex> lk(timeoutCbkMapMutex_);
132 1 : const auto eraseNum = timeoutCbkMap_.erase(timerHandle);
133 1 : if (eraseNum <= 0) {
134 0 : AICPUE_LOGE("Erase op callback in map failed, TimerHandle=%lu.", timerHandle);
135 0 : return TimerStatus::AICPU_TIMER_FAILED;
136 : }
137 1 : }
138 :
139 1 : return TimerStatus::AICPU_TIMER_SUCCESS;
140 : }
141 :
142 10 : TimerStatus AicpuTimer::StartTimerInMonitor(const TimerHandle timerHandle, const uint32_t timeInS) const
143 : {
144 10 : if (startTimerFunc_ == nullptr) {
145 1 : AICPUE_LOGE("Start op timer failed by nullptr.");
146 1 : return TimerStatus::AICPU_TIMER_FAILED;
147 : }
148 :
149 9 : startTimerFunc_(timerHandle, timeInS);
150 :
151 9 : return TimerStatus::AICPU_TIMER_SUCCESS;
152 : }
153 :
154 5 : TimerStatus AicpuTimer::StopTimerInMonitor(const TimerHandle timerHandle) const
155 : {
156 5 : if (stopTimerFunc_ == nullptr) {
157 1 : AICPUE_LOGE("Stop op timer failed by nullptr");
158 1 : return TimerStatus::AICPU_TIMER_FAILED;
159 : }
160 :
161 4 : stopTimerFunc_(timerHandle);
162 :
163 4 : return TimerStatus::AICPU_TIMER_SUCCESS;
164 : }
165 :
166 1 : bool StartTimer(TimerHandle &timerHandle, const TimeoutCallback &callBack, const uint32_t timeInS)
167 : {
168 1 : return AicpuTimer::GetInstance().StartTimer(timerHandle, callBack, timeInS) ==
169 1 : TimerStatus::AICPU_TIMER_SUCCESS ? true : false;
170 : }
171 :
172 1 : void StopTimer(const TimerHandle timerHandle)
173 : {
174 1 : (void)AicpuTimer::GetInstance().StopTimer(timerHandle);
175 1 : }
176 : } // namespace aicpu
|