1 /* 2 * Copyright (C) 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 OHOS_SAMGR_TIME_HANDLER_H 17 #define OHOS_SAMGR_TIME_HANDLER_H 18 19 #include <thread> 20 #include <string> 21 #include <functional> 22 #include <sys/epoll.h> 23 #include <signal.h> 24 #include <sys/timerfd.h> 25 #include <sys/time.h> 26 #include <errno.h> 27 #include <unistd.h> 28 #include <mutex> 29 #include <concurrent_map.h> 30 #include <atomic> 31 namespace OHOS { 32 class SamgrTimeHandler { 33 public: 34 typedef std::function<void()> TaskType; 35 ~SamgrTimeHandler(); 36 static SamgrTimeHandler* GetInstance(); 37 bool PostTask(TaskType func, uint64_t delayTime); 38 39 private: 40 SamgrTimeHandler(); 41 class Deletor { 42 public: ~Deletor()43 ~Deletor() 44 { 45 if (nullptr != SamgrTimeHandler::singleton) { 46 delete SamgrTimeHandler::singleton; 47 SamgrTimeHandler::singleton = nullptr; 48 } 49 } 50 }; 51 void StartThread(); 52 void OnTime(SamgrTimeHandler &handle, int number, struct epoll_event events[]); 53 int CreateAndRetry(); 54 55 private: 56 int epollfd = -1; 57 std::atomic<bool> flag = false; 58 ConcurrentMap <uint32_t, TaskType> timeFunc; 59 static SamgrTimeHandler* volatile singleton; 60 static Deletor deletor; 61 }; 62 } // namespace OHOS 63 #endif // OHOS_SAMGR_TIME_HANDLER_H