From ea9533cdafbe46ce0ae817f87440f09043f6083c Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 27 Feb 2023 10:53:08 -0800 Subject: [PATCH] [core] use `Min()` and `Max()` to track next timer fire time (#8808) --- src/core/coap/coap.cpp | 10 ++-------- src/core/meshcop/commissioner.cpp | 9 +-------- src/core/net/dns_client.cpp | 5 +---- src/core/net/dnssd_server.cpp | 24 ++---------------------- src/core/net/sntp_client.cpp | 5 +---- src/core/net/srp_client.cpp | 4 ++-- src/core/thread/mle.cpp | 5 +---- 7 files changed, 10 insertions(+), 52 deletions(-) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 268c0b816..b76fb378c 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -490,10 +490,7 @@ void CoapBase::HandleRetransmissionTimer(void) } } - if (nextTime > metadata.mNextTimerShot) - { - nextTime = metadata.mNextTimerShot; - } + nextTime = Min(nextTime, metadata.mNextTimerShot); } if (nextTime < now.GetDistantFuture()) @@ -1586,10 +1583,7 @@ void ResponsesQueue::HandleTimer(void) continue; } - if (metadata.mDequeueTime < nextDequeueTime) - { - nextDequeueTime = metadata.mDequeueTime; - } + nextDequeueTime = Min(nextDequeueTime, metadata.mDequeueTime); } if (nextDequeueTime < now.GetDistantFuture()) diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index be9815314..5bd024bf9 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -660,14 +660,7 @@ void Commissioner::UpdateJoinerExpirationTimer(void) continue; } - if (joiner.mExpirationTime <= now) - { - next = now; - } - else if (joiner.mExpirationTime < next) - { - next = joiner.mExpirationTime; - } + next = Min(next, Max(now, joiner.mExpirationTime)); } if (next < now.GetDistantFuture()) diff --git a/src/core/net/dns_client.cpp b/src/core/net/dns_client.cpp index b2062a5fb..ffa9c37e4 100644 --- a/src/core/net/dns_client.cpp +++ b/src/core/net/dns_client.cpp @@ -1080,10 +1080,7 @@ void Client::HandleTimer(void) SendQuery(query, info, /* aUpdateTimer */ false); } - if (nextTime > info.mRetransmissionTime) - { - nextTime = info.mRetransmissionTime; - } + nextTime = Min(nextTime, info.mRetransmissionTime); } if (nextTime < now.GetDistantFuture()) diff --git a/src/core/net/dnssd_server.cpp b/src/core/net/dnssd_server.cpp index 453eeb68e..f2c415259 100644 --- a/src/core/net/dnssd_server.cpp +++ b/src/core/net/dnssd_server.cpp @@ -1285,43 +1285,23 @@ void Server::ResetTimer(void) for (QueryTransaction &query : mQueryTransactions) { - TimeMilli expire; - if (!query.IsValid()) { continue; } - expire = query.GetStartTime() + kQueryTimeout; - if (expire <= now) - { - nextExpire = now; - } - else if (expire < nextExpire) - { - nextExpire = expire; - } + nextExpire = Min(nextExpire, Max(now, query.GetStartTime() + kQueryTimeout)); } #if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE for (UpstreamQueryTransaction &query : mUpstreamQueryTransactions) { - TimeMilli expire; - if (!query.IsValid()) { continue; } - expire = query.GetExpireTime(); - if (expire <= now) - { - nextExpire = now; - } - else if (expire < nextExpire) - { - nextExpire = expire; - } + nextExpire = Min(nextExpire, Max(now, query.GetExpireTime())); } #endif diff --git a/src/core/net/sntp_client.cpp b/src/core/net/sntp_client.cpp index 2bfccf5cc..e93effa7c 100644 --- a/src/core/net/sntp_client.cpp +++ b/src/core/net/sntp_client.cpp @@ -302,10 +302,7 @@ void Client::HandleRetransmissionTimer(void) SendCopy(message, messageInfo); } - if (nextTime > queryMetadata.mTransmissionTime) - { - nextTime = queryMetadata.mTransmissionTime; - } + nextTime = Min(nextTime, queryMetadata.mTransmissionTime); } if (nextTime < now.GetDistantFuture()) diff --git a/src/core/net/srp_client.cpp b/src/core/net/srp_client.cpp index 246003858..9f7bd180f 100644 --- a/src/core/net/srp_client.cpp +++ b/src/core/net/srp_client.cpp @@ -1763,9 +1763,9 @@ void Client::UpdateState(void) service.SetState(kToRefresh); shouldUpdate = true; } - else if (service.GetLeaseRenewTime() < earliestRenewTime) + else { - earliestRenewTime = service.GetLeaseRenewTime(); + earliestRenewTime = Min(earliestRenewTime, service.GetLeaseRenewTime()); } break; diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 80902ea16..26ee7cfe2 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -1625,10 +1625,7 @@ void Mle::HandleDelayedResponseTimer(void) if (now < metadata.mSendTime) { - if (nextSendTime > metadata.mSendTime) - { - nextSendTime = metadata.mSendTime; - } + nextSendTime = Min(nextSendTime, metadata.mSendTime); } else {