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 {
25 : AICPU_TIMER_SUCCESS,
26 : AICPU_TIMER_FAILED
27 : };
28 :
29 : class AicpuTimer {
30 : public:
31 8 : AicpuTimer() : startTimerFunc_{nullptr},
32 8 : stopTimerFunc_{nullptr},
33 8 : timerHandleCnt_(0U),
34 16 : isSupportTimer_(false) {}
35 8 : ~AicpuTimer() {}
36 :
37 : static AicpuTimer &GetInstance();
38 : void SetSupportTimer(const bool flag);
39 : void RegistMonitorFunc(const StartMonitorFunc &startFunc, const StopMonitorFunc &stopFunc);
40 : TimerStatus StartTimer(TimerHandle &timerHandle, const TimeoutCallback &callback, const uint32_t timeInS);
41 : TimerStatus StopTimer(const TimerHandle timerHandle);
42 : void CallTimeoutCallback(const TimerHandle timerHandle);
43 :
44 : private:
45 : TimerStatus RegistTimeoutCallback(const TimerHandle timerHandle, const TimeoutCallback &callback);
46 : TimerStatus UnregistTimeoutCallback(const TimerHandle timerHandle);
47 :
48 : TimerStatus StartTimerInMonitor(const TimerHandle timerHandle, const uint32_t timeInS) const;
49 : TimerStatus StopTimerInMonitor(const TimerHandle timerHandle) const;
50 :
51 : AicpuTimer(const AicpuTimer &) = delete;
52 : AicpuTimer &operator=(const AicpuTimer &) = delete;
53 : AicpuTimer(AicpuTimer &&) = delete;
54 : AicpuTimer &operator=(AicpuTimer &&) = delete;
55 :
56 : std::mutex timeoutCbkMapMutex_;
57 : std::unordered_map<TimerHandle, TimeoutCallback> timeoutCbkMap_; // key is timerHandle
58 : StartMonitorFunc startTimerFunc_;
59 : StopMonitorFunc stopTimerFunc_;
60 : std::mutex timerHandleMutex_;
61 : TimerHandle timerHandleCnt_;
62 : std::atomic<bool> isSupportTimer_;
63 : };
64 : } // namespace aicpu
65 : #endif // AICPU_SHARDER_TIMER_H
|