1 /*
2  * Copyright (c) 2022 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 OHOS_DISTRIBUTED_HARDWARE_DH_TIMER_H
17 #define OHOS_DISTRIBUTED_HARDWARE_DH_TIMER_H
18 
19 #include <condition_variable>
20 #include <cstdint>
21 #include <memory>
22 #include <mutex>
23 #include <thread>
24 
25 #include "event_handler.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 class DHTimer {
30 public:
31     DHTimer(std::string timerId, int32_t delayTimeMs);
32     virtual ~DHTimer();
33     void StartTimer();
34     void StopTimer();
35 
36 private:
37     virtual void ExecuteInner() = 0;
38     virtual void HandleStopTimer() = 0;
39     void InitTimer();
40     void ReleaseTimer();
41     void Execute();
42     void StartEventRunner();
43 
44 private:
45     std::thread eventHandlerThread_;
46     std::mutex timerMutex_;
47     std::condition_variable timerCond_;
48     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_;
49     std::string timerId_;
50     int32_t delayTimeMs_;
51 };
52 } // namespace DistributedHardware
53 } // namespace OHOS
54 #endif
55