• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <concurrent_map.h>
29 #include <atomic>
30 namespace OHOS {
31 class SamgrTimeHandler {
32 public:
33     typedef std::function<void()> TaskType;
34     ~SamgrTimeHandler();
35     static SamgrTimeHandler* GetInstance();
36     bool PostTask(TaskType func, uint64_t delayTime);
37 
38 private:
39     SamgrTimeHandler();
40     class Deletor {
41     public:
~Deletor()42         ~Deletor()
43         {
44             if (nullptr != SamgrTimeHandler::singleton) {
45                 delete SamgrTimeHandler::singleton;
46                 SamgrTimeHandler::singleton = nullptr;
47             }
48         }
49     };
50     void StartThread();
51     void OnTime(SamgrTimeHandler &handle, int number, struct epoll_event events[]);
52     int CreateAndRetry();
53 
54 private:
55     int epollfd = -1;
56     ConcurrentMap <uint32_t, TaskType> timeFunc;
57     static SamgrTimeHandler* volatile singleton;
58     static Deletor deletor;
59 };
60 } // namespace OHOS
61 #endif // OHOS_SAMGR_TIME_HANDLER_H