]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: avoid <1ms waits on Windows
authorLucian Petrut <lpetrut@cloudbasesolutions.com>
Thu, 4 May 2023 14:27:56 +0000 (14:27 +0000)
committerLucian Petrut <lpetrut@cloudbasesolutions.com>
Wed, 30 Aug 2023 12:59:00 +0000 (12:59 +0000)
std::condition_variable::wait_for uses SleepConditionVariableSRW
on Windows, which has millisecond precision.

In order to avoid busy loops, we won't wait for less than one
millisecond on Windows.

Note that this situation is quite common since on Windows,
"wait_for" often returns ~1ms before the specified timeout.

Signed-off-by: Lucian Petrut <lpetrut@cloudbasesolutions.com>
src/common/Timer.cc
src/common/ceph_timer.h

index 48c79d613d074b1ced6d7b89748489d5f60632b8..2a3277a27659b26d31380e060aaae0103eaec62d 100644 (file)
@@ -87,8 +87,20 @@ void CommonSafeTimer<Mutex>::timer_thread()
       auto p = schedule.begin();
 
       // is the future now?
-      if (p->first > now)
-       break;
+      #if defined(_WIN32)
+      if (p->first - now > std::chrono::milliseconds(1)) {
+        // std::condition_variable::wait_for uses SleepConditionVariableSRW
+        // on Windows, which has millisecond precision. Deltas <1ms will
+        // lead to busy loops, which should be avoided. This situation is
+        // quite common since "wait_for" often returns ~1ms earlier than
+        // requested.
+        break;
+      }
+      #else // !_WIN32
+      if (p->first > now) {
+        break;
+      }
+      #endif
 
       Context *callback = p->second;
       events.erase(callback);
index 74fb4add85127bc0b4f657cd538005c6589aefb6..bc324bfa24379be94e649824dc1d7314ad762632 100644 (file)
@@ -105,8 +105,20 @@ class timer {
       while (!schedule.empty()) {
        auto p = schedule.begin();
        // Should we wait for the future?
-       if (p->t > now)
-         break;
+        #if defined(_WIN32)
+        if (p->t - now > std::chrono::milliseconds(1)) {
+          // std::condition_variable::wait_for uses SleepConditionVariableSRW
+          // on Windows, which has millisecond precision. Deltas <1ms will
+          // lead to busy loops, which should be avoided. This situation is
+          // quite common since "wait_for" often returns ~1ms earlier than
+          // requested.
+          break;
+        }
+        #else // !_WIN32
+        if (p->t > now) {
+          break;
+        }
+        #endif
 
        auto& e = *p;
        schedule.erase(e);