You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by zg...@apache.org on 2019/03/12 12:45:19 UTC

[hbase] 31/133: HBASE-15823 Use call once for user util

This is an automated email from the ASF dual-hosted git repository.

zghao pushed a commit to branch HBASE-14850
in repository https://gitbox.apache.org/repos/asf/hbase.git

commit 93e803f6d5b5ee1a9f88cd9bf998cb12b61fcb6b
Author: Elliott Clark <ec...@apache.org>
AuthorDate: Fri May 13 13:07:03 2016 -0700

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

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;
   }
 }
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