1 /* 2 * Copyright (C) 2021 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 #ifndef TIMER_MANAGER_H 16 #define TIMER_MANAGER_H 17 18 #include <cinttypes> 19 #include <mutex> 20 #include <vector> 21 #include <map> 22 #include <random> 23 #include <thread> 24 #include <atomic> 25 #include <chrono> 26 #include <functional> 27 #include "batch.h" 28 #include "timer_handler.h" 29 #include "want_agent_helper.h" 30 31 namespace OHOS { 32 namespace MiscServices { 33 class TimerManager : public ITimerManager { 34 public: 35 static std::shared_ptr<TimerManager> Create(); 36 int32_t CreateTimer(TimerPara ¶s, 37 std::function<void (const uint64_t)> callback, 38 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent, 39 int uid, 40 uint64_t &timerId) override; 41 int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override; 42 int32_t StopTimer(uint64_t timerId) override; 43 int32_t DestroyTimer(uint64_t timerId) override; 44 bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override; 45 bool ResetAllProxy() override; 46 bool ShowtimerEntryMap(int fd); 47 bool ShowTimerEntryById(int fd, uint64_t timerId); 48 bool ShowTimerTriggerById(int fd, uint64_t timerId); 49 ~TimerManager() override; 50 51 private: 52 explicit TimerManager(std::shared_ptr<TimerHandler> impl); 53 void TimerLooper(); 54 55 void SetHandler(uint64_t id, 56 int type, 57 uint64_t triggerAtTime, 58 uint64_t windowLength, 59 uint64_t interval, 60 int flag, 61 std::function<void (const uint64_t)> callback, 62 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent, 63 int uid); 64 void SetHandlerLocked(uint64_t id, 65 int type, 66 std::chrono::milliseconds when, 67 std::chrono::steady_clock::time_point whenElapsed, 68 std::chrono::milliseconds windowLength, 69 std::chrono::steady_clock::time_point maxWhen, 70 std::chrono::milliseconds interval, 71 std::function<void (const uint64_t)> callback, 72 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent, 73 uint32_t flags, 74 bool doValidate, 75 uint64_t callingUid); 76 void RemoveHandler(uint64_t id); 77 void RemoveLocked(uint64_t id); 78 void ReBatchAllTimers(); 79 void ReBatchAllTimersLocked(bool doValidate); 80 void ReAddTimerLocked(std::shared_ptr<TimerInfo> timer, 81 std::chrono::steady_clock::time_point nowElapsed, 82 bool doValidate); 83 void SetHandlerLocked(std::shared_ptr<TimerInfo> alarm, bool rebatching, bool doValidate); 84 void InsertAndBatchTimerLocked(std::shared_ptr<TimerInfo> alarm); 85 int64_t AttemptCoalesceLocked(std::chrono::steady_clock::time_point whenElapsed, 86 std::chrono::steady_clock::time_point maxWhen); 87 bool TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &triggerList, 88 std::chrono::steady_clock::time_point nowElapsed); 89 void RescheduleKernelTimerLocked(); 90 void DeliverTimersLocked(const std::vector<std::shared_ptr<TimerInfo>> &triggerList, 91 std::chrono::steady_clock::time_point nowElapsed); 92 std::shared_ptr<Batch> FindFirstWakeupBatchLocked(); 93 void SetLocked(int type, std::chrono::nanoseconds when); 94 std::chrono::steady_clock::time_point ConvertToElapsed(std::chrono::milliseconds when, int type); 95 std::chrono::steady_clock::time_point GetBootTimeNs(); 96 void CallbackAlarmIfNeed(std::shared_ptr<TimerInfo> alarm); 97 bool StopTimerInner(uint64_t timerNumber, bool needDestroy); 98 void RemoveProxy(uint64_t timerNumber, int32_t uid); 99 void NotifyWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent); 100 101 std::map<uint64_t, std::shared_ptr<TimerEntry>> timerEntryMap_; 102 std::default_random_engine random_; 103 std::atomic_bool runFlag_; 104 std::shared_ptr<TimerHandler> handler_; 105 std::unique_ptr<std::thread> alarmThread_; 106 std::vector<std::shared_ptr<Batch>> alarmBatches_; 107 std::mutex mutex_; 108 std::mutex entryMapMutex_; 109 std::mutex showTimerMutex_; 110 std::chrono::system_clock::time_point lastTimeChangeClockTime_; 111 std::chrono::steady_clock::time_point lastTimeChangeRealtime_; 112 113 // proxy uid 114 std::mutex proxyMutex_; 115 std::set<int32_t> proxyUids_; 116 std::map<int32_t, std::vector<std::shared_ptr<TimerInfo>>> proxyMap_; 117 }; // timer_manager 118 } // MiscServices 119 } // OHOS 120 121 #endif