[Groonga-commit] groonga/grnxx [master] Use grnxx::Stopwatch.

アーカイブの一覧に戻る

susumu.yata null+****@clear*****
Thu Feb 28 13:40:08 JST 2013


susumu.yata	2013-02-28 13:40:08 +0900 (Thu, 28 Feb 2013)

  New Revision: d7ea6fe109462a22c6b421e0549640552e4cde8a
  https://github.com/groonga/grnxx/commit/d7ea6fe109462a22c6b421e0549640552e4cde8a

  Log:
    Use grnxx::Stopwatch.

  Modified files:
    lib/io/file-posix.cpp
    lib/io/file-windows.cpp
    lib/io/pool-impl.cpp
    lib/mutex.cpp
    lib/stopwatch.hpp

  Modified: lib/io/file-posix.cpp (+3 -3)
===================================================================
--- lib/io/file-posix.cpp    2013-02-28 12:10:04 +0900 (170f733)
+++ lib/io/file-posix.cpp    2013-02-28 13:40:08 +0900 (244f6a5)
@@ -30,7 +30,7 @@
 #include "../error.hpp"
 #include "../exception.hpp"
 #include "../logger.hpp"
-#include "../steady_clock.hpp"
+#include "../stopwatch.hpp"
 #include "../thread.hpp"
 #include "path.hpp"
 
@@ -118,8 +118,8 @@ bool FileImpl::lock(FileLockMode mode, Duration timeout) {
   if (try_lock(mode)) {
     return true;
   }
-  const Time deadline = SteadyClock::now() + timeout;
-  while (SteadyClock::now() < deadline) {
+  Stopwatch stopwatch(true);
+  while (stopwatch.elapsed() < timeout) {
     if (try_lock(mode)) {
       return true;
     }

  Modified: lib/io/file-windows.cpp (+3 -3)
===================================================================
--- lib/io/file-windows.cpp    2013-02-28 12:10:04 +0900 (c57bfcd)
+++ lib/io/file-windows.cpp    2013-02-28 13:40:08 +0900 (bd47ad5)
@@ -26,7 +26,7 @@
 #include "../error.hpp"
 #include "../exception.hpp"
 #include "../logger.hpp"
-#include "../steady_clock.hpp"
+#include "../stopwatch.hpp"
 #include "../thread.hpp"
 #include "path.hpp"
 
@@ -113,8 +113,8 @@ bool FileImpl::lock(FileLockMode mode, Duration timeout) {
   if (try_lock(mode)) {
     return true;
   }
-  const Time deadline = SteadyClock::now() + timeout;
-  while (SteadyClock::now() < deadline) {
+  Stopwatch stopwatch(true);
+  while (stopwatch.elapsed() < timeout) {
     if (try_lock(mode)) {
       return true;
     }

  Modified: lib/io/pool-impl.cpp (+3 -3)
===================================================================
--- lib/io/pool-impl.cpp    2013-02-28 12:10:04 +0900 (19250c8)
+++ lib/io/pool-impl.cpp    2013-02-28 13:40:08 +0900 (0661141)
@@ -22,7 +22,7 @@
 #include "../exception.hpp"
 #include "../lock.hpp"
 #include "../logger.hpp"
-#include "../steady_clock.hpp"
+#include "../stopwatch.hpp"
 #include "../string_format.hpp"
 #include "../thread.hpp"
 #include "path.hpp"
@@ -338,8 +338,8 @@ void PoolImpl::open_regular_pool(PoolFlags flags, const char *path,
 
   if (!header_) {
     if ((flags & POOL_OPEN) || (~flags & POOL_CREATE)) {
-      const Time start_time = SteadyClock::now();
-      while ((SteadyClock::now() - start_time) < Duration::seconds(10)) {
+      Stopwatch stopwatch(true);
+      while (stopwatch.elapsed() < Duration::seconds(10)) {
         if (files_[0]->size() != 0) {
           break;
         }

  Modified: lib/mutex.cpp (+5 -5)
===================================================================
--- lib/mutex.cpp    2013-02-28 12:10:04 +0900 (d9d89d1)
+++ lib/mutex.cpp    2013-02-28 13:40:08 +0900 (32c6711)
@@ -17,7 +17,7 @@
 */
 #include "mutex.hpp"
 
-#include "steady_clock.hpp"
+#include "stopwatch.hpp"
 #include "thread.hpp"
 
 namespace grnxx {
@@ -53,13 +53,13 @@ bool Mutex::lock_with_timeout(Duration timeout) {
   }
 
   const bool has_deadline = timeout >= Duration(0);
-  Time deadline;
+  Stopwatch stopwatch;
   if (has_deadline) {
-    deadline = SteadyClock::now() + timeout;
+    stopwatch.start();
   }
 
   for (int i = 0; i < MUTEX_CONTEXT_SWITCH_COUNT; ++i) {
-    if (has_deadline && (SteadyClock::now() >= deadline)) {
+    if (has_deadline && (stopwatch.elapsed() >= timeout)) {
       return false;
     }
     if (try_lock()) {
@@ -68,7 +68,7 @@ bool Mutex::lock_with_timeout(Duration timeout) {
     Thread::switch_to_others();
   }
 
-  while (!has_deadline || (SteadyClock::now() < deadline)) {
+  while (!has_deadline || (stopwatch.elapsed() < timeout)) {
     if (try_lock()) {
       return true;
     }

  Modified: lib/stopwatch.hpp (+13 -1)
===================================================================
--- lib/stopwatch.hpp    2013-02-28 12:10:04 +0900 (cc9bdde)
+++ lib/stopwatch.hpp    2013-02-28 13:40:08 +0900 (4f35c51)
@@ -23,10 +23,22 @@
 
 namespace grnxx {
 
+enum {
+  STOPWATCH_RUNNING
+};
+
 // To measure the amount of time elapsed.
 class Stopwatch {
  public:
-  Stopwatch() : elapsed_(0), start_time_(), is_running_(false) {}
+  // Construct a stopwatch, which is started if is_running == true.
+  explicit Stopwatch(bool is_running = false)
+    : elapsed_(0),
+      start_time_(),
+      is_running_(is_running) {
+    if (is_running) {
+      start_time_ = SteadyClock::now();
+    }
+  }
 
   // Return true iff the stopwatch is running.
   bool is_running() const {
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る