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 #ifndef DM_EXPORT 30 #define DM_EXPORT __attribute__ ((visibility ("default"))) 31 #endif // DM_EXPORT 32 33 namespace OHOS { 34 namespace DistributedHardware { 35 using TimerCallback = std::function<void (std::string name)>; 36 37 class DmTimer { 38 public: 39 DM_EXPORT DmTimer(); 40 DM_EXPORT ~DmTimer(); 41 42 /** 43 * @tc.name: DmTimer::StartTimer 44 * @tc.desc: start timer running 45 * @tc.type: FUNC 46 */ 47 DM_EXPORT int32_t StartTimer(std::string name, int32_t timeOut, 48 TimerCallback callback); 49 50 /** 51 * @tc.name: DmTimer::DeleteTimer 52 * @tc.desc: delete timer 53 * @tc.type: FUNC 54 */ 55 DM_EXPORT int32_t DeleteTimer(std::string timerName); 56 57 /** 58 * @tc.name: DmTimer::DeleteAll 59 * @tc.desc: delete all timer 60 * @tc.type: FUNC 61 */ 62 DM_EXPORT int32_t DeleteAll(); 63 64 private: 65 mutable std::mutex timerMutex_; 66 std::unordered_map<std::string, ffrt::task_handle> timerVec_ = {}; 67 std::shared_ptr<ffrt::queue> queue_; 68 }; 69 } 70 } 71 #endif