diff --git a/services/timer/include/timer_proxy.h b/services/timer/include/timer_proxy.h index 457ff76a2710357a3e7792d6d49b81fae16b5112..d8b4621015a11f43da8ea7f732ffecb6dd60920d 100644 --- a/services/timer/include/timer_proxy.h +++ b/services/timer/include/timer_proxy.h @@ -63,7 +63,8 @@ private: const uint64_t id, std::unordered_map> &idAlarmsMap); void UpdateProxyWhenElapsedForProxyTimers(const int32_t uid, const int32_t pid, const std::chrono::steady_clock::time_point &now, - std::function &alarm, bool needRetrigger)> insertAlarmCallback); + std::function &alarm, bool needRetrigger)> insertAlarmCallback, + bool needRetrigger); bool UpdateAdjustWhenElapsed(const std::chrono::steady_clock::time_point &now, uint32_t delta, uint32_t interval, std::shared_ptr &timer); bool RestoreAdjustWhenElapsed(std::shared_ptr &timer); diff --git a/services/timer/src/timer_database.cpp b/services/timer/src/timer_database.cpp index 5c8ec688dfe85db488ccf1e8230f94ee5404cfeb..654fb0b674846d0c3bb7164e7df7c120a8bcbbbb 100644 --- a/services/timer/src/timer_database.cpp +++ b/services/timer/src/timer_database.cpp @@ -180,6 +180,9 @@ std::shared_ptr TimeDatabase::Query( int count; if (result->GetRowCount(count) == OHOS::NativeRdb::E_SQLITE_CORRUPT) { RecoverDataBase(); + if (result != nullptr) { + result->Close(); + } return nullptr; } return result; diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index ffc11dd94e683bd8ec5e149a6e1d55865443c3be..2275fd011d09c17468088e0ec70abd22b3fba75f 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -1120,7 +1120,10 @@ void TimerManager::UpdateTimersState(std::shared_ptr &alarm, bool nee InsertAndBatchTimerLocked(alarm); RescheduleKernelTimerLocked(); } else { - StopTimer(alarm->id); + RemoveLocked(alarm->id, true); + TimerProxy::GetInstance().RemoveUidTimerMap(alarm->id); + bool needRecover = CheckNeedRecoverOnReboot(alarm->bundleName, alarm->type, alarm->autoRestore); + UpdateOrDeleteDatabase(false, alarm->id, needRecover); } } diff --git a/services/timer/src/timer_proxy.cpp b/services/timer/src/timer_proxy.cpp index b01ae880923e0568d0eb782de4cd1ed195f89554..d1e08191ddd59f6f5731cbdd37a66172d81011b9 100644 --- a/services/timer/src/timer_proxy.cpp +++ b/services/timer/src/timer_proxy.cpp @@ -59,7 +59,7 @@ bool TimerProxy::ProxyTimer(int32_t uid, int pid, bool isProxy, bool needRetrigg std::lock_guard lockProxy(proxyMutex_); if (isProxy) { - UpdateProxyWhenElapsedForProxyTimers(uid, pid, now, insertAlarmCallback); + UpdateProxyWhenElapsedForProxyTimers(uid, pid, now, insertAlarmCallback, needRetrigger); return true; } @@ -283,7 +283,8 @@ bool TimerProxy::IsProxy(const int32_t uid, const int32_t pid) void TimerProxy::UpdateProxyWhenElapsedForProxyTimers(int32_t uid, int pid, const std::chrono::steady_clock::time_point &now, - std::function &alarm, bool needRetrigger)> insertAlarmCallback) + std::function &alarm, bool needRetrigger)> insertAlarmCallback, + bool needRetrigger) { uint64_t key = GetProxyKey(uid, pid); auto it = proxyTimers_.find(key); @@ -291,10 +292,14 @@ void TimerProxy::UpdateProxyWhenElapsedForProxyTimers(int32_t uid, int pid, TIME_HILOGD(TIME_MODULE_SERVICE, "uid:%{public}d pid: %{public}d is already proxy", uid, pid); return; } - std::lock_guard lockUidTimers(uidTimersMutex_); + std::unordered_map>> uidTimersMap; std::vector timerList; - auto itUidTimersMap = uidTimersMap_.find(uid); - if (itUidTimersMap == uidTimersMap_.end()) { + { + std::lock_guard lockUidTimers(uidTimersMutex_); + uidTimersMap = uidTimersMap_; + } + auto itUidTimersMap = uidTimersMap.find(uid); + if (itUidTimersMap == uidTimersMap.end()) { TIME_HILOGD(TIME_MODULE_SERVICE, "uid: %{public}d in map not found", uid); proxyTimers_[key] = timerList; return; @@ -304,7 +309,10 @@ void TimerProxy::UpdateProxyWhenElapsedForProxyTimers(int32_t uid, int pid, ++itTimerInfo) { if (pid == 0 || pid == itTimerInfo->second->pid) { itTimerInfo->second->originWhenElapsed = itTimerInfo->second->whenElapsed; - timerList.push_back(itTimerInfo->first); + if (!needRetrigger) { + insertAlarmCallback(itTimerInfo->second, false); + break; + } itTimerInfo->second->UpdateWhenElapsedFromNow(now, milliseconds(proxyDelayTime_)); TIME_HILOGD(TIME_MODULE_SERVICE, "Update proxy WhenElapsed for proxy pid map. " "pid= %{public}d, id=%{public}" PRId64 ", timer whenElapsed=%{public}lld, now=%{public}lld", @@ -312,6 +320,7 @@ void TimerProxy::UpdateProxyWhenElapsedForProxyTimers(int32_t uid, int pid, itTimerInfo->second->whenElapsed.time_since_epoch().count(), now.time_since_epoch().count()); insertAlarmCallback(itTimerInfo->second, true); + timerList.push_back(itTimerInfo->first); } } proxyTimers_[key] = timerList; @@ -328,14 +337,17 @@ bool TimerProxy::RestoreProxyWhenElapsed(const int uid, const int pid, TIME_HILOGD(TIME_MODULE_SERVICE, "uid:%{public}d pid:%{public}d not in proxy", uid, pid); return false; } - std::lock_guard lockPidTimers(uidTimersMutex_); - auto itTimer = uidTimersMap_.find(uid); + std::unordered_map>> uidTimersMap; + { + std::lock_guard lockPidTimers(uidTimersMutex_); + uidTimersMap = uidTimersMap_; + } + auto itTimer = uidTimersMap.find(uid); if (uidTimersMap_.find(uid) == uidTimersMap_.end()) { TIME_HILOGD(TIME_MODULE_SERVICE, "uid:%{public}d timer info not found, erase proxy map", uid); return true; } - - for (auto elem : itProxy->second) { + for (int elem : itProxy->second) { auto itTimerInfo = itTimer->second.find(elem); if (itTimerInfo == itTimer->second.end()) { continue;