From ab2fb068702fa61ef47dfd5b149e98be46ade7aa Mon Sep 17 00:00:00 2001 From: xujunyang Date: Sat, 28 Jun 2025 16:17:54 +0800 Subject: [PATCH] =?UTF-8?q?getRealTime=E9=94=81=E8=8C=83=E5=9B=B4=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xujunyang Change-Id: I4db65ad86344a52c425779f672759fe3569ea334 --- services/time/include/ntp_trusted_time.h | 2 ++ services/time/src/ntp_trusted_time.cpp | 6 ++++++ services/time/src/ntp_update_time.cpp | 1 - 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/services/time/include/ntp_trusted_time.h b/services/time/include/ntp_trusted_time.h index a49c442c..56661134 100644 --- a/services/time/include/ntp_trusted_time.h +++ b/services/time/include/ntp_trusted_time.h @@ -18,6 +18,7 @@ #define SNTP_CLIENT_NTP_TRUSTED_TIME_H #include "time_common.h" +#include namespace OHOS { namespace MiscServices { @@ -50,6 +51,7 @@ public: private: std::shared_ptr mTimeResult {}; + static std::mutex mTimeResultMutex_; }; } // namespace MiscServices } // namespace OHOS diff --git a/services/time/src/ntp_trusted_time.cpp b/services/time/src/ntp_trusted_time.cpp index 3818696f..edd931c4 100644 --- a/services/time/src/ntp_trusted_time.cpp +++ b/services/time/src/ntp_trusted_time.cpp @@ -28,6 +28,8 @@ constexpr int NANO_TO_SECOND = 1000000000; constexpr int64_t ONE_DAY = 86400000; } // namespace +std::mutex NtpTrustedTime::mTimeResultMutex_; + NtpTrustedTime &NtpTrustedTime::GetInstance() { static NtpTrustedTime instance; @@ -39,6 +41,7 @@ bool NtpTrustedTime::ForceRefresh(const std::string &ntpServer) TIME_HILOGD(TIME_MODULE_SERVICE, "start"); SNTPClient client; if (client.RequestTime(ntpServer)) { + std::lock_guard lock(mTimeResultMutex_); if (mTimeResult != nullptr) { mTimeResult->Clear(); } @@ -54,6 +57,7 @@ bool NtpTrustedTime::ForceRefresh(const std::string &ntpServer) int64_t NtpTrustedTime::CurrentTimeMillis() { TIME_HILOGD(TIME_MODULE_SERVICE, "start"); + std::lock_guard lock(mTimeResultMutex_); if (mTimeResult == nullptr) { TIME_HILOGE(TIME_MODULE_SERVICE, "Missing authoritative time source"); return TIME_RESULT_UNINITED; @@ -65,6 +69,7 @@ int64_t NtpTrustedTime::CurrentTimeMillis() int64_t NtpTrustedTime::ElapsedRealtimeMillis() { TIME_HILOGD(TIME_MODULE_SERVICE, "start"); + std::lock_guard lock(mTimeResultMutex_); if (mTimeResult == nullptr) { TIME_HILOGE(TIME_MODULE_SERVICE, "Missing authoritative time source"); return TIME_RESULT_UNINITED; @@ -75,6 +80,7 @@ int64_t NtpTrustedTime::ElapsedRealtimeMillis() int64_t NtpTrustedTime::GetCacheAge() { + std::lock_guard lock(mTimeResultMutex_); if (mTimeResult != nullptr) { return std::chrono::duration_cast( std::chrono::steady_clock::now().time_since_epoch()) diff --git a/services/time/src/ntp_update_time.cpp b/services/time/src/ntp_update_time.cpp index b220a6e3..f0e89776 100644 --- a/services/time/src/ntp_update_time.cpp +++ b/services/time/src/ntp_update_time.cpp @@ -179,7 +179,6 @@ bool NtpUpdateTime::GetRealTimeInner(int64_t &time) bool NtpUpdateTime::GetRealTime(int64_t &time) { - std::lock_guard autoLock(requestMutex_); return GetRealTimeInner(time); } -- Gitee