• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FFRT_TIMER_MANAGER_HPP
17 #define FFRT_TIMER_MANAGER_HPP
18 
19 #include <array>
20 #include <functional>
21 #include <unordered_map>
22 #include "internal_inc/osal.h"
23 #include "internal_inc/non_copyable.h"
24 #include "cpp/queue.h"
25 #include "sync/sync.h"
26 #include "sched/execute_ctx.h"
27 #include "tm/task_base.h"
28 #ifdef FFRT_ENABLE_HITRACE_CHAIN
29 #include "dfx/trace/ffrt_trace_chain.h"
30 #endif
31 
32 namespace ffrt {
33 /** un-repeat timer: not_executed -> executintg -> executed -> ereased **/
34 /** repeat timer: not_executed -> executintg -> executed -> executing -> executed ... **/
35 enum class TimerState {
36     NOT_EXECUTED, // the timer has not expired (in the initialization state).
37     EXECUTING,    // The timer has expired and is executing the callback.
38     EXECUTED,     // The timer has expired and the callback has been executed completely.
39     INVALID
40 };
41 
42 struct TimerData {
TimerDataTimerData43     TimerData(void *dataVal, std::function<void(void *)> cbVal, bool repeat, int qos, uint64_t timeout)
44         : data(dataVal), cb(cbVal), repeat(repeat), qos(qos), timeout(timeout)
45     {
46         if (cb != nullptr) {
47 #ifdef FFRT_ENABLE_HITRACE_CHAIN
48             if (TraceChainAdapter::Instance().HiTraceChainGetId().valid == HITRACE_ID_VALID) {
49                 traceId = TraceChainAdapter::Instance().HiTraceChainCreateSpan();
50             };
51 #endif
52         }
53     }
54 
55     void* data;
56     std::function<void(void*)> cb;
57     bool repeat;
58     int qos;
59     uint64_t timeout;
60     int handle;
61     TimerState state {TimerState::NOT_EXECUTED};
62     HiTraceIdStruct traceId;
63 };
64 
65 class TimerManager : private NonCopyable {
66 public:
67     ~TimerManager();
68     static TimerManager& Instance();
69 
70     ffrt_timer_t RegisterTimer(int qos, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat = false) noexcept;
71     int UnregisterTimer(ffrt_timer_t handle) noexcept;
72     ffrt_timer_query_t GetTimerStatus(ffrt_timer_t handle) noexcept;
73 
74 private:
75     TimerManager();
76 
77     void InitWorkQueAndCb(int qos);
78     void RegisterTimerImpl(std::shared_ptr<TimerData> data);
79 
80     mutable spin_mutex timerMutex_;
81     ffrt_timer_t timerHandle_ { -1 };
82     bool teardown { false };
83     std::unordered_map<int, std::shared_ptr<TimerData>> timerMap_; // valid timer data manage
84     std::array<uint64_t, QoS::MaxNum()> workQueDeps; // deps to ensure callbacks execute in order
85     std::array<std::function<void(WaitEntry*)>, QoS::MaxNum()> workCb; // timeout cb for submit timer cb to queue
86 };
87 }
88 #endif
89