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 : #ifndef AICPU_SHARDER_TIMER_H
12 : #define AICPU_SHARDER_TIMER_H
13 :
14 : #include <mutex>
15 : #include <atomic>
16 : #include <functional>
17 : #include <unordered_map>
18 : #include "aicpu_timer_api.h"
19 :
20 : namespace aicpu {
21 : using StartMonitorFunc = std::function<void(const TimerHandle, const uint32_t)>;
22 : using StopMonitorFunc = std::function<void(const TimerHandle)>;
23 :
24 : enum class TimerStatus : int32_t { AICPU_TIMER_SUCCESS, AICPU_TIMER_FAILED };
25 :
26 : class AicpuTimer {
27 : public:
28 8 : AicpuTimer() : startTimerFunc_{nullptr}, stopTimerFunc_{nullptr}, timerHandleCnt_(0U), isSupportTimer_(false) {}
29 8 : ~AicpuTimer() {}
30 :
31 : static AicpuTimer& GetInstance();
32 : void SetSupportTimer(const bool flag);
33 : void RegistMonitorFunc(const StartMonitorFunc& startFunc, const StopMonitorFunc& stopFunc);
34 : TimerStatus StartTimer(TimerHandle& timerHandle, const TimeoutCallback& callback, const uint32_t timeInS);
35 : TimerStatus StopTimer(const TimerHandle timerHandle);
36 : void CallTimeoutCallback(const TimerHandle timerHandle);
37 :
38 : private:
39 : TimerStatus RegistTimeoutCallback(const TimerHandle timerHandle, const TimeoutCallback& callback);
40 : TimerStatus UnregistTimeoutCallback(const TimerHandle timerHandle);
41 :
42 : TimerStatus StartTimerInMonitor(const TimerHandle timerHandle, const uint32_t timeInS) const;
43 : TimerStatus StopTimerInMonitor(const TimerHandle timerHandle) const;
44 :
45 : AicpuTimer(const AicpuTimer&) = delete;
46 : AicpuTimer& operator=(const AicpuTimer&) = delete;
47 : AicpuTimer(AicpuTimer&&) = delete;
48 : AicpuTimer& operator=(AicpuTimer&&) = delete;
49 :
50 : std::mutex timeoutCbkMapMutex_;
51 : std::unordered_map<TimerHandle, TimeoutCallback> timeoutCbkMap_; // key is timerHandle
52 : StartMonitorFunc startTimerFunc_;
53 : StopMonitorFunc stopTimerFunc_;
54 : std::mutex timerHandleMutex_;
55 : TimerHandle timerHandleCnt_;
56 : std::atomic<bool> isSupportTimer_;
57 : };
58 : } // namespace aicpu
59 : #endif // AICPU_SHARDER_TIMER_H
|