/* * Copyright (C) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef TIMER_MANAGER_H #define TIMER_MANAGER_H #include #include #include #include #include #include #include #include #include #include "batch.h" #include "timer_handler.h" #include "want_agent_helper.h" namespace OHOS { namespace MiscServices { class TimerManager : public ITimerManager { public: static std::shared_ptr Create(); int32_t CreateTimer(TimerPara ¶s, std::function callback, std::shared_ptr wantAgent, int uid, uint64_t &timerId) override; int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override; int32_t StopTimer(uint64_t timerId) override; int32_t DestroyTimer(uint64_t timerId) override; bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override; bool ResetAllProxy() override; bool ShowtimerEntryMap(int fd); bool ShowTimerEntryById(int fd, uint64_t timerId); bool ShowTimerTriggerById(int fd, uint64_t timerId); bool ShowIdleTimerInfo(int fd); ~TimerManager() override; void HandleRSSDeath(); private: explicit TimerManager(std::shared_ptr impl); void TimerLooper(); void SetHandler(uint64_t id, int type, uint64_t triggerAtTime, uint64_t windowLength, uint64_t interval, int flag, std::function callback, std::shared_ptr wantAgent, int uid); void SetHandlerLocked(uint64_t id, int type, std::chrono::milliseconds when, std::chrono::steady_clock::time_point whenElapsed, std::chrono::milliseconds windowLength, std::chrono::steady_clock::time_point maxWhen, std::chrono::milliseconds interval, std::function callback, const std::shared_ptr &wantAgent, uint32_t flags, bool doValidate, uint64_t callingUid); void RemoveHandler(uint64_t id); void RemoveLocked(uint64_t id); void ReBatchAllTimers(); void ReBatchAllTimersLocked(bool doValidate); void ReAddTimerLocked(std::shared_ptr timer, std::chrono::steady_clock::time_point nowElapsed, bool doValidate); void SetHandlerLocked(std::shared_ptr alarm, bool rebatching, bool doValidate, bool isRebatched); void InsertAndBatchTimerLocked(std::shared_ptr alarm); int64_t AttemptCoalesceLocked(std::chrono::steady_clock::time_point whenElapsed, std::chrono::steady_clock::time_point maxWhen); bool TriggerTimersLocked(std::vector> &triggerList, std::chrono::steady_clock::time_point nowElapsed); void RescheduleKernelTimerLocked(); void DeliverTimersLocked(const std::vector> &triggerList, std::chrono::steady_clock::time_point nowElapsed); std::shared_ptr FindFirstWakeupBatchLocked(); void SetLocked(int type, std::chrono::nanoseconds when); std::chrono::steady_clock::time_point ConvertToElapsed(std::chrono::milliseconds when, int type); std::chrono::steady_clock::time_point GetBootTimeNs(); void CallbackAlarmIfNeed(const std::shared_ptr &alarm); int32_t StopTimerInner(uint64_t timerNumber, bool needDestroy); void RemoveProxy(uint64_t timerNumber, int32_t uid); void NotifyWantAgent(const std::shared_ptr &wantAgent); bool CheckAllowWhileIdle(uint32_t flag); bool AdjustDeliveryTimeBasedOnDeviceIdle(const std::shared_ptr &alarm); bool AdjustTimersBasedOnDeviceIdle(); std::map> timerEntryMap_; std::default_random_engine random_; std::atomic_bool runFlag_; std::shared_ptr handler_; std::unique_ptr alarmThread_; std::vector> alarmBatches_; std::mutex mutex_; std::mutex entryMapMutex_; std::mutex showTimerMutex_; std::chrono::system_clock::time_point lastTimeChangeClockTime_; std::chrono::steady_clock::time_point lastTimeChangeRealtime_; // proxy uid std::mutex proxyMutex_; std::set proxyUids_; std::map>> proxyMap_; std::vector> pendingDelayTimers_; // map for delayed timers std::map delayedTimers_; // idle timer std::shared_ptr mPendingIdleUntil_; std::mutex idleTimerMutex_; }; // timer_manager } // MiscServices } // OHOS #endif