• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 DM_TIMER_H
17 #define DM_TIMER_H
18 
19 #include <atomic>
20 #include <chrono>
21 #include <condition_variable>
22 #include <functional>
23 #include <map>
24 #include <mutex>
25 #include <unordered_map>
26 
27 #include "ffrt.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
31 using TimerCallback = std::function<void (std::string name)>;
32 
33 class DmTimer {
34 public:
35     DmTimer();
36     ~DmTimer();
37 
38     /**
39      * @tc.name: DmTimer::StartTimer
40      * @tc.desc: start timer running
41      * @tc.type: FUNC
42      */
43     int32_t StartTimer(std::string name, int32_t timeOut, TimerCallback callback);
44 
45     /**
46      * @tc.name: DmTimer::DeleteTimer
47      * @tc.desc: delete timer
48      * @tc.type: FUNC
49      */
50     int32_t DeleteTimer(std::string timerName);
51 
52     /**
53      * @tc.name: DmTimer::DeleteAll
54      * @tc.desc: delete all timer
55      * @tc.type: FUNC
56      */
57     int32_t DeleteAll();
58 
59 private:
60     mutable std::mutex timerMutex_;
61     std::unordered_map<std::string, ffrt::task_handle> timerVec_ = {};
62     std::shared_ptr<ffrt::queue> queue_;
63 };
64 }
65 }
66 #endif