You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ec...@apache.org on 2016/07/11 23:48:04 UTC

[11/50] [abbrv] hbase git commit: HBASE-15823 Use call once for user util

HBASE-15823 Use call once for user util


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/dea1eb64
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/dea1eb64
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/dea1eb64

Branch: refs/heads/HBASE-14850
Commit: dea1eb64bd6ef85ef0ca9a4c1f5c7b30043ba953
Parents: e718c7d
Author: Elliott Clark <ec...@apache.org>
Authored: Fri May 13 13:07:03 2016 -0700
Committer: Elliott Clark <ec...@apache.org>
Committed: Mon Jul 11 16:47:26 2016 -0700

----------------------------------------------------------------------
 hbase-native-client/utils/user-util.cc | 13 ++-----------
 hbase-native-client/utils/user-util.h  |  6 ++----
 2 files changed, 4 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/dea1eb64/hbase-native-client/utils/user-util.cc
----------------------------------------------------------------------
diff --git a/hbase-native-client/utils/user-util.cc b/hbase-native-client/utils/user-util.cc
index 3d963b3..9e170e0 100644
--- a/hbase-native-client/utils/user-util.cc
+++ b/hbase-native-client/utils/user-util.cc
@@ -27,22 +27,14 @@
 using namespace hbase;
 using namespace std;
 
-UserUtil::UserUtil() : init_{false}, user_name_{"drwho"}, m_() {}
+UserUtil::UserUtil() : once_flag_{}, user_name_{"drwho"} {}
 
 string UserUtil::user_name() {
-  if (!init_) {
-    compute_user_name();
-  }
+  std::call_once(once_flag_, [this]() { compute_user_name(); });
   return user_name_;
 }
 
 void UserUtil::compute_user_name() {
-  lock_guard<mutex> lock(m_);
-
-  if (init_) {
-    return;
-  }
-
   // According to the man page of getpwuid
   // this should never be free'd
   //
@@ -52,6 +44,5 @@ void UserUtil::compute_user_name() {
   // make sure that we got something.
   if (passwd && passwd->pw_name) {
     user_name_ = string{passwd->pw_name};
-    init_ = true;
   }
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/dea1eb64/hbase-native-client/utils/user-util.h
----------------------------------------------------------------------
diff --git a/hbase-native-client/utils/user-util.h b/hbase-native-client/utils/user-util.h
index 0b4cc73..fdfc0c8 100644
--- a/hbase-native-client/utils/user-util.h
+++ b/hbase-native-client/utils/user-util.h
@@ -19,8 +19,7 @@
 
 #pragma once
 
-#include <atomic>
-#include <memory>
+#include <string>
 #include <mutex>
 
 namespace hbase {
@@ -49,8 +48,7 @@ private:
    * Compute the username. This will block.
    */
   void compute_user_name();
-  std::atomic<bool> init_;
+  std::once_flag once_flag_;
   std::string user_name_;
-  std::mutex m_;
 };
 } // namespace hbase