[Groonga-commit] groonga/grnxx at 3ddcc7e [master] Update grnxx::Mutex.

Back to archive index

susumu.yata null+****@clear*****
Mon Jul 1 16:39:45 JST 2013


susumu.yata	2013-07-01 16:39:45 +0900 (Mon, 01 Jul 2013)

  New Revision: 3ddcc7e133f54712bd78d8f1563d0e1fa7ef71e5
  https://github.com/groonga/grnxx/commit/3ddcc7e133f54712bd78d8f1563d0e1fa7ef71e5

  Message:
    Update grnxx::Mutex.
    
    Remove swap() and fix the initial value to MUTEX_UNLOCKED.

  Modified files:
    lib/grnxx/array_impl.cpp
    lib/grnxx/backtrace.cpp
    lib/grnxx/logger.cpp
    lib/grnxx/map/hash_table/header.cpp
    lib/grnxx/mutex.cpp
    lib/grnxx/mutex.hpp
    lib/grnxx/os.cpp
    lib/grnxx/periodic_clock.cpp
    lib/grnxx/storage/header.cpp
    lib/grnxx/storage/storage_impl.cpp
    lib/grnxx/time.cpp
    test/test_mutex.cpp
    test/test_thread.cpp

  Modified: lib/grnxx/array_impl.cpp (+7 -7)
===================================================================
--- lib/grnxx/array_impl.cpp    2013-07-01 15:22:24 +0900 (91690bf)
+++ lib/grnxx/array_impl.cpp    2013-07-01 16:39:45 +0900 (07bacb8)
@@ -53,9 +53,9 @@ ArrayHeader::ArrayHeader()
       table_storage_node_id(STORAGE_INVALID_NODE_ID),
       secondary_table_storage_node_id(STORAGE_INVALID_NODE_ID),
       reserved(0),
-      page_mutex(MUTEX_UNLOCKED),
-      table_mutex(MUTEX_UNLOCKED),
-      secondary_table_mutex(MUTEX_UNLOCKED) {}
+      page_mutex(),
+      table_mutex(),
+      secondary_table_mutex() {}
 
 Array1D::Array1D()
     : storage_node_id_(STORAGE_INVALID_NODE_ID),
@@ -148,7 +148,7 @@ Array2D::Array2D()
       fill_page_(nullptr),
       table_(nullptr),
       table_cache_(),
-      mutex_(MUTEX_UNLOCKED) {}
+      mutex_() {}
 
 Array2D::~Array2D() {}
 
@@ -306,9 +306,9 @@ Array3D::Array3D()
       fill_page_(nullptr),
       secondary_table_(nullptr),
       table_caches_(),
-      page_mutex_(MUTEX_UNLOCKED),
-      table_mutex_(MUTEX_UNLOCKED),
-      secondary_table_mutex_(MUTEX_UNLOCKED) {}
+      page_mutex_(),
+      table_mutex_(),
+      secondary_table_mutex_() {}
 
 Array3D::~Array3D() {}
 

  Modified: lib/grnxx/backtrace.cpp (+2 -2)
===================================================================
--- lib/grnxx/backtrace.cpp    2013-07-01 15:22:24 +0900 (07fbaca)
+++ lib/grnxx/backtrace.cpp    2013-07-01 16:39:45 +0900 (361a154)
@@ -307,7 +307,7 @@ bool Resolver::resolve(void *, std::ostream *) {
 }  // namespace
 
 bool Backtrace::backtrace(int skip_count, std::vector<void *> *addresses) try {
-  static Mutex mutex(MUTEX_UNLOCKED);
+  static Mutex mutex;
   Lock lock(&mutex);
 
   if ((skip_count < BACKTRACE_MIN_SKIP_COUNT) ||
@@ -341,7 +341,7 @@ bool Backtrace::backtrace(int skip_count, std::vector<void *> *addresses) try {
 }
 
 bool Backtrace::resolve(void *address, std::string *entry) try {
-  static Mutex mutex(MUTEX_UNLOCKED);
+  static Mutex mutex;
   Lock lock(&mutex);
 
   if (!address || !entry) {

  Modified: lib/grnxx/logger.cpp (+2 -2)
===================================================================
--- lib/grnxx/logger.cpp    2013-07-01 15:22:24 +0900 (53ced0c)
+++ lib/grnxx/logger.cpp    2013-07-01 16:39:45 +0900 (66e68e7)
@@ -58,7 +58,7 @@ class LoggerSingleton {
     if (!initialized_) {
       // C++11 guarantees that a static local variable is initialized once.
       // However, some compilers don't provide the guarantee.
-      static Mutex mutex(MUTEX_UNLOCKED);
+      static Mutex mutex;
       Lock lock(&mutex);
       if (!initialized_) {
         initialize();
@@ -77,7 +77,7 @@ class LoggerSingleton {
 
 volatile bool LoggerSingleton::initialized_           = false;
 LoggerSingleton * volatile LoggerSingleton::instance_ = nullptr;
-Mutex LoggerSingleton::mutex_(MUTEX_UNLOCKED);
+Mutex LoggerSingleton::mutex_;
 
 bool LoggerSingleton::write(const StringBuilder &builder) {
   initialize_once();

  Modified: lib/grnxx/map/hash_table/header.cpp (+1 -1)
===================================================================
--- lib/grnxx/map/hash_table/header.cpp    2013-07-01 15:22:24 +0900 (e7b1404)
+++ lib/grnxx/map/hash_table/header.cpp    2013-07-01 16:39:45 +0900 (9a78f29)
@@ -29,7 +29,7 @@ Header::Header()
       old_key_ids_storage_node_id(STORAGE_INVALID_NODE_ID),
       keys_storage_node_id(STORAGE_INVALID_NODE_ID),
       num_key_ids(0),
-      mutex(MUTEX_UNLOCKED) {}
+      mutex() {}
 
 }  // namespace hash_table
 }  // namespace map

  Modified: lib/grnxx/mutex.cpp (+11 -15)
===================================================================
--- lib/grnxx/mutex.cpp    2013-07-01 15:22:24 +0900 (8b2ae14)
+++ lib/grnxx/mutex.cpp    2013-07-01 16:39:45 +0900 (ac8b575)
@@ -22,6 +22,13 @@
 #include "grnxx/stopwatch.hpp"
 
 namespace grnxx {
+namespace {
+
+constexpr int MUTEX_SPIN_COUNT           = 100;
+constexpr int MUTEX_CONTEXT_SWITCH_COUNT = 100;
+constexpr Duration MUTEX_SLEEP_DURATION  = Duration::milliseconds(10);
+
+}  // namespace
 
 void Mutex::lock_without_timeout() {
   for (int i = 0; i < MUTEX_SPIN_COUNT; ++i) {
@@ -29,38 +36,29 @@ void Mutex::lock_without_timeout() {
       return;
     }
   }
-
   for (int i = 0; i < MUTEX_CONTEXT_SWITCH_COUNT; ++i) {
     if (try_lock()) {
       return;
     }
     Thread::yield();
   }
-
   while (!try_lock()) {
     Thread::sleep_for(MUTEX_SLEEP_DURATION);
   }
 }
 
 bool Mutex::lock_with_timeout(Duration timeout) {
-  if (timeout == Duration(0)) {
+  if (timeout <= Duration(0)) {
     return false;
   }
-
   for (int i = 0; i < MUTEX_SPIN_COUNT; ++i) {
     if (try_lock()) {
       return true;
     }
   }
-
-  const bool has_deadline = timeout >= Duration(0);
-  Stopwatch stopwatch(false);
-  if (has_deadline) {
-    stopwatch.start();
-  }
-
+  Stopwatch stopwatch(true);
   for (int i = 0; i < MUTEX_CONTEXT_SWITCH_COUNT; ++i) {
-    if (has_deadline && (stopwatch.elapsed() >= timeout)) {
+    if (stopwatch.elapsed() >= timeout) {
       return false;
     }
     if (try_lock()) {
@@ -68,14 +66,12 @@ bool Mutex::lock_with_timeout(Duration timeout) {
     }
     Thread::yield();
   }
-
-  while (!has_deadline || (stopwatch.elapsed() < timeout)) {
+  while (stopwatch.elapsed() < timeout) {
     if (try_lock()) {
       return true;
     }
     Thread::sleep_for(MUTEX_SLEEP_DURATION);
   }
-
   return false;
 }
 

  Modified: lib/grnxx/mutex.hpp (+4 -20)
===================================================================
--- lib/grnxx/mutex.hpp    2013-07-01 15:22:24 +0900 (f753dff)
+++ lib/grnxx/mutex.hpp    2013-07-01 16:39:45 +0900 (f1d267c)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2012  Brazil, Inc.
+  Copyright (C) 2012-2013  Brazil, Inc.
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -20,8 +20,6 @@
 
 #include "grnxx/features.hpp"
 
-#include <utility>
-
 #include "grnxx/intrinsic.hpp"
 #include "grnxx/duration.hpp"
 #include "grnxx/types.hpp"
@@ -35,14 +33,9 @@ enum MutexStatus : uint32_t {
   MUTEX_LOCKED   = 1
 };
 
-constexpr int MUTEX_SPIN_COUNT           = 100;
-constexpr int MUTEX_CONTEXT_SWITCH_COUNT = 100;
-constexpr Duration MUTEX_SLEEP_DURATION  = Duration::milliseconds(10);
-
 class Mutex {
  public:
-  Mutex() = default;
-  explicit constexpr Mutex(MutexStatus status) : status_(status) {}
+  constexpr Mutex() : status_(MUTEX_UNLOCKED) {}
 
   void lock() {
     if (!try_lock()) {
@@ -69,28 +62,19 @@ class Mutex {
     return true;
   }
 
-  constexpr bool locked() const {
+  bool locked() const {
     return status_ != MUTEX_UNLOCKED;
   }
 
-  void swap(Mutex &mutex) {
-    using std::swap;
-    std::swap(status_, mutex.status_);
-  }
-
   StringBuilder &write_to(StringBuilder &builder) const;
 
  private:
-  MutexStatus status_;
+  volatile MutexStatus status_;
 
   void lock_without_timeout();
   bool lock_with_timeout(Duration timeout);
 };
 
-inline void swap(Mutex &lhs, Mutex &rhs) {
-  lhs.swap(rhs);
-}
-
 inline StringBuilder &operator<<(StringBuilder &builder, const Mutex &mutex) {
   return mutex.write_to(builder);
 }

  Modified: lib/grnxx/os.cpp (+1 -1)
===================================================================
--- lib/grnxx/os.cpp    2013-07-01 15:22:24 +0900 (6dab63b)
+++ lib/grnxx/os.cpp    2013-07-01 16:39:45 +0900 (7426c2a)
@@ -41,7 +41,7 @@ char *OS::get_environment_variable(const char *name) {
     GRNXX_ERROR() << "invalid argument: name = nullptr";
     return nullptr;
   }
-  static Mutex mutex(MUTEX_UNLOCKED);
+  static Mutex mutex;
   Lock lock(&mutex);
 #ifdef GRNXX_MSC
   char *value;

  Modified: lib/grnxx/periodic_clock.cpp (+1 -1)
===================================================================
--- lib/grnxx/periodic_clock.cpp    2013-07-01 15:22:24 +0900 (6d84332)
+++ lib/grnxx/periodic_clock.cpp    2013-07-01 16:39:45 +0900 (e33a1ed)
@@ -30,7 +30,7 @@ constexpr Duration UPDATE_INTERVAL = Duration::milliseconds(100);
 
 volatile uint32_t ref_count = 0;
 grnxx::Thread *thread = nullptr;
-Mutex mutex(MUTEX_UNLOCKED);
+Mutex mutex;
 
 }  // namespace
 

  Modified: lib/grnxx/storage/header.cpp (+2 -2)
===================================================================
--- lib/grnxx/storage/header.cpp    2013-07-01 15:22:24 +0900 (04c0afd)
+++ lib/grnxx/storage/header.cpp    2013-07-01 16:39:45 +0900 (26c5c2b)
@@ -51,8 +51,8 @@ Header::Header()
       latest_phantom_node_id(STORAGE_INVALID_NODE_ID),
       latest_unlinked_node_id(STORAGE_INVALID_NODE_ID),
       oldest_idle_node_ids(),
-      data_mutex(MUTEX_UNLOCKED),
-      file_mutex(MUTEX_UNLOCKED),
+      data_mutex(),
+      file_mutex(),
       reserved_2{} {
   std::strcpy(version, Grnxx::version());
   for (size_t i = 0; i < NUM_IDLE_NODE_LISTS; ++i) {

  Modified: lib/grnxx/storage/storage_impl.cpp (+1 -1)
===================================================================
--- lib/grnxx/storage/storage_impl.cpp    2013-07-01 15:22:24 +0900 (0ca0939)
+++ lib/grnxx/storage/storage_impl.cpp    2013-07-01 16:39:45 +0900 (b5749af)
@@ -86,7 +86,7 @@ StorageImpl::StorageImpl()
       root_chunk_(),
       header_chunks_(),
       body_chunks_(),
-      mutex_(MUTEX_UNLOCKED),
+      mutex_(),
       clock_() {}
 
 StorageImpl::~StorageImpl() {}

  Modified: lib/grnxx/time.cpp (+2 -2)
===================================================================
--- lib/grnxx/time.cpp    2013-07-01 15:22:24 +0900 (33ade24)
+++ lib/grnxx/time.cpp    2013-07-01 16:39:45 +0900 (abd3e53)
@@ -59,7 +59,7 @@ BrokenDownTime Time::universal_time() const {
 #else  // defined(GRNXX_HAS_GMTIME_R)
   // Lock is used for exclusive access, but it is still not thread-safe
   // because other threads may call std::gmtime().
-  static Mutex mutex(MUTEX_UNLOCKED);
+  static Mutex mutex;
   Lock lock(&mutex);
   std::tm * const shared_tm = std::gmtime(&posix_time);
   if (!shared_tm) {
@@ -84,7 +84,7 @@ BrokenDownTime Time::local_time() const {
 #else  // defined(GRNXX_HAS_LOCALTIME_R)
   // Lock is used for exclusive access, but it is still not thread-safe
   // because other threads may call std::localtime().
-  static Mutex mutex(MUTEX_UNLOCKED);
+  static Mutex mutex;
   Lock lock(&mutex);
   std::tm * const shared_tm = std::localtime(&posix_time);
   if (!shared_tm) {

  Modified: test/test_mutex.cpp (+2 -5)
===================================================================
--- test/test_mutex.cpp    2013-07-01 15:22:24 +0900 (bf63a88)
+++ test/test_mutex.cpp    2013-07-01 16:39:45 +0900 (7bca09a)
@@ -28,13 +28,11 @@ int main() {
   grnxx::Logger::set_max_level(grnxx::NOTICE_LOGGER);
 
 
-  assert(!grnxx::Mutex(grnxx::MUTEX_UNLOCKED).locked());
-  assert(grnxx::Mutex(grnxx::MUTEX_LOCKED).locked());
-
-  grnxx::Mutex mutex(grnxx::MUTEX_UNLOCKED);
+  grnxx::Mutex mutex;
 
   GRNXX_NOTICE() << "mutex = " << mutex;
 
+  assert(!mutex.locked());
   assert(mutex.try_lock());
   assert(mutex.locked());
 
@@ -61,7 +59,6 @@ int main() {
   assert(mutex.unlock());
   assert(!mutex.locked());
 
-
   enum { LOOP_COUNT = 1 << 20 };
 
   grnxx::Stopwatch stopwatch(true);

  Modified: test/test_thread.cpp (+1 -1)
===================================================================
--- test/test_thread.cpp    2013-07-01 15:22:24 +0900 (7e3ffa3)
+++ test/test_thread.cpp    2013-07-01 16:39:45 +0900 (3b55875)
@@ -27,7 +27,7 @@
 
 namespace {
 
-grnxx::Mutex mutex(grnxx::MUTEX_UNLOCKED);
+grnxx::Mutex mutex;
 
 void thread_routine() {
   grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(10));
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index