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/05/18 22:49:51 UTC
[30/42] 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/616405a6
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/616405a6
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/616405a6
Branch: refs/heads/HBASE-14850
Commit: 616405a69a3ad9c4c9ac0d7baa0f065a7a2efb33
Parents: 6bc1ab3
Author: Elliott Clark <ec...@apache.org>
Authored: Fri May 13 13:07:03 2016 -0700
Committer: Elliott Clark <el...@fb.com>
Committed: Wed May 18 15:48:52 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/616405a6/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/616405a6/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